RooGetTrait.php
[Pman.Core] / RooGetTrait.php
index f460616..bbb9b68 100644 (file)
@@ -131,19 +131,6 @@ trait Pman_Core_RooGetTrait {
        
         if (isset($_REQUEST['_delete'])) {
             $this->jerr("DELETE by GET has been removed - update the code to use POST");
-            /*
-            
-            $keys = $x->keys();
-            if (empty($keys) ) {
-                $this->jerr('no key');
-            }
-            
-            $this->key = $keys[0];
-            
-            
-            // do we really delete stuff!?!?!?
-            return $this->delete($x,$_REQUEST);
-            */
         } 
         
         
@@ -169,10 +156,10 @@ trait Pman_Core_RooGetTrait {
         
         // 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);
@@ -181,10 +168,6 @@ trait Pman_Core_RooGetTrait {
             $this->jerr("PERMISSION DENIED (g)");
         }
         
-         //print_r($x);
-        // build join if req.
-          //DB_DataObject::debugLevel(1);
-       //   var_dump($this->countWhat);
         $total = $x->count($this->countWhat);
         // sorting..
       //   
@@ -213,11 +196,8 @@ trait Pman_Core_RooGetTrait {
         
         if (false === $res) {
             $this->jerr($x->_lastError->toString());
-            
         }
         
-        
-        
         $ret = array();
         
         // ---------------- THESE ARE DEPRICATED.. they should be moved to the model...
@@ -256,7 +236,7 @@ trait Pman_Core_RooGetTrait {
         
         // filter results, and add any data that is needed...
         if (method_exists($x,'postListFilter')) {
-            $ret = $x->postListFilter($ret, $this->authUser, $_REQUEST);
+            $ret = $x->postListFilter($ret, $this->getAuthUser(), $_REQUEST);
         }
         
         
@@ -271,13 +251,6 @@ trait Pman_Core_RooGetTrait {
             
         
         }
-        //die("DONE?");
-      
-        //if ($x->tableName() == 'Documents_Tracking') {
-        //    $ret = $this->replaceSubject(&$ret, 'doc_id_subject');
-       // }
-        
-        
         
         if (!empty($_REQUEST['_requestMeta']) &&  count($ret)) {
             $meta = $this->meta($x, $ret);
@@ -322,7 +295,6 @@ trait Pman_Core_RooGetTrait {
         
         $this->loadMap($x, array(
             'columns' => $_columns,
-
         ));
         
         if ($req !== false) { 
@@ -351,7 +323,7 @@ trait Pman_Core_RooGetTrait {
         }
         // different symantics on all these calls??
         if (method_exists($x, 'toRooSingleArray')) {
-            $this->jok($x->toRooSingleArray($this->authUser, $req));
+            $this->jok($x->toRooSingleArray($this->getAuthUser(), $req));
         }
         if (method_exists($x, 'toRooArray')) {
             $this->jok($x->toRooArray($req));
@@ -440,9 +412,7 @@ trait Pman_Core_RooGetTrait {
                 continue;
                 
             }
-            
-            
-            
+
             switch($key) {
                     
                 // Events and remarks -- fixme - move to events/remarsk...
@@ -482,29 +452,190 @@ trait Pman_Core_RooGetTrait {
             }
         }
         if (!empty($q_filtered)) {
-            //var_dump($q_filtered);
-            
+            $x->setFrom($q_filtered);
+        }
+        
+        if (!empty($q['query']['name'])) {
+            if (in_array( 'name',  array_keys($x->table()))) {
+                $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
+            }
+        }
+        
+    }
+    
+    function applySort($x, $sort = '', $dir ='')
+    {
+        $sort = empty($_REQUEST['sort']) ? $sort : $_REQUEST['sort'];
+        $dir = empty($_REQUEST['dir']) ? $dir : $_REQUEST['dir'];
+        $dir = $dir == 'ASC' ? 'ASC' : 'DESC';
+         
+        $ms = empty($_REQUEST['_multisort']) ? false : $_REQUEST['_multisort'];
+        //var_Dump($ms);exit;
+        $sorted = false;
+        if (method_exists($x, 'applySort')) {
+            $sorted = $x->applySort(
+                    $this->getAuthUser(),
+                    $sort,
+                    $dir,
+                    array_keys($this->cols),
+                    $ms ? json_decode($ms) : false
+            );
+        }
+        if ($ms !== false) {
+            return $this->multiSort($x);
+        }
+        
+        if ($sorted === false) {
             
+            $cols = $x->table();
+            $excols = array_keys($this->cols);
             
-            $x->setFrom($q_filtered);
+            if (isset($x->_extra_cols)) {
+                $excols = array_merge($excols, $x->_extra_cols);
+            }
+            $sort_ar = explode(',', $sort);
+            $sort_str = array();
+          
+            foreach($sort_ar as $sort) {
+                
+                if (strlen($sort) && isset($cols[$sort]) ) {
+                    $sort_str[] =  $x->tableName() .'.'.$sort . ' ' . $dir ;
+                    
+                } else if (in_array($sort, $excols)) {
+                    $sort_str[] = $sort . ' ' . $dir ;
+                }
+            }
+             
+            if ($sort_str) {
+                $x->orderBy(implode(', ', $sort_str ));
+            }
         }
+    }
+    
+    function toCsv($data, $cols, $titles, $filename, $addDate = true)
+    {
+        $this->sessionState(0); // turn off sessions  - no locking..
+
+        require_once 'Pman/Core/SimpleExcel.php';
+        
+        $fn = (empty($filename) ? 'list-export-' : urlencode($filename)) . (($addDate) ? date('Y-m-d') : '') ;
+        
         
+        $se_config=  array(
+            'workbook' => substr($fn, 0, 31),
+            'cols' => array(),
+            'leave_open' => true
+        );
         
         
+        $se = false;
+        if (is_object($data)) {
+            $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 ($titles== '*') {
+                    $titles= array_keys($x);
+                }
+                if ($titles !== false) {
+                    
+                    foreach($cols as $i=>$col) {
+                        $se_config['cols'][] = array(
+                            'header'=> isset($titles[$i]) ? $titles[$i] : $col,
+                            'dataIndex'=> $col,
+                            'width'=>  100
+                        );
+                         $se = new Pman_Core_SimpleExcel(array(), $se_config);
        
-        // nice generic -- let's get rid of it.. where is it used!!!!
-        // used by: 
-        // Person / Group / Comapnies.... most of my queries noww...
-        if (!empty($q['query']['name'])) {
-            
+                        
+                    }
+                    
+                    
+                    //fputcsv($fh, $titles);
+                    $titles = false;
+                }
+                
+
+                $se->addLine($se_config['workbook'], $x);
+                    
+                
+            }
+            if(!$se){
+                
+                $this->jerr('no data found', false, 'text/plain');
+            }
+            $se->send($fn .'.xls');
+            exit;
             
-            if (in_array( 'name',  array_keys($x->table()))) {
-                $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
+        } 
+        
+        
+        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 ($titles !== false) {
+                foreach($cols as $i=>$col) {
+                    $se_config['cols'][] = array(
+                        'header'=> isset($titles[$i]) ? $titles[$i] : $col,
+                        'dataIndex'=> $col,
+                        'width'=>  100,
+                       //     'renderer' => array($this, 'getThumb'),
+                         //   'color' => 'yellow', // set color for the cell which is a header element
+                          // 'fillBlank' => 'gray', // set 
+                    );
+                    $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;
         
-        // - projectdirectory staff list - persn queuy
-     
+    }
+    
+    var $cols = array();
+    
+    function loadMap($do, $cfg =array())
+    {
+        $onlycolumns    = !empty($cfg['columns']) ? $cfg['columns'] : false;
+        $distinct       = !empty($cfg['distinct']) ? $cfg['distinct'] : false;
+        $excludecolumns = !empty($cfg['exclude']) ? $cfg['exclude'] : array();
+          
+        $excludecolumns[] = 'passwd'; // we never expose passwords
+       
+        $ret = $do->autoJoin(array(
+            'include' => $onlycolumns,
+            'exclude' => $excludecolumns,
+            'distinct' => $distinct
+        ));
+        
+        $this->countWhat = $ret['count'];
+        $this->cols = $ret['cols'];
+        $this->colsJname = $ret['join_names'];
+        
+        return;
         
     }
 }