You are here

function WorkflowState::deactivate in Workflow 7

Same name and namespace in other branches
  1. 7.2 includes/Entity/WorkflowState.php \WorkflowState::deactivate()

Deactivate a Workflow State, moving existing nodes to a given State.

Deprecated

workflow_delete_workflow_states_by_sid() --> WorkflowState->deactivate() + delete()

File

includes/Entity/WorkflowState.php, line 173
Contains workflow\includes\Entity\WorkflowState.

Class

WorkflowState
@file Contains workflow\includes\Entity\WorkflowState.

Code

function deactivate($new_sid) {
  $sid = $this->sid;

  // Notify interested modules. We notify first to allow access to data before we zap it.
  module_invoke_all('workflow', 'state delete', $sid, NULL, NULL, FALSE);

  // Node API: Re-parent any nodes that we don't want to orphan, whilst deactivating a State.
  // @todo Field API: Re-parent any nodes that we don't want to orphan, 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.
    $node = new stdClass();
    $node->workflow_stamp = REQUEST_TIME;
    foreach (workflow_get_workflow_node_by_sid($sid) as $data) {
      $node->nid = $data->nid;
      $node->workflow = $sid;
      $data = array(
        'nid' => $node->nid,
        'sid' => $new_sid,
        'uid' => $user->uid,
        'stamp' => $node->workflow_stamp,
      );
      workflow_update_workflow_node($data, $sid, t('Previous state deleted'));
    }
  }

  // Find out which transitions this state is involved in.
  $preexisting = array();
  foreach (workflow_get_workflow_transitions_by_sid_involved($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);
      }
    }
  }

  // Node API: Delete any lingering node to state values.
  workflow_delete_workflow_node_by_sid($sid);

  // @todo: Field API: Delete any lingering node to state values.

  //workflow_delete_workflow_field_by_sid($sid);

  // 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();
}