You are here

function PMPAPIDrupalPull::saveEntity in Public Media Platform API Integration 7

Saves a PMP doc as an entity.

Parameters

$doc object: A PMPAPIDrupal object

3 calls to PMPAPIDrupalPull::saveEntity()
PMPAPIDrupalPull::addItems in pmpapi_pull/classes/PMPAPIDrupalPull.php
Maps PMP doc items to entity fields.
PMPAPIDrupalPull::pullDoc in pmpapi_pull/classes/PMPAPIDrupalPull.php
Pulls a single doc from the PMP
PMPAPIDrupalPull::pullDocs in pmpapi_pull/classes/PMPAPIDrupalPull.php
Pulls docs from the PMP

File

pmpapi_pull/classes/PMPAPIDrupalPull.php, line 81
Contains PMPAPIDrupalPull.

Class

PMPAPIDrupalPull
Turns PMP hypermedia docs in Drupal entities.

Code

function saveEntity($doc) {
  $pieces = explode('/', $doc->links->profile[0]->href);
  $doc->profile = end($pieces);
  $mapped_entity = pmpapi_pull_find_mapped_entity($doc->profile);
  if ($mapped_entity) {
    $entity_type = $mapped_entity['entity_type'];
    $bundle_name = $mapped_entity['bundle_name'];
    $uname = $entity_type . '__' . $bundle_name;

    // is an update?
    $entity_id = pmpapi_get_eid_from_guid($doc->attributes->guid);
    if ($entity_id) {
      $entity = entity_load_single($entity_type, $entity_id);
    }
    else {
      $init = array(
        'type' => $bundle_name,
        'uid' => $this->uid,
        'name' => format_username(user_load($this->uid)),
        'pmpapi_guid' => $doc->attributes->guid,
        'pmpapi_pull' => TRUE,
      );
      $entity = entity_create($entity_type, $init);
    }

    // Add created -- or timestamp, if a file (we presume)
    $published = strtotime($doc->attributes->published);
    if ($entity_type == 'node') {
      $entity->created = $published;
    }
    else {
      $entity->timestamp = $published;
    }

    // Add valid->from and valid->to
    $entity->pmpapi_valid_from = $doc->attributes->valid->from;
    $entity->pmpapi_valid_to = $doc->attributes->valid->to;
    if (!empty($doc->links->enclosure[0])) {
      $file = $this
        ->createEnclosureFile($doc->links->enclosure[0], $doc->profile, $doc->attributes->guid);
      $entity = (object) array_merge((array) $file, (array) $entity);
    }
    if ($entity_type != 'file' && !pmpapi_doc_is_valid($doc)) {
      $entity->status = 0;
    }

    // This needs attention
    $entity->language = LANGUAGE_NONE;
    $map = variable_get('pmpapi_pull_mapping_' . $uname . '_' . $doc->profile);
    $entity_info = entity_get_info($entity_type);
    $label = !empty($entity_info['entity keys']['label']) ? $entity_info['entity keys']['label'] : NULL;
    $this
      ->mapFields($entity, $doc, $map, $label);
    $this
      ->addItems($entity, $doc, $map);

    // Fill up any unfilled fields
    foreach (field_info_instances($entity_type, $bundle_name) as $field_name => $field) {
      if (empty($entity->{$field_name})) {
        $values = array();
        $raw_values = $this
          ->getDefaultFieldValue($entity, $entity_type, $bundle_name, $field_name);
        foreach ($raw_values as $raw_value) {
          $values[] = $raw_value;
        }
        drupal_alter('pmpapi_pull_default_values', $values, $field_name);
        $entity->{$field_name}[$entity->language] = $values;
      }
    }
    if (!empty($doc->context)) {
      $entity->pmpapi_pull_context = $doc->context;
    }
    entity_save($entity_type, $entity);
    if ($label) {
      $uri = entity_uri($entity_type, $entity);
      $link = l($entity->{$label}, $uri['path']);
      drupal_set_message(t('Go to') . ' ' . $link);
    }
    return $entity;
  }
  else {
    drupal_set_message(t('Unable to pull the doc with GUID = @guid, as the profile %profile has not been mapped to any entity.', array(
      '@guid' => $doc->attributes->guid,
      '%profile' => $doc->profile,
    )), 'warning');
  }
}