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