--- checkout/checkout.module	2009-04-24 15:08:57.000000000 +0200
+++ checkout.module	2009-08-16 19:14:34.000000000 +0200
@@ -33,20 +33,6 @@ function checkout_help($path, $arg) {
 }
 
 /**
- * Implementation of hook_init().
- */
-function checkout_init() {
-  global $user;
-  if ($user->uid && user_access('check out documents')) {
-    // Avoid AJAX requests unlocking a node. This header is automatically set
-    // when doing AJAX requests through jQuery.
-    if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {
-      checkout_handle_request($user->uid);
-    }
-  }
-}
-
-/**
  * Implementation of hook_menu().
  */
 function checkout_menu() {
@@ -64,6 +50,12 @@ function checkout_menu() {
     'access arguments' => array('administer checked out documents'),
     'type' => MENU_CALLBACK,
   );
+  $items['admin/content/%/checkout/releaseown'] = array(
+    'page callback' => 'checkout_release_own_item',
+    'page arguments' => array(2),
+    'access arguments' => array('check out documents'),
+    'type' => MENU_CALLBACK,
+  );
   $items['user/%user/checkout'] = array(
     'title' => 'Locked documents',
     'page callback' => 'checkout_overview',
@@ -112,6 +104,21 @@ function checkout_form_alter(&$form, $fo
       '#description' => t('The period after which locked documents will be automatically released.'),
     );
   }
+
+  global $user;
+  if(!$user->uid || !user_access('check out documents')) { // user has no right to lock nodes at all
+    return;
+  }
+  $nid = $form['nid']['#value'];
+
+  if($form_state['submitted'] === false && $nid != "") { /// node editing starts and we are editing a node
+    // current nid
+
+
+    if(checkout_node($nid, $user->uid) == false) {// could not lock node, its locked by someone else
+        drupal_goto("node/$nid");
+    }
+  }
 }
 
 /**
@@ -148,10 +155,15 @@ function checkout_nodeapi(&$node, $op, $
         checkout_release($node->nid, $user->uid, TRUE);
       }
       break;
-    
+
     case 'delete':
       checkout_release($node->nid, NULL, TRUE);
       break;
+
+    case 'view':
+        global $user;
+        checkout_warn_pending_locks($user->uid); // check if the user has pending locks and warn him
+    break;
   }
 }
 
@@ -173,59 +185,6 @@ function checkout_cron() {
 }
 
 /**
- * Handle node locking.
- * 
- * When landing on a node edit page the current node needs to be locked.
- * When coming from an edit page the previous node needs to be unlocked.
- *
- * @param $uid
- *   The user id to (un)lock nodes for.
- */
-function checkout_handle_request($uid) {
-  global $base_path;
-  
-  // Build referer path
-  $referer_uri = parse_url(referer_uri());
-  if (variable_get('clean_url', 0)) {
-    $referer = substr($referer_uri['path'], strlen($base_path));
-  }
-  else {
-    $vars = array();
-    if (isset($referer_uri['query'])) {
-      parse_str($referer_uri['query'], $vars);
-    }
-    $referer = isset($vars['q']) ? $vars['q'] : '';
-  }
-  if ($referer = rtrim($referer, '/')) {
-    $referer = drupal_get_normal_path($referer);
-  }
-
-  // If refering and current paths match we can abort, since there can't be any
-  // locking action involved.
-  if ($_GET['q'] == $referer) {
-    return;
-  }
-
-  // Otherwise try to extract nid from path.
-  $previous_nid = checkout_get_nid($referer);
-  $current_nid = checkout_get_nid($_GET['q']);
-
-  // Check whether to release a previously edited node.
-  if ($previous_nid && (!$current_nid || $current_nid != $previous_nid)) {
-    checkout_release($previous_nid, $uid);
-  }
-
-  // Check whether to lock the current node.
-  if ($current_nid && (!$previous_nid || $previous_nid != $current_nid)) {
-    // Try to lock the node.
-    if (!checkout_node($current_nid, $uid)) {
-      // Node already locked: send back to refering page.
-      drupal_goto(referer_uri());
-    }
-  }
-}
-
-/**
  * Extract the node id from a node edit path.
  *
  * @param $path
@@ -285,11 +244,12 @@ function checkout_lock_owner($lock) {
 function checkout_node($nid, $uid) {
   if ($lock = checkout_fetch_lock($nid)) {
     // Node is already locked.
-
     // Deny editing this node even if the node is locked by the same user.
     // The only exception to this rule is when the user had previously acquired
     // a persistent lock.
-    if ($lock->uid != $uid || !$lock->persistent) {
+
+
+    if ($lock->uid != $uid) {
       $message = checkout_lock_owner($lock);
 
       if ($lock->uid == $uid) {
@@ -305,11 +265,11 @@ function checkout_node($nid, $uid) {
       drupal_set_message($message, 'error');
       return FALSE;
     }
+
   }
   else {
     // Lock node.
     db_query("INSERT INTO {checkout} (nid, uid, timestamp) VALUES (%d, %d, %d)", $nid, $uid, time());
-
     drupal_set_message(t('This document is now locked against simultaneous editing. It will unlock when you navigate elsewhere.'));
   }
 
@@ -359,19 +319,19 @@ function checkout_overview($account = NU
   if (!$account) {
     $header[] = array('data' => t('Username'), 'field' => 'u.name');
     $uid = NULL;
-  } 
+  }
   else {
     $uid = $account->uid;
   }
   $header[] = array('data' => t('Locked since'), 'field' => 'c.timestamp');
   $header[] = array('data' => t('Persistent lock'), 'field' => 'c.persistent');
   $header[] = t('Operations');
-  
+
   $rows = array();
   $add_sql = $uid ? " WHERE c.uid = %d" : '';
   $result = pager_query('SELECT c.*, n.title, u.name FROM {checkout} c INNER JOIN {node} n ON n.nid = c.nid INNER JOIN {users} u ON u.uid = c.uid'. $add_sql . tablesort_sql($header), 50, 0, NULL, $uid);
   $url = $uid ? "user/$uid/checkout/release" : 'admin/content/node/checkout/release';
-  
+
   while ($data = db_fetch_object($result)) {
     $row = array();
     $row[] = l($data->title, "node/$data->nid");
@@ -383,7 +343,7 @@ function checkout_overview($account = NU
     $row[] = l(t('check in'), "$url/$data->nid");
     $rows[] = $row;
   }
-  
+
   $output = theme('table', $header, $rows, array('id' => 'checkout'));
   if (!$rows) {
     $output .= t('No locked documents.');
@@ -411,3 +371,36 @@ function checkout_release_item($nid, $ac
   drupal_goto($account ? "user/$account->uid/checkout" : 'admin/content/node/checkout');
 }
 
+/**
+ * For every lock a user current have on any nodes, print a warning messagt
+ * with an link to release this node.
+ *
+ */
+function checkout_warn_pending_locks($uid) {
+    // cache
+    static $warned_nodes = array();
+    $result = db_query("SELECT * FROM {checkout} WHERE uid = %d", $uid);
+    while($lock = db_fetch_object($result)) {
+        // we have warned this one already, lets pass
+        if($warned_nodes[$lock->nid] === true)
+            continue;
+
+        $node = node_load($lock->nid);
+        $warned_nodes[$node->nid] = true;
+        $unlocklink = "/admin/content/$node->nid/checkout/releaseown";
+        drupal_set_message(t("You still have an open lock on '!nodetitle', so nobody can edit this node. You might <a href='!unlocklink'>unlock</a> the node by klicking <a href='!unlocklink'>here</a>", array ('!nodetitle' => $node->title, '!unlocklink' => $unlocklink)),"error");
+    }
+}
+/**
+ * Release the lock of a node. We are using the current users uid, so the user only can delete
+ * his own locks. We never fail, as if the lock does not exist, the node is unlocked anyway
+ */
+function checkout_release_own_item($node) {
+    global $user;
+
+    if(!$node)
+        echo "error";
+    checkout_release($node,$user->uid,true);
+    drupal_set_message('Lock removed from this node. Other users can now edit it again');
+    drupal_goto("/node/$node");
+}
