PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.7.2
VikBooking Hotel Booking Engine & PMS v1.7.2
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 / checkin / pax.php

pax.php in VikBooking Hotel Booking Engine & PMS 1.7.2, at admin/helpers/src/checkin/pax.php

307 lines 8.3 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) 2022 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 * Helper class to support custom pax fields data collection types.
16 *
17 * @since 1.15.0 (J) - 1.5.0 (WP)
18 */
19 final class VBOCheckinPax
20 {
21 /**
22 * Container of all pax fields collection instances.
23 *
24 * @var array
25 */
26 private static $instances = array();
27
28 /**
29 * Data container of all pax fields collection instances.
30 *
31 * @var array
32 */
33 private static $instances_data = array();
34
35 /**
36 * Retrieves the list of back-end pax fields for an extended collection type.
37 *
38 * @param string $type the pax data type representing the class.
39 *
40 * @return array the list of pax fields to collect in the back-end.
41 */
42 public static function getFields($type = '')
43 {
44 // default empty containers
45 $labels = [];
46 $attributes = [];
47
48 if (empty($type)) {
49 return [$labels, $attributes];
50 }
51
52 // invoke custom pax fields object
53 $paxf_obj = self::getInstance($type);
54 if (!$paxf_obj) {
55 return [$labels, $attributes];
56 }
57
58 return $paxf_obj->listFields();
59 }
60
61 /**
62 * Retrieves the list of pre-checkin pax fields for an extended collection type.
63 *
64 * @param string $type the pax data type representing the class.
65 * @param array $def_fields list of default pre-checkin labels and attributes.
66 *
67 * @return array the list of pax fields to collect in the front-end during pre-checkin.
68 *
69 * @since 1.17.2 (J) - 1.7.2 (WP)
70 */
71 public static function getPrecheckinFields($type = '', array $def_fields = [])
72 {
73 // default empty containers
74 $labels = [];
75 $attributes = [];
76
77 if (empty($type)) {
78 return [$labels, $attributes];
79 }
80
81 // invoke custom pax fields object
82 $paxf_obj = self::getInstance($type);
83 if (!$paxf_obj) {
84 return [$labels, $attributes];
85 }
86
87 return $paxf_obj->listPrecheckinFields($def_fields);
88 }
89
90 /**
91 * Returns the instance of the custom pax data collection type object.
92 *
93 * @param string $type the pax data type representing the class.
94 *
95 * @return null|object the requested driver object or null.
96 */
97 public static function getInstance($type = '')
98 {
99 // make sure the type of pax data collection type is set
100 if (empty($type)) {
101 if (count(static::$instances)) {
102 // we return the last previously instantiated object
103 $type = key(static::$instances);
104 return static::$instances[$type];
105 }
106 // get the currently active pax data collection type
107 $type = VBOFactory::getConfig()->getString('checkindata', 'basic');
108 }
109
110 if (isset(static::$instances[$type])) {
111 return static::$instances[$type];
112 }
113
114 // invoke custom pax fields class
115 $custom_paxf_class = self::getPaxClass($type);
116
117 if (!class_exists($custom_paxf_class)) {
118 return null;
119 }
120
121 // invoke and cache the object
122 static::$instances[$type] = new $custom_paxf_class();
123
124 // register the data container registry for this collection type
125 static::$instances_data[$type] = new JObject;
126
127 // return the object
128 return static::$instances[$type];
129 }
130
131 /**
132 * Attempts to get the instance data registry for the data collection type previously
133 * previously loaded. This serves to push and cache heavy data so that it will be read
134 * only once, in case multiple pax fields will need to access such values multiple times.
135 *
136 * @return null|JObject the driver object instance data registry, or null.
137 */
138 public static function getInstanceData()
139 {
140 if (!count(static::$instances)) {
141 // no drivers ever loaded
142 return null;
143 }
144
145 // get the first (previous) type of collection driver loaded
146 $type = key(static::$instances);
147
148 return isset(static::$instances_data[$type]) ? static::$instances_data[$type] : null;
149 }
150
151 /**
152 * Returns a list of all custom drivers available.
153 *
154 * @param bool $no_basic whether to exclude the "basic" (default) driver.
155 *
156 * @return array the associative list of custom pax data drivers.
157 */
158 public static function getDrivers($no_basic = true)
159 {
160 $drivers_list = [];
161 $drivers_base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'paxfields' . DIRECTORY_SEPARATOR;
162 $drivers_files = glob($drivers_base . '*.php');
163
164 /**
165 * Trigger event to let other plugins register additional drivers.
166 *
167 * @return array a list of supported drivers.
168 */
169 $list = VBOFactory::getPlatform()->getDispatcher()->filter('onLoadPaxdatafieldsDrivers');
170 foreach ($list as $chunk) {
171 // merge default driver files with the returned ones
172 $drivers_files = array_merge($drivers_files, (array)$chunk);
173 }
174
175 foreach ($drivers_files as $df) {
176 $driver_base_name = basename($df, '.php');
177 if ($no_basic && !strcasecmp($driver_base_name, 'basic')) {
178 continue;
179 }
180 $drivers_list[] = $driver_base_name;
181 }
182
183 return $drivers_list;
184 }
185
186 /**
187 * Helper method to get the decoded pax_data of an existing booking.
188 *
189 * @param int $bid the booking ID.
190 *
191 * @return null|array the decoded pax data or null.
192 */
193 public static function getBookingPaxData($bid)
194 {
195 if (empty($bid)) {
196 return null;
197 }
198
199 $dbo = JFactory::getDbo();
200
201 $q = "SELECT `pax_data` FROM `#__vikbooking_customers_orders` WHERE `idorder`=" . (int)$bid;
202 $dbo->setQuery($q, 0, 1);
203 $dbo->execute();
204 if (!$dbo->getNumRows()) {
205 return null;
206 }
207 $pax_data = $dbo->loadResult();
208 if (empty($pax_data)) {
209 return null;
210 }
211
212 // attempt to decode current data
213 $pax_data = json_decode($pax_data, true);
214
215 return is_array($pax_data) && count($pax_data) ? $pax_data : null;
216 }
217
218 /**
219 * Helper method to update the pax_data of an existing booking.
220 *
221 * @param int $bid the booking ID.
222 * @param array $pax_data the array of room guests pax data.
223 *
224 * @return bool true if record was updated.
225 */
226 public static function setBookingPaxData($bid, $pax_data)
227 {
228 if (empty($bid) || !is_array($pax_data) || !count($pax_data)) {
229 return false;
230 }
231
232 $dbo = JFactory::getDbo();
233
234 $record = new stdClass;
235 $record->idorder = $bid;
236 $record->pax_data = json_encode($pax_data);
237
238 return $dbo->updateObject('#__vikbooking_customers_orders', $record, 'idorder');
239 }
240
241 /**
242 * Helper method to load the previous pax data for this customer in case
243 * previous check-ins were made already for this customer. This is to
244 * quicky populate the previous check-in information.
245 *
246 * @param int $bid the booking ID.
247 * @param int $lim the maximum number of previous records to load.
248 *
249 * @return array the list of previous pax data records for this customer.
250 *
251 * @since 1.16.0 (J) - 1.6.0 (WP)
252 */
253 public static function getCustomerAllPaxData($bid, $lim = 5)
254 {
255 if (empty($bid)) {
256 return [];
257 }
258
259 $dbo = JFactory::getDbo();
260
261 $q = "SELECT `idcustomer` FROM `#__vikbooking_customers_orders` WHERE `idorder`=" . (int)$bid;
262 $dbo->setQuery($q, 0, 1);
263 $id_customer = $dbo->loadResult();
264 if (!$id_customer) {
265 return [];
266 }
267
268 $q = "SELECT `co`.`idorder`, `co`.`pax_data`, `o`.`ts`, `o`.`checkin`, `o`.`checkout` FROM `#__vikbooking_customers_orders` AS `co`
269 LEFT JOIN `#__vikbooking_orders` AS `o` ON `co`.`idorder`=`o`.`id`
270 WHERE `co`.`idcustomer`=" . (int)$id_customer . " AND `co`.`idorder` != " . (int)$bid . " AND `co`.`pax_data` IS NOT NULL ORDER BY `co`.`idorder` DESC";
271 $dbo->setQuery($q, 0, $lim);
272 $prev_records = $dbo->loadAssocList();
273 if (!$prev_records) {
274 return [];
275 }
276
277 foreach ($prev_records as $k => $v) {
278 // attempt to decode checkin data
279 $pax_data = json_decode($v['pax_data'], true);
280 if (!is_array($pax_data) || !count($pax_data)) {
281 unset($prev_records[$k]);
282 continue;
283 }
284 $prev_records[$k]['pax_data'] = $pax_data;
285 }
286
287 return $prev_records;
288 }
289
290 /**
291 * Given a type string, returns the full class name for this driver.
292 *
293 * @param string $type the pax data type representing the class.
294 *
295 * @return string the name of the class for the custom pax data collection.
296 */
297 private static function getPaxClass($type)
298 {
299 $base_paxf_class = 'VBOCheckinPaxfields';
300
301 $type_class = ucwords(str_replace(array('-', '_'), ' ', $type));
302 $type_class = preg_replace("/[^a-zA-Z0-9]/", '', $type_class);
303
304 return $base_paxf_class . $type_class;
305 }
306 }
307