You are here

public function StateFlowEntity::write_state in State Machine 7.3

Update the current status record for the revision.

Only one revision may be active at a time. If additional revisions need to be active, please use fields for now.

Parameters

object $history_entity: The related history entity.

bool $active: Define if this is the active revision.

See also

state_flow_entity_events_revision()

1 call to StateFlowEntity::write_state()
StateFlowEntity::write_active in modules/state_flow_entity/plugins/state_flow_entity.inc
Update the current status record for the revision.

File

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

Class

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

Code

public function write_state($history_entity, $active = TRUE) {

  // First default all revisions to inactive status.
  // @todo, can this happen in a less hard-coded fashion to accommodate sites
  // That may need multiple active revisions?
  if ($active) {
    db_update('state_flow_states')
      ->fields(array(
      'active' => 0,
    ))
      ->condition('entity_id', $history_entity->entity_id)
      ->condition('entity_type', $history_entity->entity_type)
      ->execute();
  }

  // Store current status, add a new record if it's a new revision or a new
  // entity.
  if (isset($this->object->revision) || $this
    ->object_is_new()) {
    if ($this->object->revision || $this
      ->object_is_new()) {
      db_insert('state_flow_states')
        ->fields(array(
        'entity_type' => $history_entity->entity_type,
        'entity_id' => $history_entity->entity_id,
        'revision_id' => $history_entity->revision_id,
        'hid' => $history_entity->hid,
        'state' => $this
          ->get_current_state(),
        'active' => (int) $active,
        'timestamp' => REQUEST_TIME,
      ))
        ->execute();
    }
    else {

      // Set existing revision as active so we can hit this call twice when
      // editing a node. Require hid so that only one record is updated.
      db_update('state_flow_states')
        ->fields(array(
        'active' => (int) $active,
        'state' => $this
          ->get_current_state(),
        'hid' => $history_entity->hid,
      ))
        ->condition('entity_id', $history_entity->entity_id)
        ->condition('entity_type', $history_entity->entity_type)
        ->condition('revision_id', $history_entity->revision_id)
        ->execute();
    }
  }
  else {

    // Not a new revision AND transition only i.e. the user did not come
    // from an entity edit form like node/%node/edit unlike the db_update
    // above, we do not need to require the hid because a transition-only
    // operation only hits this method once.
    db_update('state_flow_states')
      ->fields(array(
      'active' => (int) $active,
      'state' => $this
        ->get_current_state(),
      'hid' => $history_entity->hid,
    ))
      ->condition('entity_id', $history_entity->entity_id)
      ->condition('entity_type', $history_entity->entity_type)
      ->condition('revision_id', $history_entity->revision_id)
      ->execute();
  }
}