You are here

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

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 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

int $nid: A node id.

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

bool $quiet: Suppress any normal user messages.

Return value

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

3 calls to content_lock_node()
content_lock_form_alter in ./content_lock.module
Implements hook_form_alter().
content_lock_node_ajax_callback in ./content_lock.module
AJAX callback to lock a node manually.
content_lock_node_validate in ./content_lock.module
Implements hook_node_validate().

File

./content_lock.module, line 683
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 />';
      $message .= 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.'), '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;
}