You are here

function workbench_moderation_node_data in Workbench Moderation 7

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

Adds current and live revision data to a node.

Parameters

$node: The node being acted upon.

2 calls to workbench_moderation_node_data()
workbench_moderation_node_edit_page_override in ./workbench_moderation.module
Overrides the node/%/edit page to ensure the proper revision is shown.
workbench_moderation_node_load in ./workbench_moderation.module
Implements hook_node_load().

File

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

Code

function workbench_moderation_node_data($node) {

  // Make sure that this node type is moderated.
  if (!workbench_moderation_node_type_moderated($node->type)) {
    return;
  }

  // Path module is stupid and doesn't load its data in node_load.
  if (module_exists('path') && isset($node->nid)) {
    $path = array();
    $conditions = array(
      'source' => 'node/' . $node->nid,
    );
    $langcode = entity_language('node', $node);
    if ($langcode != LANGUAGE_NONE) {
      $conditions['language'] = $langcode;
    }
    $path = path_load($conditions);
    if ($path === FALSE) {
      $path = array();
    }
    if (isset($node->path)) {
      $path += $node->path;
    }
    $path += array(
      'pid' => NULL,
      'source' => 'node/' . $node->nid,
      'alias' => '',
      'language' => isset($node->language) ? $node->language : LANGUAGE_NONE,
    );
    $node->path = $path;
  }

  // Build a default 'current' moderation record. Nodes will lack a
  // workbench_moderation record if moderation was not enabled for their node
  // type when they were created. In that case, assume the live node is at the
  // current revision.
  $defaults = array(
    'hid' => NULL,
    'nid' => $node->nid,
    'vid' => $node->vid,
    'from_state' => workbench_moderation_state_none(),
    'state' => $node->status ? workbench_moderation_state_published() : workbench_moderation_state_none(),
    'uid' => $node->uid,
    'stamp' => $node->changed,
    'published' => $node->status,
    'is_current' => 1,
  );

  // We'll store moderation state information in an array on the node.
  $node->workbench_moderation = array();

  // Fetch the most recent revision from the {node_revision} table. This is the
  // current revision ("head").
  $query = db_select('node_revision', 'r');
  $query
    ->addJoin('LEFT OUTER', 'workbench_moderation_node_history', 'm', 'r.vid = %alias.vid');
  $query
    ->condition('r.nid', $node->nid)
    ->orderBy('r.vid', 'DESC')
    ->orderBy('m.hid', 'DESC')
    ->fields('m')
    ->fields('r', array(
    'title',
    'timestamp',
  ));
  $current = $query
    ->execute()
    ->fetchObject();
  if (!$current) {
    $current = (object) $defaults;
  }
  else {

    // Fill in any defaults that are missing from the database record. We need
    // to maintain false-y values except for NULL, so array_filter() +
    // array_merge() wouldn't work here.
    foreach (array_keys($defaults) as $key) {
      if (is_null($current->{$key})) {
        $current->{$key} = $defaults[$key];
      }
    }
  }
  $current->is_current = 1;
  $node->workbench_moderation['current'] = $current;

  // Fetch the published revision. There may not be a workbench_moderation
  // record for some nodes, but in those cases if the node is published,
  // $current->published will be TRUE.
  if ($current->published) {
    $published = $current;
  }
  else {

    // Fetch the most recent published revision.
    $query = db_select('node', 'n');
    $query
      ->addJoin('INNER', 'node_revision', 'r', 'n.vid = r.vid');
    $query
      ->addJoin('LEFT OUTER', 'workbench_moderation_node_history', 'm', 'r.vid = m.vid');
    $query
      ->condition('n.nid', $node->nid)
      ->condition('n.status', 1)
      ->orderBy('m.hid', 'DESC')
      ->fields('r', array(
      'nid',
      'vid',
      'title',
      'timestamp',
    ))
      ->fields('m');
    $published = $query
      ->execute()
      ->fetchObject();
  }

  // If we have a published copy, add that to the array.
  if ($published) {
    $published->state = workbench_moderation_state_published();
    $node->workbench_moderation['published'] = $published;
  }

  // Fetch the workbench_moderation record for this node object's revision. If
  // it is either the current or published revision of the node, that data will
  // be used.
  if ($node->vid == $current->vid) {
    $my_revision = $current;
  }
  elseif ($published && $node->vid == $published->vid) {
    $my_revision = $published;
  }
  else {
    $query = db_select('node_revision', 'r');
    $query
      ->addJoin('LEFT OUTER', 'workbench_moderation_node_history', 'm', 'r.vid = m.vid');
    $query
      ->condition('m.vid', $node->vid)
      ->orderBy('m.hid', 'DESC')
      ->fields('m')
      ->fields('r', array(
      'nid',
      'vid',
      'title',
      'timestamp',
    ));
    $my_revision = $query
      ->execute()
      ->fetchObject();

    // This might happen if you're turning workbench_moderation on and off, but
    // it should be really rare. Workbench_moderation must have recorded a
    // current revision, and then the node table must contain a different and
    // unpublished revision.
    if (!$my_revision) {
      $my_revision = (object) array(
        'hid' => NULL,
        'nid' => $node->nid,
        'vid' => $node->vid,
        'from_state' => workbench_moderation_state_none(),
        'state' => workbench_moderation_state_none(),
        'uid' => $node->uid,
        'stamp' => $node->changed,
        'published' => 0,
        'is_current' => 0,
      );
    }
  }

  // Add my revision to the array.
  $node->workbench_moderation['my_revision'] = $my_revision;
}