You are here

function revisioning_nodeapi in Revisioning 6.3

Same name and namespace in other branches
  1. 6.4 revisioning.module \revisioning_nodeapi()
  2. 6 revisioning.module \revisioning_nodeapi()

Implementation of hook_nodeapi().

This function is called serveral times during the node's life cycle, with different node operations passed in.

Typically when loading a node for viewing, the order is: 'load', 'view', 'alter'

When creating new content: Before displaying the creation form: 'prepare' When saving: 'validate', 'presave', 'insert'

When editing an existing node: Before displaying the edit form: 'load', 'prepare' When saving: 'load', 'validate', 'presave', 'update'

The same $op may be requested multiple times during the same HTTP request, especially 'load'.

File

./revisioning.module, line 370
Allows the creation and modification of pre-published as well as live content while the current revision remains unchanged and publicly visible until the changes have been reviewed by a moderator.

Code

function revisioning_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  if (!empty($node->bypass_nodeapi)) {
    return;
  }
  switch ($op) {
    case 'load':

      // called at the end of node_load()
      // The revision_moderation flag may be overridden on the node edit form
      // by users with the "administer nodes" permission
      $node->revision_moderation = node_tools_content_is_moderated($node->type);
      $node->is_pending = _revisioning_node_is_pending($node);

      // Could use following, but this seems old style, i.e. when $node is not a &ref

      //$node_extras['revision_moderation'] = $node->revision_moderation;

      //$node_extras['is_pending'] = $node->is_pending;
      break;
    case 'view':

      // called from called from node_view() before $node is fully built
      break;
    case 'alter':

      // called from node_view() after $node is fully built for display
      if (!$teaser && $node->nid == arg(1) && !empty($node->revision_moderation) && user_access('view revision status messages')) {
        drupal_set_message(_revisioning_node_info_msg($node));
      }
      break;
    case 'prepare':

      // presenting edit form
      // Check that we're dealing with the current node, not its translation source
      $current_nid = arg(1);
      if (is_numeric($current_nid) && $current_nid == $node->nid) {
        _revisioning_prepare_msg($node);
      }
      break;
    case 'presave':

      // for edits, called from node_save(), prior to _node_save_revision()
      if (!empty($node->revision_moderation)) {

        // Tick-box on edit form
        $node->is_pending = _revisioning_node_is_pending($node);
        if ($node->revision && $node->is_pending && variable_get('new_revisions_' . $node->type, NEW_REVISION_WHEN_NOT_PENDING) == NEW_REVISION_WHEN_NOT_PENDING) {
          drupal_set_message(t('Updating existing copy, not creating new revision as this one is still pending.'));

          // Update the $node object just before it is saved to the db
          $node->revision = FALSE;
        }

        // Save title of current revision to restore at $op=='update' time
        $node->current_title = db_result(db_query('SELECT title FROM {node} WHERE nid=%d', $node->nid));
        if (variable_get('revisioning_auto_publish_' . $node->type, FALSE)) {
          if (user_access('publish revisions') || user_access("publish revisions of any {$node->type} content") || user_access("publish revisions of own {$node->type} content") && $node->revision_uid == $user->uid) {
            if (!$node->status) {

              // Fix for [#751092] (thanks naquah and mirgorod_a).
              // If the Publish box has just been unticked, do not auto-publish.
              if (isset($node->nid)) {

                // Existing published node for which Publish box was unticked.
                if (db_result(db_query("SELECT status FROM {node} WHERE nid = %d", $node->nid))) {
                  break;
                }
              }
              else {
                $node_options = variable_get('node_options_' . $node->type, array());
                if (in_array('status', $node_options)) {

                  // New node for which Publish box is ticked by default but was
                  // unticked on the edit form.
                  break;
                }
              }
            }
            drupal_set_message(t('Auto-publishing this revision.'));
            $node->status = TRUE;

            // Make sure the 'update' does NOT reset vid, so that new revision becomes current
            unset($node->current_revision_id);
          }
        }
      }
      break;
    case 'insert':

      // new node, called from node_save(), after _node_save_revision()
      if (!empty($node->revision_moderation)) {
        _revisioning_insert_msg($node);
      }
      break;
    case 'update':

      // edited node, called from node_save(), after _node_save_revision()
      if (!empty($node->revision_moderation) && $node->current_revision_id && $node->current_revision_id != $node->vid) {

        // Resetting title and vid back to their originial values, thus creating pending revision.
        db_query("UPDATE {node} SET vid=%d, title='%s' WHERE nid=%d", $node->current_revision_id, $node->current_title, $node->nid);

        //$node->is_pending = TRUE;
      }
      break;
    case 'delete revision':
      module_invoke_all('revisionapi', 'post delete', $node);
      break;
  }
}