You are here

class StateFlow in State Machine 6

Same name and namespace in other branches
  1. 7 modules/state_flow/plugins/state_flow.inc \StateFlow
  2. 7.2 modules/state_flow/plugins/state_flow.inc \StateFlow

Hierarchy

Expanded class hierarchy of StateFlow

1 string reference to 'StateFlow'
state_flow_state_flow_plugins in modules/state_flow/state_flow.module
Implementation of hook_state_flow_plugins().

File

modules/state_flow/plugins/state_flow.inc, line 9

View source
class StateFlow extends StateMachine {
  public function init() {

    // Initialize states
    $this
      ->create_state('draft');
    $this
      ->create_state('published', array(
      'on_enter' => array(
        $this,
        'on_enter_published',
      ),
      'on_exit' => array(
        $this,
        'on_exit_published',
      ),
    ));

    // Initialize events
    $this
      ->create_event('publish', array(
      'origin' => 'draft',
      'target' => 'published',
    ));
    $this
      ->create_event('unpublish', array(
      'origin' => 'published',
      'target' => 'draft',
    ));
  }

  /**
   * Returns an array of available states.
   */
  public function get_available_states() {
    return $this->states;
  }
  public function on_enter_published() {
    $this
      ->set_published();
    $this
      ->set_node_revision();
    $this
      ->set_principle_revision();
  }
  public function on_exit_published() {
    $this
      ->set_unpublished();
  }
  public function persist() {
    $vid = $this
      ->get_latest_revision($this->object->nid);
    $nid = $this->object->nid;
    $state = $this
      ->get_current_state();
    $data = array(
      'vid' => $vid,
      'nid' => $nid,
      'state' => $state,
      'timestamp' => time(),
      'status' => 1,
    );
    $update = $this
      ->existing_revision($nid, $vid) ? array(
      'vid',
    ) : NULL;
    $result = drupal_write_record('node_revision_states', $data, $update);
    return;
  }
  public function load() {
    return $this
      ->latest_state($this->object->nid);
  }
  public function set_published() {
    $this->object->status = 1;
    drupal_write_record('node', $this->object, 'nid');
  }
  public function set_unpublished() {
    $this->object->status = 0;
    drupal_write_record('node', $this->object, 'nid');
  }
  public function set_node_revision() {
    $vid = $this
      ->get_latest_revision($this->object->nid);
    if (!empty($vid) && $vid != $this->object->vid) {
      $this->object->vid = $vid;
      drupal_write_record('node', $this->object, 'nid');
    }
  }
  public function set_principle_revision() {
    $nid = $this->object->nid;
    $vid = $this
      ->get_latest_revision($nid);
    $result = db_query('UPDATE {node_revision_states} SET status = 0 WHERE nid = %d AND vid != %d', $nid, $vid);
    return;
  }
  public function get_latest_revision($nid) {
    $result = db_result(db_query('SELECT MAX(vid) FROM {node_revisions} WHERE nid = %d', $nid));
    return $result;
  }
  public function existing_revision($nid, $vid) {
    $result = db_result(db_query('SELECT COUNT(*) FROM {node_revision_states} WHERE nid = %d AND vid = %d', $nid, $vid));
    return $result ? TRUE : FALSE;
  }
  public function latest_state($nid) {
    $query = "SELECT state\n              FROM {node_revision_states}\n              WHERE nid = %d AND status = 1 AND vid = %d\n              ORDER BY timestamp\n              DESC\n              LIMIT 1";
    return db_result(db_query($query, $nid, $this
      ->get_latest_revision($nid)));
  }
  public function get_event($key) {
    if (!array_key_exists($key, $this->events)) {
      return FALSE;
    }
    if (is_array($this->events[$key])) {
      $options = $this->events[$key];
      $this->events[$key] = new StateFlow_Event($this, $options);
    }
    return $this->events[$key];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StateFlow::existing_revision public function
StateFlow::get_available_states public function Returns an array of available states.
StateFlow::get_event public function Return an event instance by key, lazy-loading the instance if necessary. Overrides StateMachine::get_event
StateFlow::get_latest_revision public function
StateFlow::init public function Initialize the state machine using the provided addState and addEvent methods. Overrides StateMachine::init
StateFlow::latest_state public function
StateFlow::load public function Load the current state from the given state storage Overrides StateMachine::load
StateFlow::on_enter_published public function
StateFlow::on_exit_published public function
StateFlow::persist public function Persist the current state to the object storage. Overrides StateMachine::persist
StateFlow::set_node_revision public function
StateFlow::set_principle_revision public function
StateFlow::set_published public function
StateFlow::set_unpublished public function
StateMachine::$current protected property
StateMachine::$events protected property
StateMachine::$initial protected property
StateMachine::$object protected property
StateMachine::$states protected property
StateMachine::create_event protected function Create a new event. This method does not actually create an event instance, it only stores the options array until an instance is requested from get_event().
StateMachine::create_state protected function Create a new state. This method does not actually create a state instance, it only stores the options array until an instance is requested from get_state().
StateMachine::fire_event public function Trigger an event to process a transition. Callbacks and guard conditions will be processed in the following order:
StateMachine::get_available_events public function Returns an array of events that are valid for the current state.
StateMachine::get_current_state public function Returns the current state.
StateMachine::get_state protected function Return a state instance by key, lazy-loading the instance if necessary.
StateMachine::on_event_fail protected function Method to be called when firing an event fails for any reason.
StateMachine::set_current_state protected function Set the current state to the state identified by the specified key.
StateMachine::set_initial_state protected function Set the initial state for this machine. By default, the initial state is set the the first created state.
StateMachine::__construct public function