1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) CJT TERABYTE INC |
5
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @link: https://github.com/cjtterabytesoft/alert |
9
|
|
|
* @author: Wilmer Arámbula <[email protected]> |
10
|
|
|
* @copyright: (c) CJT TERABYTE INC |
11
|
|
|
* @widgets: [Export] |
12
|
|
|
* @since: 1.0 |
13
|
|
|
* @yii: 3.0 |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
namespace cjtterabytesoft\widgets; |
17
|
|
|
|
18
|
|
|
use Dompdf\Dompdf; |
|
|
|
|
19
|
|
|
use Dompdf\Options; |
|
|
|
|
20
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
|
|
|
|
21
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
|
|
|
|
22
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
|
|
|
|
23
|
|
|
use PhpOffice\PhpWord\IOFactory; |
|
|
|
|
24
|
|
|
use PhpOffice\PhpWord\PhpWord; |
|
|
|
|
25
|
|
|
use yii\base\InvalidConfigException; |
26
|
|
|
use yii\base\Widget; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Export File diferents format Csv, Excel, Html, Pdf, Word. |
30
|
|
|
*/ |
31
|
|
|
class Export extends Widget |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var string Charset export csv. |
35
|
|
|
*/ |
36
|
|
|
public $_csvCharset = 'UTF-8'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \yii\data\DataProviderInterface the data provider for the view. This property is required. |
40
|
|
|
*/ |
41
|
|
|
public $_dataProvider; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \app\model\searchModel. |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public $_searchModel; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string tableName. |
50
|
|
|
*/ |
51
|
|
|
public $_tableName= ''; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string Title Export. |
55
|
|
|
*/ |
56
|
|
|
public $_title = ''; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initializes the view. |
60
|
|
|
*/ |
61
|
|
|
public function init() |
62
|
|
|
{ |
63
|
|
|
parent::init(); |
64
|
|
|
|
65
|
|
|
if (empty($this->_dataProvider)) { |
66
|
|
|
throw new InvalidConfigException('The "dataProvider" property must be set.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (empty($this->_searchModel)) { |
70
|
|
|
throw new InvalidConfigException('The "dataProvider" property must be set.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->_tableName = $this->_searchModel->tableName(); |
74
|
|
|
|
75
|
|
|
if (empty($this->_title)) { |
76
|
|
|
$this->_title = $this->_tableName; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return file Csv Export. |
|
|
|
|
82
|
|
|
*/ |
83
|
|
|
public function exportCsv() |
84
|
|
|
{ |
85
|
|
|
$csvCharset = $this->_csvCharset; |
|
|
|
|
86
|
|
|
$fields = $this->getFieldsKeys($this->_searchModel->exportFields()); |
87
|
|
|
header('Pragma: public'); |
88
|
|
|
header('Expires: 0'); |
89
|
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
90
|
|
|
header('Content-Description: File Transfer'); |
91
|
|
|
header('Content-Type: text/csv'); |
92
|
|
|
$filename = $this->_tableName . '.csv'; |
93
|
|
|
header('Content-Disposition: attachment;filename=' . $filename); |
94
|
|
|
header('Content-Transfer-Encoding: binary'); |
95
|
|
|
$fp = fopen('php://output', 'w'); |
96
|
|
|
fwrite($fp, $bom = (chr(0xEF) . chr(0xBB) . chr(0xBF))); |
|
|
|
|
97
|
|
|
if ($fp) { |
|
|
|
|
98
|
|
|
$items = []; |
99
|
|
|
$i = 0; |
100
|
|
|
foreach ($fields as $one) { |
101
|
|
|
$items[$i] = $one; |
102
|
|
|
$i++; |
103
|
|
|
} |
104
|
|
|
fwrite($fp, implode($items, ',') . "\n"); |
105
|
|
|
$items = []; |
106
|
|
|
$i = 0; |
107
|
|
|
foreach ($this->_dataProvider->getModels() as $model) { |
108
|
|
|
foreach ($this->_searchModel->exportFields() as $one) { |
109
|
|
|
$item = str_replace('"', '\"', is_string($one) ? $model[$one] : $one($model)); |
110
|
|
|
$items[$i] = ($item) ? '"' . $item . '"' : $item; |
111
|
|
|
$i++; |
112
|
|
|
} |
113
|
|
|
fwrite($fp, implode($items, ',') . "\n"); |
114
|
|
|
$items = []; |
115
|
|
|
$i = 0; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
fclose($fp); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return file Excel Export. |
123
|
|
|
*/ |
124
|
|
|
public function exportExcel() |
125
|
|
|
{ |
126
|
|
|
$fields = $this->getFieldsKeys($this->_searchModel->exportFields()); |
127
|
|
|
$objPHPExcel = new Spreadsheet(); |
128
|
|
|
$objPHPExcel->setActiveSheetIndex(0); |
129
|
|
|
$objPHPExcel->getActiveSheet()->setTitle($this->_title); |
130
|
|
|
$letter = 65; |
131
|
|
|
foreach ($fields as $one) { |
132
|
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension(chr($letter))->setAutoSize(true); |
133
|
|
|
$letter++; |
134
|
|
|
} |
135
|
|
|
$letter = 65; |
136
|
|
|
foreach ($fields as $one) { |
137
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue(chr($letter) . '1', $this->_searchModel->getAttributeLabel($one)); |
138
|
|
|
$objPHPExcel->getActiveSheet()->getStyle(chr($letter) . '1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); |
139
|
|
|
$letter++; |
140
|
|
|
} |
141
|
|
|
$row = 2; |
142
|
|
|
$letter = 65; |
143
|
|
|
foreach ($this->_dataProvider->getModels() as $model) { |
144
|
|
|
foreach ($this->_searchModel->exportFields() as $one) { |
145
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue(chr($letter) . $row, (is_string($one)) ? $model[$one] : $one($model)); |
146
|
|
|
$objPHPExcel->getActiveSheet()->getStyle(chr($letter) . $row)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); |
147
|
|
|
$letter++; |
148
|
|
|
} |
149
|
|
|
$letter = 65; |
150
|
|
|
$row++; |
151
|
|
|
} |
152
|
|
|
header('Content-Type: application/vnd.ms-excel'); |
153
|
|
|
$filename = $this->_tableName . '.xlsx'; |
154
|
|
|
header('Content-Disposition: attachment;filename=' . $filename); |
155
|
|
|
header('Cache-Control: max-age=0'); |
156
|
|
|
$objWriter = new Xlsx($objPHPExcel); |
157
|
|
|
$objWriter->save('php://output'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return file Htlm Export. |
162
|
|
|
*/ |
163
|
|
|
public function exportHtml() |
164
|
|
|
{ |
165
|
|
|
$fields = $this->getFieldsKeys($this->_searchModel->exportFields()); |
166
|
|
|
$phpWord = new PhpWord(); |
167
|
|
|
$section = $phpWord->addSection(); |
168
|
|
|
$section->addTitle($this->_title); |
169
|
|
|
$table = $section->addTable( |
170
|
|
|
[ |
171
|
|
|
'name' => 'Tahoma', |
172
|
|
|
'size' => 10, |
173
|
|
|
'align' => 'center', |
174
|
|
|
] |
175
|
|
|
); |
176
|
|
|
$table->addRow(300, ['exactHeight' => true]); |
177
|
|
|
foreach ($fields as $one) { |
178
|
|
|
$table->addCell(1500, [ |
179
|
|
|
'bgColor' => 'eeeeee', |
180
|
|
|
'valign' => 'center', |
181
|
|
|
'borderTopSize' => 5, |
182
|
|
|
'borderRightSize' => 5, |
183
|
|
|
'borderBottomSize' => 5, |
184
|
|
|
'borderLeftSize' => 5, |
185
|
|
|
])->addText($this->_searchModel->getAttributeLabel($one), ['bold' => true, 'size' => 10], ['align' => 'center']); |
186
|
|
|
} |
187
|
|
|
foreach ($this->_dataProvider->getModels() as $model) { |
188
|
|
|
$table->addRow(300, ['exactHeight' => true]); |
189
|
|
|
foreach ($this->_searchModel->exportFields() as $one) { |
190
|
|
|
$table->addCell(1500, [ |
191
|
|
|
'valign' => 'center', |
192
|
|
|
'borderTopSize' => 1, |
193
|
|
|
'borderRightSize' => 1, |
194
|
|
|
'borderBottomSize' => 1, |
195
|
|
|
'borderLeftSize' => 1, |
196
|
|
|
])->addText('<p style="margin-left: 10px;">' . (is_string($one)) ? $model[$one] : $one($model) . '</p>', ['bold' => false, 'size' => 10], ['align' => 'right']); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
header('Content-Type: application/html'); |
200
|
|
|
$filename = $this->_tableName . '.html'; |
201
|
|
|
header('Content-Disposition: attachment;filename=' . $filename . ' '); |
202
|
|
|
header('Cache-Control: max-age=0'); |
203
|
|
|
$objWriter = IOFactory::createWriter($phpWord, 'HTML'); |
204
|
|
|
$objWriter->save('php://output'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return file Pdf Export. |
209
|
|
|
*/ |
210
|
|
|
public function exportPdf() |
211
|
|
|
{ |
212
|
|
|
$fields = $this->getFieldsKeys($this->_searchModel->exportFields()); |
213
|
|
|
$options = new Options(); |
214
|
|
|
$options->set('defaultFont', 'times'); |
215
|
|
|
$dompdf = new Dompdf($options); |
216
|
|
|
$html = '<html><body>'; |
217
|
|
|
$html .= '<h1>' . '<center>' . $this->_title . '</center>' . '</h1>'; |
218
|
|
|
$html .= '<table width="100%" cellspacing="0" cellpadding="0">'; |
219
|
|
|
$html .= '<tr style="background-color: #ececec;">'; |
220
|
|
|
foreach ($fields as $one) { |
221
|
|
|
$html .= '<td style="border: 2px solid #cccccc; text-align: center; font-weight: 500;">' . $this->_searchModel->getAttributeLabel($one) . '</td>'; |
222
|
|
|
} |
223
|
|
|
$html .= '</tr>'; |
224
|
|
|
foreach ($this->_dataProvider->getModels() as $model) { |
225
|
|
|
$html .= '<tr>'; |
226
|
|
|
foreach ($this->_searchModel->exportFields() as $one) { |
227
|
|
|
switch (true) { |
228
|
|
|
case is_string($one): |
229
|
|
|
$html .= '<td style="border: 1px solid #cccccc; text-align: left; font-weight: 300; padding-left: 10px;">' . $model[$one] . '</td>'; |
230
|
|
|
break; |
231
|
|
|
default: |
232
|
|
|
$html .= '<td style="border: 1px solid #cccccc; text-align: left; font-weight: 300; padding-left: 10px;">' . $one($model) . '</td>'; |
233
|
|
|
break; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
$html .= '</tr>'; |
237
|
|
|
} |
238
|
|
|
$html .= '</table>'; |
239
|
|
|
$html .= '</body></html>'; |
240
|
|
|
$dompdf->loadHtml($html); |
241
|
|
|
$dompdf->setPaper('letter', 'landscape'); |
242
|
|
|
$dompdf->render(); |
243
|
|
|
$dompdf->stream($this->_tableName . '_' . time()); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return file Word Export. |
248
|
|
|
*/ |
249
|
|
|
public function exportWord() |
250
|
|
|
{ |
251
|
|
|
$fields = $this->getFieldsKeys($this->_searchModel->exportFields()); |
252
|
|
|
$phpWord = new PhpWord(); |
253
|
|
|
$phpWord->getCompatibility()->setOoxmlVersion(15); |
254
|
|
|
$section = $phpWord->addSection(); |
255
|
|
|
$sectionStyle = $section->getSettings(); |
256
|
|
|
$sectionStyle->setLandscape(); |
257
|
|
|
$sectionStyle->setBorderTopColor('C0C0C0'); |
258
|
|
|
$sectionStyle->setMarginTop(300); |
259
|
|
|
$sectionStyle->setMarginRight(300); |
260
|
|
|
$sectionStyle->setMarginBottom(300); |
261
|
|
|
$sectionStyle->setMarginLeft(300); |
262
|
|
|
$phpWord->addTitleStyle(1, ['name' => 'HelveticaNeueLT Std Med', 'size' => 16], ['align' => 'center']); //h |
263
|
|
|
$section->addTitle($this->_title); |
264
|
|
|
$table = $section->addTable( |
265
|
|
|
[ |
266
|
|
|
'name' => 'Tahoma', |
267
|
|
|
'align' => 'center', |
268
|
|
|
'cellMarginTop' => 30, |
269
|
|
|
'cellMarginRight' => 30, |
270
|
|
|
'cellMarginBottom' => 30, |
271
|
|
|
'cellMarginLeft' => 30, |
272
|
|
|
] |
273
|
|
|
); |
274
|
|
|
$table->addRow(300, ['exactHeight' => true]); |
275
|
|
|
foreach ($fields as $one) { |
276
|
|
|
$table->addCell(1500, [ |
277
|
|
|
'bgColor' => 'eeeeee', |
278
|
|
|
'valign' => 'center', |
279
|
|
|
'borderTopSize' => 5, |
280
|
|
|
'borderRightSize' => 5, |
281
|
|
|
'borderBottomSize' => 5, |
282
|
|
|
'borderLeftSize' => 5, |
283
|
|
|
])->addText($this->_searchModel->getAttributeLabel($one), ['bold' => true, 'size' => 10], ['align' => 'center']); |
284
|
|
|
} |
285
|
|
|
foreach ($this->_dataProvider->getModels() as $model) { |
286
|
|
|
$table->addRow(300, ['exactHeight' => true]); |
287
|
|
|
foreach ($this->_searchModel->exportFields() as $one) { |
288
|
|
|
$table->addCell(1500, [ |
289
|
|
|
'valign' => 'center', |
290
|
|
|
'borderTopSize' => 1, |
291
|
|
|
'borderRightSize' => 1, |
292
|
|
|
'borderBottomSize' => 1, |
293
|
|
|
'borderLeftSize' => 1, |
294
|
|
|
])->addText(is_string($one) ? $model[$one] : $one($model), ['bold' => false, 'size' => 10], ['align' => 'left']); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
header('Content-Type: application/vnd.ms-word'); |
298
|
|
|
$filename = $this->_tableName . '.docx'; |
299
|
|
|
header('Content-Disposition: attachment;filename=' . $filename . ' '); |
300
|
|
|
header('Cache-Control: max-age=0'); |
301
|
|
|
$objWriter = IOFactory::createWriter($phpWord, 'Word2007'); |
302
|
|
|
$objWriter->save('php://output'); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
private function getFieldsKeys($fieldsSended) |
306
|
|
|
{ |
307
|
|
|
$fields = []; |
308
|
|
|
$i = 0; |
309
|
|
|
|
310
|
|
|
foreach ($fieldsSended as $key => $value) { |
311
|
|
|
switch (true) { |
312
|
|
|
case is_int($key): |
313
|
|
|
$fields[$i] = $value; |
314
|
|
|
break; |
315
|
|
|
default: |
316
|
|
|
$fields[$i] = $key; |
317
|
|
|
break; |
318
|
|
|
} |
319
|
|
|
$i++; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return $fields; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths