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