Fix #5884 - add photo to report
[Pman.Base] / Pman / Roo.php
index ba6c51c..7fdb8f6 100644 (file)
@@ -32,8 +32,8 @@ require_once 'Pman.php';
  * - beforeInsert($request,$roo) - before insert - jerr() will stop insert..
  *
  *  AFTER
- * - onUpdate($old, $request,$roo) - after update // return value ignored
- * - onInsert($request,$roo) - after insert
+ * - onUpdate($old, $request,$roo, $event) - after update // return value ignored
+ * - onInsert($request,$roo, $event) - after insert
  * - onDelete($req, $roo) - after delete
  * - onUpload($roo)
  * 
@@ -51,12 +51,13 @@ class Pman_Roo extends Pman
     
     var $key; // used by update currenly to store primary key.
     
-    var $transObj = false ; // the transaction BEGIN / ROLLBACK / COMMIT Dataobject.
-    
+     
     var $max_limit = 10000;
     
     var $debugEnabled = true; // disable this for public versions of this code.
     
+    var $do = false; // the dataobject being worked on..
+    
     function getAuth()
     {
         parent::getAuth(); // load company!
@@ -74,7 +75,7 @@ class Pman_Roo extends Pman
         return true;
     }
     /**
-     * GET method   Roo/TABLENAME.php
+     * GET method   Roo/TABLENAME
      *
      * Generally for SELECT or Single SELECT
      *
@@ -153,7 +154,8 @@ class Pman_Roo extends Pman
      *   toRooArray($request) : array
      *                      - called if singleArray is unavailable on single fetch.
      *                      - always tried for mutiple results.
-     *   toArray()          - the default method if none of the others are found. 
+     *   toArray()          - the default method if none of the others are found.
+     *   toSimpleExcelColumn($col) - see toCSV - this enables special columns to be created for exported data.
      *   
      *   autoJoin($request) 
      *                      - standard DataObject feature - causes all results to show all
@@ -282,7 +284,7 @@ class Pman_Roo extends Pman
         $this->sessionState(0);
         $res = $x->find();
         $this->sessionState(1);
-        
+                
         if (false === $res) {
             $this->jerr($x->_lastError->toString());
             
@@ -337,8 +339,7 @@ class Pman_Roo extends Pman
             
             
             $this->toCsv($ret, $_REQUEST['csvCols'], $_REQUEST['csvTitles'],
-                        empty($_REQUEST['csvFilename']) ? '' : $_REQUEST['csvFilename']
-                         );
+                        empty($_REQUEST['csvFilename']) ? '' : $_REQUEST['csvFilename'] );
             
             
         
@@ -430,7 +431,7 @@ class Pman_Roo extends Pman
     
     function toCsv($data, $cols, $titles, $filename, $addDate = true)
     {
-          
+         // die('here');
         $this->sessionState(0); // turn off sessions  - no locking..
 
         require_once 'Pman/Core/SimpleExcel.php';
@@ -455,13 +456,20 @@ class Pman_Roo extends Pman
                 if ($cols == '*') {  /// did we get cols sent to us?
                     $cols = array_keys($x);
                 }
+                
+                if(!is_array($cols)) {
+                    $cols = explode(',', $cols);
+                }
                
                 if ($titles !== false) {
                     if ($titles== '*') {
                         $titles= array_keys($x);
                     }
+                    if(!is_array($titles)) {
+                        $titles = explode(',', $titles);
+                    }
                     foreach($cols as $i=>$col) {
-                        $se_config['cols'][] = array(
+                        $add = array(
                             'header'=> isset($titles[$i]) ? $titles[$i] : $col,
                             'dataIndex'=> $col,
                             'width'=>  100,
@@ -469,10 +477,17 @@ class Pman_Roo extends Pman
                              //   'color' => 'yellow', // set color for the cell which is a header element
                               // 'fillBlank' => 'gray', // set 
                         );
-                         $se = new Pman_Core_SimpleExcel(array(), $se_config);
+                        //die('here');
+                        if (method_exists($data, 'toSimpleExcelColumn')) {
+                            $add = $data->toSimpleExcelColumn($add);
+                        } 
+                        
+                        $se_config['cols'][] = $add;
+                        
        
                         
                     }
+                    $se = new Pman_Core_SimpleExcel(array(), $se_config);
                      
                     $titles = false;
                 }
@@ -491,19 +506,29 @@ class Pman_Roo extends Pman
             
         } 
         
-        
-        foreach($data as $x) {
+         foreach($data as $x) {
             //echo "<PRE>"; print_r(array($_REQUEST['csvCols'], $x->toArray())); exit;
             $line = array();
-            if ($titles== '*') {
-                $titles= array_keys($x);
-            }
+            
             if ($cols== '*') {
                 $cols= array_keys($x);
             }
+            
+            if(!is_array($cols)) {
+                $cols = explode(',', $cols);
+            }
+
             if ($titles !== false) {
+                if ($titles== '*') {
+                    $titles= array_keys($x);
+                }
+
+                if(!is_array($titles)) {
+                    $titles = explode(',', $titles);
+                }
+
                 foreach($cols as $i=>$col) {
-                    $se_config['cols'][] = array(
+                    $add = array(
                         'header'=> isset($titles[$i]) ? $titles[$i] : $col,
                         'dataIndex'=> $col,
                         'width'=>  100,
@@ -511,27 +536,32 @@ class Pman_Roo extends Pman
                          //   'color' => 'yellow', // set color for the cell which is a header element
                           // 'fillBlank' => 'gray', // set 
                     );
-                    $se = new Pman_Core_SimpleExcel(array(),$se_config);
+                    //die('here');
+                    if (method_exists($this->do, 'toSimpleExcelColumn')) {
+                        $add = $this->do->toSimpleExcelColumn($add);
+                    } 
+                    
+                    $se_config['cols'][] = $add;
+                    
    
                     
                 }
-                
+                $se = new Pman_Core_SimpleExcel(array(),$se_config);
+               
                 
                 //fputcsv($fh, $titles);
                 $titles = false;
             }
             
-            
-            
             $se->addLine($se_config['workbook'], $x);
         }
+        
         if(!$se){
             $this->jerr('no data found');
         }
+        
         $se->send($fn .'.xls');
         exit;
-    
-        
         
     }
     
@@ -641,7 +671,7 @@ class Pman_Roo extends Pman
         $this->init(); // for pman.
          
         $x = $this->dataObject($tab);
-        
+
         $this->transObj = clone($x);
         
         $this->transObj->query('BEGIN');
@@ -658,12 +688,14 @@ class Pman_Roo extends Pman
             // do we really delete stuff!?!?!?
             return $this->delete($x,$_REQUEST);
         } 
-         
+        
+        
+        
         
         $old = false;
         
         // not sure if this is a good idea here...
-        var_dump($_REQUEST['_ids']); exit;
+
         if (!empty($_REQUEST['_ids'])) {
             $ids = explode(',',$_REQUEST['_ids']);
             $x->whereAddIn($this->key, $ids, 'int');
@@ -678,7 +710,7 @@ class Pman_Roo extends Pman
             
         }
          
-        if (!empty($_REQUEST[$this->key])) {
+        if (!empty($_REQUEST[$this->key])) { 
             // it's a create..
             if (!$x->get($this->key, $_REQUEST[$this->key]))  {
                 $this->jerr("Invalid request (id does not point to  a record.)");
@@ -689,7 +721,7 @@ class Pman_Roo extends Pman
             if (empty($_POST)) {
                 $this->jerr("No data recieved for inserting");
             }
-            
+
             $this->jok($this->insert($x, $_REQUEST));
             
         }
@@ -844,7 +876,7 @@ class Pman_Roo extends Pman
         } else if (method_exists($x, 'applyFilters')) {
             // always call apply filters even after update/insert...
             // however arguments are not passed.
-            $x->applyFilters(array(), $this->authUser, $this);
+            $x->applyFilters(array('_is_update_request' => true), $this->authUser, $this);
         }
         
         // DB_DataObject::DebugLevel(1);
@@ -932,13 +964,15 @@ class Pman_Roo extends Pman
         }
         
         $res = $x->insert();
+
         if ($res === false) {
             $this->jerr($x->_lastError->toString());
         }
+        $ev = $this->addEvent("ADD", $x);
         if (method_exists($x, 'onInsert')) {
-            $x->onInsert($_REQUEST, $this);
+            $x->onInsert($_REQUEST, $this, $ev);
         }
-        $ev = $this->addEvent("ADD", $x);
+        
         if ($ev) { 
             $ev->audit($x);
         }
@@ -1063,7 +1097,7 @@ class Pman_Roo extends Pman
         //print_r($old);
         
         $cols = $x->tableColumns();
-        //print_r($cols);
+
         if (isset($cols['modified'])) {
             $x->modified = date('Y-m-d H:i:s');
         }
@@ -1097,11 +1131,11 @@ class Pman_Roo extends Pman
         if ($res === false) {
             $this->jerr($x->_lastError->toString());
         }
-        
+        $ev = $this->addEvent("EDIT", $x);
+
         if (method_exists($x, 'onUpdate')) {
-            $x->onUpdate($old, $req, $this);
+            $x->onUpdate($old, $req, $this, $ev);
         }
-        $ev = $this->addEvent("EDIT", $x);
         if ($ev) { 
             $ev->audit($x, $old);
         }
@@ -1120,11 +1154,13 @@ class Pman_Roo extends Pman
      */
     
     function delete($x, $req)
