You are here

function cmis_sync_nodeapi in CMIS API 6.2

Same name and namespace in other branches
  1. 6.4 cmis_sync/cmis_sync.module \cmis_sync_nodeapi()
  2. 6 cmis_sync/cmis_sync.module \cmis_sync_nodeapi()
  3. 6.3 cmis_sync/cmis_sync.module \cmis_sync_nodeapi()

Implementation of hook_nodeapi() for CMIS sync module.

File

cmis_sync/cmis_sync.module, line 55

Code

function cmis_sync_nodeapi(&$node, $op, $teaser, $page) {
  if (in_array($op, array(
    'insert',
    'update',
    'delete',
  ))) {
    $cmis_object = _cmis_sync_drupal_to_cmis_prepare($node);
    if ($cmis_object && !isset($node->cmis_sync_disabled)) {
      module_load_include('api.inc', 'cmis');
      switch ($op) {

        // Node created
        case 'insert':
          try {
            $repository = cmisapi_getRepositoryInfo();

            // Destination folder properties. Each Drupal node content type has it own synchronization folder.
            $cmis_parent = cmisapi_getProperties($repository->repositoryId, $cmis_object->parentId);

            // Creating a new document
            $cmis_objectId = cmisapi_createDocument($repository->repositoryId, $cmis_object->type, $cmis_object->properties, $cmis_parent->id, $cmis_object->content);

            // Updating CMIS reference object properties
            cmisapi_updateProperties($repository->repositoryId, $cmis_objectId, NULL, $cmis_object->properties);
          } catch (CMISException $e) {
            cmis_error_handler('cmis_sync_nodeapi', $e);
            return;
          }

          // Saving CMIS reference id in {cmis_sync_node} table
          db_query('INSERT INTO {cmis_sync_node} (nid, cmis_objectId, changed_timestamp) VALUES (%d, \'%s\', %d)', $node->nid, $cmis_objectId, $_SERVER['REQUEST_TIME']);
          break;

        // Node updated
        case 'update':

          // Send updates only if the current node has a CMIS reference id.
          if ($cmis_objectId = db_result(db_query('SELECT cmis_objectId FROM {cmis_sync_node} WHERE nid = %d', $node->nid))) {
            try {
              $repository = cmisapi_getRepositoryInfo();

              // Updating CMIS reference object content
              cmisapi_setContentStream($repository->repositoryId, $cmis_objectId, TRUE, $cmis_object->content, $cmis_object->properties);

              // Updating CMIS reference object properties
              cmisapi_updateProperties($repository->repositoryId, $cmis_objectId, NULL, $cmis_object->properties);
            } catch (CMISException $e) {
              cmis_error_handler('cmis_sync_nodeapi', $e);
              return;
            }

            // Update changed timestamp
            db_query('UPDATE {cmis_sync_node} SET changed_timestamp=%d WHERE nid = %d', $_SERVER['REQUEST_TIME'], $node->nid);
          }
          break;

        // Node delete
        case 'delete':
          if ($cmis_object->sync_deletes && ($cmis_objectId = db_result(db_query('SELECT cmis_objectId FROM {cmis_sync_node} WHERE nid = %d', $node->nid)))) {
            try {

              // Delete CMIS reference object content
              $repository = cmisapi_getRepositoryInfo();
              $cmis_object_properties = cmisapi_getProperties($repository->repositoryId, $cmis_objectId);
              cmisapi_deleteObject($repository->repositoryId, $cmis_object_properties->id);
            } catch (CMISException $e) {
              cmis_error_handler('cmis_sync_nodeapi', $e);
              return;
            }

            // Delete sync refs
            db_query('DELETE FROM {cmis_sync_node} WHERE nid = %d', $node->nid);
          }
          break;
      }
    }
  }
}