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