Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cmis_bridge.inc
1 <?php
2 
19 require_once SQ_INCLUDE_PATH.'/asset.inc';
20 require_once SQ_PACKAGES_PATH.'/sharepoint/lib/cmis_common.inc';
21 
33 class CMIS_Bridge extends Asset
34 {
35 
42  public $soap = NULL;
43 
50  function __construct($assetid=0)
51  {
52  $this->_ser_attrs = TRUE;
53  parent::__construct($assetid);
54 
55  }//end constructor
56 
57 
68  protected function _preCreateCheck(Array &$link)
69  {
70  if (!parent::_preCreateCheck($link)) return FALSE;
71 
72  $name = trim($this->attr('name'));
73  if ($name == '') {
74  trigger_localised_error('CORE0083', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
75  return FALSE;
76  }
77 
78  return TRUE;
79 
80  }//end _preCreateCheck()
81 
82 
93  protected function _createAdditional(Array &$link)
94  {
95  if (!parent::_createAdditional($link)) return FALSE;
96 
97  return $this->makeAndSaveInitialWebPath(strtolower($this->attr('name')), $link);
98 
99  }//end _createAdditional()
100 
101 
115  public function _getAllowedLinks()
116  {
117 
118  $allowed = parent::_getAllowedLinks();
119  $allowed[SQ_LINK_TYPE_1]['file'] = Array('card' => 'M', 'exclusive' => FALSE);
120  $allowed[SQ_LINK_TYPE_2]['file'] = Array('card' => 'M', 'exclusive' => FALSE);
121 
122  return $allowed;
123 
124  }//end _getAllowedLinks()
125 
126 
136  protected function _getName($short_name=FALSE)
137  {
138 
139  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), 0);
140  if (empty($values) === TRUE) {
141  return parent::_getName($short_name);
142  } else {
143  return $values[$this->id];
144  }
145 
146  }//end _getName()
147 
148 
158  public function connect($service_name, $verify = FALSE)
159  {
160 
161  if (!$verify && isset($this->soap[$service_name])) return TRUE;
162 
163  $cmis = new CMIS_Common();
164  $wsdl = $this->attr('wsdl');
165  $conn_info = Array(
166  'username' => $this->attr('username'),
167  'password' => $this->attr('password'),
168  'ws_security' => $this->attr('ws_security'),
169  'verify' => $verify,
170  'service_name'=> $service_name
171  );
172 
173  // Create and connect
174  if (!$cmis->connect($wsdl[$service_name], $conn_info)) return FALSE;
175  if (is_null($cmis->ptr)) return FALSE;
176 
177  $this->soap[$service_name] = $cmis;
178  return TRUE;
179 
180  }//end connect()
181 
182 
189  public function synchronizeCheck()
190  {
191 
192  $sync_info = Array(
193  'update_assetids' => Array(),
194  'create_objectIds' => Array(),
195  'delete_assetids' => Array()
196  );
197 
198  $dont_delete = Array();
199 
200  //metadata info
201  $current_files = $this->getCurrentFileInfo();
202 
203  //soap calls
204  $repositoryId = $this->getRepositoryId();
205  if (empty($repositoryId)) return $sync_info;
206 
207  $folderId = $this->getRootFolderId($repositoryId);
208  if (empty($folderId)) return $sync_info;
209 
210  $children_data = $this->getFolderChildren($repositoryId, $folderId);
211  if ($children_data === FALSE) return $sync_info;
212 
213  foreach ($children_data as $object_data){
214 
215  //if not document type or there is no contentStreamFileName this object is not applicable.
216  if ($object_data['cmis:objectTypeId'] != 'cmis:document' || !isset($object_data['cmis:contentStreamFileName'])) continue;
217 
218  $objectId = $object_data['cmis:objectId'];
219  if (array_key_exists($objectId, $current_files)) {
220  $assetid = $current_files[$objectId]['assetid'];
221  $dont_delete[] = $assetid;
222 
223  //check if needs updating
224  if (isset($current_files[$objectId]['lastModificationDate'])){
225  $lastModifiedCMIS = strtotime($object_data['cmis:lastModificationDate']);
226  $lastModifiedMetadata = str_replace('/', '-', $current_files[$objectId]['lastModificationDate']);
227  $lastModifiedMetadata = strtotime($lastModifiedMetadata);
228 
229  if ($lastModifiedCMIS > $lastModifiedMetadata){
230  $sync_info['update_assetids'][] = Array(
231  'object_data' => $object_data,
232  'assetid' => $assetid
233  );
234  }
235  }
236  } else {//mark for creation
237  $sync_info['create_objectIds'][] = $object_data;
238  }
239  }
240 
241  //anything unmarked mark for deletion.
242  foreach ($current_files as $objectid => $info){
243  if (!in_array($info['assetid'], $dont_delete)) {
244  $sync_info['delete_assetids'][] = $info['assetid'];
245  }
246  }
247 
248  return $sync_info;
249 
250  }//end synchronizeCheck()
251 
252 
260  public function getCurrentFileInfo()
261  {
262  $file_info = Array();
263 
264  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
265  $metadata_fields = $this->attr('metadata_fields');
266  if (!empty($metadata_fields['cmis:objectId']['field'])) $sync_fields['objectId'] = $metadata_fields['cmis:objectId']['field'];
267  if (!empty($metadata_fields['cmis:lastModificationDate']['field'])) $sync_fields['lastModificationDate'] = $metadata_fields['cmis:lastModificationDate']['field'];
268 
269  if (!isset($sync_fields['objectId'])) return $file_info;
270  $field_names = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_values($sync_fields), 'metadata_field', FALSE, 'name');
271 
272  $files = $GLOBALS['SQ_SYSTEM']->am->getChildren($this->id, 'file', FALSE);
273  foreach (array_keys($files) as $file_id){
274  $metadata_values = $mm->getMetadataFieldValues($file_id, array_values($field_names));
275  $objectId = $metadata_values[$field_names[$sync_fields['objectId']]];
276  if (!empty($objectId)) {
277  $file_info[$objectId]['assetid'] = $file_id;
278  if (isset($sync_fields['lastModificationDate'])){
279  $lastModificationDate = $metadata_values[$field_names[$sync_fields['lastModificationDate']]];
280  if (!empty($lastModificationDate)) $file_info[$objectId]['lastModificationDate'] = $lastModificationDate;
281  }
282  }
283  }
284 
285  return $file_info;
286  }//end getCurrentFileInfo()
287 
288 
296  public function getAvailableDocumentFields()
297  {
298  $fields = Array();
299  if (!$this->connect('RepositoryService')) return $fields;
300 
301  $repositoryId = $this->getRepositoryId();
302  if (empty($repositoryId)) return $fields;
303 
304  $type_def = $this->soap['RepositoryService']->getTypeDefinition($repositoryId, 'cmis:document');
305  if(!isset($type_def->type->q1id) || (string)$type_def->type->q1id != 'cmis:document') return $fields;
306 
307  $properties = Array (
308  'q1propertyBooleanDefinition',
309  'q1propertyDateTimeDefinition',
310  'q1propertyDecimalDefinition',
311  'q1propertyIdDefinition',
312  'q1propertyIntegerDefinition',
313  'q1propertyStringDefinition'
314  );
315 
316  $cmis_fields = Array (
317  'cmis:objectId',
318  'cmis:lastModificationDate',
319  'cmis:createdBy',
320  'cmis:creationDate'
321  );
322  foreach($properties as $propertyType) {
323  if (isset($type_def->type->$propertyType)){
324  $type = str_replace('Definition', '', substr($propertyType, 10));
325  foreach($type_def->type->$propertyType as $property){
326  $propertyId = (string)$property->q1id;
327  if (strpos($propertyId, 'cmis:') !== FALSE && !in_array($propertyId, $cmis_fields)) continue;
328  $fields[$propertyId] = Array (
329  'display_name' => (string)$property->q1displayName,
330  'type' => $type,
331  'field' => ''
332  );
333  }
334  }
335  }
336 
337  return $fields;
338 
339  }//end getAvailableDocumentFields()
340 
341 
348  public function getRepositoryId()
349  {
350  $repositoryId = '';
351 
352  if (!$this->connect('RepositoryService')) return $repositoryId;
353 
354  $repositories = $this->soap['RepositoryService']->getRepositories();
355  if(!isset($repositories->repositories)) return $repositoryId;
356 
357  foreach($repositories->repositories as $repository){
358  if ($repository->repositoryName == $this->attr('repository_name')){
359  $repositoryId = (string)$repository->repositoryId;
360  break;
361  }
362  }
363 
364  return $repositoryId;
365  }//end getRepositoryId()
366 
367 
376  public function getRootFolderId($repositoryId)
377  {
378  $folderId = '';
379  if (!$this->connect('RepositoryService')) return $folderId;
380 
381  $repositoryInfo = $this->soap['RepositoryService']->getRepositoryInfo($repositoryId);
382  if (isset($repositoryInfo->repositoryInfo->rootFolderId)) $folderId = (string)$repositoryInfo->repositoryInfo->rootFolderId;
383 
384  return $folderId;
385  }//end getRootFolderId()
386 
387 
398  public function getFolderChildren($repositoryId, $folderId)
399  {
400  $children_data = Array();
401  if (!$this->connect('NavigationService')) return FALSE;
402 
403  $children = $this->soap['NavigationService']->getChildren($repositoryId, $folderId);
404  if (isset($children->objects->numItems)){
405  if ((int)$children->objects->numItems == 0) return $children_data;
406  }
407  if (!isset($children->objects->objects)) return FALSE;
408 
409  $key = 0;
410  $properties = Array (
411  'propertyBoolean',
412  'propertyDateTime',
413  'propertyDecimal',
414  'propertyId',
415  'propertyInteger',
416  'propertyString'
417  );
418  foreach ($children->objects->objects as $object){
419  foreach($properties as $propertyType) {
420  if (isset($object->object->properties->$propertyType)){
421  foreach($object->object->properties->$propertyType as $property){
422  $attributes = $property->attributes();
423  $children_data[$key][(string)$attributes['propertyDefinitionId']] = (string)$property->value;
424  }
425  }
426  }
427  $key++;
428  }
429 
430 
431  return $children_data;
432  }//end getFolderChildren()
433 
434 
444  public function getObjectContents($repositoryId, $objectId)
445  {
446  $binary_data = '';
447  if (!$this->connect('ObjectService')) return $binary_data;
448 
449  $binary_data = $this->soap['ObjectService']->getContentStream($repositoryId, $objectId);
450  return $binary_data;
451 
452  }//end getObjectContents()
453 
454 
455 }//end class
456 ?>