You are here

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

Same name and namespace in other branches
  1. 6.2 content_lock.module \content_lock_node()
  2. 6 content_lock.module \content_lock_node()
  3. 7.2 content_lock.module \content_lock_node()

Try to lock a document for editing.

If the lock exists, a new AJAX unlock key is created to combat AJAX unlocks during page reloads. See http://drupal.org/node/1049708.

Parameters

$nid: A node id.

$uid: The user id to lock the node for.

$quiet: Suppress any normal user messages.

Return value

FALSE, if a document has already been locked by someone else.

2 calls to content_lock_node()
content_lock_form_alter in ./content_lock.module
Implementation of hook_form_alter().
content_lock_node_validate in ./content_lock.module
Implement hook_node_validate() to check that the user is maintaining his lock.

File

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

Code

function content_lock_node($nid, $uid, $quiet = FALSE) {
  $lock = content_lock_fetch_lock($nid);
  if ($lock != FALSE && $lock->uid != $uid) {
    $message = content_lock_lock_owner($lock);
    if (user_access('administer checked out documents')) {
      $url = "admin/content/content_lock/release/{$nid}";
    }
    if (isset($url)) {
      $token = content_lock_get_release_token($nid);
      $message .= '<br />' . t('Click !here to check back in now.', array(
        '!here' => l(t('here'), $url, array(
          'query' => array(
            'token' => $token,
            'destination' => $_GET['q'],
          ),
        )),
      ));
    }
    if (!empty($message)) {
      drupal_set_message($message, 'warning', FALSE);
    }
    return FALSE;
  }
  else {

    // no lock yet, create one
    if ($lock == false) {

      // Lock node.
      $data = array(
        'nid' => $nid,
        'uid' => $uid,
        'timestamp' => time(),
        'ajax_key' => rand(),
      );
      drupal_write_record('content_lock', $data);
      if (_content_lock_verbose() && !$quiet) {
        drupal_set_message(t('This document is now locked against simultaneous editing. It will unlock when you navigate elsewhere.'), 'status', FALSE);
      }
      module_invoke_all('content_lock_locked', $nid, $uid);
    }
    else {

      /* A lock already exists: update its AJAX key */
      $lock->ajax_key = rand();
      if (!drupal_write_record('content_lock', $lock, array(
        'nid',
      ))) {

        /*
         * we encountered a race condition where the lock was deleted
         * between when we loaded it and when we tried to update it
         * with a new key. Recreate the lock then:
         */
        drupal_write_record('content_lock', $lock);
      }
    }
  }
  return TRUE;
}