SimpleExcel.php
[Pman.Core] / SimpleExcel.php
1 <?php
2
3 /**
4  * class to generate excel file from rows of data, and a configuration.
5  *
6  * usage :
7  *   $x = new Pman_Core_SimpleExcel(array())
8  *   $x->send($fn);
9  *
10  * 
11  * cfg:
12  *     formats 
13  *          name : [ Align : left, .... ]
14  *          
15  *     workbook : nameof
16  *
17  *     head  : [
18             [ "a", "b" ]
19             [],
20             [ "A", "B" ]
21             [ "a",  ["test", "left"]  ] << sub array [text, formatname]
22         ],
23  *     cols :  array(
24             array(
25                 'header'=> "Thumbnail",
26                 'dataIndex'=> 'id',
27                 'width'=>  75,
28                 'renderer' => array($this, 'getThumb'),
29  *              'color' => 'yellow', // set color for the cell which is a header element
30  *              'fillBlank' => 'gray', // set the color for the cell which is a blank area
31             ),
32         
33         // if this is set then it will add a tab foreach one.
34         workbooks = array(
35             workbook ->
36             
37             
38             
39  */
40  
41  
42  
43
44
45 class Pman_Core_SimpleExcel extends Pman
46 {
47     
48     var $workSheetCfg = array();
49     var $start_row = 0;
50     var $formats = array();
51     var $workbook = false;
52     var $worksheet= false;
53     
54     function Pman_Core_SimpleExcel($data,$cfg)
55     {
56       // print_r($cfg);exit;
57         require_once 'Spreadsheet/Excel/Writer.php';
58         // Creating a workbook
59         $outfile2 = $this->tempName('xls');
60        // var_dump($outfile2);
61         $workbook = new Spreadsheet_Excel_Writer($outfile2);
62         //$workbook = new Spreadsheet_Excel_Writer();
63         $workbook->setVersion(8);
64         // sending HTTP headers
65         
66         $formats = array();
67        
68         $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
69         foreach($cfg['formats'] as $f=>$fcfg) {
70             $formats[$f] = & $workbook->addFormat();
71             foreach($fcfg as $k=>$v) {
72                 $formats[$f]->{'set' . $k}($v);
73             }
74              
75         }
76          
77
78         if (empty($cfg['workbooks'])) {
79             $this->buildpage( $workbook,  $formats , $data,$cfg);
80         } else {
81             foreach($cfg['workbooks'] as $i =>$wcfg) {
82                 $this->buildpage( $workbook,  $formats , $data[$i],$wcfg);
83             }
84             
85         }
86         
87         if (empty($cfg['leave_open'])) {
88             $this->workbook = $workbook;
89             $this->outfile2 = $outfile2;
90             return;
91         }
92         
93         $workbook->close();
94         $this->outfile2 = $outfile2;
95          
96     }
97     
98     
99     static function date($str)
100     {
101         
102         return (strtotime($str . ' UTC') +  (86400 *  25569)) / 86400;
103         
104     }
105     
106     
107     function buildpage($workbook,  $formats , $data,$cfg)
108     {
109         //echo '<PRE>';        print_R($cfg);
110       //  print_r($cfg);exit;
111         // Creating a worksheet
112         
113         // copy the config and alias so that book can be written to..
114         $this->worksheetCfg[$cfg['workbook']] = &$cfg;
115         
116         $worksheet =  $workbook->addWorksheet($cfg['workbook']);
117         if (is_a($worksheet, 'PEAR_Error')) {
118             die($worksheet->toString());
119         }
120         //print_R($worksheet);
121         $worksheet->setInputEncoding('UTF-8'); 
122          
123         $this->worksheet = $worksheet;
124          
125         $start_row = 0;
126         
127         if (!empty($cfg['head'])) {
128             foreach($cfg['head'] as $row) { 
129                 foreach($row as $c => $col) {
130                     if (is_array($col)) {
131                         $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
132                         $worksheet->write($start_row, $c, $col[0], $format);
133                         continue;
134                     }
135                     
136                     $worksheet->write($start_row, $c, $col);
137                     
138                 }
139                 $start_row++;
140             }
141             // add a spacer..
142             $start_row++;
143         }
144             
145             
146             
147          
148         foreach($cfg['cols'] as $c=>$col_cfg) {
149             if (is_string($col_cfg)) {
150                 $cfg['cols'][$c] = array(
151                     'header' => $col_cfg,
152                     'dataIndex' => $col_cfg,
153                     'width' => 50,
154                     
155                 );
156             }
157         }
158          
159          
160         foreach($cfg['cols'] as $c=>$col_cfg) {
161             $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
162             $worksheet->write($start_row, $c, $col_cfg['header'],$format);
163             $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
164              
165         }
166         $start_row++;
167         $this->start_row = &$start_row;
168         
169         
170         $hasRender  = false;
171            //     DB_DataObject::debugLevel(1);
172         foreach($data as $r=>$clo) {
173             
174             $this->addLine($cfg['workbook'], $clo);
175              
176         }
177         /// call user render on any that are defined..
178         if ($hasRender) {
179             foreach($data as $r=>$cl) {
180             
181                 foreach($cfg['cols']   as $c=>$col_cfg) {
182                     $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
183                     if (empty($cl[$col_cfg['dataIndex']])) {
184                         continue;
185                     }
186                     if (isset($col_cfg['renderer'])) {
187                         call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
188                         
189                     }
190                   //  echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
191              
192                 }
193             }
194         }
195         $start_row += count($data);
196         
197         if (!empty($cfg['foot'])) {
198             foreach($cfg['foot'] as $row) { 
199                 foreach($row as $c => $col) {
200                     // if it's an array? - formated ???
201                     if (is_array($col)) {
202                         $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
203                         $worksheet->write($start_row, $c, $col[0], $format);
204                         continue;
205                     }
206                     $worksheet->write($start_row, $c, $col);
207                     
208                 }
209                 $start_row++;
210             }
211             // add a spacer..
212             $start_row++;
213         }
214             
215         $this->formats = $formats;
216         
217         
218         
219         
220     }
221     
222     function addLine($worksheet_name, $clo)
223     {
224         $cfg        = $this->workSheetCfg[$worksheet_name];
225         $start_row  = $this->start_row;
226         $formats    = $this->formats;
227         $worksheet  = $this->worksheet;
228         
229         $r = 0;
230        
231         $cl = $clo;
232         if (is_object($clo)) {
233             $cl = (array)$clo; // lossless converstion..
234         }
235         
236         if (isset($cfg['row_height'])) {
237             $worksheet->setRow($start_row +$r, $cfg['row_height']);
238         }
239         
240         foreach($cfg['cols']  as $c=>$col_cfg) {
241             
242             if(isset($cl[$col_cfg['dataIndex']])){
243                 $v = $cl[$col_cfg['dataIndex']];
244             }else{
245                 if(isset($col_cfg['fillBlank'])){
246                     $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
247                 }
248                 continue;
249             }
250             
251             if (empty($cl[$col_cfg['dataIndex']])) {
252                 continue;
253             }
254             if (isset($col_cfg['txtrenderer'])) {
255                 $v = call_user_func($col_cfg['txtrenderer'], 
256                         $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
257                 if ($v === false) {
258                     continue;
259                 }
260               //  var_dump($v);
261             }
262             if (isset($col_cfg['renderer'])) {
263                 $hasRender = true;
264                 continue;
265             }
266             
267             $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
268             
269             $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
270             
271             $worksheet->write($start_row+$r, $c, $v, $format);
272         }
273         $this->start_row++;
274         
275         
276     }
277     
278     
279     
280     
281     function send($fn)
282     {
283         if (empty($cfg['leave_open'])) {
284                 $this->
285         
286         require_once 'File/Convert.php';
287         $fc=  new File_Convert($this->outfile2, "application/vnd.ms-excel");
288         $fn = $fc->convert("application/vnd.ms-excel"); 
289         $fc->serve('attachment',$fn); // can fix IE Mess
290     }
291      
292     
293 }