|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
declare(strict_types=1); |
|
33
|
|
|
|
|
34
|
|
|
namespace Platine\Framework\Helper; |
|
35
|
|
|
|
|
36
|
|
|
use Platine\Cache\CacheInterface; |
|
37
|
|
|
use Platine\Config\Config; |
|
38
|
|
|
use Platine\Filesystem\FileInterface; |
|
39
|
|
|
use Platine\Filesystem\Filesystem; |
|
40
|
|
|
use Platine\PDF\PDF; |
|
41
|
|
|
use Platine\Stdlib\Helper\Json; |
|
42
|
|
|
use Platine\Template\Template; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @class PrintHelper |
|
46
|
|
|
* @package Platine\Framework\Helper |
|
47
|
|
|
* @template T |
|
48
|
|
|
*/ |
|
49
|
|
|
class PrintHelper |
|
50
|
|
|
{ |
|
51
|
|
|
/** |
|
52
|
|
|
* Create new instance |
|
53
|
|
|
* @param PDF $pdf |
|
54
|
|
|
* @param Template $template |
|
55
|
|
|
* @param Config<T> $config |
|
56
|
|
|
* @param Filesystem $filesystem |
|
57
|
|
|
* @param CacheInterface $cache |
|
58
|
|
|
* @param FileHelper $fileHelper |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct( |
|
61
|
|
|
protected PDF $pdf, |
|
62
|
|
|
protected Template $template, |
|
63
|
|
|
protected Config $config, |
|
64
|
|
|
protected Filesystem $filesystem, |
|
65
|
|
|
protected CacheInterface $cache, |
|
66
|
|
|
protected FileHelper $fileHelper, |
|
67
|
|
|
) { |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Generate the report |
|
72
|
|
|
* @param string|int $reportId |
|
73
|
|
|
* @param array<string, mixed> $data |
|
74
|
|
|
* @param string $filename |
|
75
|
|
|
* @param string $format |
|
76
|
|
|
* @return string|null the full file path |
|
77
|
|
|
*/ |
|
78
|
|
|
public function generateReport( |
|
79
|
|
|
string|int $reportId, |
|
80
|
|
|
array $data = [], |
|
81
|
|
|
string $filename = '', |
|
82
|
|
|
string $format = 'portrait' |
|
83
|
|
|
): ?string { |
|
84
|
|
|
return $this->handleReport( |
|
85
|
|
|
$reportId, |
|
86
|
|
|
$data, |
|
87
|
|
|
$filename, |
|
88
|
|
|
true, |
|
89
|
|
|
$format |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Print the report |
|
95
|
|
|
* @param string|int $reportId |
|
96
|
|
|
* @param array<string, mixed> $data |
|
97
|
|
|
* @param string $filename |
|
98
|
|
|
* @param string $format |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public function printReport( |
|
102
|
|
|
string|int $reportId, |
|
103
|
|
|
array $data = [], |
|
104
|
|
|
string $filename = '', |
|
105
|
|
|
string $format = 'portrait' |
|
106
|
|
|
): void { |
|
107
|
|
|
$this->handleReport( |
|
108
|
|
|
$reportId, |
|
109
|
|
|
$data, |
|
110
|
|
|
$filename, |
|
111
|
|
|
false, |
|
112
|
|
|
$format |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Return the main information for all report |
|
118
|
|
|
* @return array<string, mixed> |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getMainData(): array |
|
121
|
|
|
{ |
|
122
|
|
|
$data = []; |
|
123
|
|
|
$data['current_time'] = date('Y-m-d H:i:s'); |
|
124
|
|
|
$data['config'] = [ |
|
125
|
|
|
'app' => $this->config->get('app'), |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
return $data; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Return the report content if available from cache |
|
133
|
|
|
* @param string|int $reportId |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getReportContent(string|int $reportId): string |
|
137
|
|
|
{ |
|
138
|
|
|
$useCache = $this->config->get('platform.cache_print_report_content', false); |
|
139
|
|
|
if ($useCache) { |
|
140
|
|
|
$cacheKey = sprintf('report_definition_%s', $reportId); |
|
141
|
|
|
$content = $this->cache->get($cacheKey); |
|
142
|
|
|
if ($content !== null) { |
|
143
|
|
|
return $content; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
return $this->getRealReportContent($reportId); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Return the report content definition |
|
153
|
|
|
* @param string|int $reportId |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getRealReportContent(string|int $reportId): string |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
return ''; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Debug report definition if enable |
|
163
|
|
|
* @param string|int $reportId |
|
164
|
|
|
* @param array<string, mixed> $reportData |
|
165
|
|
|
* @return void |
|
166
|
|
|
*/ |
|
167
|
|
|
public function debugReport(string|int $reportId, array $reportData): void |
|
168
|
|
|
{ |
|
169
|
|
|
$reportDebugPath = $this->config->get('platform.report_debug_path'); |
|
170
|
|
|
if ($reportDebugPath !== null) { |
|
171
|
|
|
$reportDebugFile = sprintf('%s/%s.log', $reportDebugPath, $reportId); |
|
172
|
|
|
$this->filesystem->file($reportDebugFile) |
|
173
|
|
|
->write(Json::encode( |
|
174
|
|
|
$reportData, |
|
175
|
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
|
176
|
|
|
)); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Handle generation of report |
|
182
|
|
|
* @param string|int $reportId |
|
183
|
|
|
* @param array<string, mixed> $data |
|
184
|
|
|
* @param string $filename |
|
185
|
|
|
* @param bool $save whether to save |
|
186
|
|
|
* @param string $format |
|
187
|
|
|
* @return string|null the full file path |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function handleReport( |
|
190
|
|
|
string|int $reportId, |
|
191
|
|
|
array $data = [], |
|
192
|
|
|
string $filename = '', |
|
193
|
|
|
bool $save = false, |
|
194
|
|
|
string $format = 'portrait' |
|
195
|
|
|
): ?string { |
|
196
|
|
|
$content = $this->getReportContent($reportId); |
|
197
|
|
|
|
|
198
|
|
|
if (empty($filename)) { |
|
199
|
|
|
$filename = uniqid('report_' . $reportId); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
if (substr($filename, 0, -4) !== '.pdf') { |
|
203
|
|
|
$filename .= '.pdf'; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
// Add some main informations |
|
207
|
|
|
$mainInformations = $this->getMainData(); |
|
208
|
|
|
$reportData = $data + $mainInformations; |
|
209
|
|
|
|
|
210
|
|
|
// If need debug |
|
211
|
|
|
$this->debugReport($reportId, $reportData); |
|
212
|
|
|
|
|
213
|
|
|
$html = $this->template->renderString($content, $reportData); |
|
214
|
|
|
$path = $this->fileHelper->getRootPath( |
|
215
|
|
|
$this->getSaveFilePath(), |
|
216
|
|
|
true |
|
217
|
|
|
); |
|
218
|
|
|
$filepath = sprintf('%s/%s', $path, $filename); |
|
219
|
|
|
|
|
220
|
|
|
$this->pdf->setContent($html) |
|
221
|
|
|
->setFilename($save ? $filepath : $filename) |
|
222
|
|
|
->setFormat($format) |
|
223
|
|
|
->generate(); |
|
224
|
|
|
|
|
225
|
|
|
if ($save) { |
|
226
|
|
|
$this->pdf->save(); |
|
227
|
|
|
} else { |
|
228
|
|
|
$this->pdf->download(); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
if ($save === false) { |
|
232
|
|
|
return null; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** @var FileInterface|null $handle */ |
|
236
|
|
|
$handle = $this->filesystem->get($filepath); |
|
237
|
|
|
|
|
238
|
|
|
if ($handle !== null && $handle->exists()) { |
|
239
|
|
|
return $handle->getPath(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return null; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Return the save path of the generated report |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function getSaveFilePath(): string |
|
250
|
|
|
{ |
|
251
|
|
|
return $this->config->get('platform.data_print_path', ''); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.