adad761d3655026c709c2fcf4db89fefb596f2e6
[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             $cl = $clo;
174             if (is_object($clo)) {
175                 $cl = (array)$clo; // lossless converstion..
176             }
177             
178             if (isset($cfg['row_height'])) {
179                 $worksheet->setRow($start_row +$r, $cfg['row_height']);
180             }
181             
182             foreach($cfg['cols']  as $c=>$col_cfg) {
183                 
184                 if(isset($cl[$col_cfg['dataIndex']])){
185                     $v = $cl[$col_cfg['dataIndex']];
186                 }else{
187                     if(isset($col_cfg['fillBlank'])){
188                         $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
189                     }
190                     continue;
191                 }
192                 
193                 if (empty($cl[$col_cfg['dataIndex']])) {
194                     continue;
195                 }
196                 if (isset($col_cfg['txtrenderer'])) {
197                     $v = call_user_func($col_cfg['txtrenderer'], 
198                             $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
199                     if ($v === false) {
200                         continue;
201                     }
202                   //  var_dump($v);
203                 }
204                 if (isset($col_cfg['renderer'])) {
205                     $hasRender = true;
206                     continue;
207                 }
208                 
209                 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
210                 
211                 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
212                 
213                 $worksheet->write($start_row+$r, $c, $v, $format);
214             }
215         }
216         /// call user render on any that are defined..
217         if ($hasRender) {
218             foreach($data as $r=>$cl) {
219             
220                 foreach($cfg['cols']   as $c=>$col_cfg) {
221                     $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
222                     if (empty($cl[$col_cfg['dataIndex']])) {
223                         continue;
224                     }
225                     if (isset($col_cfg['renderer'])) {
226                         call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
227                         
228                     }
229                   //  echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
230              
231                 }
232             }
233         }
234         $start_row += count($data);
235         
236         if (!empty($cfg['foot'])) {
237             foreach($cfg['foot'] as $row) { 
238                 foreach($row as $c => $col) {
239                     // if it's an array? - formated ???
240                     if (is_array($col)) {
241                         $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
242                         $worksheet->write($start_row, $c, $col[0], $format);
243                         continue;
244                     }
245                     $worksheet->write($start_row, $c, $col);
246                     
247                 }
248                 $start_row++;
249             }
250             // add a spacer..
251             $start_row++;
252         }
253             
254         $this->formats = $formats;
255         
256         
257         
258         
259     }
260     
261     function addLine($worksheet_name, $clo)
262     {
263         $cfg        = $this->workSheetCfg[$worksheet_name];
264         $start_row  = $this->start_row;
265         $formats    = $this->formats;
266         $worksheet  = $this->worksheet;
267         
268         $r = 0;
269        
270         $cl = $clo;
271         if (is_object($clo)) {
272             $cl = (array)$clo; // lossless converstion..
273         }
274         
275         if (isset($cfg['row_height'])) {
276             $worksheet->setRow($start_row +$r, $cfg['row_height']);
277         }
278         
279         foreach($cfg['cols']  as $c=>$col_cfg) {
280             
281             if(isset($cl[$col_cfg['dataIndex']])){
282                 $v = $cl[$col_cfg['dataIndex']];
283             }else{
284                 if(isset($col_cfg['fillBlank'])){
285                     $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
286                 }
287                 continue;
288             }
289             
290             if (empty($cl[$col_cfg['dataIndex']])) {
291                 continue;
292             }
293             if (isset($col_cfg['txtrenderer'])) {
294                 $v = call_user_func($col_cfg['txtrenderer'], 
295                         $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
296                 if ($v === false) {
297                     continue;
298                 }
299               //  var_dump($v);
300             }
301             if (isset($col_cfg['renderer'])) {
302                 $hasRender = true;
303                 continue;
304             }
305             
306             $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
307             
308             $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
309             
310             $worksheet->write($start_row+$r, $c, $v, $format);
311         }
312         $this->start_row++;
313         
314         
315     }
316     
317     
318     
319     
320     function send($fn)
321     {
322         if (empty($cfg['leave_open'])) {
323                 $this->
324         
325         require_once 'File/Convert.php';
326         $fc=  new File_Convert($this->outfile2, "application/vnd.ms-excel");
327         $fn = $fc->convert("application/vnd.ms-excel"); 
328         $fc->serve('attachment',$fn); // can fix IE Mess
329     }
330      
331     
332 }