Pman/Images.php
[Pman.Base] / Pman / Roo.php
index 96df1db..0257aa6 100644 (file)
@@ -43,7 +43,10 @@ require_once 'Pman.php';
 
 class Pman_Roo extends Pman
 {
-    
+    /**
+     * if set to an array (when extending this, then you can restrict which tables are available
+     */
+    var $validTables = false; 
     
     var $key; // used by update currenly to store primary key.
     
@@ -100,11 +103,11 @@ class Pman_Roo extends Pman
      * DEBUGGING
      *  _post      = simulate a post with debuggin on.
      *  _delete    = delete a list of ids element. (depricated.. this will be removed...)
-     *
+     *  _debug     = turn on DB_dataobject deubbing, must be admin at present..
      *
      *
      * CALLS methods on dataobjects if they exist
-     *   checkPerm('L'/'E'/'A', $authuser)
+     *   checkPerm('S' , $authuser)
      *                      - can we list the stuff
      *                      - return false to disallow...
      *   applySort($au, $sortcol, $direction, $array_of_columns, $multisort)
@@ -120,7 +123,8 @@ class Pman_Roo extends Pman
      *   toRooSingleArray($authUser, $request) : array
      *                       - called on single fetch only, add or maniuplate returned array data.
      *   toRooArray($request) : array
-     *                      - if you need to return different data than toArray..
+     *                      - called if singleArray is unavailable on single fetch.
+     *                      - always tried for mutiple results.
      *   autoJoin($request) 
      *                      - standard DataObject feature - causes all results to show all
      *                        referenced data.
@@ -131,23 +135,30 @@ class Pman_Roo extends Pman
        //echo '<PRE>';print_R($_GET);
       //DB_DataObject::debuglevel(1);
         
-        $this->init(); // from pnan.
+        $this->init(); // from pman.
         //DB_DataObject::debuglevel(1);
         HTML_FlexyFramework::get()->generateDataobjectsCache($this->isDev);
+        
+        
+   
+        
         // debugging...
         if (!empty($_GET['_post'])) {
             $_POST  = $_GET;
             //DB_DAtaObject::debuglevel(1);
             return $this->post($tab);
         }
-        $tab = str_replace('/', '',$tab); // basic protection??
         
-        $x = DB_DataObject::factory($tab);
-        
-        if (!is_a($x, 'DB_DataObject')) {
-            $this->jerr('invalid url');
+        if (isset($_REQUEST['_debug']) && in_array('Administrators', $this->authUser->groups('name'))) {
+            DB_DAtaObject::debuglevel((int)$_REQUEST['_debug']);
         }
-        $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
+        
+        PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
+   
+         
+        $x = $this->dataObject($tab);
+        
+         $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
         
         if (isset( $_REQUEST['lookup'] ) && is_array($_REQUEST['lookup'] )) { // single fetch based on key/value pairs
             $this->jok($this->selectSingle($x, $_REQUEST['lookup']));
@@ -246,6 +257,7 @@ class Pman_Roo extends Pman
             
             $ret[] =  !$_columns ? $add : array_intersect_key($add, $_columnsf);
         }
+        
         $extra = false;
         if (method_exists($queryObj ,'postListExtra')) {
             $extra = $queryObj->postListExtra($_REQUEST, $this);
@@ -327,15 +339,67 @@ class Pman_Roo extends Pman
      * Params:
      *   _delete=1,2,3   causes a delete to occur.
      *   _ids=1,2,3,4    causes update to occur on all primary ids.
+     *  
+     *  RETURNS
+     *     = same as single SELECT GET request..
+     *
      *
      *
      * DEBUGGING
      *   _debug=1    forces debug
      *   _get=1 - causes a get request to occur when doing a POST..
      *
-     *     
+     *
+     * CALLS
+     *   these methods on dataobjects if they exist
      * 
-     */
+     *   checkPerm('E' / 'D' , $authuser)
+     *                      - can we list the stuff
+     *                      - return false to disallow...
+   
+     *   toRooSingleArray($authUser, $request) : array
+     *                       - called on single fetch only, add or maniuplate returned array data.
+     *   toRooArray($request) : array
+     *                      - Called if toSingleArray does not exist.
+     *                      - if you need to return different data than toArray..
+     *
+     *   toEventString()
+     *                  (for logging - this is generically prefixed to all database operations.)
+     *
+     *  
+     *   onUpload($roo)
+     *                  called when $_FILES is not empty
+     *
+     *                  
+     *   setFromRoo($ar, $roo)
+     *                      - alternative to setFrom() which is called if this method does not exist
+     *                      - values from post (deal with dates etc.) - return true|error string.
+     *                      - call $roo->jerr() on failure...
+     *
+     * CALLS BEFORE change occurs:
+     *  
+     *     beforeDelete($dependants_array, $roo)
+     *                      Argument is an array of un-find/fetched dependant items.
+     *                      - jerr() will stop insert.. (Prefered)
+     *                      - return false for fail and set DO->err;
+     *                      
+     *      beforeUpdate($old, $request,$roo)
+     *                      - after update - jerr() will stop insert..
+     *      beforeInsert($request,$roo)
+     *                      - before insert - jerr() will stop insert..
+     *
+     * CALLS AFTER change occured
+     * 
+     *      onUpdate($old, $request,$roo)
+     *               - after update // return value ignored
+     *
+     *      onInsert($request,$roo)
+     *                  - after insert
+     * 
+     *
+     * 
+     */                     
+     
     function post($tab) // update / insert (?? delete??)
     {
         
@@ -352,11 +416,7 @@ class Pman_Roo extends Pman
         }
       
          
-        $tab = str_replace('/', '',$tab); // basic protection??
-        $x = DB_DataObject::factory($tab);
-        if (!is_a($x, 'DB_DataObject')) {
-            $this->jerr('invalid url');
-        }
+        $x = $this->dataObject($tab);
         // find the key and use that to get the thing..
         $keys = $x->keys();
         if (empty($keys) ) {
@@ -511,6 +571,8 @@ class Pman_Roo extends Pman
        
         $this->loadMap($x, $_columns);
         
+        $this->setFilters($x, $_REQUEST);
+        
         if (is_array($id)) {
             // lookup...
             $x->setFrom($_REQUEST['lookup'] );
@@ -519,7 +581,7 @@ class Pman_Roo extends Pman
                 $this->jok(false);
             }
             
-        } else  if (!$x->get($id)) {
+        } else if (!$x->get($id)) {
             $this->jerr("no such record");
         }
         
@@ -540,12 +602,9 @@ class Pman_Roo extends Pman
     {
         
     
-        if (method_exists($x, 'checkPerm') && !$x->checkPerm('A', $this->authUser, $req))  {
-            $this->jerr("PERMISSION DENIED");
-        }
+       
+        
         
-        $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
-   
         if (method_exists($x, 'setFromRoo')) {
             $res = $x->setFromRoo($req, $this);
             if (is_string($res)) {
@@ -555,7 +614,9 @@ class Pman_Roo extends Pman
             $x->setFrom($req);
         }
         
-         
+        if (method_exists($x, 'checkPerm') && !$x->checkPerm('A', $this->authUser, $req))  {
+            $this->jerr("PERMISSION DENIED");
+        }
         $cols = $x->table();
      
         if (isset($cols['created'])) {
@@ -660,8 +721,7 @@ class Pman_Roo extends Pman
         
         
        
-        $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
-
+         
        
         $old = clone($x);
         $this->old = $x;
@@ -1177,18 +1237,51 @@ class Pman_Roo extends Pman
         // - projectdirectory staff list - persn queuy
      
         
+    }
+    /**
+     * create the  dataobject from (usually the url)
+     * This uses $this->validTables
+     *           $this->validPrefix (later..)
+     * to determine if class can be created..
+     *
+     */
+     
+    function dataObject($tab)
+    {
+        if (is_array($this->validTables) &&  !in_array($tab,$this->validTables)) {
+            $this->jerr("Invalid url");
+        }
+        $tab = str_replace('/', '',$tab); // basic protection??
+        
+        $x = DB_DataObject::factory($tab);
+        
+        if (!is_a($x, 'DB_DataObject')) {
+            $this->jerr('invalid url');
+        }
+        return $x;
+        
     }
     
+    
     function onPearError($err)
     {
-        
+        static $reported = false;
+        if ($reported) {
+            return;
+        }
+        $reported = true;
         $out = $err->toString();
         
         
         //print_R($bt); exit;
         $ret = array();
+        $n = 0;
         foreach($err->backtrace as $b) {
-            $ret[] = $b['file'] . '(' . $b['line'] . ')@' .   @$bt['class'] . '::' . @$bt['function'];  
+            $ret[] = @$b['file'] . '(' . @$b['line'] . ')@' .   @$b['class'] . '::' . @$b['function'];
+            if ($n > 20) {
+                break;
+            }
+            $n++;
         }
         //convert the huge backtrace into something that is readable..
         $out .= "\n" . implode("\n",  $ret);