1 | <?php |
||
26 | class SpreadsheetPrinter extends FileExportPrinter { |
||
27 | |||
28 | const HEADER_ROW_OFFSET = 1; |
||
29 | |||
30 | protected $fileFormats = [ |
||
31 | 'xlsx' => [ |
||
32 | 'writer' => 'Xlsx', |
||
33 | 'mimetype' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
||
34 | 'extension' => '.xlsx', |
||
35 | ], |
||
36 | 'xls' => [ |
||
37 | 'writer' => 'Xls', |
||
38 | 'mimetype' => 'aapplication/vnd.ms-excel', |
||
39 | 'extension' => '.xls', |
||
40 | ], |
||
41 | 'ods' => [ |
||
42 | 'writer' => 'Ods', |
||
43 | 'mimetype' => 'application/vnd.oasis.opendocument.spreadsheet', |
||
44 | 'extension' => '.ods', |
||
45 | ], |
||
46 | 'csv' => [ |
||
47 | 'writer' => 'Csv', |
||
48 | 'mimetype' => 'text/csv', |
||
49 | 'extension' => '.csv', |
||
50 | ], |
||
51 | ]; |
||
52 | |||
53 | protected $styled = false; |
||
54 | protected $fileFormat; |
||
55 | |||
56 | /** |
||
57 | * @see ExportPrinter::getMimeType() |
||
58 | * |
||
59 | * @since 1.8 |
||
60 | * |
||
61 | * @param SMWQueryResult $queryResult |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function getMimeType( SMWQueryResult $queryResult ) { |
||
68 | |||
69 | /** |
||
70 | * @see ExportPrinter::getFileName |
||
71 | * |
||
72 | * @param SMWQueryResult $queryResult |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | public function getFileName( SMWQueryResult $queryResult ) { |
||
79 | |||
80 | /** |
||
81 | * @see ExportPrinter::outputAsFile |
||
82 | * |
||
83 | * @param SMWQueryResult $queryResult |
||
84 | * @param array $params |
||
85 | */ |
||
86 | public function outputAsFile( SMWQueryResult $queryResult, array $params ) { |
||
96 | |||
97 | /** |
||
98 | * @param $definitions \ParamProcessor\ParamDefinition[] |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | public function getParamDefinitions( array $definitions ) { |
||
131 | |||
132 | /* |
||
133 | * Turns the PhpSpreadsheet document object into a string |
||
134 | */ |
||
135 | |||
136 | /** |
||
137 | * Return serialised results in specified format. |
||
138 | * |
||
139 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
140 | * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception |
||
141 | */ |
||
142 | 1 | protected function getResultText( SMWQueryResult $queryResult, $outputMode ) { |
|
151 | |||
152 | /** |
||
153 | * @param SMWQueryResult $queryResult |
||
154 | * |
||
155 | * @return string |
||
156 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
157 | * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception |
||
158 | */ |
||
159 | protected function getResultFileContents( SMWQueryResult $queryResult ) { |
||
175 | |||
176 | /** |
||
177 | * Creates a new PhpSpreadsheet document and returns it |
||
178 | * |
||
179 | * @return Spreadsheet |
||
180 | * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception |
||
181 | */ |
||
182 | protected function createSpreadsheet() { |
||
203 | |||
204 | /** |
||
205 | * @param $fileTitle |
||
206 | * |
||
207 | * @return Spreadsheet |
||
208 | * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception |
||
209 | */ |
||
210 | private function createSpreadsheetFromTemplate( $fileTitle ) { |
||
221 | |||
222 | /** |
||
223 | * @param SMWQueryResult $queryResult |
||
224 | * @param $worksheet |
||
225 | * |
||
226 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
227 | */ |
||
228 | protected function populateWorksheet( Worksheet $worksheet, SMWQueryResult $queryResult ) { |
||
245 | |||
246 | /** |
||
247 | * Populates the PhpSpreadsheet sheet with the headers from the result query |
||
248 | * |
||
249 | * @param Row $row |
||
250 | * @param SMWQueryResult $queryResult The query result |
||
251 | * |
||
252 | * @return Row |
||
253 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
254 | */ |
||
255 | protected function populateHeaderRow( Row $row, SMWQueryResult $queryResult ) { |
||
276 | |||
277 | /** |
||
278 | * Populates the PhpSpreadsheet document with the query data |
||
279 | * |
||
280 | * @param Row $row |
||
281 | * @param $resultRow |
||
282 | * |
||
283 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
284 | */ |
||
285 | protected function populateRow( Row $row, $resultRow ) { |
||
295 | |||
296 | /** |
||
297 | * @param Cell $cell |
||
298 | * @param SMWResultArray $field |
||
299 | * |
||
300 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
301 | */ |
||
302 | protected function populateCell( Cell $cell, SMWResultArray $field ) { |
||
328 | |||
329 | /** |
||
330 | * Checks the type of the value, and set's it in the sheet accordingly |
||
331 | * |
||
332 | * @param Cell $cell |
||
333 | * @param SMWDataValue $value |
||
334 | * |
||
335 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
336 | */ |
||
337 | protected function populateCellAccordingToType( Cell $cell, SMWDataValue $value ) { |
||
359 | |||
360 | /** |
||
361 | * Sets a quantity value at the given col,row location |
||
362 | * |
||
363 | * @param Cell $cell |
||
364 | * @param \SMWQuantityValue $value SMWDataValue the raw data value object |
||
365 | * |
||
366 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
367 | */ |
||
368 | protected function setQuantityDataValue( Cell $cell, \SMWQuantityValue $value ) { |
||
382 | |||
383 | /** |
||
384 | * Sets a numeric value at the given col,row location |
||
385 | * |
||
386 | * @param Cell $cell |
||
387 | * @param \SMWNumberValue $value SMWDataValue the raw data value object |
||
388 | * |
||
389 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
390 | */ |
||
391 | protected function setNumberDataValue( Cell $cell, \SMWNumberValue $value ) { |
||
398 | |||
399 | /** |
||
400 | * Sets a date/time value at the given col,row location |
||
401 | * |
||
402 | * @param Cell $cell |
||
403 | * @param \SMWTimeValue $value the raw data value object |
||
404 | * |
||
405 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
406 | */ |
||
407 | protected function setTimeDataValue( Cell $cell, \SMWTimeValue $value ) { |
||
420 | |||
421 | /** |
||
422 | * Sets or appends a string value at the given col,row location |
||
423 | * |
||
424 | * If there already exists a value at a given col,row location, then |
||
425 | * convert the cell to a string and append the data value. Creating |
||
426 | * a list of comma separated entries. |
||
427 | * |
||
428 | * @param Cell $cell |
||
429 | * @param $value SMWDataValue the raw data value object |
||
430 | * |
||
431 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
432 | */ |
||
433 | protected function setStringDataValue( Cell $cell, SMWDataValue $value ) { |
||
442 | |||
443 | /** |
||
444 | * @param Spreadsheet $spreadsheet |
||
445 | * |
||
446 | * @return string |
||
447 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
448 | */ |
||
449 | protected function getStringFromSpreadsheet( Spreadsheet $spreadsheet ) { |
||
457 | |||
458 | } |
||
459 | |||
460 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: