/**
  * Get all states in the system, with options to filter, only where a workflow exists.
  *
  * @param $wid
  *   The requested Workflow ID.
  * @param bool $reset
  *   An option to refresh all caches.
  *
  * @return array $states
  *   An array of cached states.
  *
  * D7.x-2.x: deprecated workflow_get_workflow_states --> workflow_state_load_multiple
  * D7.x-2.x: deprecated workflow_get_workflow_states_all --> workflow_state_load_multiple
  * D7.x-2.x: deprecated workflow_get_other_states_by_sid --> workflow_state_load_multiple
  */
 public static function getStates($wid = 0, $reset = FALSE)
 {
     if ($reset) {
         self::$states = array();
     }
     if (empty(self::$states)) {
         // Build the query, and get ALL states.
         // Note: self::states[] is populated in respective constructors.
         $query = db_select('workflow_states', 'ws');
         $query->fields('ws');
         $query->orderBy('ws.weight');
         $query->orderBy('ws.wid');
         // Just for grins, add a tag that might result in modifications.
         $query->addTag('workflow_states');
         // @see #2285983 for using SQLite.
         // $query->execute()->fetchAll(PDO::FETCH_CLASS, 'WorkflowState');
         /* @var $tmp DatabaseStatementBase */
         $statement = $query->execute();
         $statement->setFetchMode(PDO::FETCH_CLASS, 'WorkflowState');
         foreach ($statement->fetchAll() as $state) {
             self::$states[$state->sid] = $state;
         }
     }
     if (!$wid) {
         // All states are requested and cached: return them.
         return self::$states;
     } else {
         // All states of only 1 Workflow is requested: return this one.
         $result = array();
         foreach (self::$states as $state) {
             if ($state->wid == $wid) {
                 $result[$state->sid] = $state;
             }
         }
         return $result;
     }
 }
 /**
  * Deactivate a Workflow State, moving existing nodes to a given State.
  *
  * @param $new_sid
  *  the state ID, to which all affected entities must be moved. 
  *
  * @deprecated workflow_delete_workflow_states_by_sid() --> WorkflowState->deactivate() + delete()
  */
 public function deactivate($new_sid)
 {
     $current_sid = $this->sid;
     $force = TRUE;
     // Notify interested modules. We notify first to allow access to data before we zap it.
     // E.g., Node API (@todo Field API):
     // - re-parents any nodes that we don't want to orphan, whilst deactivating a State.
     // - delete any lingering node to state values.
     module_invoke_all('workflow', 'state delete', $current_sid, $new_sid, NULL, $force);
     // Re-parent any nodes that we don't want to orphan, whilst deactivating a State.
     // This is called in WorkflowState::deactivate().
     // @todo: reparent Workflow Field, whilst deactivating a state.
     if ($new_sid) {
         global $user;
         // A candidate for the batch API.
         // @TODO: Future updates should seriously consider setting this with batch.
         $comment = t('Previous state deleted');
         foreach (workflow_get_workflow_node_by_sid($current_sid) as $workflow_node) {
             // @todo: add Field support in 'state delete', by using workflow_node_history or reading current field.
             $entity_type = 'node';
             $entity = entity_load_single('node', $workflow_node->nid);
             $field_name = '';
             $transition = new WorkflowTransition($entity_type, $entity, $field_name, $current_sid, $new_sid, $user->uid, REQUEST_TIME, $comment);
             $transition->force($force);
             // Excute Transition, invoke 'pre' and 'post' events, save new state in workflow_node, save also in workflow_node_history.
             // For Workflow Node, only {workflow_node} and {workflow_node_history} are updated. For Field, also the Entity itself.
             $new_sid = workflow_execute_transition($entity_type, $entity, $field_name, $transition, $force);
         }
     }
     // Delete any lingering node to state values.
     workflow_delete_workflow_node_by_sid($current_sid);
     // Find out which transitions this state is involved in.
     $preexisting = array();
     foreach (workflow_get_workflow_transitions_by_sid_involved($current_sid) as $data) {
         $preexisting[$data->sid][$data->target_sid] = TRUE;
     }
     // Delete the transitions.
     foreach ($preexisting as $from => $array) {
         foreach (array_keys($array) as $target_id) {
             if ($transition = workflow_get_workflow_transitions_by_sid_target_sid($from, $target_id)) {
                 workflow_delete_workflow_transitions_by_tid($transition->tid);
             }
         }
     }
     // Delete the state. -- We don't actually delete, just deactivate.
     // This is a matter up for some debate, to delete or not to delete, since this
     // causes name conflicts for states. In the meantime, we just stick with what we know.
     // If you really want to delete the states, use workflow_cleanup module, or delete().
     $this->status = FALSE;
     $this->save();
     // Clear the cache.
     self::$states = array();
 }