RooTrait.php
[Pman.Core] / RooTrait.php
index c859cdf..1079dc5 100644 (file)
@@ -94,6 +94,67 @@ trait Pman_Core_RooTrait {
         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
      */
@@ -135,97 +196,35 @@ trait Pman_Core_RooTrait {
         
     }
     
-    function jok($str)
+    function addEvent($act, $obj = false, $remarks = '') 
     {
-        if ($this->transObj ) {
-            $this->transObj->query( connection_aborted() ? 'ROLLBACK' :  'COMMIT');
-        }
-        
-        return parent::jok($str);
-    }
-    
-    
-    function jerr($str, $errors=array(), $content_type = false)
-    {
-        if ($this->transObj) {
-            $this->transObj->query('ROLLBACK');
+        if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
+            return;
         }
         
-        return $this->jerror('ERROR', $str,$errors,$content_type);
-    }
-    
-    function jerror($type, $str, $errors=array(), $content_type = false) // standard error reporting..
-    {
-        if ($type !== false) {
-            $this->addEvent($type, false, $str);
-        }
+        $au = $this->getAuthUser();
+       
+        $e = DB_DataObject::factory('Events');
+        $e->init($act,$obj,$remarks); 
          
-        $cli = HTML_FlexyFramework::get()->cli;
-        if ($cli) {
-            echo "ERROR: " .$str . "\n";
-            exit;
-        }
+        $e->event_when = date('Y-m-d H:i:s');
         
+        $eid = $e->insert();
         
-        if ($content_type == 'text/plain') {
-            header('Content-Disposition: attachment; filename="error.txt"');
-            header('Content-type: '. $content_type);
-            echo "ERROR: " .$str . "\n";
-            exit;
-        } 
-        
-        require_once 'Services/JSON.php';
-        $json = new Services_JSON();
-        
-        $retHTML = isset($_SERVER['CONTENT_TYPE']) && 
-                preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
-        
-        if ($retHTML){
-            if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
-                $retHTML = false;
-            }
-        } else {
-            $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
+        // fixme - this should be in onInsert..
+        $wa = DB_DataObject::factory('core_watch');
+        if (method_exists($wa,'notifyEvent')) {
+            $wa->notifyEvent($e); // trigger any actions..
         }
         
-        if ($retHTML) {
-            header('Content-type: text/html');
-            echo "<HTML><HEAD></HEAD><BODY>";
-            echo  $json->encodeUnsafe(array(
-                    'success'=> false, 
-                    'errorMsg' => $str,
-                    'message' => $str, // compate with exeption / loadexception.
-
-                    'errors' => $errors ? $errors : true, // used by forms to flag errors.
-                    'authFailure' => !empty($errors['authFailure']),
-                ));
-            echo "</BODY></HTML>";
-            exit;
-        }
-        
-        if (isset($_REQUEST['_debug'])) {
-            echo '<PRE>'.htmlspecialchars(print_r(array(
-                'success'=> false, 
-                'data'=> array(), 
-                'errorMsg' => $str,
-                'message' => $str, // compate with exeption / loadexception.
-                'errors' => $errors ? $errors : true, // used by forms to flag errors.
-                'authFailure' => !empty($errors['authFailure']),
-            ),true));
-            exit;
-                
-        }
-        
-        echo $json->encode(array(
-            'success'=> false, 
-            'data'=> array(), 
-            'errorMsg' => $str,
-            'message' => $str, // compate with exeption / loadexception.
-            'errors' => $errors ? $errors : true, // used by forms to flag errors.
-            'authFailure' => !empty($errors['authFailure']),
-        ));
+        $e->onInsert(isset($_REQUEST) ? $_REQUEST : array() , $this);
         
-        exit;
+        return $e;
         
     }
+    
+    function getAuthUser()
+    {
+        die('Get auth user is not implement.');
+    }
 }