DataObjects/pman.links.ini
[Pman.Core] / Lock.php
index 2a5576f..a1b091f 100644 (file)
--- a/Lock.php
+++ b/Lock.php
  * 
  * 
  * 
+ * -- interacts with Roo and _lock = id..
+ * 
+ * 
+ * call : 
+ * try and lock it..
+ * baseURL + /Core/Lock/lock?on_id=...&on_table=...
+ * - returns id or an array of who has the locks.
+ * 
+ * Force an unlock after a warning..
+ * baseURL + /Core/Lock/lock?on_id=...&on_table=...&force=1
+ * - returns id..
+ * 
+ * Unlock - call when window is closed..
+ * baseURL + /Core/Lock/unlock?on_id=...&on_table=...&force=1
+ * - returns jerr or jok
  */
 
 require_once 'Pman.php';
@@ -34,18 +49,24 @@ class Pman_Core_Lock extends Pman
     }
     
     function get($action)
+    {
+        // should we allow url links to lock things???
+        // only for debugging??
+        $this->post($action);
+        // 
+        $this->jerr("invalid request");
+    }
+    
+    function post($action)
     {
         
         // default action is to attempt to lock..
-        $action = empty($action) ? 'lock' : 'unlock';
-        $this->$action($curlock);
-        
-       
-        
+        $action = empty($action) || $action == 'lock' ? 'lock' : 'unlock';
+        $this->$action();
         
     }
     
-    function unlock($curlock)
+    function unlock()
     {
     
         if (empty($_REQUEST['id'])) {
@@ -83,12 +104,35 @@ class Pman_Core_Lock extends Pman
             'on_id' => $_REQUEST['on_id'],
             'on_table' => $_REQUEST['on_table']
         ));
-        if ($curlock->count()) {
-            $err  = $this->canUnlock();
-            if ($err !== true) {
-                $this->jerr($err);
+        
+        
+        $nlocks = $curlock->count() ;
+        if ($nlocks && empty($_REQUEST['force'])) {
+           // DB_DataObjecT::debugLevel(1);
+            $ar = $curlock->fetchAll('person_id', 'created');
+            $p = DB_DataObject::factory('Person');
+            $p->selectAdd();
+            $p->selectAdd('id,name,email');
+            
+            $p->whereAddIn('id', array_keys($ar), 'int');
+            $p->find();
+            $ret = array();
+            while ($p->fetch()) {
+                $ret[$p->id] = $p->toArray();
+                $ret[$p->id]['lock_created'] = $ar[$p->id];
+            }
+            $this->jok(array_values($ret));
+            
+        }
+        if ($nlocks) {
+            // trash all the locks..
+            $curlock->find();
+            while($curlock->fetch()) {
+                $cc =clone($curlock);
+                $cc->delete();
             }
         }
+        
         // make a lock..
         
         $curlock = DB_DataObject::factory('Core_locking');
@@ -102,58 +146,7 @@ class Pman_Core_Lock extends Pman
         $this->jok($id);
         
     }
-    
-    function canUnlock()
-    {
-        // the only scenario where we can automatically unlock is:::
-        
-        // this user owns the lock.
-        
-        $curlock = DB_DataObject::factory('Core_locking');
-        $curlock->setFrom(array(
-            'on_id' => $_REQUEST['on_id'],
-            'on_table' => $_REQUEST['on_table']
-        ));
-        $cc = clone($curlock);
-        // the user who owns the lock is not logged in.. ?? - their last 
-        $curlock->find();
-        $users = array();
-        while ($curlock->fetch()) {
-            $u = DB_DataObject::factory('Person');
-            $u->get($curlock->person_id);
-            if (!$u->isCurrentlyLoggedIn()) {
-                $cc = clone($curlock);
-                $cc->delete();
-                continue;
-            }
-            $users[] = clone($u);
-            
-        }
-        if (empty($users)) {
-            return true;
-            
-        }
-        // situations
-        
-        //- the user is logged in, and we can clear it..
-        
-        //- the user is logged in multiple times, on different browser..
-        
-        //- the user is logged in multiple times on the same browser..
-        
-        
-        
-        // one of two error messages..
-        
-        $this->jerr("Item is Locked by " . $u->name . ' (' . $u->email . "),  Try asking them to log out");
-        
-        return true;
-        
-        
-        
-        
-        
-    }
+