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\toolbar; |
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
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue(chr($letter) . '1', $this->_searchModel->getAttributeLabel($one)); |
134
|
|
|
$objPHPExcel->getActiveSheet()->getStyle(chr($letter) . '1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); |
135
|
|
|
$letter++; |
136
|
|
|
} |
137
|
|
|
$row = 2; |
138
|
|
|
$letter = 65; |
139
|
|
|
foreach ($this->_dataProvider->getModels() as $model) { |
140
|
|
|
foreach ($this->_searchModel->exportFields() as $one) { |
141
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue(chr($letter) . $row, (is_string($one)) ? $model[$one] : $one($model)); |
142
|
|
|
$objPHPExcel->getActiveSheet()->getStyle(chr($letter) . $row)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); |
143
|
|
|
$letter++; |
144
|
|
|
} |
145
|
|
|
$letter = 65; |
146
|
|
|
$row++; |
147
|
|
|
} |
148
|
|
|
header('Content-Type: application/vnd.ms-excel'); |
149
|
|
|
$filename = $this->_tableName . '.xlsx'; |
150
|
|
|
header('Content-Disposition: attachment;filename=' . $filename); |
151
|
|
|
header('Cache-Control: max-age=0'); |
152
|
|
|
$objWriter = new Xlsx($objPHPExcel); |
153
|
|
|
$objWriter->save('php://output'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return file Htlm Export. |
158
|
|
|
*/ |
159
|
|
|
public function exportHtml() |
160
|
|
|
{ |
161
|
|
|
$fields = $this->getFieldsKeys($this->_searchModel->exportFields()); |
162
|
|
|
$phpWord = new PhpWord(); |
163
|
|
|
$section = $phpWord->addSection(); |
164
|
|
|
$section->addTitle($this->_title); |
165
|
|
|
$table = $section->addTable(['name' => 'Tahoma', 'size' => 10, 'align' => 'center',]); |
166
|
|
|
$table->addRow(300, ['exactHeight' => true]); |
167
|
|
|
foreach ($fields as $one) { |
168
|
|
|
$table->addCell( |
169
|
|
|
1500, |
170
|
|
|
['bgColor' => 'eeeeee', 'borderTopSize' => 5, 'borderRightSize' => 5, 'borderBottomSize' => 5,'borderLeftSize' => 5, 'valign' => 'center'] |
171
|
|
|
)->addText($this->_searchModel->getAttributeLabel($one), ['bold' => true, 'size' => 10], ['align' => 'center']); |
172
|
|
|
} |
173
|
|
|
foreach ($this->_dataProvider->getModels() as $model) { |
174
|
|
|
$table->addRow(300, ['exactHeight' => true]); |
175
|
|
|
foreach ($this->_searchModel->exportFields() as $one) { |
176
|
|
|
$table->addCell( |
177
|
|
|
1500, |
178
|
|
|
['borderTopSize' => 1, 'borderRightSize' => 1, 'borderBottomSize' => 1, 'borderLeftSize' => 1, 'valign' => 'center'] |
179
|
|
|
)->addText('<p style="margin-left: 10px;">' . (is_string($one)) ? $model[$one] : $one($model) . '</p>', ['bold' => false, 'size' => 10], ['align' => 'right']); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
header('Content-Type: application/html'); |
183
|
|
|
$filename = $this->_tableName . '.html'; |
184
|
|
|
header('Content-Disposition: attachment;filename=' . $filename . ' '); |
185
|
|
|
header('Cache-Control: max-age=0'); |
186
|
|
|
$objWriter = IOFactory::createWriter($phpWord, 'HTML'); |
187
|
|
|
$objWriter->save('php://output'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return file Pdf Export. |
192
|
|
|
*/ |
193
|
|
|
public function exportPdf() |
194
|
|
|
{ |
195
|
|
|
$fields = $this->getFieldsKeys($this->_searchModel->exportFields()); |
196
|
|
|
$options = new Options(); |
197
|
|
|
$options->set('defaultFont', 'times'); |
198
|
|
|
$dompdf = new Dompdf($options); |
199
|
|
|
$html = '<html><body>'; |
200
|
|
|
$html .= '<h1>' . '<center>' . $this->_title . '</center>' . '</h1>'; |
201
|
|
|
$html .= '<table width="100%" cellspacing="0" cellpadding="0">'; |
202
|
|
|
$html .= '<tr style="background-color: #ececec;">'; |
203
|
|
|
foreach ($fields as $one) { |
204
|
|
|
$html .= '<td style="border: 2px solid #cccccc; text-align: center; font-weight: 500;">' . $this->_searchModel->getAttributeLabel($one) . '</td>'; |
205
|
|
|
} |
206
|
|
|
$html .= '</tr>'; |
207
|
|
|
foreach ($this->_dataProvider->getModels() as $model) { |
208
|
|
|
$html .= '<tr>'; |
209
|
|
|
foreach ($this->_searchModel->exportFields() as $one) { |
210
|
|
|
switch (true) { |
211
|
|
|
case is_string($one): |
212
|
|
|
$html .= '<td style="border: 1px solid #cccccc; text-align: left; font-weight: 300; padding-left: 10px;">' . $model[$one] . '</td>'; |
213
|
|
|
break; |
214
|
|
|
default: |
215
|
|
|
$html .= '<td style="border: 1px solid #cccccc; text-align: left; font-weight: 300; padding-left: 10px;">' . $one($model) . '</td>'; |
216
|
|
|
break; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
$html .= '</tr>'; |
220
|
|
|
} |
221
|
|
|
$html .= '</table>'; |
222
|
|
|
$html .= '</body></html>'; |
223
|
|
|
$dompdf->loadHtml($html); |
224
|
|
|
$dompdf->setPaper('letter', 'landscape'); |
225
|
|
|
$dompdf->render(); |
226
|
|
|
$dompdf->stream($this->_tableName . '_' . time()); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return file Word Export. |
231
|
|
|
*/ |
232
|
|
|
public function exportWord() |
233
|
|
|
{ |
234
|
|
|
$fields = $this->getFieldsKeys($this->_searchModel->exportFields()); |
235
|
|
|
$phpWord = new PhpWord(); |
236
|
|
|
$phpWord->getCompatibility()->setOoxmlVersion(15); |
237
|
|
|
$section = $phpWord->addSection(); |
238
|
|
|
$sectionStyle = $section->getSettings(); |
239
|
|
|
$sectionStyle->setLandscape(); |
240
|
|
|
$sectionStyle->setBorderTopColor('C0C0C0'); |
241
|
|
|
$sectionStyle->setMarginTop(300); |
242
|
|
|
$sectionStyle->setMarginRight(300); |
243
|
|
|
$sectionStyle->setMarginBottom(300); |
244
|
|
|
$sectionStyle->setMarginLeft(300); |
245
|
|
|
$phpWord->addTitleStyle(1, ['name' => 'HelveticaNeueLT Std Med', 'size' => 16], ['align' => 'center']); |
246
|
|
|
$section->addTitle($this->_title); |
247
|
|
|
$table = $section->addTable( |
248
|
|
|
['name' => 'Tahoma', 'align' => 'center', 'cellMarginTop' => 30, 'cellMarginRight' => 30, 'cellMarginBottom' => 30, 'cellMarginLeft' => 30] |
249
|
|
|
); |
250
|
|
|
$table->addRow(300, ['exactHeight' => true]); |
251
|
|
|
foreach ($fields as $one) { |
252
|
|
|
$table->addCell( |
253
|
|
|
1500, |
254
|
|
|
['bgColor' => 'eeeeee', 'borderTopSize' => 5, 'borderRightSize' => 5, 'borderBottomSize' => 5,'borderLeftSize' => 5, 'valign' => 'center'] |
255
|
|
|
)->addText($this->_searchModel->getAttributeLabel($one), ['bold' => true, 'size' => 10], ['align' => 'center']); |
256
|
|
|
} |
257
|
|
|
foreach ($this->_dataProvider->getModels() as $model) { |
258
|
|
|
$table->addRow(300, ['exactHeight' => true]); |
259
|
|
|
foreach ($this->_searchModel->exportFields() as $one) { |
260
|
|
|
$table->addCell( |
261
|
|
|
1500, |
262
|
|
|
['borderTopSize' => 1, 'borderRightSize' => 1, 'borderBottomSize' => 1, 'borderLeftSize' => 1, 'valign' => 'center'] |
263
|
|
|
)->addText(is_string($one) ? $model[$one] : $one($model), ['bold' => false, 'size' => 10], ['align' => 'left']); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
header('Content-Type: application/vnd.ms-word'); |
267
|
|
|
$filename = $this->_tableName . '.docx'; |
268
|
|
|
header('Content-Disposition: attachment;filename=' . $filename . ' '); |
269
|
|
|
header('Cache-Control: max-age=0'); |
270
|
|
|
$objWriter = IOFactory::createWriter($phpWord, 'Word2007'); |
271
|
|
|
$objWriter->save('php://output'); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return array Fields List. |
276
|
|
|
*/ |
277
|
|
|
private function getFieldsKeys($fieldsSended) |
278
|
|
|
{ |
279
|
|
|
$fields = []; |
280
|
|
|
$i = 0; |
281
|
|
|
|
282
|
|
|
foreach ($fieldsSended as $key => $value) { |
283
|
|
|
switch (true) { |
284
|
|
|
case is_int($key): |
285
|
|
|
$fields[$i] = $value; |
286
|
|
|
break; |
287
|
|
|
default: |
288
|
|
|
$fields[$i] = $key; |
289
|
|
|
break; |
290
|
|
|
} |
291
|
|
|
$i++; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $fields; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|