DataObjects/Core_watch.php
[Pman.Core] / SimpleExcel.php
index 3421dc5..4c9edd4 100644 (file)
                 'width'=>  75,
                 'renderer' => array($this, 'getThumb')
             ),
-            
+        
+        // if this is set then it will add a tab foreach one.
+        workbooks = array(
+            workbook ->
             
             
             
@@ -45,9 +48,8 @@ class Pman_Core_SimpleExcel extends Pman
     {
      //  print_r($cfg);
         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 +68,34 @@ 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;
+         
+    }
+    
+    function buildpage($workbook,  $formats , $data,$cfg)
+    {
+        //echo '<PRE>';        print_R($cfg);
         
         // 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'); 
          
          
@@ -90,11 +117,22 @@ 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']);
             $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
              
@@ -102,9 +140,10 @@ class Pman_Core_SimpleExcel extends Pman
         $start_row++;
         $hasRender  = false;
            //     DB_DataObject::debugLevel(1);
-        foreach($data as $r=>$cl) {
-            if (is_object($cl)) {
-                $cl = $cl->toArray();
+        foreach($data as $r=>$clo) {
+            $cl = $clo;
+            if (is_object($clo)) {
+                $cl = (array)$clo; // lossless converstion..
             }
             
             if (isset($cfg['row_height'])) {
@@ -118,7 +157,7 @@ class Pman_Core_SimpleExcel extends Pman
                 }
                 if (isset($col_cfg['txtrenderer'])) {
                     $v = call_user_func($col_cfg['txtrenderer'], 
-                            $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
+                            $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
                     if ($v === false) {
                         continue;
                     }
@@ -171,16 +210,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");