PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.10.01
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.10.01
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
fluent-booking / app / Hooks / Handlers / NotificationHandler.php

NotificationHandler.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.10.01, at app/Hooks/Handlers/NotificationHandler.php

316 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\App\Hooks\Handlers;
4
5 use FluentBooking\App\Models\Booking;
6 use FluentBooking\App\Services\EmailNotificationService;
7 use FluentBooking\Framework\Support\Arr;
8
9 class NotificationHandler
10 {
11 public function register()
12 {
13 add_action('fluent_booking/after_booking_scheduled', [$this, 'pushBookingScheduledToQueue'], 10, 2);
14 add_action('fluent_booking/after_booking_scheduled_async', [$this, 'bookingScheduledEmails'], 10, 1);
15 add_action('fluent_booking/after_booking_pending', [$this, 'pushBookingPendingToQueue'], 10, 2);
16 add_action('fluent_booking/after_booking_pending_async', [$this, 'bookingRequestEmails'], 10, 2);
17 add_action('fluent_booking/booking_schedule_reminder', [$this, 'bookingReminderEmails'], 10, 2);
18 add_action('fluent_booking/after_booking_rescheduled', [$this, 'emailOnBookingRescheduled'], 10, 3);
19 add_action('fluent_booking/booking_schedule_cancelled', [$this, 'emailOnBookingCancelled'], 10, 2);
20 add_action('fluent_booking/booking_schedule_rejected', [$this, 'emailOnBookingRejected'], 10, 2);
21 add_action('fluent_booking/after_patch_booking_email', [$this, 'emailToUpdatedEmail'], 10, 2);
22 }
23
24 private function getReminderTime($time)
25 {
26 $timestamp = $time['value'] * 60;
27
28 if ($time['unit'] == 'hours') {
29 $timestamp = $timestamp * 60;
30 } elseif ($time['unit'] == 'days') {
31 $timestamp = $timestamp * 60 * 24;
32 }
33
34 return $timestamp;
35 }
36
37 private function pushRemindersToQueue($booking, $reminderTimes, $emailTo)
38 {
39 $this->clearGroupRemindersForHost($booking, $emailTo);
40
41 foreach ($reminderTimes as $time) {
42 $reminderTimestamp = $this->getReminderTime($time);
43
44 $happeningTimestamp = strtotime($booking->start_time);
45 $startingTo = $happeningTimestamp - time();
46
47 $bufferTime = 2 * 60; // 2 Minute Buffer Time
48 if ($startingTo > ($reminderTimestamp + $bufferTime)) {
49 as_schedule_single_action(($happeningTimestamp - $reminderTimestamp), 'fluent_booking/booking_schedule_reminder', [
50 $booking->id,
51 $emailTo
52 ], 'fluent-booking');
53 }
54 }
55 }
56
57 private function clearGroupRemindersForHost($booking, $emailTo)
58 {
59 if (!$booking->isMultiGuestBooking() || $emailTo != 'host') {
60 return;
61 }
62
63 $otherBookingIds = Booking::where('group_id', $booking->group_id)->where('id', '!=', $booking->id)->pluck('id')->toArray();
64
65 foreach ($otherBookingIds as $otherBookingId) {
66 \as_unschedule_all_actions('fluent_booking/booking_schedule_reminder', [$otherBookingId, 'host'], 'fluent-booking');
67 }
68 }
69
70 public function pushBookingScheduledToQueue($booking, $bookingEvent)
71 {
72 $notifications = $bookingEvent->getNotifications();
73
74 if (Arr::isTrue($notifications, 'booking_conf_attendee.enabled') || (Arr::isTrue($notifications, 'booking_conf_host.enabled'))) {
75 as_enqueue_async_action('fluent_booking/after_booking_scheduled_async', [
76 $booking->id,
77 $bookingEvent->id
78 ], 'fluent-booking');
79 }
80
81 if (Arr::isTrue($notifications, 'reminder_to_attendee.enabled')) {
82 $reminderTimes = Arr::get($notifications, 'reminder_to_attendee.email.times', []);
83 $this->pushRemindersToQueue($booking, $reminderTimes, 'guest');
84 }
85
86 if (Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
87 $reminderTimes = Arr::get($notifications, 'reminder_to_host.email.times', []);
88 $this->pushRemindersToQueue($booking, $reminderTimes, 'host');
89 }
90 }
91
92 public function pushBookingPendingToQueue($booking, $bookingEvent)
93 {
94 if (!$bookingEvent->isConfirmationEnabled() || $bookingEvent->isMultiGuestEvent()) {
95 return;
96 }
97
98 if ($booking->payment_method && $booking->payment_status != 'paid') {
99 return;
100 }
101
102 $notifications = $bookingEvent->getNotifications();
103
104 if (Arr::isTrue($notifications, 'booking_request_host.enabled') || (Arr::isTrue($notifications, 'booking_request_attendee.enabled'))) {
105 as_enqueue_async_action('fluent_booking/after_booking_pending_async', [
106 $booking->id,
107 $bookingEvent->id
108 ], 'fluent-booking');
109 }
110 }
111
112 public function emailToUpdatedEmail($booking, $calendarEvent)
113 {
114 $notifications = $calendarEvent->getNotifications();
115
116 if (Arr::isTrue($notifications, 'booking_conf_attendee.enabled') || (Arr::isTrue($notifications, 'booking_conf_host.enabled'))) {
117 as_enqueue_async_action('fluent_booking/after_booking_scheduled_async', [
118 $booking->id,
119 $calendarEvent->id
120 ], 'fluent-booking');
121 }
122 }
123
124 public function bookingScheduledEmails($bookingId)
125 {
126 $booking = Booking::with(['calendar', 'calendar_event'])->find($bookingId);
127
128 if (!$booking || !$booking->calendar_event) {
129 return '';
130 }
131
132 $notifications = $booking->calendar_event->getNotifications();
133
134 if (Arr::isTrue($notifications, 'booking_conf_attendee.enabled')) {
135 $email = Arr::get($notifications, 'booking_conf_attendee.email', []);
136 EmailNotificationService::emailOnBooked($booking, $email, 'guest');
137 }
138
139 if (Arr::isTrue($notifications, 'booking_conf_host.enabled')) {
140 $email = Arr::get($notifications, 'booking_conf_host.email', []);
141 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
142 if ($additionalRecipients) {
143 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
144 }
145 EmailNotificationService::emailOnBooked($booking, $email, 'host');
146 }
147
148 return true;
149 }
150
151 /**
152 * @param $booking Booking
153 * @return void
154 */
155 public function bookingReminderEmails($bookingId, $emailTo)
156 {
157 $booking = Booking::with(['calendar_event', 'calendar'])->find($bookingId);
158
159 if (!$booking || $booking->status != 'scheduled') {
160 return false;
161 }
162
163 $notifications = $booking->calendar_event->getNotifications();
164
165 if (!$notifications) {
166 return;
167 }
168
169 if ('guest' == $emailTo && Arr::isTrue($notifications, 'reminder_to_attendee.enabled')) {
170 $email = Arr::get($notifications, 'reminder_to_attendee.email', []);
171 EmailNotificationService::reminderEmail($booking, $email, $emailTo);
172 }
173
174 if ('host' == $emailTo && Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
175 $email = Arr::get($notifications, 'reminder_to_host.email', []);
176 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
177 if ($additionalRecipients) {
178 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
179 }
180 EmailNotificationService::reminderEmail($booking, $email, $emailTo);
181 }
182 }
183
184 public function bookingRequestEmails($bookingId, $calendarEventId)
185 {
186 $booking = Booking::with(['calendar', 'calendar_event'])->find($bookingId);
187
188 if (!$booking || !$booking->calendar_event) {
189 return '';
190 }
191
192 $notifications = $booking->calendar_event->getNotifications();
193
194 if (Arr::isTrue($notifications, 'booking_request_attendee.enabled')) {
195 $email = Arr::get($notifications, 'booking_request_attendee.email', []);
196 EmailNotificationService::emailOnBooked($booking, $email, 'guest', 'request');
197 }
198
199 if (Arr::isTrue($notifications, 'booking_request_host.enabled')) {
200 $email = Arr::get($notifications, 'booking_request_host.email', []);
201 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
202 if ($additionalRecipients) {
203 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
204 }
205 EmailNotificationService::emailOnBooked($booking, $email, 'host', 'request');
206 }
207
208 return true;
209 }
210
211 public function getAdditionalRecipients($additionalRecipients)
212 {
213 if ($additionalRecipients) {
214 $recipients = explode(',', $additionalRecipients);
215 $recipients = array_map('trim', $recipients);
216 return array_unique($recipients);
217 }
218 return [];
219 }
220
221 public function emailOnBookingCancelled(Booking $booking, $calendarEvent)
222 {
223 if (!$calendarEvent) {
224 return;
225 }
226
227 $notifications = $calendarEvent->getNotifications();
228 if (!$notifications) {
229 return;
230 }
231
232 $cancelledBy = $booking->getMeta('cancelled_by_type', 'host');
233
234 if ($cancelledBy == 'host') {
235 if (Arr::isTrue($notifications, 'cancelled_by_host.enabled')) {
236 // This from the host
237 $email = Arr::get($notifications, 'cancelled_by_host.email', []);
238 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'guest');
239 }
240 return;
241 }
242
243 if (Arr::isTrue($notifications, 'cancelled_by_attendee.enabled')) {
244 $email = Arr::get($notifications, 'cancelled_by_attendee.email', []);
245 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
246 if ($additionalRecipients) {
247 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
248 }
249 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'host');
250 }
251 }
252
253 public function emailOnBookingRescheduled(Booking $booking, $oldBooking, $calendarEvent)
254 {
255 if (!$calendarEvent) {
256 return;
257 }
258
259 $notifications = $calendarEvent->getNotifications();
260 if (!$notifications) {
261 return;
262 }
263
264 // Remove all reminders
265 \as_unschedule_all_actions('fluent_booking/booking_schedule_reminder', [$oldBooking->id, 'host'], 'fluent-booking');
266 \as_unschedule_all_actions('fluent_booking/booking_schedule_reminder', [$oldBooking->id, 'guest'], 'fluent-booking');
267
268 if (Arr::isTrue($notifications, 'reminder_to_attendee.enabled')) {
269 $reminderTimes = Arr::get($notifications, 'reminder_to_attendee.email.times', []);
270 $this->pushRemindersToQueue($booking, $reminderTimes, 'guest');
271 }
272
273 if (Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
274 $reminderTimes = Arr::get($notifications, 'reminder_to_host.email.times', []);
275 $this->pushRemindersToQueue($booking, $reminderTimes, 'host');
276 }
277
278 $rescheduledBy = $booking->getMeta('rescheduled_by_type', 'host');
279
280 if ($rescheduledBy == 'host') {
281 if (Arr::isTrue($notifications, 'rescheduled_by_host.enabled')) {
282 // This from the host
283 $email = Arr::get($notifications, 'rescheduled_by_host.email', []);
284 EmailNotificationService::bookingRescheduledEmail($booking, $email, 'guest');
285 }
286 return;
287 }
288
289 if (Arr::isTrue($notifications, 'rescheduled_by_attendee.enabled')) {
290 $email = Arr::get($notifications, 'rescheduled_by_attendee.email', []);
291 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
292 if ($additionalRecipients) {
293 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
294 }
295 EmailNotificationService::bookingRescheduledEmail($booking, $email, 'host');
296 }
297 }
298
299 public function emailOnBookingRejected(Booking $booking, $calendarEvent)
300 {
301 if (!$calendarEvent) {
302 return;
303 }
304
305 $notifications = $calendarEvent->getNotifications();
306 if (!$notifications) {
307 return;
308 }
309
310 if (Arr::isTrue($notifications, 'declined_by_host.enabled')) {
311 $email = Arr::get($notifications, 'declined_by_host.email', []);
312 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'guest', 'reject');
313 }
314 }
315 }
316