PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.16
VikBooking Hotel Booking Engine & PMS v1.8.16
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
← All changes | admin/helpers/report/report.php +1640 -1272 1.7.2 → 1.8.16 View file →
@@ -1,10 +1,10 @@
1 1 <?php
2 2 /**
3 3 * @package VikBooking
4 4 * @subpackage com_vikbooking
5 - * @author Alessio Gaggii - e4j - Extensionsforjoomla.com
6 - * @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved.
5 + * @author Alessio Gaggii - E4J srl
6 + * @copyright Copyright (C) 2025 E4J srl. All rights reserved.
7 7 * @license GNU General Public License version 2 or later; see LICENSE
8 8 * @link https://vikwp.com
9 9 */
10 10
@@ -15,1455 +15,1823 @@
15 15 */
16 16 #[\AllowDynamicProperties]
17 17 abstract class VikBookingReport
18 18 {
19 - /**
20 - * @var string
21 - */
22 - protected $reportName = '';
19 + /**
20 + * @var string
21 + */
22 + protected $reportName = '';
23 23
24 - /**
25 - * @var string
26 - */
27 - protected $reportFile = '';
24 + /**
25 + * @var string
26 + */
27 + protected $reportFile = '';
28 28
29 - /**
30 - * @var array
31 - */
32 - protected $reportFilters = [];
29 + /**
30 + * @var array
31 + */
32 + protected $reportFilters = [];
33 33
34 - /**
35 - * @var string
36 - */
37 - protected $reportScript = '';
34 + /**
35 + * @var string
36 + */
37 + protected $reportScript = '';
38 38
39 - /**
40 - * @var string
41 - */
42 - protected $warning = '';
39 + /**
40 + * @var string
41 + */
42 + protected $warning = '';
43 43
44 - /**
45 - * @var string
46 - */
47 - protected $error = '';
44 + /**
45 + * @var string
46 + */
47 + protected $error = '';
48 48
49 - /**
50 - * @var object
51 - */
52 - protected $dbo;
49 + /**
50 + * @var object
51 + */
52 + protected $dbo;
53 53
54 - /**
55 - * @var array
56 - */
57 - protected $cols = [];
54 + /**
55 + * @var array
56 + */
57 + protected $cols = [];
58 58
59 - /**
60 - * @var array
61 - */
62 - protected $rows = [];
59 + /**
60 + * @var array
61 + */
62 + protected $rows = [];
63 63
64 - /**
65 - * @var array
66 - */
67 - protected $footerRow = [];
64 + /**
65 + * @var array
66 + */
67 + protected $footerRow = [];
68 68
69 - /**
70 - * An array of custom options to be passed to the report.
71 - * Reports can use them before generating the report data.
72 - *
73 - * @var array
74 - *
75 - * @since 1.15.0 (J) - 1.5.0 (WP)
76 - */
77 - protected $options = [];
69 + /**
70 + * An array of custom options to be passed to the report.
71 + * Reports can use them before generating the report data.
72 + *
73 + * @var array
74 + *
75 + * @since 1.15.0 (J) - 1.5.0 (WP)
76 + */
77 + protected $options = [];
78 78
79 - /**
80 - * @var resource
81 - *
82 - * @since 1.16.1 (J) - 1.6.1 (WP)
83 - */
84 - protected $fp_export = null;
79 + /**
80 + * @var resource
81 + *
82 + * @since 1.16.1 (J) - 1.6.1 (WP)
83 + */
84 + protected $fp_export = null;
85 85
86 - /**
87 - * @var string
88 - *
89 - * @since 1.16.1 (J) - 1.6.1 (WP)
90 - */
91 - protected $csv_export_format = 'csv';
86 + /**
87 + * @var string
88 + *
89 + * @since 1.16.1 (J) - 1.6.1 (WP)
90 + */
91 + protected $csv_export_format = 'csv';
92 92
93 - /**
94 - * @var string
95 - *
96 - * @since 1.16.1 (J) - 1.6.1 (WP)
97 - */
98 - protected $csv_export_fname = '';
93 + /**
94 + * @var string
95 + *
96 + * @since 1.16.1 (J) - 1.6.1 (WP)
97 + */
98 + protected $csv_export_fname = '';
99 99
100 - /**
101 - * @var array
102 - *
103 - * @since 1.17.1 (J) - 1.7.1 (WP)
104 - */
105 - protected $actionData = [];
100 + /**
101 + * @var array
102 + *
103 + * @since 1.17.1 (J) - 1.7.1 (WP)
104 + */
105 + protected $actionData = [];
106 106
107 - /**
108 - * @var array
109 - *
110 - * @since 1.17.1 (J) - 1.7.1 (WP)
111 - */
112 - protected $resourceFiles = [];
107 + /**
108 + * @var array
109 + *
110 + * @since 1.17.1 (J) - 1.7.1 (WP)
111 + */
112 + protected $resourceFiles = [];
113 113
114 - /**
115 - * @var ?string
116 - *
117 - * @since 1.17.1 (J) - 1.7.1 (WP)
118 - */
119 - protected $scope = null;
114 + /**
115 + * @var ?string
116 + *
117 + * @since 1.17.1 (J) - 1.7.1 (WP)
118 + */
119 + protected $scope = null;
120 120
121 - /**
122 - * Class constructor should define the name of
123 - * the report and the filters to be displayed.
124 - */
125 - public function __construct()
126 - {
127 - $this->dbo = JFactory::getDbo();
128 - }
121 + /**
122 + * @var array
123 + *
124 + * @since 1.18.6 (J) - 1.8.6 (WP)
125 + */
126 + protected $reportRowClasses = [];
129 127
130 - /**
131 - * Extending Classes should define this method
132 - * to get the name of the report.
133 - */
134 - abstract public function getName();
128 + /**
129 + * Class constructor should define the name of
130 + * the report and the filters to be displayed.
131 + */
132 + public function __construct()
133 + {
134 + $this->dbo = JFactory::getDbo();
135 + }
135 136
136 - /**
137 - * Extending Classes should define this method
138 - * to get the name of class file.
139 - */
140 - abstract public function getFileName();
137 + /**
138 + * Extending Classes should define this method
139 + * to get the name of the report.
140 + */
141 + abstract public function getName();
141 142
142 - /**
143 - * Extending Classes should define this method
144 - * to get the filters of the report.
145 - */
146 - abstract public function getFilters();
143 + /**
144 + * Extending Classes should define this method
145 + * to get the name of class file.
146 + */
147 + abstract public function getFileName();
147 148
148 - /**
149 - * Extending Classes should define this method
150 - * to generate the report data (cols and rows).
151 - */
152 - abstract public function getReportData();
149 + /**
150 + * Extending Classes should define this method
151 + * to get the filters of the report.
152 + */
153 + abstract public function getFilters();
153 154
154 - /**
155 - * Main method to generate the report columns and rows. Stores on the current
156 - * resource the CSV lines and forces the browser to download the file. In case
157 - * of errors, the process is not terminated to let the View display the errors.
158 - *
159 - * @return void|bool script termination on success, false otherwise.
160 - *
161 - * @since 1.16.1 (J) - 1.6.1 (WP) drivers no longer need to implement it,
162 - * unless the driver needs to override it.
163 - */
164 - public function exportCSV()
165 - {
166 - // grab all report lines to export
167 - $csvlines = $this->getExportCSVLines();
155 + /**
156 + * Extending Classes should define this method
157 + * to generate the report data (cols and rows).
158 + */
159 + abstract public function getReportData();
168 160
169 - if (!$csvlines) {
170 - // no data to export
171 - return false;
172 - }
161 + /**
162 + * Allows the report to perform a preflight compatibility check with the environment.
163 + *
164 + * @return ?bool False if the report is not compatible with the current environment.
165 + *
166 + * @since 1.18.5 (J) - 1.8.5 (WP)
167 + */
168 + public function preflight()
169 + {
170 + return true;
171 + }
173 172
174 - // force the download of the CSV file
175 - $this->outputHeaders();
173 + /**
174 + * Allows the report to define the sub-filters template.
175 + *
176 + * @return ?string
177 + *
178 + * @since 1.18.6 (J) - 1.8.6 (WP)
179 + */
180 + public function getSubFiltersTpl()
181 + {
182 + return null;
183 + }
176 184
177 - /**
178 - * Trigger event to allow third-party plugins to set additional headers or contents to output.
179 - *
180 - * @since 1.16.8 (J) - 1.6.8 (WP)
181 - */
182 - VBOFactory::getPlatform()->getDispatcher()->trigger('onBeforeExportOutputCSV', [$this, $this->csv_export_format]);
185 + /**
186 + * Allows reports to define the default layout type to display.
187 + *
188 + * @param bool $hasChart True if the report has defined a chart.
189 + *
190 + * @return string
191 + *
192 + * @since 1.18.6 (J) - 1.8.6 (WP)
193 + */
194 + public function getDefaultLayoutType(bool $hasChart = false)
195 + {
196 + return $hasChart ? 'sheetnchart' : 'sheet';
197 + }
183 198
184 - // send lines to output
185 - $this->outputCSV($csvlines);
199 + /**
200 + * Returns the AJAX endpoint URL for the current report.
201 + *
202 + * @return string
203 + *
204 + * @since 1.18.6 (J) - 1.8.6 (WP)
205 + */
206 + public function getAjaxUrl()
207 + {
208 + return VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=invoke_report&report=' . $this->reportFile);
209 + }
186 210
187 - exit;
188 - }
211 + /**
212 + * Main method to generate the report columns and rows. Stores on the current
213 + * resource the CSV lines and forces the browser to download the file. In case
214 + * of errors, the process is not terminated to let the View display the errors.
215 + *
216 + * @return void|bool script termination on success, false otherwise.
217 + *
218 + * @since 1.16.1 (J) - 1.6.1 (WP) drivers no longer need to implement it,
219 + * unless the driver needs to override it.
220 + */
221 + public function exportCSV()
222 + {
223 + // grab all report lines to export
224 + $csvlines = $this->getExportCSVLines();
189 225
190 - /**
191 - * Returns the name to give to the CSV (or custom) file being exported.
192 - *
193 - * @param bool $cut_suffix if true the file name suffix will be cut off.
194 - * @param string $suffix the optional file name suffix, hence the extension.
195 - * @param bool $pretty if true, dashes will be converted to spaces and more.
196 - *
197 - * @return string
198 - *
199 - * @since 1.16.1 (J) - 1.6.1 (WP)
200 - */
201 - public function getExportCSVFileName($cut_suffix = false, $suffix = '.csv', $pretty = false)
202 - {
203 - if ($this->csv_export_fname) {
204 - // use the exact report file name set
205 - $export_fname = $this->csv_export_fname;
206 - } else {
207 - // use a generic file name
208 - $export_fname = date('Y-m-d_H.i.s') . '-' . $this->reportFile . '.csv';
209 - }
226 + if (!$csvlines) {
227 + // no data to export
228 + return false;
229 + }
210 230
211 - if ($cut_suffix) {
212 - // cut off the file name suffix
213 - if (!$suffix) {
214 - // detect file extension
215 - $suffix = substr($export_fname, strrpos($export_fname, '.'));
216 - }
217 - $export_fname = basename($export_fname, $suffix);
218 - }
231 + // force the download of the CSV file
232 + $this->outputHeaders();
219 233
220 - if ($pretty) {
221 - // get the default date separator char
222 - $datesep = VikBooking::getDateSeparator();
223 - // add a dash between a range of dates
224 - $export_fname = preg_replace("/([0-9])-([0-9])/i", '$1 - $2', $export_fname);
225 - // add an empty space between report name and channel name
226 - $export_fname = preg_replace("/([A-Z])-([A-Z])/i", '$1 $2', $export_fname);
227 - // add an empty space between report name and date
228 - $export_fname = preg_replace("/([A-Z0-9])-([0-9])/i", '$1 $2', $export_fname);
229 - // convert the dashes to the actual date separator char
230 - $export_fname = preg_replace("/([0-9])_([0-9])/i", '$1' . $datesep . '$2', $export_fname);
231 - }
234 + /**
235 + * Trigger event to allow third-party plugins to set additional headers or contents to output.
236 + *
237 + * @since 1.16.8 (J) - 1.6.8 (WP)
238 + */
239 + VBOFactory::getPlatform()->getDispatcher()->trigger('onBeforeExportOutputCSV', [$this, $this->csv_export_format]);
232 240
233 - return $export_fname;
234 - }
241 + // send lines to output
242 + $this->outputCSV($csvlines);
235 243
236 - /**
237 - * Sets the name to give to the CSV (or custom) file being exported.
238 - *
239 - * @param string $fname the full name of the file to export.
240 - *
241 - * @return self
242 - *
243 - * @since 1.16.1 (J) - 1.6.1 (WP)
244 - */
245 - public function setExportCSVFileName($fname)
246 - {
247 - $this->csv_export_fname = (string)$fname;
244 + exit;
245 + }
248 246
249 - return $this;
250 - }
247 + /**
248 + * Returns the name to give to the CSV (or custom) file being exported.
249 + *
250 + * @param bool $cut_suffix if true the file name suffix will be cut off.
251 + * @param string $suffix the optional file name suffix, hence the extension.
252 + * @param bool $pretty if true, dashes will be converted to spaces and more.
253 + *
254 + * @return string
255 + *
256 + * @since 1.16.1 (J) - 1.6.1 (WP)
257 + */
258 + public function getExportCSVFileName($cut_suffix = false, $suffix = '.csv', $pretty = false)
259 + {
260 + if ($this->csv_export_fname) {
261 + // use the exact report file name set
262 + $export_fname = $this->csv_export_fname;
263 + } else {
264 + // use a generic file name
265 + $export_fname = date('Y-m-d_H.i.s') . '-' . $this->reportFile . '.csv';
266 + }
251 267
252 - /**
253 - * Builds the list of the CSV lines to be exported from the current report data.
254 - *
255 - * @param bool $no_data true for actually not letting the report run.
256 - *
257 - * @return array list of CSV lines containing lists of CSV fields.
258 - *
259 - * @since 1.16.1 (J) - 1.6.1 (WP)
260 - */
261 - public function getExportCSVLines($no_data = false)
262 - {
263 - if (!$no_data && !$this->getReportData()) {
264 - // nothing to export
265 - return [];
266 - }
268 + if ($cut_suffix) {
269 + // cut off the file name suffix
270 + if (!$suffix) {
271 + // detect file extension
272 + $suffix = substr($export_fname, strrpos($export_fname, '.'));
273 + }
274 + $export_fname = basename($export_fname, $suffix);
275 + }
267 276
268 - // list of CSV lines
269 - $csvlines = [];
277 + if ($pretty) {
278 + // get the default date separator char
279 + $datesep = VikBooking::getDateSeparator();
280 + // add a dash between a range of dates
281 + $export_fname = preg_replace("/([0-9])-([0-9])/i", '$1 - $2', $export_fname);
282 + // add an empty space between report name and channel name
283 + $export_fname = preg_replace("/([A-Z])-([A-Z])/i", '$1 $2', $export_fname);
284 + // add an empty space between report name and date
285 + $export_fname = preg_replace("/([A-Z0-9])-([0-9])/i", '$1 $2', $export_fname);
286 + // convert the dashes to the actual date separator char
287 + $export_fname = preg_replace("/([0-9])_([0-9])/i", '$1' . $datesep . '$2', $export_fname);
288 + }
270 289
271 - if (!strcasecmp($this->csv_export_format, 'excel')) {
272 - // add instructions for Excel
290 + return $export_fname;
291 + }
273 292
274 - // UTF-8 BOM (not needed because we convert the encoding to UTF-16LE, which has BOM)
275 - // $csvlines[] = chr(0xEF) . chr(0xBB) . chr(0xBF);
293 + /**
294 + * Sets the name to give to the CSV (or custom) file being exported.
295 + *
296 + * @param string $fname the full name of the file to export.
297 + *
298 + * @return self
299 + *
300 + * @since 1.16.1 (J) - 1.6.1 (WP)
301 + */
302 + public function setExportCSVFileName($fname)
303 + {
304 + $this->csv_export_fname = (string)$fname;
276 305
277 - // define the separator
278 - $csvlines[] = "sep=;\n";
279 - }
306 + return $this;
307 + }
280 308
281 - // push the head of the CSV file
282 - $csvcols = [];
283 - foreach ($this->cols as $col) {
284 - if (!is_array($col) || isset($col['ignore_export'])) {
285 - // skip column
286 - continue;
287 - }
309 + /**
310 + * Builds the list of the CSV lines to be exported from the current report data.
311 + *
312 + * @param bool $no_data true for actually not letting the report run.
313 + *
314 + * @return array list of CSV lines containing lists of CSV fields.
315 + *
316 + * @since 1.16.1 (J) - 1.6.1 (WP)
317 + */
318 + public function getExportCSVLines($no_data = false)
319 + {
320 + if (!$no_data && !$this->getReportData()) {
321 + // nothing to export
322 + return [];
323 + }
288 324
289 - // push column label
290 - $csvcols[] = $this->encodeExportCSVField($col['label']);
291 - }
325 + // list of CSV lines
326 + $csvlines = [];
292 327
293 - // push all columns
294 - $csvlines[] = $csvcols;
328 + if (!strcasecmp($this->csv_export_format, 'excel')) {
329 + // add instructions for Excel
295 330
296 - // push the rows + footer row of the CSV file
297 - foreach (array_merge($this->rows, $this->footerRow) as $row) {
298 - $csvrow = [];
299 - foreach ($row as $field) {
300 - if (!is_array($field) || isset($field['ignore_export'])) {
301 - // skip value
302 - continue;
303 - }
331 + // UTF-8 BOM (not needed because we convert the encoding to UTF-16LE, which has BOM)
332 + // $csvlines[] = chr(0xEF) . chr(0xBB) . chr(0xBF);
304 333
305 - // build value for export
306 - $export_value = $field['value'];
307 - if (!isset($field['no_export_callback']) && !isset($field['no_csv_callback'])) {
308 - if (isset($field['export_callback']) && is_callable($field['export_callback'])) {
309 - // trigger closure callback to prepare the value for export
310 - $export_value = $field['export_callback']($field['value']);
311 - } elseif (isset($field['callback']) && is_callable($field['callback'])) {
312 - // trigger closure callback to prepare the value for export
313 - $export_value = $field['callback']($field['value']);
314 - }
315 - }
334 + // define the separator
335 + $csvlines[] = "sep=;\n";
336 + }
316 337
317 - // ensure no HTML is present
318 - $export_value = strip_tags($export_value);
338 + // push the head of the CSV file
339 + $csvcols = [];
340 + foreach ($this->cols as $col) {
341 + if (!is_array($col) || isset($col['ignore_export'])) {
342 + // skip column
343 + continue;
344 + }
319 345
320 - // apply encoding and transliteration, if needed
321 - $enc_trans_value = $this->encodeExportCSVField($export_value);
346 + // push column label
347 + $csvcols[] = $this->encodeExportCSVField($col['label']);
348 + }
322 349
323 - // make sure transliteration or encoding did not break the string
324 - if (empty($enc_trans_value) && !empty($export_value)) {
325 - // fallback to original and raw value
326 - $enc_trans_value = $export_value;
327 - }
350 + // push all columns
351 + $csvlines[] = $csvcols;
328 352
329 - // push row value
330 - $csvrow[] = $enc_trans_value;
331 - }
353 + // push the rows + footer row of the CSV file
354 + foreach (array_merge($this->rows, $this->footerRow) as $row) {
355 + $csvrow = [];
356 + foreach ($row as $field) {
357 + if (!is_array($field) || isset($field['ignore_export'])) {
358 + // skip value
359 + continue;
360 + }
332 361
333 - // push the whole row
334 - $csvlines[] = $csvrow;
335 - }
362 + // build value for export
363 + $export_value = $field['value'];
364 + if (!isset($field['no_export_callback']) && !isset($field['no_csv_callback'])) {
365 + if (is_callable($field['export_callback'] ?? null)) {
366 + // trigger closure callback to prepare the value for export
367 + $export_value = $field['export_callback']($field['value']);
368 + } elseif (is_callable($field['callback_export'] ?? null)) {
369 + // trigger closure callback to prepare the value for export
370 + $export_value = $field['callback_export']($field['value']);
371 + } elseif (is_callable($field['callback'] ?? null)) {
372 + // trigger closure callback to prepare the value for export
373 + $export_value = $field['callback']($field['value']);
374 + }
375 + }
336 376
337 - // return the list of lines
338 - return $csvlines;
339 - }
377 + // ensure no HTML is present
378 + $export_value = strip_tags($export_value);
340 379
341 - /**
342 - * Checks if a custom resource file pointer to export the file has been set.
343 - *
344 - * @return bool
345 - *
346 - * @since 1.16.1 (J) - 1.6.1 (WP)
347 - */
348 - public function hasExportHandler()
349 - {
350 - return is_resource($this->fp_export);
351 - }
380 + // apply encoding and transliteration, if needed
381 + $enc_trans_value = $this->encodeExportCSVField($export_value);
352 382
353 - /**
354 - * Returns the resource file pointer on which the CSV lines will be exported.
355 - *
356 - * @return resource
357 - *
358 - * @since 1.16.1 (J) - 1.6.1 (WP)
359 - */
360 - public function getExportCSVHandler()
361 - {
362 - if (is_null($this->fp_export)) {
363 - // set the default resource file pointer (output)
364 - $this->setExportCSVHandler();
365 - }
383 + // make sure transliteration or encoding did not break the string
384 + if (empty($enc_trans_value) && !empty($export_value)) {
385 + // fallback to original and raw value
386 + $enc_trans_value = $export_value;
387 + }
366 388
367 - return $this->fp_export;
368 - }
389 + // push row value
390 + $csvrow[] = $enc_trans_value;
391 + }
369 392
370 - /**
371 - * Sets the resource file pointer on which the CSV lines will be exported.
372 - * Useful in case the export should be made on a file rather than on output.
373 - *
374 - * @param string|resource $filename file path, identifier or resource.
375 - * @param string $mode the mode for opening the file pointer.
376 - *
377 - * @return self
378 - *
379 - * @since 1.16.1 (J) - 1.6.1 (WP)
380 - */
381 - public function setExportCSVHandler($filename = 'php://output', $mode = 'w')
382 - {
383 - // set resource file pointer
384 - if (is_resource($filename)) {
385 - $this->fp_export = $filename;
386 - } else {
387 - $this->fp_export = fopen($filename, $mode);
388 - }
393 + // push the whole row
394 + $csvlines[] = $csvrow;
395 + }
389 396
390 - return $this;
391 - }
397 + // return the list of lines
398 + return $csvlines;
399 + }
392 400
393 - /**
394 - * Sets the current type of CSV export format.
395 - *
396 - * @param string $format either "csv" or "excel".
397 - *
398 - * @return self
399 - *
400 - * @since 1.16.1 (J) - 1.6.1 (WP)
401 - */
402 - public function setExportCSVFormat($format)
403 - {
404 - $export_format = 'csv';
405 - if (!strcasecmp($format, 'excel')) {
406 - $export_format = 'excel';
407 - }
401 + /**
402 + * Checks if a custom resource file pointer to export the file has been set.
403 + *
404 + * @return bool
405 + *
406 + * @since 1.16.1 (J) - 1.6.1 (WP)
407 + */
408 + public function hasExportHandler()
409 + {
410 + return is_resource($this->fp_export);
411 + }
408 412
409 - // set format type
410 - $this->csv_export_format = $export_format;
413 + /**
414 + * Returns the resource file pointer on which the CSV lines will be exported.
415 + *
416 + * @return resource
417 + *
418 + * @since 1.16.1 (J) - 1.6.1 (WP)
419 + */
420 + public function getExportCSVHandler()
421 + {
422 + if (is_null($this->fp_export)) {
423 + // set the default resource file pointer (output)
424 + $this->setExportCSVHandler();
425 + }
411 426
412 - return $this;
413 - }
427 + return $this->fp_export;
428 + }
414 429
415 - /**
416 - * Sends to output the necessary headers to download the export file.
417 - *
418 - * @param array $headers optional additional or actual headers.
419 - * @param bool $replace if true, only the provided headers will be used.
420 - *
421 - * @return void
422 - */
423 - public function outputHeaders(array $headers = [], $replace = false)
424 - {
425 - foreach ($headers as $header) {
426 - // send custom header
427 - header($header);
428 - }
430 + /**
431 + * Sets the resource file pointer on which the CSV lines will be exported.
432 + * Useful in case the export should be made on a file rather than on output.
433 + *
434 + * @param string|resource $filename file path, identifier or resource.
435 + * @param string $mode the mode for opening the file pointer.
436 + *
437 + * @return self
438 + *
439 + * @since 1.16.1 (J) - 1.6.1 (WP)
440 + */
441 + public function setExportCSVHandler($filename = 'php://output', $mode = 'w')
442 + {
443 + // set resource file pointer
444 + if (is_resource($filename)) {
445 + $this->fp_export = $filename;
446 + } else {
447 + $this->fp_export = fopen($filename, $mode);
448 + }
429 449
430 - if ($replace) {
431 - // do not send any other header
432 - return;
433 - }
450 + return $this;
451 + }
434 452
435 - // force the download of the CSV file
436 - if (!strcasecmp($this->csv_export_format, 'excel')) {
437 - // file compatible with Excel
438 - header('Content-type: text/csv; charset=UTF-16LE');
439 - } else {
440 - // regular CSV
441 - header('Content-type: text/csv; charset=UTF-8');
442 - }
443 - header('Cache-Control: no-store, no-cache');
444 - header('Content-Disposition: attachment; filename="' . addslashes(basename($this->getExportCSVFileName(), '.csv')) . '.csv"');
445 - }
453 + /**
454 + * Sets the current type of CSV export format.
455 + *
456 + * @param string $format either "csv" or "excel".
457 + *
458 + * @return self
459 + *
460 + * @since 1.16.1 (J) - 1.6.1 (WP)
461 + */
462 + public function setExportCSVFormat($format)
463 + {
464 + $export_format = 'csv';
465 + if (!strcasecmp($format, 'excel')) {
466 + $export_format = 'excel';
467 + }
446 468
447 - /**
448 - * Writes the CSV lines to export onto the current resource handler.
449 - *
450 - * @return bool
451 - *
452 - * @since 1.16.1 (J) - 1.6.1 (WP)
453 - */
454 - public function outputCSV(array $csvlines)
455 - {
456 - // get resource file pointer
457 - $fp = $this->getExportCSVHandler();
469 + // set format type
470 + $this->csv_export_format = $export_format;
458 471
459 - if (!$fp) {
460 - // resource file pointer unavailable
461 - return false;
462 - }
472 + return $this;
473 + }
463 474
464 - // default CSV delimiter and enclosure
465 - $separator = ',';
466 - $enclosure = "\"";
467 - if (!strcasecmp($this->csv_export_format, 'excel')) {
468 - // for Excel we use the semicolon as separator and double quotes as enclosure
469 - $separator = ';';
470 - }
475 + /**
476 + * Sends to output the necessary headers to download the export file.
477 + *
478 + * @param array $headers optional additional or actual headers.
479 + * @param bool $replace if true, only the provided headers will be used.
480 + *
481 + * @return void
482 + */
483 + public function outputHeaders(array $headers = [], $replace = false)
484 + {
485 + foreach ($headers as $header) {
486 + // send custom header
487 + header($header);
488 + }
471 489
472 - // send lines to output
473 - foreach ($csvlines as $csvline) {
474 - if (is_string($csvline)) {
475 - // must be the first line instructions for the export format
476 - fputs($fp, $csvline);
490 + if ($replace) {
491 + // do not send any other header
492 + return;
493 + }
477 494
478 - // go to the next line
479 - continue;
480 - }
495 + // force the download of the CSV file
496 + if (!strcasecmp($this->csv_export_format, 'excel')) {
497 + // file compatible with Excel
498 + header('Content-type: text/csv; charset=UTF-16LE');
499 + } else {
500 + // regular CSV
501 + header('Content-type: text/csv; charset=UTF-8');
502 + }
503 + header('Cache-Control: no-store, no-cache');
504 + header('Content-Disposition: attachment; filename="' . addslashes(basename($this->getExportCSVFileName(), '.csv')) . '.csv"');
505 + }
481 506
482 - // put the array of values as a new CSV line
483 - fputcsv($fp, $csvline, $separator, $enclosure);
484 - }
507 + /**
508 + * Writes the CSV lines to export onto the current resource handler.
509 + *
510 + * @return bool
511 + *
512 + * @since 1.16.1 (J) - 1.6.1 (WP)
513 + */
514 + public function outputCSV(array $csvlines)
515 + {
516 + // get resource file pointer
517 + $fp = $this->getExportCSVHandler();
485 518
486 - // close the file pointer
487 - fclose($fp);
519 + if (!$fp) {
520 + // resource file pointer unavailable
521 + return false;
522 + }
488 523
489 - return true;
490 - }
524 + // default CSV delimiter and enclosure
525 + $separator = ',';
526 + $enclosure = '"';
527 + if (!strcasecmp($this->csv_export_format, 'excel')) {
528 + // for Excel we use the semicolon as separator and double quotes as enclosure
529 + $separator = ';';
530 + }
491 531
492 - /**
493 - * Loads a specific report class and returns its instance.
494 - * Should be called for instantiating any report sub-class.
495 - *
496 - * @param string $report the report file name (i.e. "revenue").
497 - *
498 - * @return mixed false or requested report object.
499 - */
500 - public static function getInstanceOf($report)
501 - {
502 - if (empty($report) || !is_string($report)) {
503 - return false;
504 - }
532 + // send lines to output
533 + foreach ($csvlines as $csvline) {
534 + if (is_string($csvline)) {
535 + // must be the first line instructions for the export format
536 + fputs($fp, $csvline);
505 537
506 - if (substr($report, -4) != '.php') {
507 - $report .= '.php';
508 - }
538 + // go to the next line
539 + continue;
540 + }
509 541
510 - $report_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . $report;
511 - $classname = 'VikBookingReport' . str_replace(' ', '', ucwords(str_replace('.php', '', str_replace('_', ' ', $report))));
542 + // put the array of values as a new CSV line
543 + fputcsv($fp, $csvline, $separator, $enclosure, $escape = '');
544 + }
512 545
513 - if (!is_file($report_path)) {
514 - /**
515 - * Trigger event to let other plugins register additional drivers.
516 - *
517 - * @since 1.16.0 (J) - 1.6.0 (WP)
518 - */
519 - $list = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadPmsReports');
520 - foreach ($list as $chunk) {
521 - if (!is_array($chunk) || !$chunk) {
522 - continue;
523 - }
524 - foreach ($chunk as $thirdp_report) {
525 - if (basename($thirdp_report) == $report) {
526 - // driver found
527 - $report_path = $thirdp_report;
528 - break;
529 - }
530 - }
531 - }
532 - }
546 + // close the file pointer
547 + fclose($fp);
533 548
534 - if (!is_file($report_path)) {
535 - // report driver file not found
536 - return false;
537 - }
549 + return true;
550 + }
538 551
539 - // load report
540 - require_once $report_path;
552 + /**
553 + * Loads a specific report class and returns its instance.
554 + * Should be called for instantiating any report sub-class.
555 + *
556 + * @param string $report the report file name (i.e. "revenue").
557 + *
558 + * @return mixed false or requested report object.
559 + */
560 + public static function getInstanceOf($report)
561 + {
562 + if (empty($report) || !is_string($report)) {
563 + return false;
564 + }
541 565
542 - if (class_exists($classname)) {
543 - // return the instance of the report object found
544 - return new $classname;
545 - }
566 + if (substr($report, -4) != '.php') {
567 + $report .= '.php';
568 + }
546 569
547 - return false;
548 - }
570 + $report_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . $report;
571 + $classname = 'VikBookingReport' . str_replace(' ', '', ucwords(str_replace('.php', '', str_replace('_', ' ', $report))));
549 572
550 - /**
551 - * Injects request variables for the report like if some filters were set.
552 - *
553 - * @param array $vars associative list of request vars to inject.
554 - *
555 - * @return void
556 - *
557 - * @since 1.16.1 (J) - 1.6.1 (WP) static-context used to construct the report object later.
558 - */
559 - public static function setRequestVars(array $vars)
560 - {
561 - foreach ($vars as $key => $value) {
562 - /**
563 - * For more safety across different platforms and versions (J3/J4 or WP)
564 - * we inject values in the super global array as well as in the input object.
565 - */
566 - VikRequest::setVar($key, $value, 'request');
567 - VikRequest::setVar($key, $value);
568 - }
569 - }
573 + if (!is_file($report_path)) {
574 + /**
575 + * Trigger event to let other plugins register additional drivers.
576 + *
577 + * @since 1.16.0 (J) - 1.6.0 (WP)
578 + */
579 + $list = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadPmsReports');
580 + foreach ($list as $chunk) {
581 + if (!is_array($chunk) || !$chunk) {
582 + continue;
583 + }
584 + foreach ($chunk as $thirdp_report) {
585 + if (basename($thirdp_report) == $report) {
586 + // driver found
587 + $report_path = $thirdp_report;
588 + break;
589 + }
590 + }
591 + }
592 + }
570 593
571 - /**
572 - * Proxy for object-context to inject request variables for the report.
573 - *
574 - * @param array $params associative list of request vars to inject.
575 - *
576 - * @return self
577 - */
578 - public function injectParams($params)
579 - {
580 - if (is_array($params) && $params) {
581 - self::setRequestVars($params);
582 - }
594 + if (!is_file($report_path)) {
595 + // report driver file not found
596 + return false;
597 + }
583 598
584 - return $this;
585 - }
599 + // load report
600 + require_once $report_path;
586 601
587 - /**
588 - * Loads Charts CSS/JS assets.
589 - *
590 - * @return self
591 - */
592 - public function loadChartsAssets()
593 - {
594 - $document = JFactory::getDocument();
595 - $document->addStyleSheet(VBO_ADMIN_URI . 'resources/Chart.min.css', ['version' => VIKBOOKING_SOFTWARE_VERSION]);
596 - $document->addScript(VBO_ADMIN_URI . 'resources/Chart.min.js', ['version' => VIKBOOKING_SOFTWARE_VERSION]);
602 + if (!class_exists($classname)) {
603 + // report class does not exist
604 + return false;
605 + }
597 606
598 - return $this;
599 - }
607 + // instantiate the report object
608 + $reportInstance = new $classname;
600 609
601 - /**
602 - * Loads the jQuery UI Datepicker.
603 - * Method used only by sub-classes.
604 - *
605 - * @return self
606 - */
607 - protected function loadDatePicker()
608 - {
609 - $vbo_app = VikBooking::getVboApplication();
610 - $vbo_app->loadDatePicker();
610 + // ensure the report is compatible with the current environment
611 + if ($reportInstance->preflight() === false) {
612 + // the report is not compatible
613 + return false;
614 + }
611 615
612 - return $this;
613 - }
616 + // return the instance of the report object found
617 + return $reportInstance;
618 + }
614 619
615 - /**
616 - * Applies the proper encoding to the field being added to
617 - * the CSV lines for export, depending on CSV or Excel.
618 - *
619 - * @param string $field the value being added to the export line.
620 - *
621 - * @return string either the original or the properly encoded field.
622 - *
623 - * @since 1.16.1 (J) - 1.6.1 (WP)
624 - * @since 1.16.8 (J) - 1.6.8 (WP) introduced hook to allow to manipulate encoding.
625 - */
626 - protected function encodeExportCSVField($field)
627 - {
628 - /**
629 - * Trigger event to allow third-party plugins to manipulate encoding,
630 - * in case certain dependencies are not available, such as "mb" support,
631 - * PECL intl for "transliterator_transliterate" or "iconv" to ASCII.
632 - *
633 - * @since 1.16.8 (J) - 1.6.8 (WP)
634 - */
635 - $apply_encoding = VBOFactory::getPlatform()->getDispatcher()->filter('onBeforeEncodingCSVField', [&$field, $this->csv_export_format]);
620 + /**
621 + * Injects request variables for the report like if some filters were set.
622 + *
623 + * @param array $vars associative list of request vars to inject.
624 + *
625 + * @return void
626 + *
627 + * @since 1.16.1 (J) - 1.6.1 (WP) static-context used to construct the report object later.
628 + */
629 + public static function setRequestVars(array $vars)
630 + {
631 + foreach ($vars as $key => $value) {
632 + /**
633 + * For more safety across different platforms and versions (J3/J4 or WP)
634 + * we inject values in the super global array as well as in the input object.
635 + */
636 + VikRequest::setVar($key, $value, 'request');
637 + VikRequest::setVar($key, $value);
638 + }
639 + }
636 640
637 - if (in_array(false, $apply_encoding, true)) {
638 - // the hook ordered to not proceed with applying any encoding
639 - return $field;
640 - }
641 + /**
642 + * Proxy for object-context to inject request variables for the report.
643 + *
644 + * @param array $params associative list of request vars to inject.
645 + *
646 + * @return self
647 + */
648 + public function injectParams($params)
649 + {
650 + if (is_array($params) && $params) {
651 + self::setRequestVars($params);
652 + }
641 653
642 - if (!is_string($field) || !strcasecmp($this->csv_export_format, 'csv')) {
643 - // apply no encoding in case of regular CSV or if non-string data type
644 - return $field;
645 - }
654 + return $this;
655 + }
646 656
647 - // process the Excel-like string field
648 - if (preg_match('/[\\x80-\\xff]/', $field)) {
649 - // UTF-8 encoding detected
650 - if (function_exists('transliterator_transliterate')) {
651 - // if Transliterator is available (PECL intl >= 2.0.0), transliterate to ASCII
652 - $field = transliterator_transliterate('Any-Latin; Latin-ASCII;', $field);
653 - }
657 + /**
658 + * Loads Charts CSS/JS assets.
659 + *
660 + * @return self
661 + */
662 + public function loadChartsAssets()
663 + {
664 + $document = JFactory::getDocument();
665 + $document->addStyleSheet(VBO_ADMIN_URI . 'resources/Chart.min.css', ['version' => VIKBOOKING_SOFTWARE_VERSION]);
666 + $document->addScript(VBO_ADMIN_URI . 'resources/Chart.min.js', ['version' => VIKBOOKING_SOFTWARE_VERSION]);
654 667
655 - // attempt to convert UTF-8 to ASCII to support currencies
656 - $field = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $field);
657 - }
668 + return $this;
669 + }
658 670
659 - if (!function_exists('mb_convert_encoding')) {
660 - // abort to prevent server errors
661 - return $field;
662 - }
671 + /**
672 + * Loads the jQuery UI Datepicker.
673 + * Method used only by sub-classes.
674 + *
675 + * @return self
676 + */
677 + protected function loadDatePicker()
678 + {
679 + $vbo_app = VikBooking::getVboApplication();
680 + $vbo_app->loadDatePicker();
663 681
664 - // convert encoding to UTF-16LE (low-endian with BOM)
665 - return mb_convert_encoding($field, 'UTF-16LE', ['ASCII', 'UTF-8', 'ISO-8859-1']);
666 - }
682 + return $this;
683 + }
667 684
668 - /**
669 - * Loads all the rooms in VBO and returns the array.
670 - *
671 - * @return array
672 - */
673 - protected function getRooms()
674 - {
675 - $q = "SELECT * FROM `#__vikbooking_rooms` ORDER BY `name` ASC;";
676 - $this->dbo->setQuery($q);
677 - $rooms = $this->dbo->loadAssocList();
685 + /**
686 + * Parses a report row into an associative list of key-value pairs.
687 + *
688 + * @param array $row The report row to parse.
689 + * @param bool $format True to format values with callbacks.
690 + *
691 + * @return array Associative list of row keys and values.
692 + *
693 + * @since 1.18.4 (J) - 1.8.4 (WP)
694 + */
695 + protected function getAssocRowFields(array $row, bool $format = false)
696 + {
697 + $row_data = [];
678 698
679 - return $rooms;
680 - }
699 + foreach ($row as $field) {
700 + $field_val = $field['value'];
701 + if (!isset($field['callback_export']) && isset($field['callback'])) {
702 + $field['callback_export'] = $field['callback'];
703 + }
704 + if ($format === true && !($field['no_export_callback'] ?? 0) && is_callable($field['callback_export'] ?? null)) {
705 + $field_val = $field['callback_export']($field_val);
706 + }
707 + $row_data[$field['key']] = $field_val;
708 + }
681 709
682 - /**
683 - * Loads all the rate plans in VBO and returns the array.
684 - *
685 - * @return array
686 - *
687 - * @since 1.15.0 (J) - 1.5.0 (WP)
688 - */
689 - protected function getRatePlans()
690 - {
691 - $q = "SELECT * FROM `#__vikbooking_prices` ORDER BY `name` ASC;";
692 - $this->dbo->setQuery($q);
693 - $rplans = $this->dbo->loadAssocList();
710 + return $row_data;
711 + }
694 712
695 - return VikBooking::sortRatePlans($rplans);
696 - }
713 + /**
714 + * Gets the CSS classes for each report row.
715 + *
716 + * @return array
717 + *
718 + * @since 1.18.6 (J) - 1.8.6 (WP)
719 + */
720 + public function getReportRowClasses()
721 + {
722 + return $this->reportRowClasses;
723 + }
697 724
698 - /**
699 - * Returns the number of total units for all rooms, or for a specific room.
700 - * By default, the rooms unpublished are skipped, and all rooms are used.
701 - *
702 - * @param [mixed] $idroom int or array.
703 - * @param [int] $published true or false.
704 - *
705 - * @return int
706 - */
707 - protected function countRooms($idroom = 0, $published = 1)
708 - {
709 - $clauses = [];
710 - if (is_int($idroom) && $idroom > 0) {
711 - $clauses[] = "`id`=".(int)$idroom;
712 - } elseif (is_array($idroom) && $idroom) {
713 - $clauses[] = "`id` IN (" . implode(', ', $idroom) . ")";
714 - }
715 - if ($published) {
716 - $clauses[] = "`avail`=1";
717 - }
725 + /**
726 + * Sets the CSS classes for each report row.
727 + *
728 + * @param array $rowClasses List of row class values.
729 + *
730 + * @return void
731 + *
732 + * @since 1.18.6 (J) - 1.8.6 (WP)
733 + */
734 + protected function setReportRowClasses(array $rowClasses)
735 + {
736 + $this->reportRowClasses = $rowClasses;
718 737
719 - $q = "SELECT SUM(`units`) FROM `#__vikbooking_rooms`".($clauses ? " WHERE ".implode(' AND ', $clauses) : "").";";
720 - $this->dbo->setQuery($q);
721 - $totrooms = (int)$this->dbo->loadResult();
738 + return;
739 + }
722 740
723 - return $totrooms;
724 - }
741 + /**
742 + * Applies the proper encoding to the field being added to
743 + * the CSV lines for export, depending on CSV or Excel.
744 + *
745 + * @param string $field the value being added to the export line.
746 + *
747 + * @return string either the original or the properly encoded field.
748 + *
749 + * @since 1.16.1 (J) - 1.6.1 (WP)
750 + * @since 1.16.8 (J) - 1.6.8 (WP) introduced hook to allow to manipulate encoding.
751 + */
752 + protected function encodeExportCSVField($field)
753 + {
754 + /**
755 + * Trigger event to allow third-party plugins to manipulate encoding,
756 + * in case certain dependencies are not available, such as "mb" support,
757 + * PECL intl for "transliterator_transliterate" or "iconv" to ASCII.
758 + *
759 + * @since 1.16.8 (J) - 1.6.8 (WP)
760 + */
761 + $apply_encoding = VBOFactory::getPlatform()->getDispatcher()->filter('onBeforeEncodingCSVField', [&$field, $this->csv_export_format]);
725 762
726 - /**
727 - * Concatenates the JavaScript rules.
728 - * Method used only by sub-classes.
729 - *
730 - * @param string $str
731 - *
732 - * @return self
733 - */
734 - protected function setScript($str)
735 - {
736 - $this->reportScript .= $str."\n";
763 + if (in_array(false, $apply_encoding, true)) {
764 + // the hook ordered to not proceed with applying any encoding
765 + return $field;
766 + }
737 767
738 - return $this;
739 - }
768 + if (!is_string($field) || !strcasecmp($this->csv_export_format, 'csv')) {
769 + // apply no encoding in case of regular CSV or if non-string data type
770 + return $field;
771 + }
740 772
741 - /**
742 - * Gets the current script string.
743 - *
744 - * @return string
745 - */
746 - public function getScript()
747 - {
748 - return rtrim($this->reportScript, "\n");
749 - }
773 + // process the Excel-like string field
774 + if (preg_match('/[\\x80-\\xff]/', $field)) {
775 + // UTF-8 encoding detected
776 + if (function_exists('transliterator_transliterate')) {
777 + // if Transliterator is available (PECL intl >= 2.0.0), transliterate to ASCII
778 + $field = transliterator_transliterate('Any-Latin; Latin-ASCII;', $field);
779 + }
750 780
751 - /**
752 - * Returns the date format in VBO for date, jQuery UI, Joomla/WordPress.
753 - * The visibility of this method should be public for anyone who needs it.
754 - *
755 - * @param string $type
756 - *
757 - * @return string
758 - */
759 - public function getDateFormat($type = 'date')
760 - {
761 - $nowdf = VikBooking::getDateFormat();
762 - if ($nowdf == "%d/%m/%Y") {
763 - $df = 'd/m/Y';
764 - $juidf = 'dd/mm/yy';
765 - } elseif ($nowdf == "%m/%d/%Y") {
766 - $df = 'm/d/Y';
767 - $juidf = 'mm/dd/yy';
768 - } else {
769 - $df = 'Y/m/d';
770 - $juidf = 'yy/mm/dd';
771 - }
781 + // attempt to convert UTF-8 to ASCII to support currencies
782 + $field = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $field);
783 + }
772 784
773 - switch ($type) {
774 - case 'jui':
775 - return $juidf;
776 - case 'joomla':
777 - case 'wordpress':
778 - return $nowdf;
779 - default:
780 - return $df;
781 - }
782 - }
785 + if (!function_exists('mb_convert_encoding')) {
786 + // abort to prevent server errors
787 + return $field;
788 + }
783 789
784 - /**
785 - * Returns the translated weekday.
786 - * Uses the back-end language definitions.
787 - *
788 - * @param int $wday
789 - * @param string $type use 'long' for the full name of the week, short for the 3-char version
790 - *
791 - * @return string
792 - */
793 - protected function getWdayString($wday, $type = 'long')
794 - {
795 - $wdays_map_long = [
796 - JText::translate('VBWEEKDAYZERO'),
797 - JText::translate('VBWEEKDAYONE'),
798 - JText::translate('VBWEEKDAYTWO'),
799 - JText::translate('VBWEEKDAYTHREE'),
800 - JText::translate('VBWEEKDAYFOUR'),
801 - JText::translate('VBWEEKDAYFIVE'),
802 - JText::translate('VBWEEKDAYSIX')
803 - ];
790 + // convert encoding to UTF-16LE (low-endian with BOM)
791 + return mb_convert_encoding($field, 'UTF-16LE', ['ASCII', 'UTF-8', 'ISO-8859-1']);
792 + }
804 793
805 - $wdays_map_short = [
806 - JText::translate('VBSUN'),
807 - JText::translate('VBMON'),
808 - JText::translate('VBTUE'),
809 - JText::translate('VBWED'),
810 - JText::translate('VBTHU'),
811 - JText::translate('VBFRI'),
812 - JText::translate('VBSAT')
813 - ];
794 + /**
795 + * Used to apply transliteration over UTF-8 characters for having only latins chars.
796 + *
797 + * @param string $value The original string value.
798 + *
799 + * @return string The transliterated string or the original string.
800 + *
801 + * @since 1.18.0 (J) - 1.8.0 (WP)
802 + */
803 + protected function transliterateToAscii(string $value)
804 + {
805 + if (!preg_match('/[\\x80-\\xff]/', $value)) {
806 + // no UTF-8 encoding (special character) detected
807 + return $value;
808 + }
814 809
815 - if ($type != 'long') {
816 - return isset($wdays_map_short[(int)$wday]) ? $wdays_map_short[(int)$wday] : '';
817 - }
810 + // make a safe copy of the original string
811 + $copy_value = $value;
818 812
819 - return isset($wdays_map_long[(int)$wday]) ? $wdays_map_long[(int)$wday] : '';
820 - }
813 + if (function_exists('transliterator_transliterate')) {
814 + // if Transliterator is available (PECL intl >= 2.0.0), transliterate to ASCII
815 + $copy_value = transliterator_transliterate('Any-Latin; Latin-ASCII;', $copy_value);
816 + }
821 817
822 - /**
823 - * Sets the columns for this report.
824 - *
825 - * @param array $arr
826 - *
827 - * @return self
828 - */
829 - public function setReportCols($arr)
830 - {
831 - $this->cols = $arr;
818 + if (function_exists('iconv')) {
819 + // attempt to convert UTF-8 to ASCII
820 + $copy_value = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $copy_value);
821 + }
832 822
833 - return $this;
834 - }
823 + if (empty($copy_value)) {
824 + // revert to the original string value
825 + $copy_value = $value;
826 + }
835 827
836 - /**
837 - * Returns the columns for this report.
838 - * Should be called after getReportData()
839 - * or the returned array will be empty.
840 - *
841 - * @return array
842 - */
843 - public function getReportCols()
844 - {
845 - return $this->cols;
846 - }
828 + return $copy_value;
829 + }
847 830
848 - /**
849 - * Sorts the rows of the report by key.
850 - *
851 - * @param string $krsort the key attribute of the array pairs
852 - * @param string $krorder ascending (ASC) or descending (DESC)
853 - *
854 - * @return void
855 - */
856 - protected function sortRows($krsort, $krorder)
857 - {
858 - if (empty($krsort) || !$this->rows) {
859 - return;
860 - }
831 + /**
832 + * Loads all the rooms in VBO and returns the array.
833 + *
834 + * @return array
835 + */
836 + protected function getRooms()
837 + {
838 + $q = "SELECT * FROM `#__vikbooking_rooms` ORDER BY `name` ASC;";
839 + $this->dbo->setQuery($q);
840 + $rooms = $this->dbo->loadAssocList();
861 841
862 - $map = [];
863 - foreach ($this->rows as $k => $row) {
864 - foreach ($row as $kk => $v) {
865 - if (isset($v['key']) && $v['key'] == $krsort) {
866 - $map[$k] = $v['value'];
867 - }
868 - }
869 - }
870 - if (!$map) {
871 - return;
872 - }
842 + return $rooms;
843 + }
873 844
874 - if ($krorder == 'ASC') {
875 - asort($map);
876 - } else {
877 - arsort($map);
878 - }
845 + /**
846 + * Loads all the rate plans in VBO and returns the array.
847 + *
848 + * @return array
849 + *
850 + * @since 1.15.0 (J) - 1.5.0 (WP)
851 + */
852 + protected function getRatePlans()
853 + {
854 + $q = "SELECT * FROM `#__vikbooking_prices` ORDER BY `name` ASC;";
855 + $this->dbo->setQuery($q);
856 + $rplans = $this->dbo->loadAssocList();
879 857
880 - $sorted = [];
881 - foreach ($map as $k => $v) {
882 - $sorted[$k] = $this->rows[$k];
883 - }
858 + return VikBooking::sortRatePlans($rplans);
859 + }
884 860
885 - $this->rows = $sorted;
886 - }
861 + /**
862 + * Returns the number of total units for all rooms, or for a specific room.
863 + * By default, the rooms unpublished are skipped, and all rooms are used.
864 + *
865 + * @param [mixed] $idroom int or array.
866 + * @param [int] $published true or false.
867 + *
868 + * @return int
869 + */
870 + protected function countRooms($idroom = 0, $published = 1)
871 + {
872 + $clauses = [];
873 + if (is_int($idroom) && $idroom > 0) {
874 + $clauses[] = "`id`=".(int)$idroom;
875 + } elseif (is_array($idroom) && $idroom) {
876 + $clauses[] = "`id` IN (" . implode(', ', $idroom) . ")";
877 + }
878 + if ($published) {
879 + $clauses[] = "`avail`=1";
880 + }
887 881
888 - /**
889 - * Sets the rows for this report.
890 - *
891 - * @param array $arr
892 - *
893 - * @return self
894 - */
895 - public function setReportRows($arr)
896 - {
897 - $this->rows = $arr;
882 + $q = "SELECT SUM(`units`) FROM `#__vikbooking_rooms`".($clauses ? " WHERE ".implode(' AND ', $clauses) : "").";";
883 + $this->dbo->setQuery($q);
884 + $totrooms = (int)$this->dbo->loadResult();
898 885
899 - return $this;
900 - }
886 + return $totrooms;
887 + }
901 888
902 - /**
903 - * Returns the rows for this report.
904 - * Should be called after getReportData()
905 - * or the returned array will be empty.
906 - *
907 - * @return array
908 - */
909 - public function getReportRows()
910 - {
911 - return $this->rows;
912 - }
889 + /**
890 + * Concatenates the JavaScript rules.
891 + * Method used only by sub-classes.
892 + *
893 + * @param string $str
894 + *
895 + * @return self
896 + */
897 + protected function setScript($str)
898 + {
899 + $this->reportScript .= $str."\n";
913 900
914 - /**
915 - * This method returns one or more rows (given the depth) generated by
916 - * the current report invoked. It is useful to clean up the callbacks
917 - * of the various cell-rows, to obtain a parsable result.
918 - * Can be called as first method, by skipping also getReportData().
919 - *
920 - * @param int $depth how many records to obtain, null for all.
921 - *
922 - * @return array the queried report value in the given depth.
923 - *
924 - * @uses getReportData()
925 - */
926 - public function getReportValues($depth = null)
927 - {
928 - if (!$this->rows && !$this->getReportData()) {
929 - return [];
930 - }
901 + return $this;
902 + }
931 903
932 - $report_values = [];
904 + /**
905 + * Gets the current script string.
906 + *
907 + * @return string
908 + */
909 + public function getScript()
910 + {
911 + return rtrim($this->reportScript, "\n");
912 + }
933 913
934 - foreach ($this->rows as $rk => $row) {
935 - $report_values[$rk] = [];
936 - foreach ($row as $col => $coldata) {
937 - $display_value = $coldata['value'];
938 - if (isset($coldata['callback']) && is_callable($coldata['callback'])) {
939 - // launch callback
940 - $display_value = $coldata['callback']($coldata['value']);
941 - }
942 - // push column value
943 - $report_values[$rk][$coldata['key']] = [
944 - 'value' => $coldata['value'],
945 - 'display_value' => $display_value,
946 - ];
947 - /**
948 - * We also pass along any reserved key for this row-data.
949 - *
950 - * @since 1.15.0 (J) - 1.5.0 (WP)
951 - */
952 - foreach ($coldata as $res_key => $data_val) {
953 - if (substr($res_key, 0, 1) == '_') {
954 - // push this reserved key
955 - $report_values[$rk][$coldata['key']][$res_key] = $data_val;
956 - }
957 - }
958 - }
959 - }
914 + /**
915 + * Returns the date format in VBO for date, jQuery UI, Joomla/WordPress.
916 + * The visibility of this method should be public for anyone who needs it.
917 + *
918 + * @param string $type
919 + *
920 + * @return string
921 + */
922 + public function getDateFormat($type = 'date')
923 + {
924 + $nowdf = VikBooking::getDateFormat();
925 + if ($nowdf == "%d/%m/%Y") {
926 + $df = 'd/m/Y';
927 + $juidf = 'dd/mm/yy';
928 + } elseif ($nowdf == "%m/%d/%Y") {
929 + $df = 'm/d/Y';
930 + $juidf = 'mm/dd/yy';
931 + } else {
932 + $df = 'Y/m/d';
933 + $juidf = 'yy/mm/dd';
934 + }
960 935
961 - if (!$report_values) {
962 - return [];
963 - }
936 + switch ($type) {
937 + case 'jui':
938 + return $juidf;
939 + case 'joomla':
940 + case 'wordpress':
941 + return $nowdf;
942 + default:
943 + return $df;
944 + }
945 + }
964 946
965 - if ($depth === 1) {
966 - // get an associative array with the first row calculated
967 - return $report_values[0];
968 - }
947 + /**
948 + * Returns the translated weekday.
949 + * Uses the back-end language definitions.
950 + *
951 + * @param int $wday
952 + * @param string $type use 'long' for the full name of the week, short for the 3-char version
953 + *
954 + * @return string
955 + */
956 + protected function getWdayString($wday, $type = 'long')
957 + {
958 + $wdays_map_long = [
959 + JText::translate('VBWEEKDAYZERO'),
960 + JText::translate('VBWEEKDAYONE'),
961 + JText::translate('VBWEEKDAYTWO'),
962 + JText::translate('VBWEEKDAYTHREE'),
963 + JText::translate('VBWEEKDAYFOUR'),
964 + JText::translate('VBWEEKDAYFIVE'),
965 + JText::translate('VBWEEKDAYSIX')
966 + ];
969 967
970 - if (is_int($depth) && $depth > 0 && count($report_values) >= $depth) {
971 - // get the requested portion of the array
972 - return array_slice($report_values, 0, $depth);
973 - }
968 + $wdays_map_short = [
969 + JText::translate('VBSUN'),
970 + JText::translate('VBMON'),
971 + JText::translate('VBTUE'),
972 + JText::translate('VBWED'),
973 + JText::translate('VBTHU'),
974 + JText::translate('VBFRI'),
975 + JText::translate('VBSAT')
976 + ];
974 977
975 - return $report_values;
976 - }
978 + if ($type != 'long') {
979 + return isset($wdays_map_short[(int)$wday]) ? $wdays_map_short[(int)$wday] : '';
980 + }
977 981
978 - /**
979 - * Maps the columns labels to an associative array to be used for the values.
980 - *
981 - * @return array associative list of column keys and related values.
982 - */
983 - public function getColumnsValues()
984 - {
985 - if (!$this->cols) {
986 - return [];
987 - }
982 + return isset($wdays_map_long[(int)$wday]) ? $wdays_map_long[(int)$wday] : '';
983 + }
988 984
989 - $col_values = [];
985 + /**
986 + * Sets the columns for this report.
987 + *
988 + * @param array $arr
989 + *
990 + * @return self
991 + */
992 + public function setReportCols($arr)
993 + {
994 + $this->cols = $arr;
990 995
991 - foreach ($this->cols as $col) {
992 - if (!isset($col['key'])) {
993 - continue;
994 - }
995 - $col_values[$col['key']] = $col;
996 - unset($col_values[$col['key']]['key']);
997 - }
996 + return $this;
997 + }
998 998
999 - return $col_values;
1000 - }
999 + /**
1000 + * Returns the columns for this report.
1001 + * Should be called after getReportData()
1002 + * or the returned array will be empty.
1003 + *
1004 + * @return array
1005 + */
1006 + public function getReportCols()
1007 + {
1008 + return $this->cols;
1009 + }
1001 1010
1002 - /**
1003 - * Gets a property defined by the report. Useful to get custom
1004 - * properties set up by a specific report maybe for the Chart.
1005 - *
1006 - * @param string $property the name of the property needed.
1007 - * @param mixed $def default value to return.
1008 - *
1009 - * @return mixed false on failure, property requested otherwise.
1010 - */
1011 - public function getProperty($property, $def = false)
1012 - {
1013 - if (isset($this->{$property})) {
1014 - return $this->{$property};
1015 - }
1011 + /**
1012 + * Sorts the rows of the report by key.
1013 + *
1014 + * @param string $krsort the key attribute of the array pairs
1015 + * @param string $krorder ascending (ASC) or descending (DESC)
1016 + *
1017 + * @return void
1018 + */
1019 + protected function sortRows($krsort, $krorder)
1020 + {
1021 + if (empty($krsort) || !$this->rows) {
1022 + return;
1023 + }
1016 1024
1017 - return $def;
1018 - }
1025 + $map = [];
1026 + foreach ($this->rows as $k => $row) {
1027 + foreach ($row as $kk => $v) {
1028 + if (isset($v['key']) && $v['key'] == $krsort) {
1029 + $map[$k] = $v['value'];
1030 + }
1031 + }
1032 + }
1033 + if (!$map) {
1034 + return;
1035 + }
1019 1036
1020 - /**
1021 - * Counts the number of days of difference between two timestamps.
1022 - *
1023 - * @param int $to_ts the target end date timestamp.
1024 - * @param int $from_ts the starting date timestamp.
1025 - *
1026 - * @return int the days of difference between from and to timestamps.
1027 - */
1028 - public function countDaysTo($to_ts, $from_ts = 0)
1029 - {
1030 - if (empty($from_ts)) {
1031 - $from_ts = time();
1032 - }
1037 + if ($krorder == 'ASC') {
1038 + asort($map);
1039 + } else {
1040 + arsort($map);
1041 + }
1033 1042
1034 - // whether DateTime can be used
1035 - $usedt = false;
1043 + $sorted = [];
1044 + foreach ($map as $k => $v) {
1045 + $sorted[$k] = $this->rows[$k];
1046 + }
1036 1047
1037 - if (class_exists('DateTime')) {
1038 - $from_date = new DateTime(date('Y-m-d', $from_ts));
1039 - if (method_exists($from_date, 'diff')) {
1040 - $usedt = true;
1041 - }
1042 - }
1048 + $this->rows = $sorted;
1049 + }
1043 1050
1044 - if ($usedt) {
1045 - $to_date = new DateTime(date('Y-m-d', $to_ts));
1046 - $daysdiff = (int)$from_date->diff($to_date)->format('%a');
1047 - if ($to_ts < $from_ts) {
1048 - // we need a negative integer number
1049 - $daysdiff = $daysdiff - ($daysdiff * 2);
1050 - }
1051 - return $daysdiff;
1052 - }
1051 + /**
1052 + * Sets the rows for this report.
1053 + *
1054 + * @param array $arr
1055 + *
1056 + * @return self
1057 + */
1058 + public function setReportRows($arr)
1059 + {
1060 + $this->rows = $arr;
1053 1061
1054 - return (int)round(($to_ts - $from_ts) / 86400);
1055 - }
1062 + return $this;
1063 + }
1056 1064
1057 - /**
1058 - * Counts the average difference between two integers.
1059 - *
1060 - * @param int $in_days_from days to the lowest timestamp.
1061 - * @param int $in_days_to days to the highest timestamp.
1062 - *
1063 - * @return int the average number between the two values.
1064 - */
1065 - public function countAverageDays($in_days_from, $in_days_to)
1066 - {
1067 - return (int)floor(($in_days_from + $in_days_to) / 2);
1068 - }
1065 + /**
1066 + * Returns the rows for this report.
1067 + * Should be called after getReportData()
1068 + * or the returned array will be empty.
1069 + *
1070 + * @return array
1071 + */
1072 + public function getReportRows()
1073 + {
1074 + return $this->rows;
1075 + }
1069 1076
1070 - /**
1071 - * Sets the footer row (the totals) for this report.
1072 - *
1073 - * @param array $arr
1074 - *
1075 - * @return self
1076 - */
1077 - protected function setReportFooterRow($arr)
1078 - {
1079 - $this->footerRow = $arr;
1077 + /**
1078 + * This method returns one or more rows (given the depth) generated by
1079 + * the current report invoked. It is useful to clean up the callbacks
1080 + * of the various cell-rows, to obtain a parsable result.
1081 + * Can be called as first method, by skipping also getReportData().
1082 + *
1083 + * @param ?int $depth how many records to obtain, null for all.
1084 + *
1085 + * @return array the queried report value in the given depth.
1086 + *
1087 + * @uses getReportData()
1088 + */
1089 + public function getReportValues(?int $depth = null)
1090 + {
1091 + if (!$this->rows && !$this->getReportData()) {
1092 + return [];
1093 + }
1080 1094
1081 - return $this;
1082 - }
1095 + $report_values = [];
1083 1096
1084 - /**
1085 - * Returns the footer row for this report.
1086 - * Should be called after getReportData()
1087 - * or the returned array will be empty.
1088 - *
1089 - * @return array
1090 - */
1091 - public function getReportFooterRow()
1092 - {
1093 - return $this->footerRow;
1094 - }
1097 + foreach ($this->rows as $rk => $row) {
1098 + $report_values[$rk] = [];
1099 + foreach ($row as $col => $coldata) {
1100 + $display_value = $coldata['value'];
1101 + if (isset($coldata['callback']) && is_callable($coldata['callback'])) {
1102 + // launch callback
1103 + $display_value = $coldata['callback']($coldata['value']);
1104 + }
1105 + // push column value
1106 + $report_values[$rk][$coldata['key']] = [
1107 + 'value' => $coldata['value'],
1108 + 'display_value' => $display_value,
1109 + ];
1110 + /**
1111 + * We also pass along any reserved key for this row-data.
1112 + *
1113 + * @since 1.15.0 (J) - 1.5.0 (WP)
1114 + */
1115 + foreach ($coldata as $res_key => $data_val) {
1116 + if (substr($res_key, 0, 1) == '_') {
1117 + // push this reserved key
1118 + $report_values[$rk][$coldata['key']][$res_key] = $data_val;
1119 + }
1120 + }
1121 + }
1122 + }
1095 1123
1096 - /**
1097 - * Sub-classes can extend this method to define the
1098 - * the canvas HTML tag for rendenring the Chart.
1099 - * Any necessary script shall be set within this method.
1100 - * Data can be passed as a mixed value through the argument.
1101 - * This is the first method to be called when working with the Chart.
1102 - *
1103 - * @param mixed $data any necessary value to render the Chart.
1104 - *
1105 - * @return string the HTML of the canvas element.
1106 - */
1107 - public function getChart($data = null)
1108 - {
1109 - return '';
1110 - }
1124 + if (!$report_values) {
1125 + return [];
1126 + }
1111 1127
1112 - /**
1113 - * Sub-classes can extend this method to define the
1114 - * the title of the Chart to be rendered.
1115 - *
1116 - * @return string the title of the Chart.
1117 - */
1118 - public function getChartTitle()
1119 - {
1120 - return '';
1121 - }
1128 + if ($depth === 1) {
1129 + // get an associative array with the first row calculated
1130 + return $report_values[0];
1131 + }
1122 1132
1123 - /**
1124 - * Sub-classes can extend this method to define
1125 - * the meta data for the Chart containing stats.
1126 - * An array for each meta-data should be returned.
1127 - *
1128 - * @param mixed $position string for the meta-data position
1129 - * in the Chart (top, right, bottom).
1130 - * @param mixed $data some arguments to be passed.
1131 - *
1132 - * @return array
1133 - */
1134 - public function getChartMetaData($position = null, $data = null)
1135 - {
1136 - return [];
1137 - }
1133 + if (is_int($depth) && $depth > 0 && count($report_values) >= $depth) {
1134 + // get the requested portion of the array
1135 + return array_slice($report_values, 0, $depth);
1136 + }
1138 1137
1139 - /**
1140 - * Sets an array of custom options for this report. Useful to inject
1141 - * params before getting the report data and changing the behavior.
1142 - *
1143 - * @param array $options The associative options to set.
1144 - *
1145 - * @return self
1146 - *
1147 - * @since 1.15.0 (J) - 1.5.0 (WP)
1148 - */
1149 - public function setReportOptions(array $options = [])
1150 - {
1151 - $this->options = $options;
1138 + return $report_values;
1139 + }
1152 1140
1153 - return $this;
1154 - }
1141 + /**
1142 + * Maps the columns labels to an associative array to be used for the values.
1143 + *
1144 + * @return array associative list of column keys and related values.
1145 + */
1146 + public function getColumnsValues()
1147 + {
1148 + if (!$this->cols) {
1149 + return [];
1150 + }
1155 1151
1156 - /**
1157 - * Returns the custom options for the report. Useful to
1158 - * behave differently depending on who calls the report.
1159 - * By default, the method returns an instance of JObject
1160 - * to easily access all custom options defined, if any.
1161 - *
1162 - * @param bool $registry true to get a JObject instance.
1163 - *
1164 - * @return mixed instance of JObject or raw array.
1165 - *
1166 - * @since 1.15.0 (J) - 1.5.0 (WP)
1167 - */
1168 - public function getReportOptions($registry = true)
1169 - {
1170 - if ($registry) {
1171 - return new JObject($this->options);
1172 - }
1152 + $col_values = [];
1173 1153
1174 - return $this->options;
1175 - }
1154 + foreach ($this->cols as $col) {
1155 + if (!isset($col['key'])) {
1156 + continue;
1157 + }
1158 + $col_values[$col['key']] = $col;
1159 + unset($col_values[$col['key']]['key']);
1160 + }
1176 1161
1177 - /**
1178 - * Defines an associative list of action data.
1179 - *
1180 - * @param array $data Associative list of action data.
1181 - *
1182 - * @return self
1183 - *
1184 - * @since 1.17.1 (J) - 1.7.1 (WP)
1185 - */
1186 - public function setActionData(array $data)
1187 - {
1188 - $this->actionData = $data;
1162 + return $col_values;
1163 + }
1189 1164
1190 - return $this;
1191 - }
1165 + /**
1166 + * Gets a property defined by the report. Useful to get custom
1167 + * properties set up by a specific report maybe for the Chart.
1168 + *
1169 + * @param string $property the name of the property needed.
1170 + * @param mixed $def default value to return.
1171 + *
1172 + * @return mixed false on failure, property requested otherwise.
1173 + */
1174 + public function getProperty($property, $def = false)
1175 + {
1176 + if (isset($this->{$property})) {
1177 + return $this->{$property};
1178 + }
1192 1179
1193 - /**
1194 - * Returns the action data, either raw or as a registry.
1195 - *
1196 - * @param bool $registry True to get a JObject instance.
1197 - *
1198 - * @return array|JObject
1199 - *
1200 - * @since 1.17.1 (J) - 1.7.1 (WP)
1201 - */
1202 - public function getActionData($registry = true)
1203 - {
1204 - if ($registry) {
1205 - return new JObject($this->actionData);
1206 - }
1180 + return $def;
1181 + }
1207 1182
1208 - return $this->actionData;
1209 - }
1183 + /**
1184 + * Counts the number of days of difference between two timestamps.
1185 + *
1186 + * @param int $to_ts the target end date timestamp.
1187 + * @param int $from_ts the starting date timestamp.
1188 + *
1189 + * @return int the days of difference between from and to timestamps.
1190 + */
1191 + public function countDaysTo($to_ts, $from_ts = 0)
1192 + {
1193 + if (empty($from_ts)) {
1194 + $from_ts = time();
1195 + }
1210 1196
1211 - /**
1212 - * Sets the global scope for the report.
1213 - *
1214 - * @param string $scope The scope to set.
1215 - *
1216 - * @return self
1217 - *
1218 - * @since 1.17.1 (J) - 1.7.1 (WP)
1219 - */
1220 - public function setScope($scope)
1221 - {
1222 - $this->scope = $scope;
1197 + // whether DateTime can be used
1198 + $usedt = false;
1223 1199
1224 - return $this;
1225 - }
1200 + if (class_exists('DateTime')) {
1201 + $from_date = new DateTime(date('Y-m-d', $from_ts));
1202 + if (method_exists($from_date, 'diff')) {
1203 + $usedt = true;
1204 + }
1205 + }
1226 1206
1227 - /**
1228 - * Returns the global scope of the invoked report.
1229 - *
1230 - * @return ?string
1231 - *
1232 - * @since 1.17.1 (J) - 1.7.1 (WP)
1233 - */
1234 - public function getScope()
1235 - {
1236 - return $this->scope;
1237 - }
1207 + if ($usedt) {
1208 + $to_date = new DateTime(date('Y-m-d', $to_ts));
1209 + $daysdiff = (int)$from_date->diff($to_date)->format('%a');
1210 + if ($to_ts < $from_ts) {
1211 + // we need a negative integer number
1212 + $daysdiff = $daysdiff - ($daysdiff * 2);
1213 + }
1214 + return $daysdiff;
1215 + }
1238 1216
1239 - /**
1240 - * Returns the path to the PMS media data directory.
1241 - *
1242 - * @return string
1243 - *
1244 - * @since 1.17.1 (J) - 1.7.1 (WP)
1245 - */
1246 - public function getDataMediaPath()
1247 - {
1248 - return implode(DIRECTORY_SEPARATOR, [VBO_ADMIN_PATH, 'resources', 'pmsdata']);
1249 - }
1217 + return (int)round(($to_ts - $from_ts) / 86400);
1218 + }
1250 1219
1251 - /**
1252 - * Returns the URL to the PMS media data directory.
1253 - *
1254 - * @return string
1255 - *
1256 - * @since 1.17.1 (J) - 1.7.1 (WP)
1257 - */
1258 - public function getDataMediaUrl()
1259 - {
1260 - return VBO_ADMIN_URI . 'resources/pmsdata/';
1261 - }
1220 + /**
1221 + * Counts the average difference between two integers.
1222 + *
1223 + * @param int $in_days_from days to the lowest timestamp.
1224 + * @param int $in_days_to days to the highest timestamp.
1225 + *
1226 + * @return int the average number between the two values.
1227 + */
1228 + public function countAverageDays($in_days_from, $in_days_to)
1229 + {
1230 + return (int)floor(($in_days_from + $in_days_to) / 2);
1231 + }
1262 1232
1263 - /**
1264 - * Returns the optional report custom setting fields.
1265 - *
1266 - * @return array
1267 - *
1268 - * @since 1.17.1 (J) - 1.7.1 (WP)
1269 - */
1270 - public function getSettingFields()
1271 - {
1272 - return [];
1273 - }
1233 + /**
1234 + * Sets the footer row (the totals) for this report.
1235 + *
1236 + * @param array $arr
1237 + *
1238 + * @return self
1239 + */
1240 + protected function setReportFooterRow($arr)
1241 + {
1242 + $this->footerRow = $arr;
1274 1243
1275 - /**
1276 - * Returns the report custom settings defined.
1277 - *
1278 - * @return array
1279 - *
1280 - * @since 1.17.1 (J) - 1.7.1 (WP)
1281 - */
1282 - public function loadSettings()
1283 - {
1284 - return (array) VBOFactory::getConfig()->getArray('report_settings_' . $this->getFileName(), []);
1285 - }
1244 + return $this;
1245 + }
1286 1246
1287 - /**
1288 - * Saves the report custom settings defined.
1289 - * The visibility should be public.
1290 - *
1291 - * @param array $data The associative list of settings to save.
1292 - * @param bool $merge If true, the previous settings will be merged.
1293 - *
1294 - * @return void
1295 - *
1296 - * @since 1.17.1 (J) - 1.7.1 (WP)
1297 - */
1298 - public function saveSettings(array $data, $merge = true)
1299 - {
1300 - if ($merge) {
1301 - $data = array_merge($this->loadSettings(), $data);
1302 - }
1247 + /**
1248 + * Returns the footer row for this report.
1249 + * Should be called after getReportData()
1250 + * or the returned array will be empty.
1251 + *
1252 + * @return array
1253 + */
1254 + public function getReportFooterRow()
1255 + {
1256 + return $this->footerRow;
1257 + }
1303 1258
1304 - VBOFactory::getConfig()->set('report_settings_' . $this->getFileName(), $data);
1305 - }
1259 + /**
1260 + * Sub-classes can extend this method to define the
1261 + * the canvas HTML tag for rendenring the Chart.
1262 + * Any necessary script shall be set within this method.
1263 + * Data can be passed as a mixed value through the argument.
1264 + * This is the first method to be called when working with the Chart.
1265 + *
1266 + * @param ?array $data any necessary value to render the Chart.
1267 + *
1268 + * @return string the HTML of the canvas element.
1269 + */
1270 + public function getChart(?array $data = null)
1271 + {
1272 + return '';
1273 + }
1306 1274
1307 - /**
1308 - * Returns a numeric list of scoped extra actions.
1309 - *
1310 - * @param string $scope Optional scope identifier (cron, web, etc..).
1311 - * @param bool $visible If true, the hidden actions will not be returned.
1312 - *
1313 - * @return array
1314 - *
1315 - * @since 1.17.1 (J) - 1.7.1 (WP)
1316 - */
1317 - public function getScopedActions($scope = null, $visible = true)
1318 - {
1319 - return [];
1320 - }
1275 + /**
1276 + * Sub-classes can extend this method to define the
1277 + * the title of the Chart to be rendered.
1278 + *
1279 + * @return string the title of the Chart.
1280 + */
1281 + public function getChartTitle()
1282 + {
1283 + return '';
1284 + }
1321 1285
1322 - /**
1323 - * Executes a custom scoped action within the report.
1324 - *
1325 - * @param string $action The custom action to invoke.
1326 - * @param string $scope Optional scope identifier (cron, web, etc..).
1327 - * @param array $data Optional data for the action to execute.
1328 - *
1329 - * @return mixed
1330 - *
1331 - * @throws Exception
1332 - *
1333 - * @since 1.17.1 (J) - 1.7.1 (WP)
1334 - */
1335 - public function executeAction($action, $scope = null, array $data = [])
1336 - {
1337 - if (is_null($scope)) {
1338 - $scope = $this->getScope();
1339 - }
1286 + /**
1287 + * Sub-classes can extend this method to define
1288 + * the meta data for the Chart containing stats.
1289 + * An array for each meta-data should be returned.
1290 + *
1291 + * @param mixed $position string for the meta-data position
1292 + * in the Chart (top, right, bottom).
1293 + * @param mixed $data some arguments to be passed.
1294 + *
1295 + * @return array
1296 + */
1297 + public function getChartMetaData($position = null, $data = null)
1298 + {
1299 + return [];
1300 + }
1340 1301
1341 - if (!$data) {
1342 - $data = $this->getActionData($registry = false);
1343 - }
1302 + /**
1303 + * Sets an array of custom options for this report. Useful to inject
1304 + * params before getting the report data and changing the behavior.
1305 + *
1306 + * @param array $options The associative options to set.
1307 + *
1308 + * @return self
1309 + *
1310 + * @since 1.15.0 (J) - 1.5.0 (WP)
1311 + */
1312 + public function setReportOptions(array $options = [])
1313 + {
1314 + $this->options = $options;
1344 1315
1345 - $callable = [$this, $action];
1316 + return $this;
1317 + }
1346 1318
1347 - if (!is_callable($callable)) {
1348 - throw new Exception('Could not call the requested report action.', 500);
1349 - }
1319 + /**
1320 + * Returns the custom options for the report. Useful to
1321 + * behave differently depending on who calls the report.
1322 + *
1323 + * @param bool $registry True to wrap options into a registry.
1324 + * @param bool $profileListings True to merge report profile listings setting.
1325 + *
1326 + * @return JObject|array
1327 + *
1328 + * @since 1.15.0 (J) - 1.5.0 (WP)
1329 + * @since 1.18.7 (J) - 1.8.7 (WP) added argument $profileListings.
1330 + */
1331 + public function getReportOptions(bool $registry = true, bool $profileListings = true)
1332 + {
1333 + if ($profileListings && $this->allowsProfileListings()) {
1334 + // load report settings
1335 + $reportSettings = $this->loadSettings();
1336 + if ($reportSettings['_listings'] ?? null) {
1337 + // options should force profile listings
1338 + $this->options['listings'] = (array) $reportSettings['_listings'];
1339 + }
1340 + }
1350 1341
1351 - return call_user_func_array($callable, [$scope, $data]);
1352 - }
1342 + if ($registry) {
1343 + return new JObject($this->options);
1344 + }
1353 1345
1354 - /**
1355 - * Proxy for executing a custom scoped action and returning a property.
1356 - *
1357 - * @param string $action The custom action to invoke.
1358 - * @param string $return The action result property to return.
1359 - * @param string $scope Optional scope identifier (cron, web, etc..).
1360 - * @param array $data Optional data for the action to execute.
1361 - *
1362 - * @return mixed
1363 - *
1364 - * @throws Exception
1365 - *
1366 - * @since 1.17.1 (J) - 1.7.1 (WP)
1367 - */
1368 - public function _callActionReturn($action, $return, $scope = null, array $data = [])
1369 - {
1370 - if (is_null($scope)) {
1371 - $scope = $this->getScope();
1372 - }
1346 + return $this->options;
1347 + }
1373 1348
1374 - if (!$data) {
1375 - $data = $this->getActionData($registry = false);
1376 - }
1349 + /**
1350 + * Defines an associative list of action data.
1351 + *
1352 + * @param array $data Associative list of action data.
1353 + *
1354 + * @return self
1355 + *
1356 + * @since 1.17.1 (J) - 1.7.1 (WP)
1357 + */
1358 + public function setActionData(array $data)
1359 + {
1360 + $this->actionData = $data;
1377 1361
1378 - $result = (array) $this->executeAction($action, $scope, $data);
1362 + return $this;
1363 + }
1379 1364
1380 - if (!isset($result[$return])) {
1381 - throw new Exception('Could not return the requested action result property.', 500);
1382 - }
1365 + /**
1366 + * Returns the action data, either raw or as a registry.
1367 + *
1368 + * @param bool $registry True to get a JObject instance.
1369 + *
1370 + * @return array|JObject
1371 + *
1372 + * @since 1.17.1 (J) - 1.7.1 (WP)
1373 + */
1374 + public function getActionData($registry = true)
1375 + {
1376 + if ($registry) {
1377 + return new JObject($this->actionData);
1378 + }
1383 1379
1384 - return $result[$return];
1385 - }
1380 + return $this->actionData;
1381 + }
1386 1382
1387 - /**
1388 - * Defines a new resource file generated through a custom action.
1389 - *
1390 - * @param array $data Resource file data to bind.
1391 - *
1392 - * @return self
1393 - *
1394 - * @since 1.17.1 (J) - 1.7.1 (WP)
1395 - */
1396 - protected function defineResourceFile(array $data)
1397 - {
1398 - $element = new VBOReportResourceElement($data);
1383 + /**
1384 + * Sets the global scope for the report.
1385 + *
1386 + * @param string $scope The scope to set.
1387 + *
1388 + * @return self
1389 + *
1390 + * @since 1.17.1 (J) - 1.7.1 (WP)
1391 + */
1392 + public function setScope($scope)
1393 + {
1394 + $this->scope = $scope;
1399 1395
1400 - if ($element->getUrl()) {
1401 - // ensure the resource is available
1402 - $this->resourceFiles[] = $element;
1403 - }
1396 + return $this;
1397 + }
1404 1398
1405 - return $this;
1406 - }
1399 + /**
1400 + * Returns the global scope of the invoked report.
1401 + *
1402 + * @return ?string
1403 + *
1404 + * @since 1.17.1 (J) - 1.7.1 (WP)
1405 + */
1406 + public function getScope()
1407 + {
1408 + return $this->scope;
1409 + }
1407 1410
1408 - /**
1409 - * Returns the resource files generated through custom actions.
1410 - *
1411 - * @return array
1412 - *
1413 - * @since 1.17.1 (J) - 1.7.1 (WP)
1414 - */
1415 - public function getResourceFiles()
1416 - {
1417 - return $this->resourceFiles;
1418 - }
1411 + /**
1412 + * Returns the path to the PMS media data directory.
1413 + *
1414 + * @return string
1415 + *
1416 + * @since 1.17.1 (J) - 1.7.1 (WP)
1417 + */
1418 + public function getDataMediaPath()
1419 + {
1420 + return implode(DIRECTORY_SEPARATOR, [VBO_ADMIN_PATH, 'resources', 'pmsdata']);
1421 + }
1419 1422
1420 - /**
1421 - * Sets warning messages by concatenating the existing ones.
1422 - * Method used only by sub-classes.
1423 - *
1424 - * @param string $str
1425 - *
1426 - * @return self
1427 - */
1428 - protected function setWarning($str)
1429 - {
1430 - $this->warning .= $str."\n";
1423 + /**
1424 + * Returns the URL to the PMS media data directory.
1425 + *
1426 + * @return string
1427 + *
1428 + * @since 1.17.1 (J) - 1.7.1 (WP)
1429 + */
1430 + public function getDataMediaUrl()
1431 + {
1432 + return VBO_ADMIN_URI . 'resources/pmsdata/';
1433 + }
1431 1434
1432 - return $this;
1433 - }
1435 + /**
1436 + * Returns the optional report custom setting fields at profile level.
1437 + * This method allows to load the report setting fields by injecting
1438 + * profile-level (protected) preferences, such as listings filtered.
1439 + *
1440 + * @return array
1441 + *
1442 + * @since 1.18.7 (J) - 1.8.7 (WP)
1443 + */
1444 + public function getProfileSettingFields()
1445 + {
1446 + // load all report setting fields
1447 + $reportSettingFields = $this->getSettingFields();
1434 1448
1435 - /**
1436 - * Gets the current warning string.
1437 - *
1438 - * @return string
1439 - */
1440 - public function getWarning()
1441 - {
1442 - return rtrim($this->warning, "\n");
1443 - }
1449 + if ($reportSettingFields && $this->allowsProfileListings()) {
1450 + // set protected listing setting field
1451 + $reportSettingFields['_listings'] = [
1452 + 'type' => 'listings',
1453 + 'label' => JText::translate('VBO_LISTINGS'),
1454 + 'help' => sprintf('(%s)', JText::translate('VBOFILTEISROPTIONAL')),
1455 + 'multiple' => true,
1456 + 'asset_options' => [
1457 + 'placeholder' => JText::translate('VBO_LISTINGS'),
1458 + 'allowClear' => true,
1459 + ],
1460 + ];
1461 + }
1444 1462
1445 - /**
1446 - * Sets errors by concatenating the existing ones.
1447 - * Method used only by sub-classes.
1448 - *
1449 - * @param string $str
1450 - *
1451 - * @return self
1452 - */
1453 - protected function setError($str)
1454 - {
1455 - $this->error .= $str."\n";
1463 + // return the full list of setting fields
1464 + return $reportSettingFields;
1465 + }
1456 1466
1457 - return $this;
1458 - }
1467 + /**
1468 + * Returns the optional report custom setting fields.
1469 + *
1470 + * @return array
1471 + *
1472 + * @since 1.17.1 (J) - 1.7.1 (WP)
1473 + */
1474 + public function getSettingFields()
1475 + {
1476 + return [];
1477 + }
1459 1478
1460 - /**
1461 - * Gets the current error string.
1462 - *
1463 - * @return string
1464 - */
1465 - public function getError()
1466 - {
1467 - return rtrim($this->error, "\n");
1468 - }
1479 + /**
1480 + * Tells whether the current report allows to save
1481 + * the settings across multiple profile identifiers.
1482 + *
1483 + * @return bool
1484 + *
1485 + * @since 1.17.7 (J) - 1.7.7 (WP)
1486 + */
1487 + public function allowsProfileSettings()
1488 + {
1489 + // report profile settings disabled by default
1490 + return false;
1491 + }
1492 +
1493 + /**
1494 + * Tells whether the current report allows to save
1495 + * the listings across multiple profile identifiers.
1496 + *
1497 + * @return bool
1498 + *
1499 + * @since 1.18.7 (J) - 1.8.7 (WP)
1500 + */
1501 + public function allowsProfileListings()
1502 + {
1503 + // report profile listings enabled as long as multiple profiles are enabled
1504 + return $this->allowsProfileSettings();
1505 + }
1506 +
1507 + /**
1508 + * Returns the report currently active profile identifier, if any.
1509 + *
1510 + * @return string
1511 + *
1512 + * @since 1.17.7 (J) - 1.7.7 (WP)
1513 + */
1514 + public function getActiveProfile()
1515 + {
1516 + $profile = (string) VBOFactory::getConfig()->getString('report_active_profile_' . $this->getFileName(), '');
1517 +
1518 + return $profile;
1519 + }
1520 +
1521 + /**
1522 + * Sets the report currently active profile identifier.
1523 + *
1524 + * @param string $profile_id The profile identifier to set.
1525 + *
1526 + * @return void
1527 + *
1528 + * @since 1.17.7 (J) - 1.7.7 (WP)
1529 + */
1530 + public function setActiveProfile(string $profile_id)
1531 + {
1532 + VBOFactory::getConfig()->set('report_active_profile_' . $this->getFileName(), $profile_id);
1533 + }
1534 +
1535 + /**
1536 + * Returns an associative list for the report setting profiles available.
1537 + *
1538 + * @return array
1539 + *
1540 + * @since 1.17.7 (J) - 1.7.7 (WP)
1541 + */
1542 + public function getSettingProfiles()
1543 + {
1544 + return (array) VBOFactory::getConfig()->getArray('report_profile_list_' . $this->getFileName(), []);
1545 + }
1546 +
1547 + /**
1548 + * Sets a new report setting profile identifier.
1549 + *
1550 + * @param string $profile_name The profile name.
1551 + *
1552 + * @return array List of profile identifier and name.
1553 + *
1554 + * @since 1.17.7 (J) - 1.7.7 (WP)
1555 + */
1556 + public function setSettingProfile(string $profile_name)
1557 + {
1558 + // get all report profiles
1559 + $profiles = $this->getSettingProfiles();
1560 +
1561 + // build profile identifier
1562 + $profile_id = preg_replace('/[^A-Z0-9]/i', '', strtolower($profile_name));
1563 + $profile_id = $profile_id ?: uniqid();
1564 +
1565 + // set report profile
1566 + $profiles[$profile_id] = $profile_name;
1567 + VBOFactory::getConfig()->set('report_profile_list_' . $this->getFileName(), $profiles);
1568 +
1569 + return [$profile_id, $profile_name];
1570 + }
1571 +
1572 + /**
1573 + * Clears all profile settings and related data.
1574 + *
1575 + * @return void
1576 + *
1577 + * @since 1.17.7 (J) - 1.7.7 (WP)
1578 + */
1579 + public function clearProfiles()
1580 + {
1581 + // unset active profile
1582 + VBOFactory::getConfig()->set('report_active_profile_' . $this->getFileName(), '');
1583 +
1584 + // unset profiles list
1585 + VBOFactory::getConfig()->set('report_profile_list_' . $this->getFileName(), []);
1586 +
1587 + // clear profile settings
1588 + VBOFactory::getConfig()->set('report_profile_settings_' . $this->getFileName(), []);
1589 + }
1590 +
1591 + /**
1592 + * Returns the current report custom settings, optionally loaded from a
1593 + * given profile identifier in case the report supports multiple settings.
1594 + *
1595 + * @param string $profile Optional settings profile identifier.
1596 + *
1597 + * @return array
1598 + *
1599 + * @since 1.17.1 (J) - 1.7.1 (WP)
1600 + */
1601 + public function loadSettings(string $profile = '')
1602 + {
1603 + // access report current settings
1604 + $current_settings = (array) VBOFactory::getConfig()->getArray('report_settings_' . $this->getFileName(), []);
1605 +
1606 + // default settings
1607 + $default_settings = [];
1608 +
1609 + if (!$profile && $this->allowsProfileSettings()) {
1610 + // load current profile settings
1611 + $profile = $this->getActiveProfile();
1612 + $default_settings = $current_settings;
1613 + }
1614 +
1615 + if ($profile) {
1616 + // check if the requested profile settings are available
1617 + $profile_settings = (array) VBOFactory::getConfig()->getArray('report_profile_settings_' . $this->getFileName(), []);
1618 +
1619 + // overwrite report current settings
1620 + $current_settings = $profile_settings[$profile] ?? $default_settings;
1621 + }
1622 +
1623 + return $current_settings;
1624 + }
1625 +
1626 + /**
1627 + * Saves the report custom settings defined.
1628 + * The visibility should be public.
1629 + *
1630 + * @param array $data The associative list of settings to save.
1631 + * @param bool $merge If true, the previous settings will be merged.
1632 + * @param string $profile Optional settings profile identifier.
1633 + *
1634 + * @return void
1635 + *
1636 + * @since 1.17.1 (J) - 1.7.1 (WP)
1637 + * @since 1.17.7 (J) - 1.7.7 (WP) added 3rd argument $profile and related support.
1638 + */
1639 + public function saveSettings(array $data, $merge = true, string $profile = '')
1640 + {
1641 + if ($merge) {
1642 + // build report global settings
1643 + $data = array_merge($this->loadSettings($profile), $data);
1644 + }
1645 +
1646 + // save report global settings
1647 + VBOFactory::getConfig()->set('report_settings_' . $this->getFileName(), $data);
1648 +
1649 + if ($profile) {
1650 + // get report current profile settings
1651 + $profile_settings = (array) VBOFactory::getConfig()->getArray('report_profile_settings_' . $this->getFileName(), []);
1652 +
1653 + // set new profile settings
1654 + $profile_settings[$profile] = $data;
1655 +
1656 + // save report profile settings
1657 + VBOFactory::getConfig()->set('report_profile_settings_' . $this->getFileName(), $profile_settings);
1658 + }
1659 + }
1660 +
1661 + /**
1662 + * Returns a numeric list of scoped extra actions.
1663 + *
1664 + * @param string $scope Optional scope identifier (cron, web, etc..).
1665 + * @param bool $visible If true, the hidden actions will not be returned.
1666 + *
1667 + * @return array
1668 + *
1669 + * @since 1.17.1 (J) - 1.7.1 (WP)
1670 + */
1671 + public function getScopedActions($scope = null, $visible = true)
1672 + {
1673 + return [];
1674 + }
1675 +
1676 + /**
1677 + * Executes a custom scoped action within the report.
1678 + *
1679 + * @param string $action The custom action to invoke.
1680 + * @param string $scope Optional scope identifier (cron, web, etc..).
1681 + * @param array $data Optional data for the action to execute.
1682 + *
1683 + * @return mixed
1684 + *
1685 + * @throws Exception
1686 + *
1687 + * @since 1.17.1 (J) - 1.7.1 (WP)
1688 + */
1689 + public function executeAction($action, $scope = null, array $data = [])
1690 + {
1691 + if (is_null($scope)) {
1692 + $scope = $this->getScope();
1693 + }
1694 +
1695 + if (!$data) {
1696 + $data = $this->getActionData($registry = false);
1697 + }
1698 +
1699 + $callable = [$this, $action];
1700 +
1701 + if (!is_callable($callable)) {
1702 + throw new Exception('Could not call the requested report action.', 500);
1703 + }
1704 +
1705 + return call_user_func_array($callable, [$scope, $data]);
1706 + }
1707 +
1708 + /**
1709 + * Proxy for executing a custom scoped action and returning a property.
1710 + *
1711 + * @param string $action The custom action to invoke.
1712 + * @param string $return The action result property to return.
1713 + * @param string $scope Optional scope identifier (cron, web, etc..).
1714 + * @param array $data Optional data for the action to execute.
1715 + *
1716 + * @return mixed
1717 + *
1718 + * @throws Exception
1719 + *
1720 + * @since 1.17.1 (J) - 1.7.1 (WP)
1721 + */
1722 + public function _callActionReturn($action, $return, $scope = null, array $data = [])
1723 + {
1724 + if (is_null($scope)) {
1725 + $scope = $this->getScope();
1726 + }
1727 +
1728 + if (!$data) {
1729 + $data = $this->getActionData($registry = false);
1730 + }
1731 +
1732 + $result = (array) $this->executeAction($action, $scope, $data);
1733 +
1734 + if (!isset($result[$return])) {
1735 + throw new Exception('Could not return the requested action result property.', 500);
1736 + }
1737 +
1738 + return $result[$return];
1739 + }
1740 +
1741 + /**
1742 + * Defines a new resource file generated through a custom action.
1743 + *
1744 + * @param array $data Resource file data to bind.
1745 + *
1746 + * @return self
1747 + *
1748 + * @since 1.17.1 (J) - 1.7.1 (WP)
1749 + */
1750 + protected function defineResourceFile(array $data)
1751 + {
1752 + $element = new VBOReportResourceElement($data);
1753 +
1754 + if ($element->getUrl()) {
1755 + // ensure the resource is available
1756 + $this->resourceFiles[] = $element;
1757 + }
1758 +
1759 + return $this;
1760 + }
1761 +
1762 + /**
1763 + * Returns the resource files generated through custom actions.
1764 + *
1765 + * @return array
1766 + *
1767 + * @since 1.17.1 (J) - 1.7.1 (WP)
1768 + */
1769 + public function getResourceFiles()
1770 + {
1771 + return $this->resourceFiles;
1772 + }
1773 +
1774 + /**
1775 + * Sets warning messages by concatenating the existing ones.
1776 + * Method used only by sub-classes.
1777 + *
1778 + * @param string $str
1779 + *
1780 + * @return self
1781 + */
1782 + protected function setWarning($str)
1783 + {
1784 + $this->warning .= $str."\n";
1785 +
1786 + return $this;
1787 + }
1788 +
1789 + /**
1790 + * Gets the current warning string.
1791 + *
1792 + * @return string
1793 + */
1794 + public function getWarning()
1795 + {
1796 + return rtrim($this->warning, "\n");
1797 + }
1798 +
1799 + /**
1800 + * Resets any errors previously set.
1801 + *
1802 + * @return self
1803 + *
1804 + * @since 1.18.3 (J) - 1.8.3 (WP)
1805 + */
1806 + protected function resetErrors()
1807 + {
1808 + $this->error = '';
1809 +
1810 + return $this;
1811 + }
1812 +
1813 + /**
1814 + * Sets errors by concatenating the existing ones.
1815 + * Method used only by sub-classes.
1816 + *
1817 + * @param string $str
1818 + *
1819 + * @return self
1820 + */
1821 + protected function setError($str)
1822 + {
1823 + $this->error .= $str."\n";
1824 +
1825 + return $this;
1826 + }
1827 +
1828 + /**
1829 + * Gets the current error string.
1830 + *
1831 + * @return string
1832 + */
1833 + public function getError()
1834 + {
1835 + return rtrim($this->error, "\n");
1836 + }
1469 1837 }