pdo fix as _query is private
[Pman.Base] / Pman / Roo.php
index b8f3408..6363917 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)
  * 
@@ -56,6 +56,8 @@ class Pman_Roo extends Pman
     
     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!
@@ -73,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
      *
@@ -152,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
@@ -240,10 +243,10 @@ class Pman_Roo extends Pman
        
         // sets map and countWhat
         $this->loadMap($x, array(
-                    'columns' => $_columns,
-                    'distinct' => empty($_REQUEST['_distinct']) ? false:  $_REQUEST['_distinct'],
-                    'exclude' => empty($_REQUEST['_exclude_columns']) ? false:  explode(',', $_REQUEST['_exclude_columns'])
-            ));
+                'columns' => $_columns,
+                'distinct' => empty($_REQUEST['_distinct']) ? false:  $_REQUEST['_distinct'],
+                'exclude' => empty($_REQUEST['_exclude_columns']) ? false:  explode(',', $_REQUEST['_exclude_columns'])
+        ));
         
         
         $this->setFilters($x,$_REQUEST);
@@ -255,8 +258,18 @@ class Pman_Roo extends Pman
          //print_r($x);
         // build join if req.
           //DB_DataObject::debugLevel(1);
-       //   var_dump($this->countWhat);
-        $total = $x->count($this->countWhat);
+        // count with multiple joins and no conditions can be quite slow - so if there are no conditions - just remove the joins from the count.
+        $xx = clone($x);
+        $old_where = $x->whereAdd();
+        if (empty($old_where )) {
+            $xx->_join = '';
+        } else {
+            $x->whereAdd($old_where);
+            $xx=clone($x);
+            
+        }
+       
+        $total = $xx->count($this->countWhat);
         // sorting..
       //   
         //var_dump($total);exit;
@@ -281,7 +294,7 @@ class Pman_Roo extends Pman
         $this->sessionState(0);
         $res = $x->find();
         $this->sessionState(1);
-        
+                
         if (false === $res) {
             $this->jerr($x->_lastError->toString());
             
@@ -336,8 +349,7 @@ class Pman_Roo extends Pman
             
             
             $this->toCsv($ret, $_REQUEST['csvCols'], $_REQUEST['csvTitles'],
-                        empty($_REQUEST['csvFilename']) ? '' : $_REQUEST['csvFilename']
-                         );
+                        empty($_REQUEST['csvFilename']) ? '' : $_REQUEST['csvFilename'] );
             
             
         
@@ -429,7 +441,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';
@@ -449,18 +461,24 @@ class Pman_Roo extends Pman
             $rooar = method_exists($data, 'toRooArray');
             while($data->fetch()) {
                 $x = $rooar  ? $data->toRooArray($q) : $data->toArray();
-                
-                
+                 
                 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,
@@ -468,10 +486,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;
                 }
@@ -490,19 +515,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,
@@ -510,27 +545,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;
-    
-        
         
     }
     
@@ -605,7 +645,7 @@ class Pman_Roo extends Pman
      *                      - return false for fail and set DO->err;
      *                      
      *      beforeUpdate($old, $request,$roo)
-     *                      - after update - jerr() will stop insert..
+     *                      - before update - jerr() will stop insert..
      *      beforeInsert($request,$roo)
      *                      - before insert - jerr() will stop insert..
      *
@@ -664,7 +704,7 @@ class Pman_Roo extends Pman
         $old = false;
         
         // not sure if this is a good idea here...
-        
+
         if (!empty($_REQUEST['_ids'])) {
             $ids = explode(',',$_REQUEST['_ids']);
             $x->whereAddIn($this->key, $ids, 'int');
@@ -845,7 +885,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);
@@ -937,10 +977,12 @@ class Pman_Roo extends Pman
         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);
         }
@@ -1099,11 +1141,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);
         }
@@ -1387,7 +1429,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)) {
@@ -1455,7 +1497,7 @@ class Pman_Roo extends Pman
                 
             }
             
-            
+                
             
             switch($key) {
                     
@@ -1468,7 +1510,7 @@ class Pman_Roo extends Pman
                         $x->whereAdd("(on_id = $oid  OR 
                                 on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o ) 
                             )");
-                        continue;
+                        continue 2;
                                 
                     }
                     $x->on_id = $val;
@@ -1486,7 +1528,7 @@ class Pman_Roo extends Pman
                         
                         // the aobve rule for !strlen non-joined cols should apply to joined ones.
                         if (!strlen($val)) {
-                            continue;
+                            continue 2;
                         }
                         
                         
@@ -1499,7 +1541,7 @@ class Pman_Roo extends Pman
                     }
                     
                     
-                    continue;
+                    continue 2;
             }
         }
         if (!empty($q_filtered)) {
@@ -1560,7 +1602,7 @@ 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;
         
     }