You are here

function workbench_moderation_messages in Workbench Moderation 7

Same name and namespace in other branches
  1. 7.3 workbench_moderation.module \workbench_moderation_messages()
  2. 7.2 workbench_moderation.module \workbench_moderation_messages()

Sets status messages for a node.

Note that these status messages aren't relevant to the session, only the current page view.

Parameters

$context: A string, either 'view' or 'edit'.

$node: A node object. The current menu object will be used if it is a node and this variable was not set.

See also

workbench_moderation_set_message()

2 calls to workbench_moderation_messages()
workbench_moderation_form_node_form_alter in ./workbench_moderation.module
Implements hook_form_BASE_FORM_ID_alter().
workbench_moderation_node_view in ./workbench_moderation.module
Implements hook_node_view().

File

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

Code

function workbench_moderation_messages($context, $node = NULL) {
  global $user;
  static $workbench_moderation_messages_set;
  if (!user_access('view moderation messages') || !$node && !($node = menu_get_object()) || !workbench_moderation_node_type_moderated($node->type) || $workbench_moderation_messages_set) {
    return;
  }
  $node_published = FALSE;
  $revision_published = FALSE;
  $revision_current = FALSE;
  $workbench_moderation_messages_set = TRUE;

  // For new content, this property will not be set.
  if (isset($node->workbench_moderation)) {
    $state = $node->workbench_moderation;
    if (!empty($state['published'])) {
      $node_published = TRUE;
    }
    if ($state['my_revision']->published) {
      $revision_published = TRUE;
    }
    if ($state['my_revision']->vid == $state['current']->vid) {
      $revision_current = TRUE;
    }
  }

  // An array of messages to add to the general workbench block.
  $info_block_messages = array();
  if ($context == 'view') {
    if (workbench_moderation_messages_shown($context, $node)) {

      // Prevent multiple moderation status.
      return;
    }
    $info_block_messages[] = array(
      'label' => t('Revision state'),
      'message' => check_plain(workbench_moderation_state_label($state['my_revision']->state)),
    );
    $info_block_messages[] = array(
      'label' => t('Most recent revision'),
      'message' => !empty($revision_current) ? t('Yes') : t('No'),
    );

    // Check node access.
    drupal_static('_node_revision_access', array(), TRUE);

    // Add a moderation form.
    if ($revision_current && !$revision_published && _workbench_moderation_access('update', $node) && ($moderate_form = drupal_get_form('workbench_moderation_moderate_form', $node, "node/{$node->nid}/current-revision"))) {
      if ($moderate_form = drupal_render($moderate_form)) {
        $info_block_messages[] = array(
          'label' => t('Set moderation state'),
          'message' => $moderate_form,
        );
      }
    }

    // Add an unpublish link.
    $next_states = workbench_moderation_states_next(workbench_moderation_state_published(), $user, $node);
    if ($revision_published && !empty($next_states) && ($link = workbench_moderation_access_link(t('Unpublish this revision'), "node/{$node->nid}/moderation/{$node->vid}/unpublish"))) {
      $info_block_messages[] = array(
        'label' => t('Actions'),
        'message' => $link,
      );
    }

    // Revision navigation links. This is disabled for the time being, since
    // node tabs are lost when navigating through old revisions.
    // @TODO remove this entirely?
    if (variable_get('workbench_moderation_show_revision_navigation', FALSE) && user_access('view revisions')) {
      $links = array();

      // Get previous and next revision ids.
      $args = array(
        ':nid' => $node->nid,
        ':vid' => $node->vid,
      );
      if ($prev_vid = db_query_range("SELECT nr.vid FROM {node_revision} nr WHERE nr.nid = :nid AND nr.vid < :vid ORDER BY nr.vid DESC", 0, 1, $args)
        ->fetchField()) {
        $links[$prev_vid] = array(
          'title' => t('Previous revision'),
          'href' => "node/{$node->nid}/revisions/{$prev_vid}/view",
        );
      }
      if ($next_vid = db_query_range("SELECT nr.vid FROM {node_revision} nr WHERE nr.nid = :nid AND nr.vid > :vid ORDER BY nr.vid ASC", 0, 1, $args)
        ->fetchField()) {
        $links[$next_vid] = array(
          'title' => t('Next revision'),
          'href' => "node/{$node->nid}/revisions/{$next_vid}/view",
        );
      }

      // If the current revision is next or previous, use the "node/%node/current-revision" path.
      if (($current = $state['current']->vid) && isset($links[$current])) {
        $links[$current]['href'] = "node/{$node->nid}/current-revision";
      }

      // If the published revision is next or previous, use the "node/%node" path.
      if (isset($state['published']) && ($published = $state['published']->vid) && isset($links[$published])) {
        $links[$published]['href'] = "node/{$node->nid}";
      }

      // Link it up, with access checks.
      foreach ($links as $key => $args) {
        $links[$key] = call_user_func_array('workbench_moderation_access_link', $args);
      }

      // Post the links in a non-repeating message.
      if (!empty($links)) {
        $info_block_messages[] = array(
          'label' => t('View'),
          'message' => implode(', ', $links),
        );
      }
    }
  }
  elseif ($context == 'edit') {
    if ($node_published && $revision_published) {
      $info_block_messages[] = array(
        'label' => t('Status'),
        'message' => t('New draft of live content.'),
      );
    }
    elseif ($node_published && !$revision_published) {
      $info_block_messages[] = array(
        'label' => t('Status'),
        'message' => t('New draft from current revision'),
      );
      $link = workbench_moderation_access_link(t('Create a new draft from the published revision.'), "node/{$node->nid}/revisions/{$state['published']->vid}/revert");
      $info_block_messages[] = array(
        'label' => t('Actions'),
        'message' => $link,
      );
    }
    else {

      // New content.
      $info_block_messages[] = array(
        'label' => t('New content'),
        'message' => t('Your draft will be placed in moderation.'),
      );
    }
  }

  // Send the info block array to a static variable.
  workbench_moderation_set_message($info_block_messages);
}