public function TripalEntityController::save

3.x TripalEntityController.inc public TripalEntityController::save($entity, DatabaseTransaction $transaction = NULL)

Saves a new entity.

Parameters

$entity: A TripalEntity object to save.

Return value

The saved entity object with updated properties.

File

tripal/includes/TripalEntityController.inc, line 325

Class

TripalEntityController
TripalEntityController extends DrupalDefaultEntityController.

Code

public function save($entity, DatabaseTransaction $transaction = NULL) {
  global $user;
  $pkeys = array();

  // Get the author information.
  $author = $user;
  if (property_exists($entity, 'uid')) {
    $author = user_load($entity->uid);
  }

  $changed_date = time();
  $create_date = $changed_date;
  if (property_exists($entity, 'created')) {
    if (!is_numeric($entity->created)) {
      $temp = new DateTime($entity->created);
      $create_date = $temp->getTimestamp();
    }
  }

  $status = 1;
  if (property_exists($entity, 'status')) {
    if ($entity->status === 0 or $entity->status === 1) {
      $status = $entity->status;
    }
  }

  $transaction = isset($transaction) ? $transaction : db_transaction();
  try {
    // If our entity has no id, then we need to give it a
    // time of creation.
    if (empty($entity->id)) {
      $entity->created = time();
      $invocation = 'entity_insert';
    }
    else {
      $invocation = 'entity_update';
      $pkeys = array('id');
    }

    // Invoke hook_entity_presave().
    module_invoke_all('entity_presave', $entity, $entity->type);

    // Write out the entity record.
    $record = array(
      'term_id' => $entity->term_id,
      'type' => $entity->type,
      'bundle' => $entity->bundle,
      'title' => $entity->title,
      'uid' => $author->uid,
      'created' => $create_date,
      'changed' => $changed_date,
      'status' => $status,
    );
    if (property_exists($entity, 'nid') and $entity->nid) {
      $record['nid'] = $entity->nid;
    }
    if ($invocation == 'entity_update') {
      $record['id'] = $entity->id;
    }
    $success = drupal_write_record('tripal_entity', $record, $pkeys);
    if ($success == SAVED_NEW) {
      $entity->id = $record['id'];
    }

    // Now we need to either insert or update the fields which are
    // attached to this entity. We use the same primary_keys logic
    // to determine whether to update or insert, and which hook we
    // need to invoke.
    if ($invocation == 'entity_insert') {
      field_attach_insert('TripalEntity', $entity);
    }
    else {
      field_attach_update('TripalEntity', $entity);
    }

    // Set the title for this entity.
    $this->setTitle($entity);

    // Set the path/url alias for this entity.
    $this->setAlias($entity);

    // Invoke either hook_entity_update() or hook_entity_insert().
    module_invoke_all('entity_postsave', $entity, $entity->type);
    module_invoke_all($invocation, $entity, $entity->type);

    // Clear any cache entries for this entity so it can be reloaded using
    // the values that were just saved.
    $cid = 'field:TripalEntity:' . $entity->id;
    cache_clear_all($cid, 'cache_field', TRUE);

    return $entity;
  }
  catch (Exception $e) {
    $transaction->rollback();
    watchdog_exception('tripal', $e);
    drupal_set_message("Could not save the entity: " . $e->getMessage(), "error");
    return FALSE;
  }


}