You are here

function _cmis_sync_drupal_cmis_prepare in CMIS API 7.2

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

Maps a drupal node to a cmis_object

Parameters

$node:

Return value

stdClass $cmis_object wrapper

1 call to _cmis_sync_drupal_cmis_prepare()
_cmis_sync_drupal_cmis_update in cmis_sync/cmis_sync.drupal.inc
Handles Drupal to CMIS updates.

File

cmis_sync/cmis_sync.drupal.inc, line 96

Code

function _cmis_sync_drupal_cmis_prepare($node) {
  $sync_map = variable_get('cmis_sync_map', array());

  // is cmis sync enabled for this node type?
  if (!empty($sync_map[$node->type]) && $sync_map[$node->type]['enabled']) {
    $cmis_object = new stdClass();

    // merge in defaults
    $sync_map[$node->type] += array(
      'fields' => array(),
      'content_field' => 'body',
      'cmis_type' => 'cmis:document',
      'cmis_repositoryId' => 'default',
      'subfolders' => FALSE,
      'deletes' => FALSE,
      'full_sync_next_cron' => FALSE,
    );

    // CMIS destination type
    $cmis_object->type = $sync_map[$node->type]['cmis_type'];

    // CMIS destination type
    $cmis_repository = cmis_get_repository($sync_map[$node->type]['cmis_repositoryId']);
    $cmis_object->repositoryId = $cmis_repository->repositoryId;

    // CMIS destination folder id
    if (isset($sync_map[$node->type]['cmis_folderId'])) {
      $cmis_object->folderId = $sync_map[$node->type]['cmis_folderId'];
    }
    elseif (isset($sync_map[$node->type]['cmis_folderPath'])) {
      $folder = cmisapi_getObjectByPath($cmis_repository->repositoryId, $sync_map[$node->type]['cmis_folderPath']);
      $cmis_object->folderId = $folder->id;
    }
    else {
      throw new CMISException(t("Please set `cmis_folderPath` or `cmis_folderId` properties for [@type] Drupal type.", array(
        '@type' => $node->type,
      )));
    }

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

    // map Drupal node field as object's content
    if (array_key_exists('content_field', $sync_map[$node->type])) {
      $cmis_object->content = _cmis_sync_drupal_node_field_value($node, $sync_map[$node->type]['content_field']);

      // setting content's content-type
      $cmis_object->properties['content-type'] = 'text/html';
      $fields = field_info_instances('node', $node->type);
      $content_field_name = $sync_map[$node->type]['content_field'];
      if (array_key_exists($content_field_name, $fields)) {
        if (in_array($fields[$content_field_name]['widget']['type'], array(
          'image_image',
          'file_generic',
        ))) {
          $items = field_get_items('node', $node, $content_field_name);
          $file = file_load($items[0]['fid']);
          $cmis_object->properties['content-type'] = $file->filemime;
        }
      }
    }

    // clean up properties array
    $cmis_object->contentStreamMimeType = $cmis_object->properties['content-type'];
    unset($cmis_object->properties['content-type']);

    // make sure that cmis:name property is filled.
    if (!array_key_exists('cmis:name', $cmis_object->properties)) {
      $cmis_object->properties['cmis:name'] = $node->title;
    }
    $cmis_object->name = $cmis_object->properties['cmis:name'];

    // sync deletes flag
    $cmis_object->sync_deletes = $sync_map[$node->type]['deletes'];

    // call hook_sync_drupal_cmis_prepare() hooks
    module_invoke_all('sync_drupal_cmis_prepare', $node, $cmis_object);
    return $cmis_object;
  }
  return FALSE;
}