From b8763acd144de0936794ab3a784bcbf6e5761039 Mon Sep 17 00:00:00 2001 From: edward Date: Wed, 30 Mar 2016 19:01:31 +0800 Subject: [PATCH] RooGetTrait.php --- RooGetTrait.php | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/RooGetTrait.php b/RooGetTrait.php index cd940bbb..7f951968 100644 --- a/RooGetTrait.php +++ b/RooGetTrait.php @@ -518,4 +518,109 @@ 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 "
"; 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;
+    
+        
+        
+    }
 }
-- 
2.39.2