DataObjects/Core_watch.php
[Pman.Core] / SimpleExcel.php
index b5c0396..0aef6e1 100644 (file)
@@ -24,6 +24,7 @@
             array(
                 'header'=> "Thumbnail",
                 'dataIndex'=> 'id',
+ *              'dataFormat' => 'string' // to force a string..
                 'width'=>  75,
                 'renderer' => array($this, 'getThumb'),
  *              'color' => 'yellow', // set color for the cell which is a header element
@@ -40,6 +41,7 @@
  
  
  
+require_once 'Pman.php';
 
 
 class Pman_Core_SimpleExcel extends Pman
@@ -62,30 +64,34 @@ class Pman_Core_SimpleExcel extends Pman
         //$workbook = new Spreadsheet_Excel_Writer();
         $workbook->setVersion(8);
         // sending HTTP headers
-        
+        $this->workbook = $workbook;
+            
         $formats = array();
        
         $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
+        
         foreach($cfg['formats'] as $f=>$fcfg) {
+            
             $formats[$f] = & $workbook->addFormat();
-            foreach($fcfg as $k=>$v) {
+            foreach((array)$fcfg as $k=>$v) {
                 $formats[$f]->{'set' . $k}($v);
             }
              
         }
          
-
-        if (empty($cfg['workbooks'])) {
-            $this->buildpage( $workbook,  $formats , $data,$cfg);
-        } else {
+         
+        if (!empty($cfg['workbook'])) {
+            $this->buildPage(   $formats , $data,$cfg);
+        } elseif (!empty($cfg['workbooks'])) {
             foreach($cfg['workbooks'] as $i =>$wcfg) {
-                $this->buildpage( $workbook,  $formats , $data[$i],$wcfg);
+                $this->buildPage(   $formats , $data[$i],$wcfg);
             }
             
         }
+        // if workbooks == false - > the user can call buildpage..
+        
         
         if (!empty($cfg['leave_open'])) {
-            $this->workbook = $workbook;
             $this->outfile2 = $outfile2;
             return;
         }
@@ -96,6 +102,8 @@ class Pman_Core_SimpleExcel extends Pman
     }
     
     
+    
+    
     static function date($str)
     {
         
@@ -104,8 +112,9 @@ class Pman_Core_SimpleExcel extends Pman
     }
     
     
-    function buildpage($workbook,  $formats , $data,$cfg)
+    function buildPage( $formats , $data, $cfg)
     {
+        $workbook = $this->workbook;
         //echo '<PRE>';        print_R($cfg);
       //  print_r($cfg);exit;
         // Creating a worksheet
@@ -113,6 +122,27 @@ class Pman_Core_SimpleExcel extends Pman
         // copy the config and alias so that book can be written to..
         $this->worksheet_cfg[$cfg['workbook']] = &$cfg;
         
+        
+        //$this->formats = (array)$formats;
+        
+        if (isset($cfg['formats']) && empty($formats)) {
+            
+            foreach($cfg['formats'] as $f=>$fcfg) {
+                
+                $formats[$f] = & $workbook->addFormat();
+                foreach((array)$fcfg as $k=>$v) {
+                    $formats[$f]->{'set' . $k}($v);
+                }
+                 
+            }
+            
+            $this->formats = $formats;
+        }
+        
+        
+        
+        //var_dump($cfg['workbook']);
+
         $worksheet =  $workbook->addWorksheet($cfg['workbook']);
         if (is_a($worksheet, 'PEAR_Error')) {
             die($worksheet->toString());
@@ -139,19 +169,16 @@ class Pman_Core_SimpleExcel extends Pman
                 $start_row++;
             }
             // add a spacer..
-            $start_row++;
+            if(!isset($cfg['nonspacer'])){ $start_row++; }
         }
             
-            
-            
-         
+               
         foreach($cfg['cols'] as $c=>$col_cfg) {
             if (is_string($col_cfg)) {
                 $cfg['cols'][$c] = array(
                     'header' => $col_cfg,
                     'dataIndex' => $col_cfg,
                     'width' => 50,
-                    
                 );
             }
         }
@@ -161,16 +188,19 @@ class Pman_Core_SimpleExcel extends Pman
             $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);
-             
         }
         $start_row++;
         $this->start_row = &$start_row;
         
         
         $hasRender  = false;
-           //     DB_DataObject::debugLevel(1);
+         
+        if (empty($data)) {
+            return;
+        }
+        
+        
         foreach($data as $r=>$clo) {
-            
             $hasRenderRow = $this->addLine($cfg['workbook'], $clo);
             $hasRender = ($hasRender  || $hasRenderRow) ? true : false;
              
@@ -214,9 +244,6 @@ class Pman_Core_SimpleExcel extends Pman
             $start_row++;
         }
             
-        $this->formats = $formats;
-        
-        
         
         
     }
@@ -225,7 +252,7 @@ class Pman_Core_SimpleExcel extends Pman
     {
         $cfg        = $this->worksheet_cfg[$worksheet_name];
         $start_row  = $this->start_row;
-        $formats    = $this->formats;
+        $formats    = (array)$this->formats;
         $worksheet  = $this->worksheet;
         
         $hasRender   = false;
@@ -251,7 +278,7 @@ class Pman_Core_SimpleExcel extends Pman
                 continue;
             }
             
-            if (empty($cl[$col_cfg['dataIndex']])) {
+            if (!isset($cl[$col_cfg['dataIndex']])) {
                 continue;
             }
             if (isset($col_cfg['txtrenderer'])) {
@@ -269,19 +296,29 @@ class Pman_Core_SimpleExcel extends Pman
             
             $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
             
-            $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
+            $dataFormat = empty($col_cfg['dataFormat']) ? '' : $col_cfg['dataFormat'];
+             ;
+            
             
-            $worksheet->write($start_row+$r, $c, $v, $format);
+            $format = isset($col_cfg['format'])  && isset($formats[$col_cfg['format']] )   ? $formats[$col_cfg['format']] : false;
+          //  print_R(array($start_row+$r, $c, $v, $format));exit;
+          // handle 0 prefixes..
+            if ( (is_numeric($v) &&  strlen($v) > 1 && substr($v,0,1) == '0' && substr($v,1,1) != '.') 
+                    || 
+                    $dataFormat == 'string' ) {
+                $worksheet->writeString($start_row+$r, $c, $v, $format);
+            } else {
+          
+                $worksheet->write($start_row+$r, $c, $v, $format);
+            }
         }
         $this->start_row++;
         
         return $hasRender;
     }
+     
     
-    
-    
-    
-    function send($fn)
+    function send($fname)
     {
         if (!empty($this->workbook)) {
             $this->workbook->close();
@@ -291,7 +328,7 @@ class Pman_Core_SimpleExcel extends Pman
         require_once 'File/Convert.php';
         $fc=  new File_Convert($this->outfile2, "application/vnd.ms-excel");
         $fn = $fc->convert("application/vnd.ms-excel"); 
-        $fc->serve('attachment',$fn); // can fix IE Mess
+        $fc->serve('attachment',$fname); // can fix IE Mess
     }