-    {
+    { 
         // do we really delete stuff!?!?!?
         if (empty($req['_delete'])) {
             $this->jerr("Delete Requested with no value");
         }
+        
+        
         // build a list of tables to queriy for dependant data..
         $map = $x->links();
         
@@ -1194,6 +1230,7 @@ class Pman_Roo extends Pman
                 }          
             }
             
+            
             $has_beforeDelete = method_exists($xx, 'beforeDelete');
             // before delte = allows us to trash dependancies if needed..
             $match_total = 0;
@@ -1382,7 +1419,7 @@ class Pman_Roo extends Pman
         $q_filtered = array();
         
         $keys = $x->keys();
-        // var_dump($keys);exit;
+
         foreach($q as $key=>$val) {
             
             if (in_array($key,$keys) && !is_array($val)) {
@@ -1450,7 +1487,7 @@ class Pman_Roo extends Pman
                 
             }
             
-            
+                
             
             switch($key) {
                     
@@ -1555,39 +1592,13 @@ class Pman_Roo extends Pman
         if (!is_a($x, 'DB_DataObject') && !is_a($x, 'PDO_DataObject')) {
             $this->jerr('invalid url - no dataobject');
         }
-    
+        $this->do = $x;
         return $x;
         
     }
     
-     
-    // our handlers to commit / rollback.
-    
       
     
-    function jok($str)
-    {
-        // note that commit will only work if an insert/update was done,
-        // so some stored proc calls may not have flagged this.
-        
-        if ($this->transObj ) {
-            $this->transObj->query( connection_aborted() ? 'ROLLBACK' :  'COMMIT');
-        }
-        return parent::jok($str);
-    }
-    
-    
-    function jerr($str, $errors=array(), $content_type = false)
-    {
-        // standard error reporting..
-        if ($this->transObj) {
-            $this->transObj->query('ROLLBACK');
-        }
-        return parent::jerr($str,$errors,$content_type);
-    
-    }
-    
-