PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.4
VikBooking Hotel Booking Engine & PMS v1.6.4
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 / notification / scheduler.php

scheduler.php in VikBooking Hotel Booking Engine & PMS 1.6.4, at admin/helpers/src/notification/scheduler.php

375 lines 9.8 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 * Browser notifications scheduler handler.
16 *
17 * @since 1.15.0 (J) - 1.5.0 (WP)
18 */
19 final class VBONotificationScheduler
20 {
21 /**
22 * @var mixed
23 */
24 private $data = null;
25
26 /**
27 * @var JDocument
28 */
29 private $doc = null;
30
31 /**
32 * Proxy to immediately access the object.
33 *
34 * @param mixed $data optional data to set.
35 * @param JDocument $doc optional document object.
36 *
37 * @return self
38 */
39 public static function getInstance($data = null, $doc = null)
40 {
41 return new static($data, $doc);
42 }
43
44 /**
45 * Class constructor.
46 *
47 * @param mixed $data
48 */
49 public function __construct($data = null, $doc = null)
50 {
51 // bind data
52 $this->data = $data;
53
54 // set the document object
55 if (!$doc) {
56 $doc = JFactory::getDocument();
57 }
58 $this->doc = $doc;
59 }
60
61 /**
62 * Given a reminder record object, composes the notification
63 * data object to schedule a browser notification.
64 *
65 * @param object $reminder the reminder record object.
66 * @param int $max_future_hours optional value to check if the reminder
67 * is too far ahead in the future.
68 *
69 * @return null|object
70 */
71 public function getReminderDataObject($reminder, $max_future_hours = 0)
72 {
73 if (!is_object($reminder) || empty($reminder->id) || empty($reminder->duedate)) {
74 return null;
75 }
76
77 if ($max_future_hours > 0) {
78 // this reminder requires a validation of the future time
79 $now_info = getdate();
80 $next_hours = ($now_info['hours'] + $max_future_hours);
81 $max_future_ts = mktime($next_hours, $now_info['minutes'], 0, $now_info['mon'], $now_info['mday'], $now_info['year']);
82 $scheduled_ts = strtotime($reminder->duedate);
83 if ($scheduled_ts > $max_future_ts) {
84 // no need to schedule a notification for this reminder as it's too far ahead
85 return null;
86 }
87 }
88
89 // get a new notification data object
90 $notification = VBONotificationDataReminder::getInstance();
91
92 // set data
93 $notification->setReminderId($reminder->id);
94 $notification->setDateTime($reminder->duedate);
95 if (!$reminder->usetime) {
96 $notification->setNoTime(true);
97 }
98
99 return $notification->toDataObject();
100 }
101
102 /**
103 * Builds a notification display data object for a recent history
104 * event involving a reservation. This kind of notification will
105 * be actually dispatched immediately with no scheduling, and so
106 * so it won't provide a build-URL, it will provide display data.
107 *
108 * @param object $h_event history record object to parse.
109 *
110 * @return null|object
111 */
112 public function getHistoryDataObject($h_event)
113 {
114 if (!is_object($h_event) || empty($h_event->idorder)) {
115 return null;
116 }
117
118 // get a new notification data object
119 $notification = VBONotificationDataHistory::getInstance();
120
121 // inject all record properties as "reserved" properties
122 foreach ($h_event as $prop => $val) {
123 // properties starting with an underscore are "reserved"
124 $notification->set("_{$prop}", $val);
125 }
126
127 // the notification will be dispatched immediately
128 $notification->setDateTime('now');
129
130 // let the notification build the display data
131 $notification->buildDisplayData();
132
133 // return the notification data-object
134 return $notification->toDataObject();
135 }
136
137 /**
138 * Given a list of recent worthy-of-notification history events,
139 * builds a list of notification data objects of type "history".
140 * Such notifications will be actually dispatched immediately,
141 * with no needs to schedule a notification timer. Moreover,
142 * these notifications will contain immediately the necessary
143 * display data without needing to build them through a callback.
144 *
145 * @param array $events list of recent history record objects.
146 *
147 * @return array list of browser notification display data objects.
148 */
149 public function buildHistoryDataObjects($events = [])
150 {
151 if (!is_array($events) || !$events) {
152 return [];
153 }
154
155 // container of notification display data objects
156 $data_objects = [];
157
158 foreach ($events as $history) {
159 $data_object = $this->getHistoryDataObject($history);
160 if ($data_object) {
161 $data_objects[] = $data_object;
162 }
163 }
164
165 return $data_objects;
166 }
167
168 /**
169 * Builds a notification display data object for a recent guest
170 * review involving a reservation. This kind of notification will
171 * be actually dispatched immediately with no scheduling, and so
172 * so it won't provide a build-URL, it will provide display data.
173 *
174 * @param object $review guest activity record object to parse.
175 *
176 * @return null|object
177 */
178 public function getReviewDataObject($review)
179 {
180 if (!is_object($review) || empty($review->id_review) || empty($review->idorder)) {
181 return null;
182 }
183
184 // get a new notification data object
185 $notification = VBONotificationDataReview::getInstance();
186
187 // inject all record properties as "reserved" properties
188 foreach ($review as $prop => $val) {
189 // properties starting with an underscore are "reserved"
190 $notification->set("_{$prop}", $val);
191 }
192
193 // the notification will be dispatched immediately
194 $notification->setDateTime('now');
195
196 // let the notification build the display data
197 $notification->buildDisplayData();
198
199 // return the notification data-object
200 return $notification->toDataObject();
201 }
202
203 /**
204 * Given a list of recent guest activity records, builds a list
205 * of notification data objects of type "review".
206 * Such notifications will be actually dispatched immediately,
207 * with no needs to schedule a notification timer. Moreover,
208 * these notifications will contain immediately the necessary
209 * display data without needing to build them through a callback.
210 *
211 * @param array $reviews list of recent guest activity record objects.
212 *
213 * @return array list of browser notification display data objects.
214 */
215 public function buildReviewDataObjects($reviews = [])
216 {
217 if (!is_array($reviews) || !$reviews) {
218 return [];
219 }
220
221 // container of notification display data objects
222 $data_objects = [];
223
224 foreach ($reviews as $review) {
225 $data_object = $this->getReviewDataObject($review);
226 if ($data_object) {
227 $data_objects[] = $data_object;
228 }
229 }
230
231 return $data_objects;
232 }
233
234 /**
235 * Builds a notification display data object for a recent guest
236 * message involving a reservation. This kind of notification will
237 * be actually dispatched immediately with no scheduling, and so
238 * so it won't provide a build-URL, it will provide display data.
239 *
240 * @param object $message guest activity record object to parse.
241 *
242 * @return null|object
243 */
244 public function getGuestMessageDataObject($message)
245 {
246 if (!is_object($message) || empty($message->idorder)) {
247 return null;
248 }
249
250 // get a new notification data object
251 $notification = VBONotificationDataMessage::getInstance();
252
253 // inject all record properties as "reserved" properties
254 foreach ($message as $prop => $val) {
255 // properties starting with an underscore are "reserved"
256 $notification->set("_{$prop}", $val);
257 }
258
259 // the notification will be dispatched immediately
260 $notification->setDateTime('now');
261
262 // let the notification build the display data
263 $notification->buildDisplayData();
264
265 // return the notification data-object
266 return $notification->toDataObject();
267 }
268
269 /**
270 * Given a list of recent guest activity records, builds a list
271 * of notification data objects of type (guest) "message".
272 * Such notifications will be actually dispatched immediately,
273 * with no needs to schedule a notification timer. Moreover,
274 * these notifications will contain immediately the necessary
275 * display data without needing to build them through a callback.
276 *
277 * @param array $messages list of recent guest activity record objects.
278 *
279 * @return array list of browser notification display data objects.
280 */
281 public function buildGuestMessageDataObjects($messages = [])
282 {
283 if (!is_array($messages) || !$messages) {
284 return [];
285 }
286
287 // container of notification display data objects
288 $data_objects = [];
289
290 foreach ($messages as $message) {
291 $data_object = $this->getGuestMessageDataObject($message);
292 if ($data_object) {
293 $data_objects[] = $data_object;
294 }
295 }
296
297 return $data_objects;
298 }
299
300 /**
301 * Enqueues a list of notification objects for the overdue reminders.
302 * This should be called upon a regular page loading, not during AJAX
303 * requests, as it attachs a script declaration to the document.
304 *
305 * @param array $reminders list of reminder record objects.
306 *
307 * @return void
308 */
309 public function enqueueReminders($reminders)
310 {
311 if (!is_array($reminders) || !$reminders) {
312 return;
313 }
314
315 // build the queue of reminders to be scheduled
316 $queue = [];
317
318 foreach ($reminders as $reminder) {
319 if (empty($reminder->duedate)) {
320 continue;
321 }
322
323 // compose reminder data object
324 $notification = $this->getReminderDataObject($reminder);
325 if ($notification) {
326 // push reminder to queue
327 $queue[] = $notification;
328 }
329 }
330
331 if (!count($queue)) {
332 return;
333 }
334
335 $js_queue = json_encode($queue);
336
337 $this->doc->addScriptDeclaration(
338 <<<JS
339 jQuery(function() {
340 setTimeout(() => {
341 VBOCore.enqueueNotifications($js_queue);
342 }, 300);
343 });
344 JS
345 );
346 }
347
348 /**
349 * Registers a list of widgets watching data.
350 *
351 * @param array $watch_data associative list of widgets
352 * and preloaded watch data.
353 *
354 * @return void
355 */
356 public function registerWatchData($watch_data = [])
357 {
358 if (!is_array($watch_data) || !$watch_data) {
359 return;
360 }
361
362 $js_watch_data = json_encode($watch_data);
363
364 $this->doc->addScriptDeclaration(
365 <<<JS
366 jQuery(function() {
367 setTimeout(() => {
368 VBOCore.registerWatchData($js_watch_data);
369 }, 300);
370 });
371 JS
372 );
373 }
374 }
375