083d140bd1ab5276970621ec48948cfbde1ad37d
[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          
82          
83         
84         
85         $workbook->close();
86         $this->outfile2 = $outfile2;
87          
88     }
89     
90     
91     static function date($str)
92     {
93         
94         return (strtotime($str . ' UTC') +  (86400 *  25569)) / 86400;
95         
96     }
97     
98     
99     function buildpage($workbook,  $formats , $data,$cfg)
100     {
101         //echo '<PRE>';        print_R($cfg);
102       //  print_r($cfg);exit;
103         // Creating a worksheet
104         $worksheet =  $workbook->addWorksheet($cfg['workbook']);
105         if (is_a($worksheet, 'PEAR_Error')) {
106             die($worksheet->toString());
107         }
108         //print_R($worksheet);
109         $worksheet->setInputEncoding('UTF-8'); 
110          
111          
112          
113         $start_row = 0;
114         
115         if (!empty($cfg['head'])) {
116             foreach($cfg['head'] as $row) { 
117                 foreach($row as $c => $col) {
118                     if (is_array($col)) {
119                         $format = isset($formats[$col[1]] ) ? $formats[$col[1]] : false;
120                         $worksheet->write($start_row, $c, $col[0], $format);
121                         continue;
122                     }
123                     
124                     $worksheet->write($start_row, $c, $col);
125                     
126                 }
127                 $start_row++;
128             }
129             // add a spacer..
130             $start_row++;
131         }
132             
133             
134             
135          
136         foreach($cfg['cols'] as $c=>$col_cfg) {
137             if (is_string($col_cfg)) {
138                 $cfg['cols'][$c] = array(
139                     'header' => $col_cfg,
140                     'dataIndex' => $col_cfg,
141                     'width' => 50,
142                     
143                 );
144             }
145         }
146          
147          
148         foreach($cfg['cols'] as $c=>$col_cfg) {
149             print_r($col_cfg);
150             print_r('<br/>');
151             print_r($start_row);
152             print_r('<br/>');
153             print_r($c);
154             print_r('<br/>');
155             print_r($col_cfg['header']);
156             print_r('<br/>');
157             $worksheet->write($start_row, $c, $col_cfg['header']);
158             $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
159              
160         }
161         $start_row++;
162         $hasRender  = false;
163            //     DB_DataObject::debugLevel(1);
164         foreach($data as $r=>$clo) {
165             $cl = $clo;
166             if (is_object($clo)) {
167                 $cl = (array)$clo; // lossless converstion..
168             }
169             
170             if (isset($cfg['row_height'])) {
171                 $worksheet->setRow($start_row +$r, $cfg['row_height']);
172             }
173             
174             foreach($cfg['cols']  as $c=>$col_cfg) {
175                 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
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                 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
194                 
195           //    echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
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 }