You are here

function _handle_view_op in Revisioning 6

Handle the 'view' operation on the node, displaying links as per the user's access permission.

Called from revisioning_nodeapi().

Parameters

$node: The node that is about to be viewed

Return value

nothing

1 call to _handle_view_op()
revisioning_nodeapi in ./revisioning.module
Implementation of hook_nodeapi().

File

./revisioning.module, line 562

Code

function _handle_view_op($node) {
  $args = arg();
  $access_view = module_grants_node_revision_access('view revisions', $node);
  $access_edit = module_grants_node_revision_access('edit revisions', $node);
  $access_delete = module_grants_node_revision_access('delete revisions', $node);

  // Given $access_view, the following three lines are more efficient short-cuts
  // for module_grants_node_revision_access('publish/revert/unpublish', $node);
  $access_publish = $access_view && user_access('publish revisions');
  $access_revert = $access_view && user_access('revert revisions');
  $access_unpublish = $access_view && user_access('unpublish current revision');
  $current_revision = _get_current_revision($node->nid);
  $is_current = $node->vid == $current_revision->vid;

  // Note that definition of pending is based on vid, not timestamp
  $is_pending = $node->vid > $current_revision->vid;
  $links = array();

  // Links to compare/revert/publish/delete revisions
  if ($access_view) {

    // Get username for the revision rather than the original node.
    $revision_author = user_load($node->revision_uid);
    $published = $node->status ? t('current, published') : t('current, unpublished');
    $placeholder_data = array(
      '%current' => $published,
      '%title' => check_plain($node->title),
      '!author' => theme('username', $revision_author),
      '@date' => format_date($node->revision_timestamp, 'small'),
    );

    // Messages below coded with some duplication to improve translatability
    if ($is_current) {
      $message = t('Displaying %current revision of %title, last modified by !author on @date', $placeholder_data);
    }
    else {
      $message = $is_pending ? t('Displaying pending revision of %title, last modified by !author on @date', $placeholder_data) : t('Displaying old revision of %title, last modified by !author on @date', $placeholder_data);
    }

    // Add a link to the diff if we have Diff module installed.
    if (!$is_current && module_exists('diff')) {
      $comparison_url = "node/{$node->nid}/revisions/view/";

      // Make sure that latest of the two revisions is on the right
      if ($is_pending) {
        $comparison_url .= "{$current_revision->vid}/{$node->vid}";
      }
      else {
        $comparison_url .= "{$node->vid}/{$current_revision->vid}";
      }
      $links[] = l(t('Compare to current'), $comparison_url);
    }
  }
  $base_url = "node/{$node->nid}/revisions/{$node->vid}";
  if ($access_edit) {
    $links[] = l(t('Edit this revision'), "{$base_url}/edit");
  }

  // If this revision is pending or current but not published, show a
  // publish link, otherwise show a revert link.
  if ($access_publish && ($is_pending || $is_current && !$node->status)) {
    $links[] = l(t('Publish this revision'), "{$base_url}/publish");
  }
  elseif ($access_revert && !$is_pending && !$is_current) {
    $links[] = l(t('Revert to this revision'), "{$base_url}/revert");
  }
  if ($access_unpublish && $is_current && $node->status) {
    $links[] = l(t('Unpublish this revision'), "node/{$node->nid}/unpublish");
  }
  if ($access_delete && !$is_current) {

    // Don't provide link to delete current -- node must point to a revision.
    $links[] = l(t('Delete this revision'), "{$base_url}/delete");
  }
  if ($access_view && $args[2] == 'revisions') {

    // Don't provide link to 'Show all revisions' when Revisions tab is available
    $links[] = l(t('Show all revisions'), "node/{$node->nid}/revisions");
  }
  if ($message) {

    // @todo create a revisioning theme, see theme.inc for examples
    drupal_set_message($message . theme('item_list', $links));
  }
}