You are here

function content_lock_node_validate in Content locking (anti-concurrent editing) 7

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

Implement hook_node_validate() to check that the user is maintaining his lock.

Parameters

$node: The node whose lock to validate.

$form: The node's form.

$form_state: Form state.

File

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

Code

function content_lock_node_validate($node, $form, &$form_state) {
  global $user;
  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. Please !discard.', array(
          '!discard' => l('discard your changes', 'node/' . $node->nid),
        )));
      }
    }
  }
}