You are here

function _cmis_sync_drupal_node_field_value in CMIS API 6.3

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

Utility that gets/sets node field value. Only supports regular, text and filefield fields types.

2 calls to _cmis_sync_drupal_node_field_value()
_cmis_sync_cmis_drupal_prepare in cmis_sync/cmis_sync.cmis.inc
Maps a cmis_object to a drupal node.
_cmis_sync_drupal_cmis_prepare in cmis_sync/cmis_sync.drupal.inc
Maps a drupal node to a cmis_object

File

cmis_sync/cmis_sync.drupal.inc, line 189

Code

function _cmis_sync_drupal_node_field_value(&$node, $field_name, $field_value = NULL, $context = array()) {
  if (isset($node)) {
    $content_type = content_types($node->type);
    $value = NULL;
    if (array_key_exists($field_name, $content_type['fields'])) {

      // CCK field
      $content_field = $node->{$field_name};
      switch ($content_type['fields'][$field_name]['type']) {
        case 'filefield':
          if ($field_value == NULL) {
            if (realpath($content_field[0]['filepath'])) {
              $value = file_get_contents($content_field[0]['filepath']);
            }
          }
          else {
            if (is_array($content_field) && is_array($content_field[0]) && array_key_exists('filepath', $content_field[0])) {

              // update Drupal file node on filesystem
              file_put_contents($content_field[0]['filepath'], $field_value);
            }
            else {

              // create Drupal file node on filesystem
              $file_context = array();
              if (array_key_exists('file', $context)) {
                $file_context = $context['file'];
              }

              // merge in defaults
              $file_context += array(
                'id' => uniqid('cmis_sync_'),
                'mime' => 'text/html',
              );

              // create file
              $file_drupal_path = file_directory_path() . '/' . $file_context['id'];
              file_put_contents($file_drupal_path, $field_value);

              // create file field
              $file = new stdClass();
              $file->filename = basename($file_drupal_path);
              $file->filepath = $file_drupal_path;
              $file->filemime = $file_context['mime'];
              $file->filesize = filesize($file_drupal_path);
              $file->status = FILE_STATUS_PERMANENT;
              $file->timestamp = time();
              drupal_write_record('files', $file);

              // create new filefield
              $node->{$field_name} = array(
                array(
                  'fid' => $file->fid,
                  'filename' => $file->filename,
                  'filepath' => $file->filepath,
                  'filesize' => $file->filesize,
                  'filemime' => $file->filemime,
                  'list' => 1,
                ),
              );
            }
          }
          break;
        case 'text':
        case 'date':
        case 'datestamp':
        case 'datetime':
        case 'number_decimal':
        case 'number_float':
        case 'number_integer':
          if ($field_value == NULL) {
            $value = $content_field[0]['value'];
          }
          else {
            $content_field[0]['value'] = $field_value;
            $node->{$field_name} = $content_field;
          }
          break;
        default:
          throw new CMISException(t('Unsupported sync drupal content field type [@field_type] for field [@field_name]. Please review your configuration.', array(
            '@field_name' => $field_name,
            '@field_type' => $content_type['fields'][$field_name]['type'],
          )));
          break;
      }
    }
    else {

      // regular node field value
      if ($field_value == NULL) {
        $value = $node->{$field_name};
      }
      else {
        $node->{$field_name} = $field_value;
      }
    }
    return $value;
  }
  else {
    return NULL;
  }
}