Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_import_file.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
31 {
32 
33 
39  function HIPO_Job_Import_File($code_name='')
40  {
41  $this->uses_trans = FALSE;
42  $this->HIPO_Job($code_name);
43  $GLOBALS['SQ_SYSTEM']->lm->includePackageStrings('file');
44  $this->_hipo_vars['job_dir'] = SQ_PACKAGES_PATH.'/filesystem/hipo_jobs';
45 
46  }//end constructor
47 
48 
58  public static function paintConfig(&$o, $class, $write_access)
59  {
60  $o->openField(translate('file_hipo_import_file_threshold'));
61 
62  if ($write_access) {
63  text_box($class.'[SQ_HIPO_IMPORT_FILE_THRESHOLD]', SQ_HIPO_IMPORT_FILE_THRESHOLD, 5);
64  } else {
65  echo SQ_HIPO_IMPORT_FILE_THRESHOLD;
66  }
67  echo ' '.translate('assets');
68 
69  $o->closeField();
70 
71  }//end paintConfig()
72 
73 
80  public static function getConfigVars()
81  {
82  return Array(
83  'SQ_HIPO_IMPORT_FILE_THRESHOLD' => Array('editable' => 1, 'default' => 5),
84  );
85 
86  }//end getConfigVars()
87 
88 
99  {
100  if (SQ_HIPO_IMPORT_FILE_THRESHOLD == 0) return 0;
101  if (!isset($this->_running_vars['todo_assetids'])) {
102  return 0;
103  }
104 
105  return ((count($this->_running_vars['todo_assetids']) / SQ_HIPO_IMPORT_FILE_THRESHOLD) * 100);
106 
107  }//end getThresholdPercentageRequired()
108 
109 
119  function getCodeName()
120  {
121  return parent::getCodeName().'-'.uniqid();
122 
123  }//end getCodeName()
124 
125 
132  function getHipoName()
133  {
134  return translate('file_hipo_name_import_file');
135 
136  }//end getHipoName()
137 
138 
147  {
148  return Array(
149  Array(
150  'name' => $this->getHipoName(),
151  'function_call' => Array(
152  'process_function' => 'processImport',
153  ),
154  'running_mode' => 'server',
155  'auto_step' => TRUE,
156  'percent_done' => 0,
157  'complete' => FALSE,
158  'message' => '',
159  'allow_cancel' => TRUE,
160  ),
161  );
162 
163  }//end getInitialStepData()
164 
165 
172  function freestyle()
173  {
174  while (!empty($this->_running_vars['todo_assetids']) || !empty($this->_running_vars['dependant_hipo_job'])) {
175  if (!$this->processImport($this->_steps[0], get_class_lower($this), TRUE)) {
176  return FALSE;
177  }
178  }
179  return TRUE;
180 
181  }//end freestyle()
182 
183 
190  function prepare()
191  {
192  $allowable_file_types = Array('physical_file', 'physical_folder');
193  $allowable_root_nodes = Array('physical_file', 'physical_folder', 'file_bridge');
194  // Parent (where to put the files/folders) bail if none set
195  if (empty($this->_running_vars['parent_assetid']) || is_null($GLOBALS['SQ_SYSTEM']->am->getAsset($this->_running_vars['parent_assetid']))) {
196  trigger_localised_error('FIL0003', E_USER_WARNING);
197  return FALSE;
198  }//end if
199 
200  // No root node is set, bail out...
201  if (is_null($this->_running_vars['root_assetid']) || empty($this->_running_vars['root_assetid'])) {
202  trigger_localised_error('FIL0001', E_USER_WARNING);
203  return FALSE;
204  }//end if
205  foreach ($this->_running_vars['root_assetid'] as $assetid) {
206  // so we have an assetid to start at, but make sure it is a valid assetid
207  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
208  if (is_null($asset)) {
209  trigger_localised_error('FIL0001', E_USER_WARNING);
210  return FALSE;
211  }//end if
212 
213  if (!in_array(strtolower($asset->type()), $allowable_root_nodes)) {
214  // What the heck..? We cannot import anything NOT under the file bridge
215  trigger_localised_error('FIL0002', E_USER_WARNING, $assetid);
216  return FALSE;
217  }//end if
218 
219  if (strtolower($asset->type()) == 'physical_file') {
220  $child_assets = Array(
221  $asset->id => Array(
222  Array(
223  'type_code' => $asset->type(),
224  ),
225  ),
226  );
227  } else {
228  $child_assets = $GLOBALS['SQ_SYSTEM']->am->getChildren($asset->id);
229  }//end if
230  $this->_running_vars['todo_assetids'] = $child_assets;
231  $this->_running_vars['done_assetids'] = Array();
232 
233  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset, TRUE);
234  unset($asset);
235  }//end foreach
236 
237  return parent::prepare();
238 
239  }//end prepare()
240 
241 
252  function processImport(&$step_data, $prefix, $freestyle=FALSE)
253  {
254  // process dependant hipo_job_import_file if necessary
255  if (!empty($this->_running_vars['dependant_hipo_job'])) {
256  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
257  $hipo = $hh->getJob($this->_running_vars['dependant_hipo_job']);
258 
259  // bail if we've completed this job
260  if (is_null($hipo)) return FALSE;
261 
262  if ($freestyle && $hipo->freestyle()) {
263  // we're freestylin' and that it, done
264  unset($this->_running_vars['dependant_hipo_job']);
265  $hipo->save();
266  } else if ($hipo->process() && $hipo->complete()) {
267  // process the sub hipo on each refresh
268  unset($this->_running_vars['dependant_hipo_job']);
269  }
270  return TRUE;
271 
272  }//end if
273 
274  $allowable_file_types = Array('physical_file', 'physical_folder', 'file_bridge');
275  if (!empty($this->_running_vars['todo_assetids'])) {
276 
277  // get next asset to import
278  $array_keys = array_keys($this->_running_vars['todo_assetids']);
279  $assetid = array_shift($array_keys);
280  unset($this->_running_vars['todo_assetids'][$assetid]);
281  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
282  $parent = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->_running_vars['parent_assetid']);
283 
284  if (!is_null($parent) && !is_null($asset) && in_array(strtolower($asset->type()), $allowable_file_types) && (!$this->_running_vars['index_file'] || $asset->name != 'index.html')) {
285  if (strtolower($asset->type()) == 'physical_file') {
286  $step_data['message'] = translate('file_importing_file', htmlentities($asset->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
287  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
288  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
289 
290  $file_path = $asset->getFilePath();
291  $file_asset_type = $this->_getFileAssetType($file_path);
292 
293  // Import file
294  $temp_info = Array(
295  'name' => $asset->name,
296  'tmp_name' => $file_path,
297  'non_uploaded_file' => TRUE,
298  );
299 
300  $GLOBALS['SQ_SYSTEM']->am->includeAsset($file_asset_type);
301  $new = new $file_asset_type();
302  $new->_tmp['uploading_file'] = TRUE;
303  $new->setAttrValue('name', $asset->name);
304  $new->setAttrValue('allow_unrestricted', FALSE);
305  $link = Array(
306  'asset' => &$parent,
307  'link_type' => SQ_LINK_TYPE_1,
308  'link_value' => '',
309  'sort_order' => 0,
310  'is_exclusive' => FALSE,
311  'is_dependant' => FALSE,
312  );
313  $created = $new->create($link, $temp_info);
314 
315  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
316  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
317  } else {
318  $step_data['message'] = translate('file_importing_folder', htmlentities($asset->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
319  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
320  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
321 
322  $folders_children = $GLOBALS['SQ_SYSTEM']->am->getChildren($asset->id);
323 
324  // If the folder has "index.html" file,
325  // all other files in the folder should be listed underneath the file
326  $index_file = FALSE;
327  if ($this->_running_vars['index_file']) {
328  $index_file_id = isset($folders_children[$asset->id.'/index.html']) ? $asset->id.'/index.html' : FALSE;
329  if ($index_file_id) {
330  $index_file = $GLOBALS['SQ_SYSTEM']->am->getAsset($index_file_id);
331  }
332  }
333  if ($index_file && $index_file->type() == 'physical_file') {
334  $GLOBALS['SQ_SYSTEM']->am->includeAsset('page_standard');
335  $new = new Page_Standard();
336  $new->setAttrValue('name', $asset->name);
337  $link = Array(
338  'asset' => &$parent,
339  'link_type' => SQ_LINK_TYPE_1,
340  'link_value' => '',
341  'sort_order' => 0,
342  'is_exclusive' => FALSE,
343  'is_dependant' => FALSE,
344  );
345  $created = $new->create($link);
346  // Save file's contnet into standard page
347  $file_path = $index_file->getFilePath();
348  $file_contents = file_get_contents($file_path);
349  if (!empty($file_contents)) {
350 
351  $bodycopy_div = $GLOBALS['SQ_SYSTEM']->am->getAsset($new->id+2);
352  $wysiwyg_content = $GLOBALS['SQ_SYSTEM']->am->getAsset($new->id+3);
353 
354  if (!is_null($bodycopy_div) && !is_null($wysiwyg_content)) {
355  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
356  $wysiwyg_content->setContent($file_contents);
357  $bodycopy_div_edit_fns = $bodycopy_div->getEditFns();
358  $bodycopy_div_edit_fns->generateContentFile($bodycopy_div);
359  $new->saveAttributes();
360  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
361 
362  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($bodycopy_div, TRUE);
363  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($wysiwyg_content, TRUE);
364  unset($bodycopy_div);
365  unset($wysiwyg_contnet);
366  unset($bodycopy_div_edit_fns);
367  unset($file_contents);
368  }//end if
369  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($bodycopy, TRUE);
370  unset($bodycopy);
371  }//end if
372 
373  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($index_file, TRUE);
374  unset($index_file);
375  } else {
376  // Import folder
377  $GLOBALS['SQ_SYSTEM']->am->includeAsset('folder');
378  $new = new Folder();
379  $new->setAttrValue('name', $asset->name);
380  $link = Array(
381  'asset' => &$parent,
382  'link_type' => SQ_LINK_TYPE_1,
383  'link_value' => '',
384  'sort_order' => 0,
385  'is_exclusive' => FALSE,
386  'is_dependant' => FALSE,
387  );
388  $created = $new->create($link);
389  }
390 
391  // If folder is empty we needn't and shouldn't spawn new hipo job for it
392  if (!empty($folders_children) && $created && $this->_running_vars['recursive']) {
393  if ($code_name = $this->spawnDependantImportFileHipo($asset->id, $new->id)) {
394  $this->_running_vars['dependant_hipo_job'] = $code_name;
395  }
396  }//end if
397 
398  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
399  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
400  }//end if
401 
402  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset, TRUE);
403  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($parent, TRUE);
404  unset($asset);
405  unset($parent);
406  }//end if
407 
408  // add this assetid to the done array
409  $this->_running_vars['done_assetids'][] = $assetid;
410  }//end if
411 
412  if (empty($this->_running_vars['todo_assetids'])) {
413  $step_data['percent_done'] = 100;
414  $step_data['complete'] = TRUE;
415  } else {
416  $total = count($this->_running_vars['todo_assetids']) + count($this->_running_vars['done_assetids']);
417  $step_data['percent_done'] =(count($this->_running_vars['done_assetids']) / $total) * 100;
418  $step_data['complete'] = FALSE;
419  }//end if
420 
421  return TRUE;
422 
423  }//end processImport()
424 
425 
435  function spawnDependantImportFileHipo($assetid, $parentid)
436  {
437  $init_hipo = new Hipo_Job_Import_File();
438 
439  $options['auto_complete'] = TRUE;
440 
441  $running_vars = Array(
442  'root_assetid' => Array($assetid),
443  'parent_assetid' => $parentid,
444  'process_dependants' => $assetid.'-'.$parentid,
445  'recursive' => TRUE,
446  'index_file' => $this->_running_vars['index_file'],
447  );
448 
449  $init_hipo->setRunningVars($running_vars);
450  foreach ($options as $k => $v) {
451  $init_hipo->setOption($k, $v);
452  }
453 
454  // create our sub-sub-hipo thingy, until this job is completed, we need
455  // to process it in place of our todo assets
456  $code_name = $init_hipo->initialise($this->source_code_name);
457  if ($code_name) return $code_name;
458  return FALSE;
459 
460  }//end spawnDependantImportFileHipo()
461 
462 
471  function _getFileAssetType($filename)
472  {
473  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
474  $extension = get_file_type($filename);
475 
476  switch($extension) {
477  case 'css':
478  $type = 'CSS_File';
479  break;
480 
481  case 'xls':
482  case 'xlt':
483  case 'xlsx':
484  case 'xlsm':
485  case 'xltx':
486  case 'xltm':
487  $type = 'Excel_Doc';
488  break;
489 
490  case 'flv':
491  $type = 'FLV_File';
492  break;
493 
494  case 'mp3':
495  $type = 'MP3_File';
496  break;
497 
498  case 'ppt':
499  case 'pot':
500  case 'pps':
501  case 'pptx':
502  case 'potx':
503  case 'ppsx':
504  case 'pptm':
505  case 'potm':
506  case 'ppsm':
507  $type = 'Powerpoint_Doc';
508  break;
509 
510  case 'txt':
511  $type ='Text_file';
512  break;
513 
514  case 'mov':
515  case 'avi':
516  case 'wmv':
517  case 'asf':
518  case 'flv':
519  case 'mp4':
520  case 'm4v':
521  case 'mpg':
522  case 'mpeg':
523  case 'ogv':
524  case 'ogg':
525  $type = 'Video_File';
526  break;
527 
528  case 'xml':
529  $type = 'Xml_File';
530  break;
531 
532  case 'gif':
533  case 'jpg':
534  case 'jpeg':
535  case 'png':
536  $type = 'Image';
537  break;
538 
539  case 'js':
540  $type = 'JS_File';
541  break;
542 
543  case 'pdf':
544  $type = 'PDF_File';
545  break;
546 
547  case 'rtf':
548  $type =' Rtf_File';
549  break;
550 
551  case 'doc':
552  case 'dot':
553  case 'docx':
554  case 'docm':
555  case 'dotx':
556  case 'dotm':
557  $type = 'Word_Doc';
558  break;
559 
560  case 'xsl':
561  $type = 'Xsl_File';
562  break;
563 
564  default:
565  $type = 'File';
566  break;
567 
568  }//end switch
569 
570  return $type;
571  }//end _getFileAssetType
572 
573 
574 }//end class
575 
576 ?>