DataObjects/core.sql
[Pman.Core] / SimpleExcel.php
index 3faee55..65e42c4 100644 (file)
             [ "a", "b" ]
             [],
             [ "A", "B" ]
+            [ "a",  ["test", "left"]  ] << sub array [text, formatname]
         ],
  *     cols :  array(
             array(
                 'header'=> "Thumbnail",
                 'dataIndex'=> 'id',
                 'width'=>  75,
-                'renderer' => array($this, 'getThumb')
+                'renderer' => array($this, 'getThumb'),
+ *              'color' => 'yellow', // set color for the cell which is a header element
+ *              'fillBlank' => 'gray', // set the color for the cell which is a blank area
             ),
-            
+        
+        // if this is set then it will add a tab foreach one.
+        workbooks = array(
+            workbook ->
             
             
             
@@ -43,11 +49,10 @@ class Pman_Core_SimpleExcel extends Pman
     
     function Pman_Core_SimpleExcel($data,$cfg)
     {
-     //  print_r($cfg);
+      // print_r($cfg);exit;
         require_once 'Spreadsheet/Excel/Writer.php';
-        $pman = new Pman();
         // Creating a workbook
-        $outfile2 = $pman->tempName('xls');
+        $outfile2 = $this->tempName('xls');
        // var_dump($outfile2);
         $workbook = new Spreadsheet_Excel_Writer($outfile2);
         //$workbook = new Spreadsheet_Excel_Writer();
@@ -66,9 +71,39 @@ class Pman_Core_SimpleExcel extends Pman
         }
          
 
+        if (empty($cfg['workbooks'])) {
+            $this->buildpage( $workbook,  $formats , $data,$cfg);
+        } else {
+            foreach($cfg['workbooks'] as $i =>$wcfg) {
+                $this->buildpage( $workbook,  $formats , $data[$i],$wcfg);
+            }
+            
+        }
+        
+        $workbook->close();
+        $this->outfile2 = $outfile2;
+         
+    }
+    
+    
+    static function date($str)
+    {
+        
+        return (strtotime($str . ' UTC') +  (86400 *  25569)) / 86400;
         
+    }
+    
+    
+    function buildpage($workbook,  $formats , $data,$cfg)
+    {
+        //echo '<PRE>';        print_R($cfg);
+      //  print_r($cfg);exit;
         // Creating a worksheet
-        $worksheet =& $workbook->addWorksheet($cfg['workbook']);
+        $worksheet =  $workbook->addWorksheet($cfg['workbook']);
+        if (is_a($worksheet, 'PEAR_Error')) {
+            die($worksheet->toString());
+        }
+        //print_R($worksheet);
         $worksheet->setInputEncoding('UTF-8'); 
          
          
@@ -78,6 +113,12 @@ class Pman_Core_SimpleExcel extends Pman
         if (!empty($cfg['head'])) {
             foreach($cfg['head'] as $row) { 
                 foreach($row as $c => $col) {
+                    if (is_array($col)) {
+                        $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
+                        $worksheet->write($start_row, $c, $col[0], $format);
+                        continue;
+                    }
+                    
                     $worksheet->write($start_row, $c, $col);
                     
                 }
@@ -90,12 +131,21 @@ class Pman_Core_SimpleExcel extends Pman
             
             
          
-         
-         
+        foreach($cfg['cols'] as $c=>$col_cfg) {
+            if (is_string($col_cfg)) {
+                $cfg['cols'][$c] = array(
+                    'header' => $col_cfg,
+                    'dataIndex' => $col_cfg,
+                    'width' => 50,
+                    
+                );
+            }
+        }
          
          
         foreach($cfg['cols'] as $c=>$col_cfg) {
-            $worksheet->write($start_row, $c, $col_cfg['header']);
+            $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
+            $worksheet->write($start_row, $c, $col_cfg['header'],$format);
             $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
              
         }
@@ -105,7 +155,7 @@ class Pman_Core_SimpleExcel extends Pman
         foreach($data as $r=>$clo) {
             $cl = $clo;
             if (is_object($clo)) {
-                $cl = $cl->toArray();
+                $cl = (array)$clo; // lossless converstion..
             }
             
             if (isset($cfg['row_height'])) {
@@ -113,7 +163,16 @@ class Pman_Core_SimpleExcel extends Pman
             }
             
             foreach($cfg['cols']  as $c=>$col_cfg) {
-                $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
+                
+                if(isset($cl[$col_cfg['dataIndex']])){
+                    $v = $cl[$col_cfg['dataIndex']];
+                }else{
+                    if(isset($col_cfg['fillBlank'])){
+                        $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
+                    }
+                    continue;
+                }
+                
                 if (empty($cl[$col_cfg['dataIndex']])) {
                     continue;
                 }
@@ -131,9 +190,9 @@ class Pman_Core_SimpleExcel extends Pman
                 }
                 
                 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
+                
                 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
                 
-          //    echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
                 $worksheet->write($start_row+$r, $c, $v, $format);
             }
         }
@@ -161,6 +220,11 @@ class Pman_Core_SimpleExcel extends Pman
             foreach($cfg['foot'] as $row) { 
                 foreach($row as $c => $col) {
                     // if it's an array? - formated ???
+                    if (is_array($col)) {
+                        $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
+                        $worksheet->write($start_row, $c, $col[0], $format);
+                        continue;
+                    }
                     $worksheet->write($start_row, $c, $col);
                     
                 }
@@ -172,16 +236,12 @@ class Pman_Core_SimpleExcel extends Pman
             
         
         
-        $workbook->close();
-        $this->outfile2 = $outfile2;
-         
+        
+        
     }
     
     function send($fn)
     {
-        
-     
-       
         require_once 'File/Convert.php';
         $fc=  new File_Convert($this->outfile2, "application/vnd.ms-excel");
         $fn = $fc->convert("application/vnd.ms-excel");