You are here

function content_lock_nodeapi in Content locking (anti-concurrent editing) 6

Same name and namespace in other branches
  1. 6.2 content_lock.module \content_lock_nodeapi()

Implementation of hook_nodeapi().

File

./content_lock.module, line 101
Allows users to lock documents for modification.

Code

function content_lock_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
  static $messages_shown = false;
  switch ($op) {
    case 'validate':
      if (isset($node->nid) && _content_lock_is_lockable_node($node) && user_access('check out documents')) {

        // Existing node. Check if we still own the lock.
        if ($lock = content_lock_fetch_lock($node->nid)) {
          if ($lock->uid != $user->uid) {

            // Lock is no longer ours.
            form_set_error('changed', t('Your lock has been removed!') . '<br />' . content_lock_lock_owner($lock) . '<br />' . t('You can still save the content if this user aborts the edit operation without saving changes.'));
          }
        }
        else {

          // Node is not locked. Try to re-lock if node is unchanged.
          if (node_last_changed($node->nid) > $node->changed || !content_lock_node($node->nid, $user->uid)) {
            form_set_error('alsochanged', t('Your lock has been removed due to inactivity or by an administrator. Failed to regain the lock since the document has been changed since.'));
          }
        }
      }
      break;
    case 'update':
      if (_content_lock_is_lockable_node($node)) {
        content_lock_release($node->nid, $user->uid);
      }
      break;
    case 'delete':
      if (_content_lock_is_lockable_node($node)) {
        content_lock_release($node->nid, NULL);
      }
      break;
    case 'view':
      global $user;
      if (!_content_lock_is_lockable_node($node)) {
        break;
      }
      if (!$messages_shown) {
        _content_lock_show_warnings();
        $messages_shown = true;
      }
      if ($node->build_mode != NODE_BUILD_PREVIEW) {
        content_lock_warn_pending_locks($user->uid);

        // check if the user has pending locks and warn him
      }
      break;
  }
}