You are here

function workbench_moderation_save in Workbench Moderation 7.3

Saves the moderation history for the node.

2 calls to workbench_moderation_save()
workbench_moderation_node_insert in ./workbench_moderation.module
Implements hook_node_insert().
workbench_moderation_node_update in ./workbench_moderation.module
Implements hook_node_update().

File

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

Code

function workbench_moderation_save($node) {
  $current_draft =& drupal_static(__FUNCTION__, 0);
  global $user;

  // Don't proceed if moderation is not enabled on this content or if we
  // are saving the published version from drafty.
  if (!workbench_moderation_node_moderated($node)) {
    return;
  }

  // Ensure that we have loaded our data onto the node. This function will
  // check that the required properties are set for all nodes.
  workbench_moderation_set_node_state($node);

  // Prepare the state information.
  $state = $node->workbench_moderation_state_new;
  $old_revision = $node->workbench_moderation['my_revision'];
  $node->is_current = FALSE;
  if (empty($node->default_revision)) {
    $node->is_current = TRUE;
  }

  // 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' => !empty($node->is_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 {workbench_moderation_node_history}.
  // Also applies in the case where we unpublish a live revision.
  if ($new_revision->published || !$node->status) {
    $query = db_update('workbench_moderation_node_history')
      ->condition('nid', $node->nid)
      ->fields(array(
      'published' => 0,
    ))
      ->execute();
  }

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

  // On a moderation loop, inform other modules of the change.
  if (!empty($node->is_current)) {

    // Clear the node's cache.
    entity_get_controller('node')
      ->resetCache(array(
      $node->nid,
    ));

    // Notify other modules that the state was changed.
    module_invoke_all('workbench_moderation_transition', $node, $node->workbench_moderation['my_revision']->state, $node->workbench_moderation_state_new);
  }
}