You are here

function _cmis_sync_cmis_drupal_handle_updates in CMIS API 7

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

Creates/updates Drupal nodes with CMIS content.

Parameters

$repository:

$sync_map_type:

$node_type:

1 call to _cmis_sync_cmis_drupal_handle_updates()
_cmis_sync_cmis_drupal_update in cmis_sync/cmis_sync.cmis.inc
Handles CMIS to Drupal updates.

File

cmis_sync/cmis_sync.cmis.inc, line 67

Code

function _cmis_sync_cmis_drupal_handle_updates($repository, $sync_map_type, $node_type) {

  // get CMIS object properties
  if (isset($sync_map_type['cmis_folderId'])) {
    $cmis_folder = cmisapi_getProperties($repository->repositoryId, $sync_map_type['cmis_folderId']);
  }
  elseif (isset($sync_map_type['cmis_folderPath'])) {
    $cmis_folder = cmisapi_getObjectByPath($repository->repositoryId, $sync_map_type['cmis_folderPath']);
  }
  else {
    throw new CMISException(t("Please set `cmis_folderPath` or `cmis_folderId` properties for [@type] Drupal type.", array(
      '@type' => $node_type,
    )));
  }

  // select updated objects
  $sync_subfolders_rule = $sync_map_type['subfolders'] ? 'IN_TREE' : 'IN_FOLDER';
  $sync_full_rule = $sync_map_type['full_sync_next_cron'] ? '' : sprintf('AND cmis:lastModificationDate >= \'%s\'', date_create('12 hour ago')
    ->format('Y-m-d\\TH:i:s.000-00:00'));

  // grab last updates
  $cmis_query = sprintf('SELECT * FROM %s WHERE %s(\'%s\') %s', $sync_map_type['cmis_type'], $sync_subfolders_rule, $cmis_folder->id, $sync_full_rule);
  $cmis_updates = cmisapi_query($repository->repositoryId, $cmis_query);
  foreach ($cmis_updates->objectList as $cmis_update) {

    // build/lookup Drupal node
    $drupal_node = _cmis_sync_cmis_drupal_prepare($repository, $sync_map_type, $node_type, $cmis_update);

    // unable to map current CMIS object to any Drupal content type
    if (FALSE === $drupal_node) {
      continue;
    }

    // mark the Drupal node in order to bypass nodeapi cmis_sync hook
    $drupal_node->cmis_sync_disabled = TRUE;

    // save Drupal node
    node_save($drupal_node);

    // update/insert changed timestamp
    $result = db_query('SELECT nid FROM {cmis_sync_node} WHERE cmis_objectId = :cid', array(
      ':cid' => $cmis_update->id,
    ));
    if ($result
      ->rowCount()) {
      db_update('cmis_sync_node')
        ->fields(array(
        'changed_timestamp' => $_SERVER['REQUEST_TIME'],
        'nid' => $drupal_node->nid,
      ))
        ->condition('cmis_objectId', $cmis_update->id)
        ->execute();
      watchdog('cmis_sync_cron', 'Updated nid @nid', array(
        '@nid' => $drupal_node->nid,
      ));
    }
    else {
      db_insert('cmis_sync_node')
        ->fields(array(
        'nid' => $drupal_node->nid,
        'cmis_repositoryId' => $repository->repositoryId,
        'cmis_objectId' => $cmis_update->id,
        'changed_timestamp' => $_SERVER['REQUEST_TIME'],
      ))
        ->execute();
      watchdog('cmis_sync_cron', 'Added nid @nid', array(
        '@nid' => $drupal_node->nid,
      ));
    }
  }
}