You are here

public function StateFlowEntity::get_history_entity in State Machine 7.3

Get the state flow history entity.

5 calls to StateFlowEntity::get_history_entity()
StateFlowEntity::delete_state_flow_revision in modules/state_flow_entity/plugins/state_flow_entity.inc
StateFlowEntity::entityPresave in modules/state_flow_entity/plugins/state_flow_entity.inc
Called by hook_entity_presave().
StateFlowEntity::entity_saved in modules/state_flow_entity/plugins/state_flow_entity.inc
Called by hook_entity_insert() / hook_entity_update().
StateFlowEntity::history_entity_form_submit_build_entity in modules/state_flow_entity/plugins/state_flow_entity.inc
Build up a full state flow history entity from form values.
StateFlowEntity::write_history in modules/state_flow_entity/plugins/state_flow_entity.inc
Write the transaction to the history table.

File

modules/state_flow_entity/plugins/state_flow_entity.inc, line 226
State Flow implementation of the State Machine class.

Class

StateFlowEntity
@file State Flow implementation of the State Machine class.

Code

public function get_history_entity() {
  $entity_type = $this
    ->get_entity_type();
  $id_key = $this
    ->get_entity_id_key();
  $revision_key = $this
    ->get_revision_key();

  // Check if the entity has a prepared history entity attached.
  $entity = $this
    ->get_object();
  if (!empty($entity->state_flow_history_entity)) {
    $this->history_entity = $entity->state_flow_history_entity;
  }
  if (empty($this->history_entity)) {

    // Make a new empty class for the history entity.
    $history_entity = new stdClass();

    // If this object has a revision key or id key, get it's previous history.
    // If this is a new object, like on the node add form, it won't have a
    // previous history object.
    if (!empty($entity->{$revision_key}) || !empty($entity->{$id_key})) {
      $query = db_select('state_flow_history', 'sfh')
        ->fields('sfh', array(
        'hid',
      ))
        ->orderBy('hid', 'DESC')
        ->range(0, 1)
        ->condition('entity_type', $entity_type);

      // Set a condition for the vid or nid.
      if (!empty($entity->{$revision_key})) {
        $query
          ->condition('revision_id', $entity->{$revision_key});
      }
      elseif (!empty($entity->{$id_key})) {
        $query
          ->condition('entity_id', $entity->{$id_key});
      }

      // Get the results of the query.
      $result = $query
        ->execute()
        ->fetchCol('hid');

      // Load up the fields of the history entity.
      if (!empty($result[0])) {
        $hid = $result[0];
        $history_entities = entity_load('state_flow_history_entity', array(
          'hid' => $hid,
        ));
        if (!empty($history_entities[$hid])) {
          $history_entity = clone $history_entities[$hid];
        }
      }

      // Unset the hid and the log message so those don't carry over.
      unset($history_entity->hid);
      unset($history_entity->log);
    }
    $this->history_entity = $history_entity;
    $this->history_entity->state = $this
      ->get_current_state();
  }

  // Populate the entity with current information.
  // Not sure how much of this is necessary.
  $this->history_entity->revision_id = NULL;
  if (!empty($entity->{$revision_key})) {
    $this->history_entity->revision_id = $entity->{$revision_key};
  }
  $this->history_entity->entity_id = NULL;
  if (!empty($entity->{$id_key})) {
    $this->history_entity->entity_id = $entity->{$id_key};
  }
  $this->history_entity->timestamp = REQUEST_TIME;
  $this->history_entity->entity_type = $entity_type;
  return $this->history_entity;
}