-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
296 lines (269 loc) · 6.91 KB
/
code.power
File metadata and controls
296 lines (269 loc) · 6.91 KB
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
/**
* The current active user.
*
* @var User $user
* @since 3.2.0
*/
private User $user;
/**
* The PhpSpreadsheet object used to create and manage the spreadsheet.
*
* @var Spreadsheet $spreadsheet
* @since 3.2.0
*/
private Spreadsheet $spreadsheet;
/**
* The name of the file to be exported, including the date if not provided.
*
* @var string $fileName
* @since 3.2.0
*/
private string $fileName;
/**
* The format of the exported file, typically 'Xls' or 'Csv'.
*
* @var string $fileType
* @since 3.2.0
*/
private string $fileType;
/**
* The name of the worksheet tab in the exported spreadsheet.
*
* @var string $subjectTab
* @since 3.2.0
*/
private string $subjectTab;
/**
* The styles applied to the header row, including font size, color, and bold formatting.
*
* @var array $headerStyles
* @since 3.2.0
*/
private array $headerStyles;
/**
* The styles applied to the first column (side) of the spreadsheet, usually for labeling rows.
*
* @var array $sideStyles
* @since 3.2.0
*/
private array $sideStyles;
/**
* The styles applied to normal cells in the spreadsheet, such as font color and size.
*
* @var array $normalStyles
* @since 3.2.0
*/
private array $normalStyles;
/**
* SpreadsheetExporter constructor.
* Initializes styles and the Spreadsheet object.
*
* @since 3.2.0
*/
public function __construct()
{
$this->user = Factory::getApplication()->getIdentity();
$this->spreadsheet = new Spreadsheet();
$this->headerStyles = [
'font' => [
'bold' => true,
'color' => ['rgb' => '1171A3'],
'size' => 13,
'name' => 'Verdana'
]
];
$this->sideStyles = [
'font' => [
'bold' => true,
'color' => ['rgb' => '444444'],
'size' => 11,
'name' => 'Verdana'
]
];
$this->normalStyles = [
'font' => [
'color' => ['rgb' => '444444'],
'size' => 11,
'name' => 'Verdana'
]
];
}
/**
* Prepares the spreadsheet with data.
*
* @param array $rows
* @param string|null $fileName
* @param string|null $title
* @param string|null $subjectTab
* @param string $creator
* @param string|null $description
* @param string|null $category
* @param string|null $keywords
* @param string|null $modified
*
* @return void
* @throws Exception
* @since 3.2.0
*/
public function export(
array $rows,
?string $fileName = null,
?string $title = null,
?string $subjectTab = null,
string $creator = 'Vast Development Method',
?string $description = null,
?string $category = null,
?string $keywords = null,
?string $modified = null
): void {
$this->fileName = $fileName ?? 'exported_' . Factory::getDate()->format('jS_F_Y');
$this->fileType = 'Xls';
$this->subjectTab = $subjectTab ?? 'Sheet1';
$this->setDocumentProperties($creator, $title, $description, $category, $keywords, $modified);
$this->populateSpreadsheet($rows);
// Output the spreadsheet
$this->outputSpreadsheet();
}
/**
* Set the document properties for the spreadsheet.
*
* @param string $creator
* @param string|null $title
* @param string|null $description
* @param string|null $category
* @param string|null $keywords
* @param string|null $modified
* @since 3.2.0
*/
private function setDocumentProperties(
string $creator,
?string $title = null,
?string $description = null,
?string $category = null,
?string $keywords = null,
?string $modified = null
): void
{
$modifiedBy = $modified ?? $this->user->name;
$this->spreadsheet->getProperties()
->setCreator($creator)
->setCompany('Vast Development Method')
->setLastModifiedBy($modifiedBy)
->setTitle($title ?? 'Book1')
->setSubject($this->subjectTab);
if ($description)
{
$this->spreadsheet->getProperties()->setDescription($description);
}
if ($category)
{
$this->spreadsheet->getProperties()->setCategory($category);
}
if ($keywords)
{
$this->spreadsheet->getProperties()->setKeywords($keywords);
}
}
/**
* Populate the spreadsheet with the provided rows.
*
* @param array $rows
*
* @since 3.2.0
*/
private function populateSpreadsheet(array $rows): void
{
if (($size = ArrayHelper::check($rows)) === false)
{
return;
}
$xlsMode = $this->determineXlsMode($size);
$activeSheet = $this->spreadsheet->setActiveSheetIndex(0);
$rowIndex = 1;
foreach ($rows as $array)
{
$columnIndex = 'A';
foreach ($array as $value)
{
$activeSheet->setCellValue($columnIndex . $rowIndex, $value);
$this->applyStyles($activeSheet, $rowIndex, $columnIndex, $xlsMode);
$columnIndex++;
}
$rowIndex++;
}
$activeSheet->setTitle($this->subjectTab);
}
/**
* Determine the XLS mode based on the number of rows.
*
* @param int $size
* @return int
*
* @since 3.2.0
*/
private function determineXlsMode(int $size): int
{
if ($size > 3000)
{
$this->fileType = 'Csv';
return 3;
}
if ($size > 2000)
{
return 2;
}
return 1;
}
/**
* Apply styles to the cells based on the row and column index.
*
* @param Worksheet $sheet
* @param int $rowIndex
* @param string $columnIndex
* @param int $xlsMode
*
* @since 3.2.0
*/
private function applyStyles(Worksheet $sheet, int $rowIndex, string $columnIndex, int $xlsMode): void
{
if ($xlsMode === 3)
{
return;
}
if ($rowIndex === 1)
{
$sheet->getColumnDimension($columnIndex)->setAutoSize(true);
$sheet->getStyle($columnIndex . $rowIndex)->applyFromArray($this->headerStyles);
$sheet->getStyle($columnIndex . $rowIndex)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$row_height = ($this->headerStyles['font']['size'] ?? 13) + 5;
$sheet->getRowDimension($rowIndex)->setRowHeight($row_height);
}
elseif ($columnIndex === 'A')
{
$sheet->getStyle($columnIndex . $rowIndex)->applyFromArray($this->sideStyles);
}
else
{
$sheet->getStyle($columnIndex . $rowIndex)->applyFromArray($this->normalStyles);
}
}
/**
* Output the spreadsheet as an Excel or CSV file.
*
* @return void
* @throws Exception
* @since 3.2.0
*/
private function outputSpreadsheet(): void
{
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $this->fileName . '.' . strtolower($this->fileType) . '"');
header('Cache-Control: max-age=0');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$writer = IOFactory::createWriter($this->spreadsheet, $this->fileType);
$writer->save('php://output');
exit;
}