You are here

function workbench_moderation_moderate in Workbench Moderation 7

Same name and namespace in other branches
  1. 7.3 workbench_moderation.module \workbench_moderation_moderate()

Provide quick moderation of nodes.

Access is controlled by the menu router to these pseudo-form callbacks. This function is also abstracted so that it can be called from any node context.

Parameters

$node: The node being acted upon.

$state: The new moderation state requested.

See also

_workbench_moderation_moderate_access()

workbench_moderation_menu()

workbench_moderation_node_update()

4 calls to workbench_moderation_moderate()
workbench_moderation_moderate_callback in ./workbench_moderation.module
Helper function to redirect after a state change submission.
workbench_moderation_moderate_form_submit in ./workbench_moderation.module
workbench_moderation_node_unpublish_form_submit in ./workbench_moderation.node.inc
Submit handler for unpublishing a live revision of a node.
workbench_moderation_node_update in ./workbench_moderation.module
Implements hook_node_update().

File

./workbench_moderation.module, line 1644
Content moderation for Workbench.

Code

function workbench_moderation_moderate($node, $state) {
  global $user;
  $old_revision = $node->workbench_moderation['my_revision'];

  // Get the number of revisions for this node with vids greater than $node->vid
  $vid_count = db_select('node_revision', 'r')
    ->condition('r.nid', $node->nid)
    ->condition('r.vid', $node->vid, '>')
    ->countQuery()
    ->execute()
    ->fetchField();

  // If the number of greater vids is 0, then this is the most current revision
  $current = $vid_count == 0;

  // Build a history record.
  $new_revision = (object) array(
    'from_state' => $old_revision->state,
    'state' => $state,
    'nid' => $node->nid,
    'vid' => $node->vid,
    'uid' => $user->uid,
    'is_current' => $current,
    'published' => $state == workbench_moderation_state_published(),
    'stamp' => $_SERVER['REQUEST_TIME'],
  );

  // If this is the new 'current' moderation record, it should be the only one
  // flagged 'current' in {workbench_moderation_node_history}.
  if ($new_revision->is_current) {
    $query = db_update('workbench_moderation_node_history')
      ->condition('nid', $node->nid)
      ->fields(array(
      'is_current' => 0,
    ))
      ->execute();
  }

  // If this revision is to be published, the new moderation record should be
  // the only one flagged 'published' in both
  // {workbench_moderation_node_history} AND {node_revision}
  if ($new_revision->published) {
    $query = db_update('workbench_moderation_node_history')
      ->condition('nid', $node->nid)
      ->fields(array(
      'published' => 0,
    ))
      ->execute();
    $query = db_update('node_revision')
      ->condition('nid', $node->nid)
      ->fields(array(
      'status' => 0,
    ))
      ->execute();
  }

  // Save the node history record.
  drupal_write_record('workbench_moderation_node_history', $new_revision);

  // Update the node's content_moderation information so that we can publish it
  // if necessary.
  $node->workbench_moderation['my_revision'] = $new_revision;
  if ($new_revision->is_current) {
    $node->workbench_moderation['current'] = $new_revision;
  }

  // Handle the published revision.
  if ($new_revision->published) {

    // If we're moderating a revision to the published state, mark the new
    // revision as the published revision.
    $node->workbench_moderation['published'] = $new_revision;
  }
  elseif (isset($node->workbench_moderation['published']) && $new_revision->vid == $node->workbench_moderation['published']->vid && $new_revision->from_state == workbench_moderation_state_published()) {

    // If we're moderating the published revision to a non-published state,
    // remove the workbench moderation 'published' property.
    $query = db_update('workbench_moderation_node_history')
      ->condition('hid', $node->workbench_moderation['published']->hid)
      ->fields(array(
      'published' => 0,
    ))
      ->execute();
    unset($node->workbench_moderation['published']);
    $node->workbench_moderation['current']->unpublishing = TRUE;
  }

  // If we need to make changes to the currently published node we do this in a
  // shutdown function to avoid race conditions when running node_save() from
  // within a node submission. We need to change the published node:
  // - If we're moderating an unpublished revision and there is an existing
  //   published revision, make sure that the published revision is live.
  // - If we are moving to unpublished state we should make sure the published
  //   revision is the 'current' revision.
  if (!empty($node->workbench_moderation['published']) || !empty($node->workbench_moderation['current']->unpublishing)) {

    // Clone the node to make sure our data arrives intact in the shutdown
    // function. It might still be altered before the shutdown is reached.
    drupal_register_shutdown_function('workbench_moderation_store', clone $node);
  }
  else {
    entity_get_controller('node')
      ->resetCache(array(
      $node->nid,
    ));
  }

  // Notify other modules that the state was changed.
  module_invoke_all('workbench_moderation_transition', $node, $old_revision->state, $state);
}