Pman/Roo.php
[Pman.Base] / Pman / Roo.php
index f74c386..fe3a6ac 100644 (file)
@@ -26,7 +26,7 @@ require_once 'Pman.php';
  * - onUpload($roo)
  * - setFromRoo($ar) - values from post (deal with dates etc.) - return true|error string.
  * 
- * - toEventString (for logging)
+ * - toEventString (for logging - this is generically prefixed to all database operations.)
  */
 
 class Pman_Roo extends Pman
@@ -73,7 +73,7 @@ class Pman_Roo extends Pman
     function get($tab)
     {
          //  $this->jerr("Not authenticated", array('authFailure' => true));
-       echo '<PRE>';print_R($_GET);  DB_DataObject::debuglevel(1);
+       //echo '<PRE>';print_R($_GET);  DB_DataObject::debuglevel(1);
         
         $this->init(); // from pnan.
         
@@ -157,15 +157,14 @@ class Pman_Roo extends Pman
         $this->loadMap($x, $_columns, empty($_REQUEST['_distinct']) ? false:  $_REQUEST['_distinct']);
         
         $this->setFilters($x,$_REQUEST);
-        
-        ;
-        
+      
+         //print_r($x);
         // build join if req.
-         
+          //DB_DataObject::debugLevel(1);
         $total = $x->count($this->countWhat);
         // sorting..
-      //   DB_DataObject::debugLevel(1);
-        
+      //   
+        //var_dump($total);exit;
         $this->applySort($x);
         
         
@@ -260,12 +259,13 @@ class Pman_Roo extends Pman
      * @arg   DB_DataObject $x
      * 
      */
-    function applySort($x)
+    function applySort($x, $sort = '', $dir ='')
     {
         
        // Db_DataObject::debugLevel(1);
-        $sort = empty($_REQUEST['sort']) ? '' : $_REQUEST['sort'];
-        $dir = (empty($_REQUEST['dir']) || strtoupper($_REQUEST['dir']) == 'ASC' ? 'ASC' : 'DESC');
+        $sort = empty($_REQUEST['sort']) ? $sort : $_REQUEST['sort'];
+        $dir = empty($_REQUEST['dir']) ? $dir : $_REQUEST['dir'];
+        $dir = $dir == 'ASC' ? 'ASC' : 'DESC';
         
         
         
@@ -433,7 +433,7 @@ class Pman_Roo extends Pman
         if (method_exists($x, 'onInsert')) {
             $x->onInsert($_REQUEST, $this);
         }
-        $this->addEvent("ADD", $x, $x->toEventString());
+        $this->addEvent("ADD", $x);
         
         // note setFrom might handle this before hand...!??!
         if (!empty($_FILES) && method_exists($x, 'onUpload')) {
@@ -461,6 +461,35 @@ class Pman_Roo extends Pman
             $this->jerr("PERMISSION DENIED");
         }
        
+        // check any locks..
+        // only done if we recieve a lock_id.
+        // we are very trusing here.. that someone has not messed around with locks..
+        // the object might want to check in their checkPerm - if locking is essential..
+        
+        $lock = DB_DataObjecT::factory('Core_locking');
+        if (is_a($lock,'DB_DataObject'))  {
+                 
+            $lock->on_id = $x->id;
+            $lock->on_table= $x->tableName();
+            if (!empty($_REQUEST['_lock_id'])) {
+                $lock->whereAdd('id != ' . ((int)$_REQUEST['_lock_id']));
+            }
+            $lock->limit(1);
+            if ($lock->find(true)) {
+                // it's locked by someone else..
+                $p = $lock->person();
+                $this->jerr("Your lock is invalid, This record is locked by " . $p->name . " at " .$lock->created);
+            }
+            // check the users lock.. - no point.. ??? - if there are no other locks and it's not the users, then they can 
+            // edit it anyways...
+            
+        }
+        
+         
+        
+        
+        
+       
         $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
 
        
@@ -475,7 +504,7 @@ class Pman_Roo extends Pman
         } else {
             $x->setFrom($req);
         }
-        $this->addEvent("EDIT", $x, $x->toEventString());
+        $this->addEvent("EDIT", $x);
         //print_r($x);
         //print_r($old);
         
@@ -566,7 +595,21 @@ class Pman_Roo extends Pman
         while ($x->fetch()) {
             $xx = clone($x);
             
+           
+            // perms first.
             
+            if (method_exists($x, 'checkPerm') && !$x->checkPerm('D', $this->authUser))  {
+                $this->jerr("PERMISSION DENIED");
+            }
+            
+            // before delte = allows us to trash dependancies if needed..
+            if ( method_exists($xx, 'beforeDelete') && ($xx->beforeDelete() === false)) {
+                $errs[] = "Delete failed ({$xx->id})\n". (isset($xx->err) ? $xx->err : '');
+                continue;
+            }
+            
+            // now check deps.
+             
             foreach($affects as $k=> $true) {
                 $ka = explode('.', $k);
                 $chk = DB_DataObject::factory($ka[0]);
@@ -578,18 +621,10 @@ class Pman_Roo extends Pman
                     $this->jerr('Delete Dependant records first ('. $ka[0]. ':' . $ka[1] .'='.$xx->$pk.')');
                 }
             }
+            // finally log it.. 
             
+            $this->addEvent("DELETE", $x);
             
-            
-            if (method_exists($x, 'checkPerm') && !$x->checkPerm('D', $this->authUser))  {
-                $this->jerr("PERMISSION DENIED");
-            }
-            
-            $this->addEvent("DELETE", $x, $x->toEventString());
-            if ( method_exists($xx, 'beforeDelete') && ($xx->beforeDelete() === false)) {
-                $errs[] = "Delete failed ({$xx->id})\n". (isset($xx->err) ? $xx->err : '');
-                continue;
-            }
             $xx->delete();
         }
         if ($errs) {
@@ -749,7 +784,7 @@ class Pman_Roo extends Pman
         //DB_DataObject::debugLevel(1);
         // we do select as after everything else as we need to plop distinct at the beginning??
         /// well I assume..
-       // echo '<PRE>';print_r($selectAs );exit;
+       // echo '<PRE>';print_r($this->colsJname);exit;
         foreach($selectAs as $ar) {
             $do->selectAs($ar[0], $ar[1], $ar[2]);
         }
@@ -841,9 +876,9 @@ class Pman_Roo extends Pman
                     
                     
                     $x->whereAddIn(
-                        isset($this->colsJoinName[$key]) ? 
-                            $this->colsJoinName[$key] :
-                            $x->tableName(). '.'.$key,
+                        isset($this->colsJname[$key]) ? 
+                            $this->colsJname[$key] :
+                            ($x->tableName(). '.'.$key),
                         $ar, $quote ? 'string' : 'int');
                 }
                 
@@ -857,8 +892,8 @@ class Pman_Roo extends Pman
                 $key  = substr($key, 1) ;
                 
                 $x->whereAdd(   (
-                        isset($this->colsJoinName[$key]) ? 
-                            $this->colsJoinName[$key] :
+                        isset($this->colsJname[$key]) ? 
+                            $this->colsJname[$key] :
                             $x->tableName(). '.'.$key ) . ' != ' .
                     (is_numeric($val) ? $val : "'".  $x->escape($val) . "'")
                 );