RooTrait.php
[Pman.Core] / RooTrait.php
index 752a9c4..1079dc5 100644 (file)
@@ -2,6 +2,14 @@
 
 trait Pman_Core_RooTrait {
     
+    var $validTables = false; 
+    
+    var $key;
+    
+    var $transObj = false;
+    
+    var $debugEnabled = true;
+    
     function init() 
     {
         if (isset($this->_hasInit)) {
@@ -30,19 +38,193 @@ trait Pman_Core_RooTrait {
         
     }
     
+    function checkDebug($req = false)
+    {
+        $req =  $req === false  ? $_REQUEST : $req;
+        if (isset($req['_debug']) 
+                && 
+                $this->authUser
+                &&
+                (
+                    (
+                        method_exists($this->authUser,'canDebug')
+                        &&
+                        $this->authUser->canDebug()
+                    )
+                ||
+                    (  
+                    
+                        method_exists($this->authUser,'groups') 
+                        &&
+                        is_a($this->authUser, 'Pman_Core_DataObjects_Person')
+                        &&
+                        in_array('Administrators', $this->authUser->groups('name'))
+                    )
+                )
+                
+            ){
+            DB_DAtaObject::debuglevel((int)$req['_debug']);
+        }
+        
+    }
+    
     function checkDebugPost()
     {
         return (!empty($_GET['_post']) || !empty($_GET['_debug_post'])) && 
                     $this->authUser && 
                     method_exists($this->authUser,'groups') &&
                     in_array('Administrators', $this->authUser->groups('name')); 
+        
     }
     
-    function checkDebug($req = false)
+    function dataObject($tab)
+    {
+        if (is_array($this->validTables) &&  !in_array($tab, $this->validTables)) {
+            $this->jerr("Invalid url - not listed in validTables");
+        }
+        
+        $tab = str_replace('/', '',$tab); // basic protection??
+        
+        $x = DB_DataObject::factory($tab);
+        
+        if (!is_a($x, 'DB_DataObject')) {
+            $this->jerr('invalid url - no dataobject');
+        }
+    
+        return $x;
+    }
+    
+    function selectSingle($x, $id, $req=false)
+    {
+        $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
+
+        //var_dump(array(!is_array($id) , empty($id)));
+        if (!is_array($id) && empty($id)) {
+            
+            if (method_exists($x, 'toRooSingleArray')) {
+                $this->jok($x->toRooSingleArray($this->authUser, $req));
+            }
+            
+            if (method_exists($x, 'toRooArray')) {
+                $this->jok($x->toRooArray($req));
+            }
+            
+            $this->jok($x->toArray());
+        }
+       
+        
+        $this->loadMap($x, array(
+                    'columns' => $_columns,
+                     
+            ));
+        if ($req !== false) { 
+            $this->setFilters($x, $req);
+        }
+        
+        // DB_DataObject::DebugLevel(1);
+        if (is_array($id)) {
+            // lookup...
+            $x->setFrom($req['lookup'] );
+            $x->limit(1);
+            if (!$x->find(true)) {
+                if (!empty($id['_id'])) {
+                    // standardize this?
+                    $this->jok($x->toArray());
+                }
+                $this->jok(false);
+            }
+            
+        } else if (!$x->get($id)) {
+            $this->jerr("selectSingle: no such record ($id)");
+        }
+        
+        // ignore perms if comming from update/insert - as it's already done...
+        if ($req !== false && !$this->checkPerm($x,'S'))  {
+            $this->jerr("PERMISSION DENIED - si");
+        }
+        // different symantics on all these calls??
+        if (method_exists($x, 'toRooSingleArray')) {
+            $this->jok($x->toRooSingleArray($this->authUser, $req));
+        }
+        if (method_exists($x, 'toRooArray')) {
+            $this->jok($x->toRooArray($req));
+        }
+        
+        $this->jok($x->toArray());
+        
+        
+    }
+    
+    /*
+     * From Pman.php
+     */
+    
+    static $permitError = false;
+    
+    function onPearError($err)
+    {
+        static $reported = false;
+        if ($reported) {
+            return;
+        }
+        
+        if (Pman::$permitError) {
+             
+            return;
+            
+        }
+        
+        $reported = true;
+        $out = $err->toString();
+        
+        $ret = array();
+        $n = 0;
+        
+        foreach($err->backtrace as $b) {
+            $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);
+     
+        print_R($out);exit;
+        
+        $this->jerr($out);
+        
+    }
+    
+    function addEvent($act, $obj = false, $remarks = '') 
+    {
+        if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
+            return;
+        }
+        
+        $au = $this->getAuthUser();
+       
+        $e = DB_DataObject::factory('Events');
+        $e->init($act,$obj,$remarks); 
+         
+        $e->event_when = date('Y-m-d H:i:s');
+        
+        $eid = $e->insert();
+        
+        // fixme - this should be in onInsert..
+        $wa = DB_DataObject::factory('core_watch');
+        if (method_exists($wa,'notifyEvent')) {
+            $wa->notifyEvent($e); // trigger any actions..
+        }
+        
+        $e->onInsert(isset($_REQUEST) ? $_REQUEST : array() , $this);
+        
+        return $e;
+        
+    }
+    
+    function getAuthUser()
     {
-        /*
-         * Not allow to doing this
-         */
-        return false;
+        die('Get auth user is not implement.');
     }
 }