1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Fi\CoreBundle\Utils\GrigliaFiltriUtils; |
6
|
|
|
use TCPDF; |
7
|
|
|
|
8
|
|
|
class StampatabellaController extends FiCoreController |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public function __construct($container = null) |
12
|
|
|
{ |
13
|
|
|
if ($container) { |
14
|
|
|
$this->setContainer($container); |
15
|
|
|
} |
16
|
|
|
} |
17
|
|
|
|
18
|
2 |
|
public function stampa($parametri = array()) |
19
|
|
|
{ |
20
|
2 |
|
$testata = $parametri['testata']; |
|
|
|
|
21
|
2 |
|
$request = $parametri['request']; |
|
|
|
|
22
|
2 |
|
$nometabella = $request->get('nometabella'); |
23
|
|
|
|
24
|
2 |
|
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); |
25
|
|
|
|
26
|
|
|
//echo PDF_HEADER_LOGO; |
27
|
2 |
|
$pdftitle = isset($testata['titolo']) && ($testata['titolo'] != '') ? $testata['titolo'] : 'Elenco ' . $nometabella; |
28
|
2 |
|
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'FiFree2', $pdftitle, array(0, 0, 0), array(0, 0, 0)); |
29
|
2 |
|
$pdf->setFooterData(array(0, 0, 0), array(0, 0, 0)); |
30
|
|
|
|
31
|
2 |
|
$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); |
32
|
2 |
|
$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); |
33
|
|
|
|
34
|
2 |
|
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); |
35
|
2 |
|
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); |
36
|
2 |
|
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
37
|
2 |
|
$pdf->SetFillColor(220, 220, 220); |
38
|
|
|
|
39
|
2 |
|
$pdf->AddPage('L'); |
40
|
|
|
$arraystampaparm = array( |
41
|
2 |
|
"larghezzaform" => 900, |
|
|
|
|
42
|
2 |
|
"h" => 6, |
|
|
|
|
43
|
2 |
|
"border" => 1, |
|
|
|
|
44
|
2 |
|
"align" => 'L', |
|
|
|
|
45
|
2 |
|
"fill" => 0, |
|
|
|
|
46
|
|
|
"ln" => 0 |
|
|
|
|
47
|
2 |
|
); |
48
|
2 |
|
$parametristampa = array_merge($parametri, $arraystampaparm); |
49
|
|
|
|
50
|
2 |
|
$this->stampaTestata($pdf, $parametristampa); |
51
|
|
|
|
52
|
2 |
|
$this->stampaDettaglio($pdf, $parametristampa); |
53
|
|
|
|
54
|
|
|
/* |
55
|
|
|
I: send the file inline to the browser (default). The plug-in is used if available. |
56
|
|
|
The name given by name is used when one selects the “Save as” option on the link generating the PDF. |
57
|
|
|
D: send to the browser and force a file download with the name given by name. |
58
|
|
|
F: save to a local server file with the name given by name. |
59
|
|
|
S: return the document as a string (name is ignored). |
60
|
|
|
FI: equivalent to F + I option |
61
|
|
|
FD: equivalent to F + D option |
62
|
|
|
E: return the document as base64 mime multi-part email attachment (RFC 2045) |
63
|
|
|
*/ |
64
|
|
|
|
65
|
|
|
/* In caso il pdf stampato nel browser resti fisso a caricare la pagina, |
66
|
|
|
impostare 'D' per forzare lo scarico del file, oppure |
67
|
|
|
mettere exit al posto di return 0; questo opzione però non è accettata da gli strumenti di controllo del codice che non si |
68
|
|
|
aspettano exit nel codice |
69
|
|
|
*/ |
70
|
2 |
|
$pdf->Output($request->get('nometabella') . '.pdf', 'I'); |
71
|
|
|
|
72
|
2 |
|
return 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function esportaexcel($parametri = array()) |
76
|
|
|
{ |
77
|
|
|
set_time_limit(960); |
78
|
|
|
ini_set('memory_limit', '2048M'); |
79
|
|
|
|
80
|
|
|
$cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp; |
|
|
|
|
81
|
|
|
$cacheSettings = array('memoryCacheSize' => '8MB'); |
82
|
|
|
\PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings); |
83
|
|
|
|
84
|
|
|
//Creare un nuovo file |
85
|
|
|
$objPHPExcel = new \PHPExcel(); |
86
|
|
|
|
87
|
|
|
$objPHPExcel->setActiveSheetIndex(0); |
88
|
|
|
|
89
|
|
|
// Set properties |
90
|
|
|
$objPHPExcel->getProperties()->setCreator('Comune di Firenze'); |
91
|
|
|
$objPHPExcel->getProperties()->setLastModifiedBy('Comune di Firenze'); |
92
|
|
|
|
93
|
|
|
$testata = $parametri['testata']; |
|
|
|
|
94
|
|
|
$rispostaj = $parametri['griglia']; |
95
|
|
|
|
96
|
|
|
$modellicolonne = $testata['modellocolonne']; |
97
|
|
|
|
98
|
|
|
//Scrittura su file |
99
|
|
|
$sheet = $objPHPExcel->getActiveSheet(); |
|
|
|
|
100
|
|
|
$titolosheet = 'Esportazione ' . $testata['tabella']; |
101
|
|
|
$sheet->setTitle(substr($titolosheet, 0, 30)); |
102
|
|
|
$sheet->getParent()->getDefaultStyle()->getFont()->setName('Verdana'); |
103
|
|
|
|
104
|
|
|
$this->printHeaderXls($modellicolonne, $testata, $sheet); |
105
|
|
|
|
106
|
|
|
$risposta = json_decode($rispostaj); |
107
|
|
|
if (isset($risposta->rows)) { |
108
|
|
|
$righe = $risposta->rows; |
109
|
|
|
} else { |
110
|
|
|
$righe = array(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->printBodyXls($righe, $modellicolonne, $sheet); |
114
|
|
|
|
115
|
|
|
//Si crea un oggetto |
116
|
|
|
$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); |
117
|
|
|
$todaydate = date('d-m-y'); |
118
|
|
|
|
119
|
|
|
$filename = 'Exportazione_' . $testata['tabella']; |
120
|
|
|
$filename = $filename . '-' . $todaydate . '-' . strtoupper(md5(uniqid(rand(), true))); |
121
|
|
|
$filename = $filename . '.xls'; |
122
|
|
|
$filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename; |
123
|
|
|
|
124
|
|
|
if (file_exists($filename)) { |
125
|
|
|
unlink($filename); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$objWriter->save($filename); |
129
|
|
|
|
130
|
|
|
return $filename; |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
private function printHeaderXls($modellicolonne, $testata, $sheet) |
134
|
|
|
{ |
135
|
2 |
|
$indicecolonnaheader = 0; |
136
|
2 |
|
$letteracolonna = 0; |
|
|
|
|
137
|
2 |
|
foreach ($modellicolonne as $modellocolonna) { |
138
|
|
|
//Si imposta la larghezza delle colonne |
139
|
2 |
|
$letteracolonna = \PHPExcel_Cell::stringFromColumnIndex($indicecolonnaheader); |
|
|
|
|
140
|
2 |
|
$width = (int) $modellocolonna['width'] / 7; |
|
|
|
|
141
|
2 |
|
$indicecolonnaheadertitle = $testata['nomicolonne'][$indicecolonnaheader]; |
142
|
2 |
|
$coltitlecalc = isset($indicecolonnaheadertitle) ? $indicecolonnaheadertitle : $modellocolonna['name']; |
|
|
|
|
143
|
2 |
|
$coltitle = strtoupper($coltitlecalc); |
|
|
|
|
144
|
2 |
|
$sheet->setCellValueByColumnAndRow($indicecolonnaheader, 1, $coltitle); |
145
|
2 |
|
$sheet->getColumnDimension($letteracolonna)->setWidth($width); |
146
|
|
|
|
147
|
2 |
|
++$indicecolonnaheader; |
148
|
2 |
|
} |
149
|
|
|
|
150
|
2 |
|
if ($indicecolonnaheader > 0) { |
151
|
|
|
//Si imposta il colore dello sfondo delle celle |
152
|
|
|
//Colore header |
153
|
|
|
$style_header = array( |
154
|
|
|
'fill' => array( |
155
|
2 |
|
'type' => \PHPExcel_Style_Fill::FILL_SOLID, |
156
|
2 |
|
'color' => array('rgb' => '0404B4'), |
157
|
2 |
|
), |
158
|
|
|
'font' => array( |
159
|
2 |
|
'bold' => true, |
160
|
2 |
|
'color' => array('rgb' => 'FFFFFF'), |
161
|
2 |
|
), |
162
|
2 |
|
); |
163
|
2 |
|
$sheet->getStyle('A1:' . $letteracolonna . '1')->applyFromArray($style_header); |
164
|
2 |
|
} |
165
|
|
|
|
166
|
2 |
|
$sheet->getRowDimension('1')->setRowHeight(20); |
167
|
2 |
|
} |
168
|
|
|
|
169
|
|
|
private function getValueCell($tipocampo, $vettorecella) |
170
|
|
|
{ |
171
|
|
|
$valore = null; |
172
|
|
|
switch ($tipocampo) { |
173
|
|
|
case 'date': |
174
|
|
|
$d = (int) substr($vettorecella, 0, 2); |
|
|
|
|
175
|
|
|
$m = (int) substr($vettorecella, 3, 2); |
|
|
|
|
176
|
|
|
$y = (int) substr($vettorecella, 6, 4); |
|
|
|
|
177
|
|
|
$t_date = \PHPExcel_Shared_Date::FormattedPHPToExcel($y, $m, $d); |
178
|
|
|
$valore = $t_date; |
179
|
|
|
break; |
180
|
|
|
case 'boolean': |
181
|
|
|
$valore = ($vettorecella == 1) ? 'SI' : 'NO'; |
182
|
|
|
break; |
183
|
|
|
default: |
184
|
|
|
$valore = $vettorecella; |
185
|
|
|
break; |
186
|
|
|
} |
187
|
|
|
return $valore; |
188
|
|
|
} |
189
|
|
|
|
190
|
2 |
|
private function printBodyXls($righe, $modellicolonne, $sheet) |
191
|
|
|
{ |
192
|
2 |
|
$row = 2; |
193
|
2 |
|
foreach ($righe as $riga) { |
194
|
2 |
|
$vettorecelle = $riga->cell; |
195
|
2 |
|
$col = 0; |
|
|
|
|
196
|
2 |
|
foreach ($vettorecelle as $vettorecella) { |
197
|
2 |
|
if ($vettorecella === '' || $vettorecella === null) { |
198
|
1 |
|
$col = $col + 1; |
|
|
|
|
199
|
1 |
|
continue; |
200
|
|
|
} |
201
|
2 |
|
$valore = $this->getValueCell($modellicolonne[$col]['tipocampo'], $vettorecella); |
202
|
2 |
|
$sheet->setCellValueByColumnAndRow($col, $row, $valore); |
203
|
2 |
|
$col = $col + 1; |
|
|
|
|
204
|
2 |
|
} |
205
|
2 |
|
$sheet->getRowDimension($row)->setRowHeight(18); |
206
|
2 |
|
++$row; |
207
|
2 |
|
} |
208
|
|
|
|
209
|
2 |
|
$indicecolonna = 0; |
210
|
2 |
|
foreach ($modellicolonne as $modellocolonna) { |
211
|
2 |
|
$letteracolonna = \PHPExcel_Cell::stringFromColumnIndex($indicecolonna); |
212
|
2 |
|
switch ($modellocolonna['tipocampo']) { |
213
|
2 |
|
case 'text': |
214
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
215
|
|
|
->getNumberFormat() |
216
|
|
|
->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL); |
217
|
|
|
break; |
218
|
2 |
|
case 'string': |
219
|
2 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
220
|
2 |
|
->getNumberFormat() |
221
|
2 |
|
->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL); |
222
|
2 |
|
break; |
223
|
2 |
|
case 'integer': |
224
|
2 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
225
|
2 |
|
->getNumberFormat() |
226
|
2 |
|
->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER); |
227
|
2 |
|
break; |
228
|
1 |
|
case 'float': |
229
|
1 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
230
|
1 |
|
->getNumberFormat() |
231
|
1 |
|
->setFormatCode('#,##0.00'); |
232
|
1 |
|
break; |
233
|
1 |
|
case 'number': |
234
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
235
|
|
|
->getNumberFormat() |
236
|
|
|
->setFormatCode('#,##0.00'); |
237
|
|
|
break; |
238
|
1 |
|
case 'datetime': |
239
|
|
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
240
|
|
|
->getNumberFormat() |
241
|
|
|
->setFormatCode('dd/mm/yyyy'); |
242
|
|
|
break; |
243
|
1 |
|
case 'date': |
244
|
1 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
245
|
1 |
|
->getNumberFormat() |
246
|
1 |
|
->setFormatCode('dd/mm/yyyy'); |
247
|
1 |
|
break; |
248
|
1 |
|
default: |
249
|
1 |
|
$sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
250
|
1 |
|
->getNumberFormat() |
251
|
1 |
|
->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL); |
252
|
1 |
|
break; |
253
|
2 |
|
} |
254
|
|
|
|
255
|
2 |
|
++$indicecolonna; |
256
|
2 |
|
} |
257
|
2 |
|
} |
258
|
|
|
|
259
|
2 |
|
private function stampaTestata($pdf, $parametri) |
260
|
|
|
{ |
261
|
2 |
|
$ln = $parametri['ln']; |
|
|
|
|
262
|
2 |
|
$fill = $parametri['fill']; |
|
|
|
|
263
|
2 |
|
$align = $parametri['align']; |
|
|
|
|
264
|
2 |
|
$border = $parametri['border']; |
|
|
|
|
265
|
2 |
|
$h = $parametri['h']; |
|
|
|
|
266
|
2 |
|
$larghezzaform = $parametri['larghezzaform']; |
|
|
|
|
267
|
2 |
|
$testata = $parametri['testata']; |
|
|
|
|
268
|
2 |
|
$nomicolonne = $testata['nomicolonne']; |
|
|
|
|
269
|
2 |
|
$modellicolonne = $testata['modellocolonne']; |
270
|
|
|
|
271
|
|
|
// Testata |
272
|
2 |
|
$pdf->SetFont('helvetica', 'B', 9); |
273
|
2 |
|
$arr_heights = array(); |
274
|
|
|
// store current object |
275
|
2 |
|
$pdf->startTransaction(); |
276
|
2 |
|
foreach ($nomicolonne as $posizione => $nomecolonna) { |
277
|
2 |
|
$width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform); |
278
|
|
|
// get the number of lines |
279
|
2 |
|
$arr_heights[] = $pdf->MultiCell($width, 0, $nomecolonna, $border, $align, $fill, 0, '', '', true, 0, false, true, 0); |
280
|
2 |
|
} |
281
|
|
|
// restore previous object |
282
|
2 |
|
$pdf->rollbackTransaction(true); |
283
|
|
|
//work out the number of lines required |
284
|
2 |
|
$rowcount = max($arr_heights); |
285
|
|
|
//now draw it |
286
|
2 |
|
foreach ($nomicolonne as $posizione => $nomecolonna) { |
287
|
2 |
|
$width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform); |
288
|
2 |
|
$pdf->MultiCell($width, $rowcount * $h, $nomecolonna, $border, $align, $fill, $ln); |
289
|
2 |
|
} |
290
|
2 |
|
$pdf->SetFont('helvetica', '', 9); |
291
|
2 |
|
$pdf->Ln(); |
292
|
2 |
|
} |
293
|
|
|
|
294
|
|
|
private function stampaDettaglio($pdf, $parametri) |
295
|
|
|
{ |
296
|
|
|
|
297
|
|
|
$ln = $parametri['ln']; |
|
|
|
|
298
|
|
|
$fill = $parametri['fill']; |
|
|
|
|
299
|
|
|
$align = $parametri['align']; |
|
|
|
|
300
|
|
|
$border = $parametri['border']; |
|
|
|
|
301
|
|
|
$h = $parametri['h']; |
|
|
|
|
302
|
|
|
$larghezzaform = $parametri['larghezzaform']; |
|
|
|
|
303
|
|
|
$testata = $parametri['testata']; |
|
|
|
|
304
|
|
|
$modellicolonne = $testata['modellocolonne']; |
305
|
|
|
|
306
|
|
|
$rispostaj = $parametri['griglia']; |
307
|
|
|
// Dati |
308
|
|
|
$risposta = json_decode($rispostaj); |
|
|
|
|
309
|
|
|
$dimensions = $pdf->getPageDimensions(); |
310
|
|
|
$righe = $risposta->rows; |
|
|
|
|
311
|
|
|
$pdf->SetFont('helvetica', '', 9); |
312
|
|
|
foreach ($righe as $riga) { |
313
|
|
|
$fill = !$fill; |
|
|
|
|
314
|
|
|
$vettorecelle = $riga->cell; |
315
|
|
|
|
316
|
|
|
$arr_heights = array(); |
317
|
|
|
// store current object |
318
|
|
|
$pdf->startTransaction(); |
319
|
|
|
foreach ($vettorecelle as $posizione => $valore) { |
320
|
|
|
if (!is_object($valore)) { |
321
|
|
|
$width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform); |
322
|
|
|
// get the number of lines |
323
|
|
|
$arr_heights[] = $pdf->MultiCell($width, 0, $valore, $border, $align, $fill, 0, '', '', true, 0, false, true, 0); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
// restore previous object |
327
|
|
|
$pdf->rollbackTransaction(true); |
328
|
|
|
//work out the number of lines required |
329
|
|
|
$rowcount = max($arr_heights); |
330
|
|
|
$startY = $pdf->GetY(); |
|
|
|
|
331
|
|
|
if (($startY + $rowcount * $h) + $dimensions['bm'] > ($dimensions['hk'])) { |
332
|
|
|
// page break |
333
|
|
|
$pdf->AddPage('L'); |
334
|
|
|
// stampa testata |
335
|
|
|
$this->stampaTestata($pdf, $parametri); |
336
|
|
|
} |
337
|
|
|
//now draw it |
338
|
|
|
foreach ($vettorecelle as $posizione => $valore) { |
339
|
|
|
if (!is_object($valore)) { |
340
|
|
|
$width = $this->getWidthColumn($modellicolonne, $posizione, $larghezzaform); |
341
|
|
|
$pdf->MultiCell($width, $rowcount * $h, $valore, $border, $align, $fill, $ln); |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
$pdf->Ln(); |
345
|
|
|
} |
346
|
|
|
$pdf->Cell(0, 10, GrigliaFiltriUtils::traduciFiltri(array('filtri' => $risposta->filtri)), 0, false, 'L', 0, '', 0, false, 'T', 'M'); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
private function getWidthColumn($modellicolonne, $posizione, $larghezzaform) |
350
|
|
|
{ |
351
|
|
|
if (isset($modellicolonne[$posizione])) { |
352
|
|
|
$width = ((297 * $modellicolonne[$posizione]['width']) / $larghezzaform) / 2; |
353
|
|
|
} else { |
354
|
|
|
$width = ((297 * 100) / $larghezzaform) / 2; |
355
|
|
|
} |
356
|
|
|
return $width; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.