RooGetTrait.php
[Pman.Core] / RooGetTrait.php
index cd940bb..7f95196 100644 (file)
@@ -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 "<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;
+    
+        
+        
+    }
 }