Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_sync_cmis_bridge.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_Sync_CMIS_Bridge($code_name='')
38  {
39  $this->uses_trans = FALSE;
40  $this->HIPO_Job($code_name);
41 
42  }//end constructor
43 
44 
51  function getCodeName()
52  {
53 
54  $repositoryId = '';
55  if (!empty($this->_running_vars['repositoryId'])) $repositoryId = $this->_running_vars['repositoryId'];
56 
57  return parent::getCodeName().'-'.$repositoryId;
58 
59  }//end getCodeName()
60 
61 
68  function getHipoName()
69  {
70  return translate('snyc_cmis_bridge_hipo_name');
71 
72  }//end getHipoName()
73 
74 
82  function getInitialStepData()
83  {
84  return Array(
85  Array(
86  'name' => 'Trash Files',
87  'function_call' => Array(
88  'process_function' => 'processTrashFiles',
89  ),
90  'running_mode' => 'server',
91  'auto_step' => TRUE,
92  'skip_step' => FALSE,
93  'allow_cancel' => TRUE,
94  'percent_done' => 0,
95  'complete' => FALSE,
96  'message' => '',
97  ),
98  Array(
99  'name' => 'Update Files',
100  'function_call' => Array(
101  'process_function' => 'processUpdateFiles',
102  ),
103  'running_mode' => 'server',
104  'auto_step' => TRUE,
105  'skip_step' => FALSE,
106  'allow_cancel' => TRUE,
107  'percent_done' => 0,
108  'complete' => FALSE,
109  'message' => '',
110  ),
111  Array(
112  'name' => 'Create Files',
113  'function_call' => Array(
114  'process_function' => 'processCreateFiles',
115  ),
116  'running_mode' => 'server',
117  'auto_step' => TRUE,
118  'skip_step' => FALSE,
119  'allow_cancel' => TRUE,
120  'percent_done' => 0,
121  'complete' => FALSE,
122  'message' => '',
123  ),
124  );
125 
126  }//end getInitialStepData()
127 
128 
135  function prepare()
136  {
137  if (empty($this->_running_vars['bridge_id'])) {
138  trigger_localised_error('SHAR0002', E_USER_WARNING);
139  return FALSE;
140  }
141 
142  //some tests before we start
143  $bridge = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->_running_vars['bridge_id']);
144  foreach ($bridge->attr('wsdl') as $service_name => $url){
145  $connected = $bridge->connect($service_name, TRUE);
146  if (!$connected) {
147  trigger_localised_error('SHAR0005', E_USER_WARNING, $bridge->id);
148  return FALSE;
149  }
150  }
151 
152  $repositoryId = $bridge->getRepositoryId();
153  if (empty($repositoryId)){
154  trigger_localised_error('SHAR0003', E_USER_WARNING, $bridge->id);
155  return FALSE;
156  }
157 
158  $this->_running_vars['repositoryId'] = $repositoryId;
159 
160  $sync_info = $bridge->synchronizeCheck();
161  if (!empty($sync_info['delete_assetids'])) {
162  $this->_running_vars['todo_trash_assetids'] = $sync_info['delete_assetids'];
163  $this->_running_vars['done_trash_assetids'] = Array();
164  }
165 
166  if (!empty($sync_info['update_assetids'])) {
167  $this->_running_vars['todo_update_assetids'] = $sync_info['update_assetids'];
168  $this->_running_vars['done_update_assetids'] = Array();
169  }
170 
171  if (!empty($sync_info['create_objectIds'])){
172  $this->_running_vars['todo_create_objectIds'] = $sync_info['create_objectIds'];
173  $this->_running_vars['done_create_objectIds'] = Array();
174  }
175 
176  return parent::prepare();
177 
178  }//end prepare()
179 
180 
181 
188  function freestyle()
189  {
190  while (!empty($this->_running_vars['todo_trash_assetids'])) {
191  if (!$this->processTrashFiles($this->_steps[0], get_class($this))) {
192  return FALSE;
193  }
194  }
195 
196  while (!empty($this->_running_vars['todo_update_assetids'])) {
197  if (!$this->processUpdateFiles($this->_steps[1], get_class($this))) {
198  return FALSE;
199  }
200  }
201 
202  while (!empty($this->_running_vars['todo_create_objectIds'])) {
203  if (!$this->processCreateFiles($this->_steps[2], get_class($this))) {
204  return FALSE;
205  }
206  }
207 
208  return TRUE;
209 
210  }//end freestyle()
211 
212 
222  function processTrashFiles(&$step_data, $prefix)
223  {
224  if (!empty($this->_running_vars['todo_trash_assetids'])) {
225  $assetid = array_shift($this->_running_vars['todo_trash_assetids']);
226  $step_data['message'] = "Trashing Asset (Id: #$assetid)";
227 
228  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_SECURE);
229  $GLOBALS['SQ_SYSTEM']->am->trashAsset($assetid);
230  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
231 
232  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
233  $asset->updateLookups();
234  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
235 
236  $this->_running_vars['done_trash_assetids'][] = $assetid;
237  }
238 
239  if (empty($this->_running_vars['todo_trash_assetids'])) {
240  $step_data['percent_done'] = 100;
241  $step_data['complete'] = TRUE;
242  } else {
243  $total = count($this->_running_vars['todo_trash_assetids']) + count($this->_running_vars['done_trash_assetids']);
244  $step_data['percent_done'] =(count($this->_running_vars['done_trash_assetids']) / $total) * 100;
245  $step_data['complete'] = FALSE;
246  }
247 
248  return TRUE;
249 
250  }//end processTrashFiles()
251 
252 
262  function processUpdateFiles(&$step_data, $prefix)
263  {
264  if (!empty($this->_running_vars['todo_update_assetids'])) {
265  $todo = array_shift($this->_running_vars['todo_update_assetids']);
266 
267  $bridge = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->_running_vars['bridge_id']);
268  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($todo['assetid']);
269  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($asset->id, 'all')) {
270  $binary = $bridge->getObjectContents($this->_running_vars['repositoryId'], $todo['object_data']['cmis:objectId']);
271  if (!empty($binary)) {
272  //Status changes
273  $available_statii = $asset->getAvailableStatii();
274  if (isset($available_statii[SQ_STATUS_UNDER_CONSTRUCTION])) $asset->processStatusChange(SQ_STATUS_UNDER_CONSTRUCTION);
275 
276  $full_path = tempnam($bridge->data_path, "cmis:update_");
277  file_put_contents($full_path, $binary);
278 
279  $file_name = $todo['object_data']['cmis:contentStreamFileName'];
280  $temp_info = Array('name' => $file_name, 'tmp_name' => $full_path, 'non_uploaded_file' => TRUE);
281 
282  require_once SQ_INCLUDE_PATH.'/backend_outputter.inc';
283  $o = new Backend_Outputter();
284  $edit_fns = $asset->getEditFns();
285  $edit_fns->processFileUpload($asset, $o, $prefix, $temp_info);
286 
287  $asset->setAttrValue('title', $todo['object_data']['cmis:contentStreamFileName']);
288  $asset->saveAttributes();
289 
290  $metadata = Array();
291  foreach ($bridge->attr('metadata_fields') as $property => $info){
292  if ($info['field'] && isset($todo['object_data'][$property])) $metadata[$info['field']][0]['value'] = $todo['object_data'][$property];
293  }
294  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
295  $mm->setMetadata($asset->id, $metadata);
296  $mm->regenerateMetadata($asset->id);
297 
298  $step_data['message'] = "Updated Asset $asset->name (Id: #$asset->id)";
299  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
300 
301  unlink($full_path);
302  } else {
303  $step_data['message'] = translate('hipo_skipping_asset', $asset->id);
304  $this->_addError(translate('snyc_cmis_bridge_hipo_can_not_update_file_no_content_stream', $todo['object_data']['cmis:contentStreamFileName']), TRUE);
305  }
306  $GLOBALS['SQ_SYSTEM']->am->releaseLock($asset->id, 'all');
307 
308  } else {
309  $step_data['message'] = translate('hipo_skipping_asset', $asset->id);
310  $this->_addError(translate('snyc_cmis_bridge_hipo_can_not_update_file', $asset->name, $asset->id), TRUE);
311  }
312 
313  $this->_running_vars['done_update_assetids'][] = $todo;
314  }
315 
316  if (empty($this->_running_vars['todo_update_assetids'])) {
317  $step_data['percent_done'] = 100;
318  $step_data['complete'] = TRUE;
319  } else {
320  $total = count($this->_running_vars['todo_update_assetids']) + count($this->_running_vars['done_update_assetids']);
321  $step_data['percent_done'] =(count($this->_running_vars['done_update_assetids']) / $total) * 100;
322  $step_data['complete'] = FALSE;
323  }
324 
325  return TRUE;
326 
327  }//end processUpdateFiles()
328 
329 
339  function processCreateFiles(&$step_data, $prefix)
340  {
341  if (!empty($this->_running_vars['todo_create_objectIds'])) {
342  $todo = array_shift($this->_running_vars['todo_create_objectIds']);
343  $bridge = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->_running_vars['bridge_id']);
344 
345  $binary = $bridge->getObjectContents($this->_running_vars['repositoryId'], $todo['cmis:objectId']);
346  if (!empty($binary)) {
347  $import_link = Array('asset' => &$bridge, 'link_type' => SQ_LINK_TYPE_1);
348 
349  $file_type = get_file_type($todo['cmis:contentStreamFileName']);
350  switch ($file_type) {
351  case 'doc' :
352  case 'docx':
353  case 'dot' :
354  $GLOBALS['SQ_SYSTEM']->am->includeAsset('word_doc');
355  $new_asset_type = 'word_doc';
356  break;
357  case 'pdf' :
358  $GLOBALS['SQ_SYSTEM']->am->includeAsset('pdf_file');
359  $new_asset_type = 'pdf_file';
360  break;
361  case 'xls' :
362  case 'xlt' :
363  case 'xlsx' :
364  case 'xlsm' :
365  case 'xltx' :
366  case 'xltm' :
367  $GLOBALS['SQ_SYSTEM']->am->includeAsset('excel_doc');
368  $new_asset_type = 'excel_doc';
369  break;
370  case 'ppt' :
371  case 'pot' :
372  case 'pps' :
373  case 'pptx' :
374  case 'potx' :
375  case 'ppsx' :
376  case 'pptm' :
377  case 'potm' :
378  case 'ppsm' :
379  $GLOBALS['SQ_SYSTEM']->am->includeAsset('powerpoint_doc');
380  $new_asset_type = 'powerpoint_doc';
381  break;
382  case 'rtf' :
383  $GLOBALS['SQ_SYSTEM']->am->includeAsset('rtf_file');
384  $new_asset_type = 'rtf_file';
385  break;
386  case 'txt' :
387  $GLOBALS['SQ_SYSTEM']->am->includeAsset('text_file');
388  $new_asset_type = 'text_file';
389  break;
390  case 'js' :
391  $GLOBALS['SQ_SYSTEM']->am->includeAsset('js_file');
392  $new_asset_type = 'js_file';
393  break;
394  case 'css' :
395  $GLOBALS['SQ_SYSTEM']->am->includeAsset('css_file');
396  $new_asset_type = 'css_file';
397  break;
398  case 'gif' :
399  case 'jpg' :
400  case 'jpeg' :
401  case 'png' :
402  $GLOBALS['SQ_SYSTEM']->am->includeAsset('image');
403  $new_asset_type = 'image';
404  break;
405  case 'mp3' :
406  $GLOBALS['SQ_SYSTEM']->am->includeAsset('mp3_file');
407  $new_asset_type = 'mp3_file';
408  break;
409  case 'mov' :
410  case 'avi' :
411  case 'wmv' :
412  case 'asf' :
413  case 'flv' :
414  case 'mp4' :
415  case 'm4v' :
416  case 'mpg' :
417  case 'mpeg':
418  $GLOBALS['SQ_SYSTEM']->am->includeAsset('video_file');
419  $new_asset_type = 'video_file';
420  break;
421  default :
422  $GLOBALS['SQ_SYSTEM']->am->includeAsset('file');
423  $new_asset_type = 'file';
424  break;
425  }
426 
427  $full_path = tempnam($bridge->data_path, "cmis:create_");
428  file_put_contents($full_path, $binary);
429 
430  $file_name = $todo['cmis:contentStreamFileName'];
431  $temp_info = Array('name' => $file_name, 'tmp_name' => $full_path, 'non_uploaded_file' => TRUE);
432  $new_file = new $new_asset_type();
433  $new_file->_tmp['uploading_file'] = TRUE;
434  $new_file->setAttrValue('name', $file_name);
435  $new_file->setAttrValue('title', $todo['cmis:contentStreamFileName']);
436  $new_file->setAttrValue('allow_unrestricted', TRUE);
437 
438  if (!$new_file->create($import_link, $temp_info)) {
439  $step_data['message'] = translate('snyc_cmis_bridge_hipo_skip_doc', $todo['cmis:contentStreamFileName']);
440  $this->_addError(translate('snyc_cmis_bridge_hipo_can_not_create_file', $todo['cmis:contentStreamFileName'], $bridge->id), TRUE);
441  } else {
442  $GLOBALS['SQ_SYSTEM']->am->acquireLock($new_file->id, 'metadata');
443 
444  $metadata = Array();
445  foreach ($bridge->attr('metadata_fields') as $property => $info){
446  if ($info['field'] && isset($todo[$property])) $metadata[$info['field']][0]['value'] = $todo[$property];
447  }
448  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
449  $mm->setMetadata($new_file->id, $metadata);
450  $mm->regenerateMetadata($new_file->id);
451 
452  $GLOBALS['SQ_SYSTEM']->am->releaseLock($new_file->id, 'metadata');
453 
454  $step_data['message'] = "Created Asset $new_file->name (Id: #$new_file->id)";
455  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($new_file);
456  }
457 
458  unlink($full_path);
459  } else {
460  $step_data['message'] = translate('snyc_cmis_bridge_hipo_skip_doc', $todo['cmis:contentStreamFileName']);
461  $this->_addError(translate('snyc_cmis_bridge_hipo_can_not_create_file_no_content_stream', $todo['cmis:contentStreamFileName']), TRUE);
462  }
463 
464  $this->_running_vars['done_create_objectIds'][] = $todo;
465 
466  }//end if
467 
468  if (empty($this->_running_vars['todo_create_objectIds'])) {
469  $step_data['percent_done'] = 100;
470  $step_data['complete'] = TRUE;
471  unset($this->_running_vars['done_create_objectIds']);
472  } else {
473  $total = count($this->_running_vars['todo_create_objectIds']) + count($this->_running_vars['done_create_objectIds']);
474  $step_data['percent_done'] = (count($this->_running_vars['done_create_objectIds']) / $total) * 100;
475  $step_data['complete'] = FALSE;
476  }
477 
478  return TRUE;
479 
480  }//end processCreateFiles()
481 
482 
483 }//end class
484 
485 ?>