You are here

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

Same name and namespace in other branches
  1. 7.3 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_node_prepare()
  2. 7.2 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_node_prepare()

Implemention of hook_form_alter().

Break stale locks on edit.

Breaks an individual lock when a user attempts to edit a form. This way, if the lock timeout is a low value such as 20 minutes and cron only runs every few hours, a workflow of quick lock breaks can be maintained.

File

modules/content_lock_timeout/content_lock_timeout.module, line 89
Allowed time-based automatic unlocking of nodes locked for editing with content_lock.

Code

function content_lock_timeout_node_prepare($node) {
  global $user;
  if (!variable_get('content_lock_timeout_on_edit', TRUE)) {
    return;
  }
  $timeout_minutes = variable_get('content_lock_timeout_minutes', 30);
  $last_valid_time = time() - 60 * $timeout_minutes;
  if (!empty($node->nid) && is_object($lock = content_lock_fetch_lock($node->nid)) && $lock->uid != $user->uid && $lock->timestamp < $last_valid_time && user_access('check out documents') && $user->uid > 0) {
    content_lock_release($node->nid, $lock->uid);
    if (_content_lock_verbose()) {
      $username = theme('username', array(
        'account' => user_load($lock->uid),
      ));
      $date = format_date($lock->timestamp, 'medium');
      $stale_time = format_interval($last_valid_time - $lock->timestamp);
      drupal_set_message(t('Breaking existing lock by !name so that you may edit this node. (This lock was set on @date and was @stale_time stale.)', array(
        '!name' => $username,
        '@date' => $date,
        '@stale_time' => $stale_time,
      )));
    }
  }
}