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             ),
30         
31         // if this is set then it will add a tab foreach one.
32         workbooks = array(
33             workbook ->
34             
35             
36             
37  */
38  
39  
40  
41
42
43 class Pman_Core_SimpleExcel extends Pman
44 {
45     
46     
47     
48     function Pman_Core_SimpleExcel($data,$cfg)
49     {
50       // print_r($cfg);exit;
51         require_once 'Spreadsheet/Excel/Writer.php';
52         // Creating a workbook
53         $outfile2 = $this->tempName('xls');
54        // var_dump($outfile2);
55         $workbook = new Spreadsheet_Excel_Writer($outfile2);
56         //$workbook = new Spreadsheet_Excel_Writer();
57         $workbook->setVersion(8);
58         // sending HTTP headers
59         
60         $formats = array();
61        
62         $cfg['formats'] = isset($cfg['formats']) ? $cfg['formats'] : array();
63         foreach($cfg['formats'] as $f=>$fcfg) {
64             $formats[$f] = & $workbook->addFormat();
65             foreach($fcfg as $k=>$v) {
66                 $formats[$f]->{'set' . $k}($v);
67             }
68              
69         }
70          
71
72         if (empty($cfg['workbooks'])) {
73             $this->buildpage( $workbook,  $formats , $data,$cfg);
74         } else {
75             foreach($cfg['workbooks'] as $i =>$wcfg) {
76                 $this->buildpage( $workbook,  $formats , $data[$i],$wcfg);
77             }
78             
79         }
80         
81         $workbook->close();
82         $this->outfile2 = $outfile2;
83          
84     }
85     
86     
87     static function date($str)
88     {
89         
90         return (strtotime($str . ' UTC') +  (86400 *  25569)) / 86400;
91         
92     }
93     
94     
95     function buildpage($workbook,  $formats , $data,$cfg)
96     {
97         //echo '<PRE>';        print_R($cfg);
98       //  print_r($cfg);exit;
99         // Creating a worksheet
100         $worksheet =  $workbook->addWorksheet($cfg['workbook']);
101         if (is_a($worksheet, 'PEAR_Error')) {
102             die($worksheet->toString());
103         }
104         //print_R($worksheet);
105         $worksheet->setInputEncoding('UTF-8'); 
106          
107          
108          
109         $start_row = 0;
110         
111         if (!empty($cfg['head'])) {
112             foreach($cfg['head'] as $row) { 
113                 foreach($row as $c => $col) {
114                     if (is_array($col)) {
115                         $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
116                         $worksheet->write($start_row, $c, $col[0], $format);
117                         continue;
118                     }
119                     
120                     $worksheet->write($start_row, $c, $col);
121                     
122                 }
123                 $start_row++;
124             }
125             // add a spacer..
126             $start_row++;
127         }
128             
129             
130             
131          
132         foreach($cfg['cols'] as $c=>$col_cfg) {
133             if (is_string($col_cfg)) {
134                 $cfg['cols'][$c] = array(
135                     'header' => $col_cfg,
136                     'dataIndex' => $col_cfg,
137                     'width' => 50,
138                     
139                 );
140             }
141         }
142          
143          
144         foreach($cfg['cols'] as $c=>$col_cfg) {
145             $format = isset($col_cfg['color']) ? $formats[$col_cfg['color']] : false;
146             $worksheet->write($start_row, $c, $col_cfg['header'],$format);
147             $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
148              
149         }
150         $start_row++;
151         $hasRender  = false;
152            //     DB_DataObject::debugLevel(1);
153         foreach($data as $r=>$clo) {
154             $cl = $clo;
155             if (is_object($clo)) {
156                 $cl = (array)$clo; // lossless converstion..
157             }
158             
159             if (isset($cfg['row_height'])) {
160                 $worksheet->setRow($start_row +$r, $cfg['row_height']);
161             }
162             
163             foreach($cfg['cols']  as $c=>$col_cfg) {
164                 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
165                 if (empty($cl[$col_cfg['dataIndex']])) {
166                     continue;
167                 }
168                 if (isset($col_cfg['txtrenderer'])) {
169                     $v = call_user_func($col_cfg['txtrenderer'], 
170                             $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
171                     if ($v === false) {
172                         continue;
173                     }
174                   //  var_dump($v);
175                 }
176                 if (isset($col_cfg['renderer'])) {
177                     $hasRender = true;
178                     continue;
179                 }
180                 
181                 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
182                 
183                 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
184                 
185                 $worksheet->write($start_row+$r, $c, $v, $format);
186             }
187         }
188         /// call user render on any that are defined..
189         if ($hasRender) {
190             foreach($data as $r=>$cl) {
191             
192                 foreach($cfg['cols']   as $c=>$col_cfg) {
193                     $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
194                     if (empty($cl[$col_cfg['dataIndex']])) {
195                         continue;
196                     }
197                     if (isset($col_cfg['renderer'])) {
198                         call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
199                         
200                     }
201                   //  echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
202              
203                 }
204             }
205         }
206         $start_row += count($data);
207         
208         if (!empty($cfg['foot'])) {
209             foreach($cfg['foot'] as $row) { 
210                 foreach($row as $c => $col) {
211                     // if it's an array? - formated ???
212                     if (is_array($col)) {
213                         $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
214                         $worksheet->write($start_row, $c, $col[0], $format);
215                         continue;
216                     }
217                     $worksheet->write($start_row, $c, $col);
218                     
219                 }
220                 $start_row++;
221             }
222             // add a spacer..
223             $start_row++;
224         }
225             
226         
227         
228         
229         
230     }
231     
232     function send($fn)
233     {
234         require_once 'File/Convert.php';
235         $fc=  new File_Convert($this->outfile2, "application/vnd.ms-excel");
236         $fn = $fc->convert("application/vnd.ms-excel"); 
237         $fc->serve('attachment',$fn); // can fix IE Mess
238     }
239      
240     
241 }