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