PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.5
VikBooking Hotel Booking Engine & PMS v1.6.5
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
vikbooking / admin / helpers / src / report / loader.php

loader.php in VikBooking Hotel Booking Engine & PMS 1.6.5, at admin/helpers/src/report/loader.php

209 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author Alessio Gaggii - E4J s.r.l.
6 * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Class handler for loading PMS Reports.
16 *
17 * @since 1.16.1 (J) - 1.6.1 (WP)
18 */
19 final class VBOReportLoader
20 {
21 /**
22 * The singleton instance of the class.
23 *
24 * @var VBOReportLoader
25 */
26 private static $instance = null;
27
28 /**
29 * @var array
30 */
31 private $global_reports = [];
32
33 /**
34 * @var array
35 */
36 private $country_reports = [];
37
38 /**
39 * @var array
40 */
41 private $countries = [];
42
43 /**
44 * Proxy to construct the object.
45 *
46 * @return self
47 */
48 public static function getInstance()
49 {
50 if (is_null(static::$instance)) {
51 static::$instance = new static();
52 }
53
54 return static::$instance;
55 }
56
57 /**
58 * Class constructor will require the necessary dependencies.
59 */
60 public function __construct()
61 {
62 // require the report abstract class
63 $report_base = $this->getReportBasePath();
64 require_once $report_base . 'report.php';
65 }
66
67 /**
68 * Gets the instance of a specific report class.
69 *
70 * @param string $report the name of the report to load.
71 *
72 * @return mixed false or report object instance.
73 */
74 public function getDriver($report)
75 {
76 return VikBooking::getReportInstance($report);
77 }
78
79 /**
80 * Loads and returns the list of available PMS Report objects.
81 *
82 * @return array list of "global" and "country" reports.
83 */
84 public function getDrivers($type = '')
85 {
86 if ($this->global_reports || $this->country_reports) {
87 // drivers loaded already
88 return [$this->global_reports, $this->country_reports];
89 }
90
91 // read all driver files
92 $report_files = glob($this->getReportBasePath() . '*.php');
93
94 /**
95 * Trigger event to let other plugins register additional driver full paths.
96 *
97 * @since 1.16.0 (J) - 1.6.0 (WP)
98 */
99 $list = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadPmsReports');
100 foreach ($list as $chunk) {
101 // merge default driver files with the returned ones
102 $report_files = array_merge($report_files, (array)$chunk);
103 }
104
105 // parse and invoke all drivers
106 foreach ($report_files as $k => $report_path) {
107 // driver file name
108 $report_file = basename($report_path, '.php');
109
110 if ($report_file == 'report') {
111 // exclude protected abstract report class
112 unset($report_files[$k]);
113 continue;
114 }
115
116 // load report file
117 require_once $report_path;
118
119 // build report class name
120 $classname = 'VikBookingReport' . str_replace(' ', '', ucwords(str_replace('_', ' ', $report_file)));
121 if (!class_exists($classname)) {
122 // invalid class name declared
123 unset($report_files[$k]);
124 continue;
125 }
126
127 if ($report_file == 'revenue' && $this->global_reports) {
128 // make the "revenue" the first element of the list
129 array_unshift($this->global_reports, new $classname);
130 } elseif (strpos($report_file, 'istat') !== false || strpos($report_file, 'polizia') !== false) {
131 // this is a country (italy) specific report so we push it to a separate array
132 $country_key = 'IT';
133 if (!isset($this->country_reports[$country_key])) {
134 $this->country_reports[$country_key] = [];
135 }
136 $this->country_reports[$country_key][] = new $classname;
137 } elseif (substr($report_file, 2, 1) == '_') {
138 // this is probably a country specific report so we push it to a separate array (two-letter country code + underscore in file name)
139 $country_key = strtoupper(substr($report_file, 0, 2));
140 if (!isset($this->country_reports[$country_key])) {
141 $this->country_reports[$country_key] = [];
142 }
143 $this->country_reports[$country_key][] = new $classname;
144 } else {
145 // push this object as a global report
146 $this->global_reports[] = new $classname;
147 }
148 }
149
150 // get countries information for the reports
151 $countries = $this->getInvolvedCountries();
152
153 // check whether some country specific reports truly belong to a country
154 foreach ($this->country_reports as $ckey => $cvalue) {
155 if (!isset($countries[$ckey])) {
156 // this country does not exist, so maybe the report file was given a short beginning name. Push it to the global reports array
157 unset($countries[$ckey]);
158 $this->global_reports = array_merge($this->global_reports, $cvalue);
159 }
160 }
161
162 // return the list of drivers
163 return [$this->global_reports, $this->country_reports];
164 }
165
166 /**
167 * Returns the associative list of the involved countries for the reports.
168 *
169 * @return array
170 */
171 public function getInvolvedCountries()
172 {
173 if ($this->countries || !$this->country_reports) {
174 return $this->countries;
175 }
176
177 $dbo = JFactory::getDbo();
178
179 // get countries information for the reports
180 $country_keys = array_keys($this->country_reports);
181 $country_keys = array_map(array($dbo, 'quote'), $country_keys);
182
183 $q = $dbo->getQuery(true);
184
185 $q->select($dbo->qn(['country_name', 'country_2_code']));
186 $q->from($dbo->qn('#__vikbooking_countries'));
187 $q->where($dbo->qn('country_2_code') . ' IN (' . implode(', ', $country_keys) . ')');
188
189 $dbo->setQuery($q);
190 $cdata = $dbo->loadAssocList();
191
192 foreach ($cdata as $cd) {
193 $this->countries[$cd['country_2_code']] = $cd['country_name'];
194 }
195
196 return $this->countries;
197 }
198
199 /**
200 * Returns the report base path.
201 *
202 * @return string the base path for the reports.
203 */
204 private function getReportBasePath()
205 {
206 return VBO_ADMIN_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'report' . DIRECTORY_SEPARATOR;
207 }
208 }
209