DataObjects/Core_locking.php
[Pman.Core] / Lock.php
index b06a262..92025d8 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';
@@ -40,15 +52,12 @@ class Pman_Core_Lock extends Pman
     {
         
         // 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'])) {
@@ -88,19 +97,32 @@ class Pman_Core_Lock extends Pman
         ));
         
         
-        
-        if ($curlock->count() && empty($_REQUEST['force'])) {
+        $nlocks = $curlock->count() ;
+        if ($nlocks && empty($_REQUEST['force'])) {
             $curlock->selectAdd();
-            $curlock->selectAdd('distinct(person_id)');
-            $ar = $curlock->fetchAll('person_id');
-            $p = DB_DataObject::factory('Person');
+            $curlock->selectAdd('distinct(person_id), created');
+            
             
+            $ar = $curlock->fetchAll('person_id', 'created');
+            $p = DB_DataObject::factory('Person');
+            $p->whereAddIn('id', $ar, 'int');
+            $p->find();
+            while ($p->fetch()) {
+                $ret[$p->id] = $p->toRooArray();
+                $ret[$p->id]->lock_created = $ar[$p->id];
+            }
+            $this->jok(array_values($ret));
             
-            $err  = $this->canUnlock();
-            if ($err !== true) {
-                $this->jerr($err);
+        }
+        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');