Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_bulk_file_import.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
19 
29 {
30 
31 
37  function HIPO_Job_Bulk_File_Import($code_name='')
38  {
39  $this->uses_trans = FALSE;
40  $this->HIPO_Job($code_name);
41 
42  }//end constructor
43 
44 
54  public static function paintConfig(&$o, $class, $write_access)
55  {
56  $o->openField('File Import Threshold');
57  if ($write_access) {
58  text_box($class.'[SQ_HIPO_BULK_FILE_IMPORT_THRESHOLD]', SQ_HIPO_BULK_FILE_IMPORT_THRESHOLD, 5);
59  echo ' '.translate('assets');
60  } else {
61  echo SQ_HIPO_BULK_FILE_IMPORT_THRESHOLD;
62  echo ' '.translate('assets');
63  }
64 
65  $o->closeField();
66 
67  }//end paintConfig()
68 
69 
76  public static function getConfigVars()
77  {
78  return Array(
79  'SQ_HIPO_BULK_FILE_IMPORT_THRESHOLD' => Array('editable' => 1, 'default' => 5),
80  );
81 
82  }//end getConfigVars()
83 
84 
91  function getCodeName()
92  {
93  $dir_names = '';
94  if (!empty($this->_running_vars['server_file_import'])) {
95  $dir_names = implode('_', array_keys($this->_running_vars['server_file_import']));
96  }
97 
98  $file_names = '';
99  if (!empty($this->_running_vars['local_file_import'])) {
100  foreach ($this->_running_vars['local_file_import']['files'] as $data) {
101  $file_names .= $data['name'];
102  }
103  }
104 
105  return 'HIPO_Job_File_Import-'.md5($dir_names.$file_names);
106 
107  }//end getCodeName()
108 
109 
116  function getHipoName()
117  {
118  return translate('file_import_tools_bulk_file_import_hipo_name');
119 
120  }//end getHipoName()
121 
122 
131  {
132  return Array(
133  Array(
134  'name' => 'Import Files',
135  'function_call' => Array(
136  'process_function' => 'importServerFiles',
137  ),
138  'running_mode' => 'server',
139  'auto_step' => TRUE,
140  'allow_cancel' => TRUE,
141  'percent_done' => 0,
142  'complete' => FALSE,
143  'message' => '',
144  ),
145  Array(
146  'name' => 'Import Files',
147  'function_call' => Array(
148  'process_function' => 'importLocalFiles',
149  ),
150  'running_mode' => 'server',
151  'auto_step' => TRUE,
152  'allow_cancel' => TRUE,
153  'percent_done' => 0,
154  'complete' => FALSE,
155  'message' => '',
156  ),
157  );
158 
159  }//end getInitialStepData()
160 
161 
168  function prepare()
169  {
170  // Nothing to import, return
171  if (empty($this->_running_vars['server_file_import']) && empty($this->_running_vars['local_file_import'])) {
172  return FALSE;
173  }
174 
175  $this->_running_vars['server_todo_counts'] = Array();
176  $this->_running_vars['local_todo_counts'] = Array();
177 
178  // Add server files if there are any
179  if (!empty($this->_running_vars['server_file_import'])) {
180  foreach ($this->_running_vars['server_file_import'] as $key => $data) {
181  foreach ($data['file_list'] as $file_name) {
182  $this->_running_vars['server_todo_counts'][] = Array(
183  'file_name' => $file_name,
184  'full_path' => $key.'/'.$file_name,
185  'root_id' => $data['root_id'],
186  );
187  }
188  }
189  $this->_running_vars['server_done_counts'] = Array();
190  }
191 
192  // Add local files if there are any
193  if (!empty($this->_running_vars['local_file_import'])) {
194  foreach ($this->_running_vars['local_file_import']['files'] as $data) {
195  $file_info = Array(
196  'file_name' => $data['name'],
197  'full_path' => $this->_running_vars['local_file_import']['location'].'/'.$data['name'],
198  'root_id' => $this->_running_vars['local_file_import']['rootid'],
199  'type' => $data['file_type'],
200  'title' => $data['title'],
201  );
202  if ($file_info['type'] == 'image') {
203  $file_info['alt'] = $data['alt'];
204  $file_info['caption'] = $data['caption'];
205  }
206  $this->_running_vars['local_todo_counts'][] = $file_info;
207  }
208  $this->_running_vars['local_done_counts'] = Array();
209  }
210 
211  // Global Options
212  if (!isset($this->_running_vars['allow_unrestricted'])) {
213  $this->_running_vars['allow_unrestricted'] = TRUE;
214  }
215  if (!isset($this->_running_vars['link_type'])) {
216  $this->_running_vars['link_type'] = SQ_LINK_TYPE_1;
217  }
218 
219  return parent::prepare();
220 
221  }//end prepare()
222 
223 
233  {
234  if (SQ_HIPO_BULK_FILE_IMPORT_THRESHOLD == 0) return 0;
235  $server_files = count($this->_running_vars['server_todo_counts']);
236  $local_files = count($this->_running_vars['local_todo_counts']);
237  $total = $server_files + $local_files;
238  if ($total == 0) return 0;
239 
240  return ($total / SQ_HIPO_BULK_FILE_IMPORT_THRESHOLD) * 100;
241 
242  }//end getThresholdPercentageRequired()
243 
244 
251  function freestyle()
252  {
253  while (!empty($this->_running_vars['server_todo_counts'])) {
254  if (!$this->importServerFiles($this->_steps[1], get_class($this))) {
255  return FALSE;
256  }
257  }
258 
259  while (!empty($this->_running_vars['local_todo_counts'])) {
260  if (!$this->importLocalFiles($this->_steps[1], get_class($this))) {
261  return FALSE;
262  }
263  }
264  return TRUE;
265 
266  }//end freestyle()
267 
268 
278  function importServerFiles(&$step_data, $prefix)
279  {
280  if (!empty($this->_running_vars['server_todo_counts'])) {
281 
282  $todo = array_shift($this->_running_vars['server_todo_counts']);
283  $parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($todo['root_id']);
284  $import_link = Array('asset' => &$parent_asset, 'link_type' => $this->_running_vars['link_type']);
285  $file_type = get_file_type($todo['file_name']);
286  switch ($file_type) {
287  case 'doc' :
288  case 'dot' :
289  $GLOBALS['SQ_SYSTEM']->am->includeAsset('word_doc');
290  $new_asset_type = 'word_doc';
291  break;
292  case 'pdf' :
293  $GLOBALS['SQ_SYSTEM']->am->includeAsset('pdf_file');
294  $new_asset_type = 'pdf_file';
295  break;
296  case 'xls' :
297  case 'xlt' :
298  case 'xlsx' :
299  case 'xlsm' :
300  case 'xltx' :
301  case 'xltm' :
302  $GLOBALS['SQ_SYSTEM']->am->includeAsset('excel_doc');
303  $new_asset_type = 'excel_doc';
304  break;
305  case 'ppt' :
306  case 'pot' :
307  case 'pps' :
308  case 'pptx' :
309  case 'potx' :
310  case 'ppsx' :
311  case 'pptm' :
312  case 'potm' :
313  case 'ppsm' :
314  $GLOBALS['SQ_SYSTEM']->am->includeAsset('powerpoint_doc');
315  $new_asset_type = 'powerpoint_doc';
316  break;
317  case 'rtf' :
318  $GLOBALS['SQ_SYSTEM']->am->includeAsset('rtf_file');
319  $new_asset_type = 'rtf_file';
320  break;
321  case 'txt' :
322  $GLOBALS['SQ_SYSTEM']->am->includeAsset('text_file');
323  $new_asset_type = 'text_file';
324  break;
325  case 'js' :
326  $GLOBALS['SQ_SYSTEM']->am->includeAsset('js_file');
327  $new_asset_type = 'js_file';
328  break;
329  case 'css' :
330  $GLOBALS['SQ_SYSTEM']->am->includeAsset('css_file');
331  $new_asset_type = 'css_file';
332  break;
333  case 'gif' :
334  case 'jpg' :
335  case 'jpeg' :
336  case 'png' :
337  $GLOBALS['SQ_SYSTEM']->am->includeAsset('image');
338  $new_asset_type = 'image';
339  break;
340  case 'mp3' :
341  $GLOBALS['SQ_SYSTEM']->am->includeAsset('mp3_file');
342  $new_asset_type = 'mp3_file';
343  break;
344  case 'mov' :
345  case 'avi' :
346  case 'wmv' :
347  case 'asf' :
348  case 'flv' :
349  case 'mp4' :
350  case 'm4v' :
351  case 'mpg' :
352  case 'mpeg':
353  $GLOBALS['SQ_SYSTEM']->am->includeAsset('video_file');
354  $new_asset_type = 'video_file';
355  break;
356  default :
357  $GLOBALS['SQ_SYSTEM']->am->includeAsset('file');
358  $new_asset_type = 'file';
359  break;
360  }
361 
362  // create an asset under the new parent of the correct type
363  $temp_info = Array('name' => $todo['file_name'], 'tmp_name' => $todo['full_path'], 'non_uploaded_file' => TRUE);
364  $new_file = new $new_asset_type();
365  $new_file->_tmp['uploading_file'] = TRUE;
366  $new_file->setAttrValue('name', $todo['file_name']);
367  $new_file->setAttrValue('allow_unrestricted', $this->_running_vars['allow_unrestricted']);
368 
369  if (!$new_file->create($import_link, $temp_info)) {
370  $this->_addError(translate('file_import_tools_hipo_can_not_create_file', $todo['file_name'], $parent->id), TRUE);
371  }
372  $this->_running_vars['server_done_counts'][] = $todo;
373 
374  }//end if
375 
376  if (empty($this->_running_vars['server_todo_counts'])) {
377  $step_data['percent_done'] = 100;
378  $step_data['complete'] = TRUE;
379  unset($this->_running_vars['server_done_counts']);
380  } else {
381  $total = count($this->_running_vars['server_todo_counts']) + count($this->_running_vars['server_done_counts']);
382  $step_data['percent_done'] = (count($this->_running_vars['server_done_counts']) / $total) * 100;
383  $step_data['complete'] = FALSE;
384  }
385 
386  return TRUE;
387 
388  }//end importServerFiles()
389 
390 
400  function importLocalFiles(&$step_data, $prefix)
401  {
402  if (!empty($this->_running_vars['local_todo_counts'])) {
403 
404  $todo = array_shift($this->_running_vars['local_todo_counts']);
405  $parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($todo['root_id']);
406  $import_link = Array('asset' => &$parent_asset, 'link_type' => $this->_running_vars['link_type']);
407  switch ($todo['type']) {
408  case 'word' :
409  $GLOBALS['SQ_SYSTEM']->am->includeAsset('word_doc');
410  $new_asset_type = 'word_doc';
411  break;
412  case 'pdf' :
413  $GLOBALS['SQ_SYSTEM']->am->includeAsset('pdf_file');
414  $new_asset_type = 'pdf_file';
415  break;
416  case 'powerpoint' :
417  $GLOBALS['SQ_SYSTEM']->am->includeAsset('powerpoint_doc');
418  $new_asset_type = 'powerpoint_doc';
419  break;
420  case 'excel' :
421  $GLOBALS['SQ_SYSTEM']->am->includeAsset('excel_doc');
422  $new_asset_type = 'excel_doc';
423  break;
424  case 'rtf' :
425  $GLOBALS['SQ_SYSTEM']->am->includeAsset('rtf_file');
426  $new_asset_type = 'rtf_file';
427  break;
428  case 'text' :
429  $GLOBALS['SQ_SYSTEM']->am->includeAsset('text_file');
430  $new_asset_type = 'text_file';
431  break;
432  case 'css' :
433  $GLOBALS['SQ_SYSTEM']->am->includeAsset('css_file');
434  $new_asset_type = 'css_file';
435  break;
436  case 'js' :
437  $GLOBALS['SQ_SYSTEM']->am->includeAsset('js_file');
438  $new_asset_type = 'js_file';
439  break;
440  case 'image' :
441  $GLOBALS['SQ_SYSTEM']->am->includeAsset('image');
442  $new_asset_type = 'image';
443  break;
444  case 'mp3' :
445  $GLOBALS['SQ_SYSTEM']->am->includeAsset('mp3_file');
446  $new_asset_type = 'mp3_file';
447  break;
448  case 'video' :
449  $GLOBALS['SQ_SYSTEM']->am->includeAsset('video_file');
450  $new_asset_type = 'video_file';
451  break;
452  default :
453  $GLOBALS['SQ_SYSTEM']->am->includeAsset('file');
454  $new_asset_type = 'file';
455  break;
456  }
457 
458  // create an asset under the new parent of the correct type
459  $temp_info = Array('name' => $todo['file_name'], 'tmp_name' => $todo['full_path'], 'non_uploaded_file' => TRUE);
460  $new_file = new $new_asset_type();
461  $new_file->_tmp['uploading_file'] = TRUE;
462  $new_file->setAttrValue('name', $todo['file_name']);
463  $new_file->setAttrValue('title', $todo['title']);
464  $new_file->setAttrValue('allow_unrestricted', $this->_running_vars['allow_unrestricted']);
465 
466  // Extra attributes for particular asset type
467  switch ($todo['type']) {
468  case 'image' :
469  $new_file->setAttrValue('alt', $todo['alt']);
470  $new_file->setAttrValue('caption', $todo['caption']);
471  break;
472  }
473 
474  if (!$new_file->create($import_link, $temp_info)) {
475  $this->_addError(translate('file_import_tools_hipo_can_not_create_file', $todo['file_name'], $parent->id), TRUE);
476  }
477  $this->_running_vars['local_done_counts'][] = $todo;
478 
479  }//end if
480 
481  if (empty($this->_running_vars['local_todo_counts'])) {
482  $step_data['percent_done'] = 100;
483  $step_data['complete'] = TRUE;
484  unset($this->_running_vars['local_todo_counts']);
485  } else {
486  $total = count($this->_running_vars['local_todo_counts']) + count($this->_running_vars['local_done_counts']);
487  $step_data['percent_done'] = (count($this->_running_vars['local_done_counts']) / $total) * 100;
488  $step_data['complete'] = FALSE;
489  }
490 
491  return TRUE;
492 
493  }//end importLocalFiles()
494 
495 
496 }//end class
497 
498 ?>