RooGetTrait.php
[Pman.Core] / RooGetTrait.php
index 028f65c..1ddc7d2 100644 (file)
@@ -196,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...
@@ -239,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);
         }
         
         
@@ -254,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);
@@ -285,65 +275,6 @@ trait Pman_Core_RooGetTrait {
         
     }
     
-    function selectSingle($x, $id, $req=false)
-    {
-        $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
-
-        if (!is_array($id) && empty($id)) {
-            
-            if (method_exists($x, 'toRooSingleArray')) {
-                $this->jok($x->toRooSingleArray($this->getAuthUser(), $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);
-        }
-        
-        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->getAuthUser(), $req));
-        }
-        if (method_exists($x, 'toRooArray')) {
-            $this->jok($x->toRooArray($req));
-        }
-        
-        $this->jok($x->toArray());
-        
-        
-    }
-    
     function setFilters($x, $q)
     {
         if (method_exists($x, 'applyFilters')) {
@@ -484,7 +415,7 @@ trait Pman_Core_RooGetTrait {
         $sorted = false;
         if (method_exists($x, 'applySort')) {
             $sorted = $x->applySort(
-                    $this->authUser,
+                    $this->getAuthUser(),
                     $sort,
                     $dir,
                     array_keys($this->cols),
@@ -521,4 +452,145 @@ trait Pman_Core_RooGetTrait {
             }
         }
     }
+    
+    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);
+       
+                        
+                    }
+                    
+                    
+                    //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;
+            
+        } 
+        
+        
+        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;
+        
+    }
+    
+    function meta($x, $data)
+    {
+        $lost = 0;
+        $cols  = array_keys($data[0]);
+     
+        $options = &PEAR::getStaticProperty('DB_DataObject','options');
+        $reader = $options["ini_{$x->_database}"] .'.reader';
+        if (!file_exists( $reader )) {
+            return;
+        }
+        
+        $rdata = unserialize(file_get_contents($reader));
+        
+        $keys = $x->keys();
+        $key = empty($keys) ? 'id' : $keys[0];
+        
+        
+        $meta = array();
+        foreach($cols as $c ) {
+            if (!isset($this->cols[$c]) || !isset($rdata[$this->cols[$c]]) || !is_array($rdata[$this->cols[$c]])) {
+                $meta[] = $c;
+                continue;    
+            }
+            $add = $rdata[$this->cols[$c]];
+            $add['name'] = $c;
+            $meta[] = $add;
+        }
+        return array(
+            'totalProperty' =>  'total',
+            'successProperty' => 'success',
+            'root' => 'data',
+            'id' => $key, // was 'id'...
+            'fields' => $meta
+        );
+         
+        
+    }
 }