DataObjects/core.sql
[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            //     DB_DataObject::debugLevel(1);
104         foreach($data as $r=>$cl) {
105             
106             if (isset($cfg['row_height'])) {
107                 $worksheet->setRow($start_row +$r, $cfg['row_height']);
108                }
109             
110             foreach($cfg['cols']  as $c=>$col_cfg) {
111                 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
112                 if (empty($cl[$col_cfg['dataIndex']])) {
113                     continue;
114                 }
115                 if (isset($col_cfg['txtrenderer'])) {
116                     $v = call_user_func($col_cfg['txtrenderer'], 
117                             $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
118                     if ($v === false) {
119                         continue;
120                     }
121                   //  var_dump($v);
122                 }
123                 if (isset($col_cfg['renderer'])) {
124                     continue;
125                 }
126                 
127                 $v = @iconv('UTF-8', 'UTF-8//IGNORE', $v);
128                 $format = isset($col_cfg['format']) ? $formats[$col_cfg['format']] : false;
129                 
130           //    echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c,$v), true));
131                 $worksheet->write($start_row+$r, $c, $v, $format);
132             }
133         }
134         
135         foreach($data as $r=>$cl) {
136         
137             foreach($cfg['cols']   as $c=>$col_cfg) {
138                 $v = isset($cl[$col_cfg['dataIndex']]) ? $cl[$col_cfg['dataIndex']] : '';
139                 if (empty($cl[$col_cfg['dataIndex']])) {
140                     continue;
141                 }
142                 if (isset($col_cfg['renderer'])) {
143                     call_user_func($col_cfg['renderer'], $cl[$col_cfg['dataIndex']], $worksheet, $r+1, $c, $cl);
144                     
145                 }
146               //  echo "<PRE>WRITE: ". htmlspecialchars(print_r(array($r+1, $c, $cl[$col_cfg['dataIndex']]), true));
147          
148             }
149         }
150            $workbook->close();
151         $this->outfile2 = $outfile2;
152          
153     }
154     
155     function send($fn)
156     {
157         
158      
159        
160         require_once 'File/Convert.php';
161         $fc=  new File_Convert($this->outfile2, "application/vnd.ms-excel");
162         $fn = $fc->convert("application/vnd.ms-excel"); 
163         $fc->serve('attachment',$fn); // can fix IE Mess
164     }
165      
166     
167 }