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 / checkin / pax.php

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

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