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     
51     function Pman_Core_SimpleExcel($data,$cfg)
52     {
53       // print_r($cfg);exit;
54         require_once 'Spreadsheet/Excel/Writer.php';
55         // Creating a workbook
56         $outfile2 = $this->tempName('xls');
57        // var_dump($outfile2);
58         $workbook = new Spreadsheet_Excel_Writer($outfile2);
59         //$workbook = new Spreadsheet_Excel_Writer();
60         $workbook->setVersion(8);
61         // sending HTTP headers
62         
63         $formats = array();
64        
65         $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
66         foreach($cfg['formats'] as $f=>$fcfg) {
67             $formats[$f] = & $workbook->addFormat();
68             foreach($fcfg as $k=>$v) {
69                 $formats[$f]->{'set' . $k}($v);
70             }
71              
72         }
73          
74
75         if (empty($cfg['workbooks'])) {
76             $this->buildpage( $workbook,  $formats , $data,$cfg);
77         } else {
78             foreach($cfg['workbooks'] as $i =>$wcfg) {
79                 $this->buildpage( $workbook,  $formats , $data[$i],$wcfg);
80             }
81             
82         }
83         
84         if (empty($cfg['leave_open'])) {
85             
86             $this->outfile2 = $outfile2;
87             return;
88         }
89         
90         $workbook->close();
91         $this->outfile2 = $outfile2;
92          
93     }
94     
95     
96     static function date($str)
97     {
98         
99         return (strtotime($str . ' UTC') +  (86400 *  25569)) / 86400;
100         
101     }
102     
103     
104     function buildpage($workbook,  $formats , $data,$cfg)
105     {
106         //echo '<PRE>';        print_R($cfg);
107       //  print_r($cfg);exit;
108         // Creating a worksheet
109         
110         // copy the config and alias so that book can be written to..
111         $this->worksheetCfg[$cfg['workbook']] = &$cfg;
112         
113         $worksheet =  $workbook->addWorksheet($cfg['workbook']);
114         if (is_a($worksheet, 'PEAR_Error')) {
115             die($worksheet->toString());
116         }
117         //print_R($worksheet);
118         $worksheet->setInputEncoding('UTF-8'); 
119          
120          
121          
122         $start_row = 0;
123         
124         if (!empty($cfg['head'])) {
125             foreach($cfg['head'] as $row) { 
126                 foreach($row as $c => $col) {
127                     if (is_array($col)) {
128                         $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
129                         $worksheet->write($start_row, $c, $col[0], $format);
130                         continue;
131                     }
132                     
133                     $worksheet->write($start_row, $c, $col);
134                     
135                 }
136                 $start_row++;
137             }
138             // add a spacer..
139             $start_row++;
140         }
141             
142             
143             
144          
145         foreach($cfg['cols'] as $c=>$col_cfg) {
146             if (is_string($col_cfg)) {
147                 $cfg['cols'][$c] = array(
148                     'header' => $col_cfg,
149                     'dataIndex' => $col_cfg,
150                     'width' => 50,
151                     
152                 );
153             }
154         }
155          
156          
157         foreach($cfg['cols'] as $c=>$col_cfg) {
158             $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
159             $worksheet->write($start_row, $c, $col_cfg['header'],$format);
160             $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
161              
162         }
163         $start_row++;
164         $this->start_row = &$start_row;
165         
166         
167         $hasRender  = false;
168            //     DB_DataObject::debugLevel(1);
169         foreach($data as $r=>$clo) {
170             $cl = $clo;
171             if (is_object($clo)) {
172                 $cl = (array)$clo; // lossless converstion..
173             }
174             
175             if (isset($cfg['row_height'])) {
176                 $worksheet->setRow($start_row +$r, $cfg['row_height']);
177             }
178             
179             foreach($cfg['cols']  as $c=>$col_cfg) {
180                 
181                 if(isset($cl[$col_cfg['dataIndex']])){
182                     $v = $cl[$col_cfg['dataIndex']];
183                 }else{
184                     if(isset($col_cfg['fillBlank'])){
185                         $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
186                     }
187                     continue;
188                 }
189                 
190                 if (empty($cl[$col_cfg['dataIndex']])) {
191                     continue;
192                 }
193                 if (isset($col_cfg['txtrenderer'])) {
194                     $v = call_user_func($col_cfg['txtrenderer'], 
195                             $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
196                     if ($v === false) {
197                         continue;
198                     }
199                   //  var_dump($v);
200                 }
201                 if (isset($col_cfg['renderer'])) {
202                     $hasRender = true;
203                     continue;
204                 }
205                 
206                 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
207                 
208                 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
209                 
210                 $worksheet->write($start_row+$r, $c, $v, $format);
211             }
212         }
213         /// call user render on any that are defined..
214         if ($hasRender) {
215             foreach($data as $r=>$cl) {
216             
217                 foreach($cfg['cols']   as $c=>$col_cfg) {
218                     $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
219                     if (empty($cl[$col_cfg['dataIndex']])) {
220                         continue;
221                     }
222                     if (isset($col_cfg['renderer'])) {
223                         call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
224                         
225                     }
226                   //  echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
227              
228                 }
229             }
230         }
231         $start_row += count($data);
232         
233         if (!empty($cfg['foot'])) {
234             foreach($cfg['foot'] as $row) { 
235                 foreach($row as $c => $col) {
236                     // if it's an array? - formated ???
237                     if (is_array($col)) {
238                         $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
239                         $worksheet->write($start_row, $c, $col[0], $format);
240                         continue;
241                     }
242                     $worksheet->write($start_row, $c, $col);
243                     
244                 }
245                 $start_row++;
246             }
247             // add a spacer..
248             $start_row++;
249         }
250             
251         $this->formats = $formats;
252         
253         
254         
255     }
256     
257     function addLine($worksheet_name, $clo)
258     {
259         $cfg = $this->workSheetCfg[$worksheet_name];
260         $start_row = $this->start_row;
261         
262         $r = 0;
263        
264         $cl = $clo;
265         if (is_object($clo)) {
266             $cl = (array)$clo; // lossless converstion..
267         }
268         
269         if (isset($cfg['row_height'])) {
270             $worksheet->setRow($start_row +$r, $cfg['row_height']);
271         }
272         
273         foreach($cfg['cols']  as $c=>$col_cfg) {
274             
275             if(isset($cl[$col_cfg['dataIndex']])){
276                 $v = $cl[$col_cfg['dataIndex']];
277             }else{
278                 if(isset($col_cfg['fillBlank'])){
279                     $worksheet->write($start_row+$r, $c, '', $formats[$col_cfg['fillBlank']]);
280                 }
281                 continue;
282             }
283             
284             if (empty($cl[$col_cfg['dataIndex']])) {
285                 continue;
286             }
287             if (isset($col_cfg['txtrenderer'])) {
288                 $v = call_user_func($col_cfg['txtrenderer'], 
289                         $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
290                 if ($v === false) {
291                     continue;
292                 }
293               //  var_dump($v);
294             }
295             if (isset($col_cfg['renderer'])) {
296                 $hasRender = true;
297                 continue;
298             }
299             
300             $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
301             
302             $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
303             
304             $worksheet->write($start_row+$r, $c, $v, $format);
305         }
306         $this->start_row++;
307         
308         
309     }
310     
311     
312     
313     
314     function send($fn)
315     {
316         require_once 'File/Convert.php';
317         $fc=  new File_Convert($this->outfile2, "application/vnd.ms-excel");
318         $fn = $fc->convert("application/vnd.ms-excel"); 
319         $fc->serve('attachment',$fn); // can fix IE Mess
320     }
321      
322     
323 }