1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
<?php
/**
* @version $Header:$
*
* Copyright (c) 2006 bitweaver.org
* All Rights Reserved. See below for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
*
* @package bitexcel
*/
/**
* required setup
*/
$php_excel_loaded = FALSE;
if (@include_once("PEAR.php")) {
ini_set('include_path', ini_get('include_path').':/usr/share/pear/PHPExcel/');
if( @include_once("PHPExcel.php") )
if( @include_once("PHPExcel/Writer/Excel2007.php") )
$php_excel_loaded = TRUE;
}
if( !$php_excel_loaded ){
require_once( EXTERNALS_PKG_PATH.'phpexcel/Classes/PHPExcel.php');
require_once( EXTERNALS_PKG_PATH.'phpexcel/Classes/PHPExcel/Writer/Excel2007.php');
}
require_once( UTIL_PKG_INCLUDE_PATH.'phpcontrib_lib.php' );
require_once( LIBERTY_PKG_PATH . 'LibertyValidator.php' );
define( 'EXCEL_TEMP_PATH', TEMP_PKG_PATH.'excel/' );
define( 'EXCEL_TEMP_URL', TEMP_PKG_URL.'excel/' );
/**
* @package bitexcel
*/
class BitExcel extends BitBase{
private $mPHPExcel;
private $mConfig;
public function __construct( $pConfig=array() ){
// set default config values
$this->mConfig = array(
'cache_method' => 'cache_in_memory',
'auto_name_doc' => TRUE,
'auto_sanitize_doc_name' => TRUE,
);
// override default config
if( !empty( $pConfig ) ){
extract_to( $pConfig, $this->mConfig, EXTR_IF_EXISTS );
}
}
private function configExcel( $pParamHash ){
// config PHPExcel
// cache method
switch( $this->getConfig( 'cache_method' ) ){
case 'cache_to_discISAM':
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
break;
case 'cache_in_memory_serialized':
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;
break;
case 'cache_in_memory':
default:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory;
break;
}
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
// create excel object
if( !isset( $this->mPHPExcel ) ){
$this->mPHPExcel = new PHPExcel();
}
// set doc properties
$this->mPHPExcel->getProperties()->setTitle( $pParamHash['workbook']['title'] );
// $this->mPHPExcel->getProperties()->setCreator("Maarten Balliauw");
// $this->mPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
// $this->mPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
// $this->mPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
}
/**
* write Excel 2007 file
*/
public function writeWorkbook( $pParamHash ){
if( $this->verifyWorkbook( $pParamHash ) ){
// init the excel object
$this->configExcel( $pParamHash );
if( !is_dir( EXCEL_TEMP_PATH ) ){
mkdir_p( EXCEL_TEMP_PATH );
}
/*
$pParamHash['workbook'] = array(
'title' => // document title used inside the document
'doc_name' => // document doc name
'worksheets' => array(
array( // worksheet
'title' => 'foo', // worksheet title
'rows' => array(
array( // row
<col> => <val>,
<col> => array( <val>, <val>, <val> ),
<col> => array( <val>, array() ),
),
array( ... ),
array( ... ),
)
)
),
array( ... ) // worksheet
)
)
*/
// prep doc name
// auto gen doc name
if( $this->getConfig( 'auto_name_doc' ) ){
$docName = "somedocName";
// defined doc name
}else{
// get doc name from
$docName = $pParamHash['workbook']['doc_name'];
// sanitize name
if( $this->getConfig( 'auto_sanitize_doc_name' ) ){
// replace white spaces
$docName = str_replace( ' ', '-', $docName );
// strip nasty chars
$docName = preg_replace( '/[&$\?\*\%:\/\\\]/', '', $docName );
}
}
// validate filename - we validate in all cases to be certain
// a little hack so we can use validator
$dataHash = array();
$array1 = array( 'filename' => array( 'excel_file_name' => array(), ), );
$array2 = array( 'excel_file_name' => $docName );
LibertyValidator::validate(
$array1,
$array2,
$this,
$dataHash
);
// construct the workbook
if( count( $this->getErrors() ) == 0 ){
foreach( $pParamHash['workbook']['worksheets'] as $index=>$worksheetData ){
// first sheet is automatically generated, for all others create it
if( $index > 0 ){
// create sheet
$index = $this->mPHPExcel->getIndex( $this->mPHPExcel->createSheet() );
}
$this->initWorksheet( $index, $worksheetData );
$this->updateWorksheet( $index, $worksheetData );
}
}
// no errors then write the doc
if( count( $this->getErrors() ) == 0 ){
$fullDocName = $docName.'.xlsx';
$xlsxFile = EXCEL_TEMP_PATH.$fullDocName;
// echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = new PHPExcel_Writer_Excel2007( $this->mPHPExcel );
$objWriter->save( $xlsxFile );
// return the doc reference
return $xlsxFile;
}
// default fail
return FALSE;
}
}
/**
* updateWorkbook
* adds data to an existing workbook
* @param pFileName - name of the workbook (without the extension)
* @param pParamHash['title'] - name of the worksheet to insert data into
* @param pParamHash['rows'] - data to insert
* @return bool
*/
public function updateWorkbook( $pFileName, $pParamHash ){
$xlsxFile = EXCEL_TEMP_PATH.$pFileName.".xlsx";
if( !empty( $pParamHash['title'] ) && !empty( $pParamHash['rows'] ) ){
if( file_exists( $xlsxFile ) ){
require_once( EXTERNALS_PKG_PATH.'phpexcel/Classes/PHPExcel/IOFactory.php' );
if( $this->mPHPExcel = PHPExcel_IOFactory::load( $xlsxFile ) ){
// @TODO support adding any new sheet - for now we assume top level data has an existing sheet
$index = $this->mPHPExcel->getIndex( $this->mPHPExcel->getSheetByName( $pParmaHash['title'] ) );
$this->updateWorksheet( $index, $pParamHash );
// return file path
return $xlsxFile;
}else{
$this->setError( 'workbook', 'The workbook document failed to load' );
}
}else{
$this->setError( 'workbook', 'The workbook document could not be found' );
}
}else{
$this->setError( 'worksheet', 'No title or rows data provided' );
}
return FALSE;
}
public function saveWorkbook(){
$xlsxFile = EXCEL_TEMP_PATH.$docName.'.xlsx';
// echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = new PHPExcel_Writer_Excel2007( $this->mPHPExcel );
$objWriter->save( $xlsxFile );
// return the doc reference
return $xlsxFile;
}
private function verifyWorkbook( $pParamHash ){
if( empty( $pParamHash['workbook'] ) ){
$this->setError( 'workbook', 'No workbook data provided' );
}
if( empty( $pParamHash['workbook']['title'] ) ){
$this->setError( 'workbook', 'No workbook title provided' );
}
if( !$this->getConfig('auto_name_doc') && empty( $pParamHash['workbook']['doc_name'] ) ){
$this->setError( 'workbook', 'No document name provided' );
}
// @TODO verify each worksheet has a title and rows
return ( count($this->getErrors() ) == 0);
}
private function initWorksheet( $pIndex, $pParamHash ){
$this->mPHPExcel->setActiveSheetIndex($pIndex);
// create title
// sanitize sheet titles
$sheetTitle = preg_replace( '/[\[\]\?\*:\/\\\]/', '', $pParamHash['title'] );
$this->mPHPExcel->getActiveSheet()->setTitle( $sheetTitle );
// create column headers from first row of data
$headings = array_keys( ( is_array( current( $pParamHash['rows'] ) )?current($pParamHash['rows']):$pParamHash['rows'] ) );
// naturally in excel rows start at 1
$row = 1;
$this->insertWorksheetRow( $pIndex, $headings, $row );
}
private function updateWorksheet( $pIndex, $pParamHash ){
$this->mPHPExcel->setActiveSheetIndex($pIndex);
// get next available row
$row = $this->mPHPExcel->getActiveSheet()->getHighestRow();;
$row++;
// fill in rows
foreach( $pParamHash['rows'] as $rowData ){
// reset the col and val
$col = 0;
$val = NULL;
// write row
$this->insertWorksheetRow( $pIndex, $rowData, $row );
$row++;
}
}
/**
* inserts a row into the active worksheet
* be sure you have set the active worksheet to the one you
* want to insert into
*/
private function insertWorksheetRow( $pIndex, $pParamHash, $pRow=NULL ){
$this->mPHPExcel->setActiveSheetIndex($pIndex);
$row = $pRow;
// undefined row get row autoamtically
if( is_null( $pRow ) ){
if( $row = $this->mPHPExcel->getActiveSheet()->getHighestRow() ){
$row++;
}else{
// default first row
$row = 1;
}
}
// insert the data hash
// key of hash can not always be trusted to be numeric so manage col value
$col = 0;
foreach( $pParamHash as $key=>$val ){
// string insert into cel
if( is_numeric( $val ) || is_string( $val ) || $val == '' ){
$this->mPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $val);
// array add to worksheet
}elseif( is_array( $val ) ){
$hasArrays = array_map( 'is_array', $val );
$isNumeric = array_map( 'is_numeric', array_keys( $val ) );
// array values contain arrays or the keys are strings
if( in_array( TRUE, $hasArrays ) || in_array( FALSE, $isNumeric ) ){
$worksheetData = array( 'title'=>$key,'rows'=>$val );
// create a worksheet for these values if it doesnt already exist
$sheets = $this->mPHPExcel->getSheetNames();
// if keys are numeric proceed with filling out sheet
if( !in_array( $key, $sheets ) ){
$index = $this->mPHPExcel->getIndex( $this->mPHPExcel->createSheet() );
$this->initWorksheet( $index, $worksheetData );
// get the existing worksheet;
}else{
$index = $this->mPHPExcel->getIndex( $this->mPHPExcel->getSheetByName($key) );
}
// entried nested in an array we update
if( in_array( TRUE, $hasArrays ) ) {
$this->mPHPExcel->setActiveSheetIndex($index);
$remoteRow1 = $this->mPHPExcel->getActiveSheet()->getHighestRow() + 1;
$this->updateWorksheet( $index, $worksheetData );
$remoteRow = $this->mPHPExcel->getActiveSheet()->getHighestRow(); // last row inserted
$remoteRowLink = "A".$remoteRow;
if( $remoteRow1 != $remoteRow ){
$remoteRowLink = "A".$remoteRow1.':A'.$remoteRow;
$remoteRow = $remoteRow1.':'.$remoteRow;
}
// single entry we insert
}else{
$this->insertWorksheetRow( $index, $val );
$remoteRow = $this->mPHPExcel->getActiveSheet()->getHighestRow(); // last row inserted
$remoteRowLink = "A".$remoteRow;
}
// the active sheet was switched so switch back
$this->mPHPExcel->setActiveSheetIndex($pIndex);
$this->mPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, tra('See sheet:').$key.' '.tra('row').$remoteRow );
// create a link to the related sheet
$sheetLink = "sheet://'".$key."'!".$remoteRowLink;
$this->mPHPExcel->getActiveSheet()->getCellByColumnAndRow($col, $row)->getHyperlink()->setUrl($sheetLink);
$this->mPHPExcel->getActiveSheet()->getCellByColumnAndRow($col, $row)->getHyperlink()->setTooltip(tra('Click for details'));
}else{
// array does not contain arrays and keys are numeric
$val2 = implode( ',', $val );
$this->mPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $val2);
}
}
$col++;
}
}
private function getConfig( $pKey ){
if( isset( $this->mConfig[$pKey] ) ){
return $this->mConfig[$pKey];
}
return NULL;
}
}
|