You are here

function _cmis_sync_cmis_drupal_prepare in CMIS API 6.3

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

Maps a cmis_object to a drupal node.

@todo Add workflow properties

Parameters

$cmis_repository :

$sync_map_type Sync rules for current type:

$cmis_object:

Return value

$drupal_node

1 call to _cmis_sync_cmis_drupal_prepare()
_cmis_sync_cmis_drupal_handle_updates in cmis_sync/cmis_sync.cmis.inc
Creates/updates Drupal nodes with CMIS content.

File

cmis_sync/cmis_sync.cmis.inc, line 162

Code

function _cmis_sync_cmis_drupal_prepare($repository, $sync_map_type, $node_type, $cmis_object) {
  module_load_include('api.inc', 'cmis');
  if ($sync_map_type['enabled']) {
    module_load_include('drupal.inc', 'cmis_sync');
    $drupal_nid = NULL;

    // identify Drupal nid
    if (!array_key_exists('nid', $sync_map_type['fields'])) {
      if ($cmis_sync_node = db_fetch_object(db_query('SELECT nid FROM {cmis_sync_node} WHERE cmis_objectId = \'%s\'', $cmis_object->id))) {
        $drupal_nid = $cmis_sync_node->nid;
      }
    }
    else {
      $drupal_nid = $cmis_object->properties[$sync_map_type['fields']['nid']];
    }

    // load Drupal node
    $node = node_load($drupal_nid);
    $node->type = $node_type;

    // map cmis properties to drupal node fields
    foreach ($sync_map_type['fields'] as $node_field => $cmis_field) {
      if (is_string($cmis_field)) {
        _cmis_sync_drupal_node_field_value($node, $node_field, $cmis_object->properties[$cmis_field]);
      }
      elseif (is_array($cmis_field)) {
        if (array_key_exists('cmis to drupal', $cmis_field) && $cmis_field['cmis to drupal'] === FALSE) {
          continue;
        }
        _cmis_sync_drupal_node_field_value($node, $cmis_field['drupal'], $cmis_object->properties[$cmis_field['cmis']]);
      }
      else {
        throw new CMISException(t('Unknown field map type. Expects "string" or "array". Received "@type"', array(
          '@type' => gettype($cmis_field),
        )));
      }
    }

    // fix node title
    if (!isset($node->title)) {
      $node->title = $cmis_object->properties['cmis:name'];
    }

    // load content field
    if (array_key_exists('content_field', $sync_map_type)) {
      $cmis_content_context = array();
      $cmis_content_context['file'] = array(
        'mime' => $cmis_object->properties['cmis:contentStreamMimeType'],
        'id' => _cmis_sync_cmis_drupal_file_id($repository, $cmis_object),
      );
      try {
        _cmis_sync_drupal_node_field_value($node, $sync_map_type['content_field'], cmisapi_getContentStream($repository->repositoryId, $cmis_object->id), $cmis_content_context);
      } catch (CMISException $e) {

        // @todo: handle error better, there could be a lot of issues in addition to "empty" bodies, check for error codes other than 404
        watchdog('cmis_sync_cmis', 'Error retrieving content for node #@nid - @title - @code - @message', array(
          '@nid' => $node->nid,
          '@title' => $node->title,
          '@code' => $e
            ->getCode(),
          '@message' => $e
            ->getMessage(),
        ), WATCHDOG_WARNING);
      }
    }

    // call hook_sync_cmis_drupal_prepare() hooks
    module_invoke_all('sync_cmis_drupal_prepare', $cmis_object, &$node);
    return $node;
  }
  return FALSE;
}