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         ],
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         $pman = new Pman();
52         // Creating a workbook
53         $outfile2 = $pman->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     function buildpage($workbook,  $formats , $data,$cfg)
91     {
92         echo '<PRE>';
93         print_R($cfg);
94         
95         // Creating a worksheet
96         $worksheet =  $workbook->addWorksheet($cfg['workbook']);
97         print_R($worksheet);
98         $worksheet->setInputEncoding('UTF-8'); 
99          
100          
101          
102         $start_row = 0;
103         
104         if (!empty($cfg['head'])) {
105             foreach($cfg['head'] as $row) { 
106                 foreach($row as $c => $col) {
107                     $worksheet->write($start_row, $c, $col);
108                     
109                 }
110                 $start_row++;
111             }
112             // add a spacer..
113             $start_row++;
114         }
115             
116             
117             
118          
119         foreach($cfg['cols'] as $c=>$col_cfg) {
120             if (is_string($col_cfg)) {
121                 $cfg['cols'][$c] = array(
122                     'header' => $col_cfg,
123                     'dataIndex' => $col_cfg,
124                     'width' => 50,
125                     
126                 );
127             }
128         }
129          
130          
131         foreach($cfg['cols'] as $c=>$col_cfg) {
132             
133             
134             
135             $worksheet->write($start_row, $c, $col_cfg['header']);
136             $worksheet->setColumn ( $c, $c, $col_cfg['width'] / 5);
137              
138         }
139         $start_row++;
140         $hasRender  = false;
141            //     DB_DataObject::debugLevel(1);
142         foreach($data as $r=>$clo) {
143             $cl = $clo;
144             if (is_object($clo)) {
145                 $cl = (array)$clo; // lossless converstion..
146             }
147             
148             if (isset($cfg['row_height'])) {
149                 $worksheet->setRow($start_row +$r, $cfg['row_height']);
150             }
151             
152             foreach($cfg['cols']  as $c=>$col_cfg) {
153                 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
154                 if (empty($cl[$col_cfg['dataIndex']])) {
155                     continue;
156                 }
157                 if (isset($col_cfg['txtrenderer'])) {
158                     $v = call_user_func($col_cfg['txtrenderer'], 
159                             $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $clo);
160                     if ($v === false) {
161                         continue;
162                     }
163                   //  var_dump($v);
164                 }
165                 if (isset($col_cfg['renderer'])) {
166                     $hasRender = true;
167                     continue;
168                 }
169                 
170                 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
171                 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
172                 
173           //    echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
174                 $worksheet->write($start_row+$r, $c, $v, $format);
175             }
176         }
177         /// call user render on any that are defined..
178         if ($hasRender) {
179             foreach($data as $r=>$cl) {
180             
181                 foreach($cfg['cols']   as $c=>$col_cfg) {
182                     $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
183                     if (empty($cl[$col_cfg['dataIndex']])) {
184                         continue;
185                     }
186                     if (isset($col_cfg['renderer'])) {
187                         call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
188                         
189                     }
190                   //  echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
191              
192                 }
193             }
194         }
195         $start_row += count($data);
196         
197         if (!empty($cfg['foot'])) {
198             foreach($cfg['foot'] as $row) { 
199                 foreach($row as $c => $col) {
200                     // if it's an array? - formated ???
201                     $worksheet->write($start_row, $c, $col);
202                     
203                 }
204                 $start_row++;
205             }
206             // add a spacer..
207             $start_row++;
208         }
209             
210         
211         
212         
213         
214     }
215     
216     
217     
218     function send($fn)
219     {
220         
221      
222        
223         require_once 'File/Convert.php';
224         $fc=  new File_Convert($this->outfile2, "application/vnd.ms-excel");
225         $fn = $fc->convert("application/vnd.ms-excel"); 
226         $fc->serve('attachment',$fn); // can fix IE Mess
227     }
228      
229     
230 }