You are here

function _cmis_sync_drupal_cmis_update in CMIS API 7

Same name and namespace in other branches
  1. 6.4 cmis_sync/cmis_sync.drupal.inc \_cmis_sync_drupal_cmis_update()
  2. 6.3 cmis_sync/cmis_sync.drupal.inc \_cmis_sync_drupal_cmis_update()
  3. 7.2 cmis_sync/cmis_sync.drupal.inc \_cmis_sync_drupal_cmis_update()

Handles Drupal to CMIS updates.

3 calls to _cmis_sync_drupal_cmis_update()
cmis_sync_node_delete in cmis_sync/cmis_sync.module
Implements hook_node_delete
cmis_sync_node_insert in cmis_sync/cmis_sync.module
Implements hook_node_insert
cmis_sync_node_update in cmis_sync/cmis_sync.module
Implements hook_node_update

File

cmis_sync/cmis_sync.drupal.inc, line 6

Code

function _cmis_sync_drupal_cmis_update($node, $op) {
  module_load_include('api.inc', 'cmis');
  $cmis_object = _cmis_sync_drupal_cmis_prepare($node);
  if ($cmis_object && !isset($node->cmis_sync_disabled)) {
    switch ($op) {

      // node created
      case 'insert':
        try {
          $new_cmis_object = cmisapi_createDocument($cmis_object->repositoryId, $cmis_object->folderId, $cmis_object->name, $cmis_object->properties, $cmis_object->content, $cmis_object->contentStreamMimeType);
        } catch (CMISException $e) {
          cmis_error_handler('cmis_sync_nodeapi', $e);
          return;
        }

        // saving CMIS reference id in {cmis_sync_node} table
        db_insert('cmis_sync_node')
          ->fields(array(
          'nid' => $node->nid,
          'cmis_repositoryId' => $cmis_object->repositoryId,
          'cmis_objectId' => $new_cmis_object->id,
          'changed_timestamp' => $_SERVER['REQUEST_TIME'],
        ))
          ->execute();
        break;

      // node updated
      case 'update':

        // send updates only if the current node has a CMIS reference id.
        $result = db_query('SELECT cmis_objectId FROM {cmis_sync_node} WHERE nid = :nid', array(
          ':nid' => $node->nid,
        ));
        foreach ($result as $row) {
          try {

            // updating CMIS reference object content stream
            if ($cmis_object->content) {
              cmisapi_setContentStream($cmis_object->repositoryId, $row->cmis_objectId, $cmis_object->content, $cmis_object->contentStreamMimeType);
            }

            // updating CMIS properties
            cmisapi_updateProperties($cmis_object->repositoryId, $row->cmis_objectId, $cmis_object->properties);

            // update changed timestamp
            db_query('UPDATE {cmis_sync_node} SET changed_timestamp = :changed WHERE nid = :nid', array(
              ':changed' => $_SERVER['REQUEST_TIME'],
              ':nid' => $node->nid,
            ));
          } catch (CMISException $e) {
            cmis_error_handler('cmis_sync_nodeapi', $e);
            return;
          }
        }
        break;

      // node delete
      case 'delete':
        $result = db_query('SELECT cmis_objectId FROM {cmis_sync_node} WHERE nid = :nid', array(
          ':nid' => $node->nid,
        ));
        if ($cmis_object->sync_deletes) {
          foreach ($result as $row) {
            try {

              // delete CMIS reference object content
              $cmis_object_properties = cmisapi_getProperties($cmis_object->repositoryId, $row->cmis_objectId);
              cmisapi_deleteObject($cmis_object->repositoryId, $cmis_object_properties->id);

              // delete sync refs
              db_delete('cmis_sync_node')
                ->condition('nid', $node->nid)
                ->execute();
            } catch (CMISException $e) {
              cmis_error_handler('cmis_sync_nodeapi', $e);
              return;
            }
          }
        }
        break;
    }
  }
}