You are here

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

Same name and namespace in other branches
  1. 6.2 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_nodeapi()

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 86
Allowed time-based automatic unlocking of nodes locked for editing with content_lock.

Code

function content_lock_timeout_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
  if (!variable_get('content_lock_timeout_on_edit', FALSE)) {
    return;
  }
  $timeout_minutes = variable_get('content_lock_timeout_minutes', 120);
  switch ($op) {
    case 'prepare':
      $last_valid_time = time() - 60 * $timeout_minutes;
      $lock = content_lock_fetch_lock($node->nid);
      if (is_object($lock) && $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', $lock);
          $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,
          )));
        }
      }
      break;
  }
}