| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services\Integrations\FluentCRM; |
| 4 |
|
| 5 |
use FluentBooking\Framework\Support\Arr; |
| 6 |
use FluentBooking\App\Models\Calendar; |
| 7 |
use FluentCrm\App\Services\Funnel\FunnelHelper; |
| 8 |
use FluentCrm\App\Services\Funnel\FunnelProcessor; |
| 9 |
use FluentBooking\App\Services\PermissionManager; |
| 10 |
use FluentCrm\App\Services\Funnel\BaseTrigger; |
| 11 |
use FluentBooking\App\Services\CalendarService; |
| 12 |
|
| 13 |
class BookingCompletedTrigger extends BaseTrigger |
| 14 |
{ |
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
$this->triggerName = 'fluent_booking/booking_schedule_completed'; |
| 18 |
$this->actionArgNum = 1; |
| 19 |
$this->priority = 20; |
| 20 |
parent::__construct(); |
| 21 |
} |
| 22 |
|
| 23 |
public function getCalendarOptions() |
| 24 |
{ |
| 25 |
$calendarOptions = CalendarService::getCalendarOptionsByTitle(); |
| 26 |
|
| 27 |
return apply_filters('fluent_booking/crm_trigger_calendar_options', $calendarOptions); |
| 28 |
} |
| 29 |
|
| 30 |
public function getTrigger() |
| 31 |
{ |
| 32 |
return [ |
| 33 |
'category' => __('Booking', 'fluent-booking'), |
| 34 |
'label' => __('Booking Completed', 'fluent-booking'), |
| 35 |
'description' => __('This Funnel will be initiated when a booking has been marked as completed (manually or automatically)', 'fluent-booking') |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
public function getFunnelSettingsDefaults() |
| 40 |
{ |
| 41 |
return [ |
| 42 |
'subscription_status' => 'subscribed' |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
public function getFunnelConditionDefaults($funnel) |
| 47 |
{ |
| 48 |
return [ |
| 49 |
'run_only_one' => 'no' |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
public function getConditionFields($funnel) |
| 54 |
{ |
| 55 |
return [ |
| 56 |
'run_only_one' => [ |
| 57 |
'type' => 'yes_no_check', |
| 58 |
'label' => '', |
| 59 |
'check_label' => __('Run this automation only once per contact. If unchecked then it will over-write existing flow', 'fluent-booking'), |
| 60 |
'help' => __('If you enable this then this will run only once per customer otherwise, It will delete the existing automation flow and start new', 'fluent-booking'), |
| 61 |
'options' => FunnelHelper::getUpdateOptions() |
| 62 |
], |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public function getSettingsFields($funnel) |
| 67 |
{ |
| 68 |
return [ |
| 69 |
'title' => __('Booking Completed Funnel', 'fluent-booking'), |
| 70 |
'sub_title' => __('This Funnel will be initiated when a booking has been marked as completed (manually or automatically)', 'fluent-booking'), |
| 71 |
'fields' => [ |
| 72 |
'event_id' => [ |
| 73 |
'type' => 'grouped-select', |
| 74 |
'label' => __('Booking Calendar', 'fluent-booking'), |
| 75 |
'placeholder' => __('Select Calendar', 'fluent-booking'), |
| 76 |
'is_multiple' => false, |
| 77 |
'options' => $this->getCalendarOptions() |
| 78 |
], |
| 79 |
'subscription_status' => [ |
| 80 |
'type' => 'option_selectors', |
| 81 |
'option_key' => 'editable_statuses', |
| 82 |
'is_multiple' => false, |
| 83 |
'label' => __('Subscription Status', 'fluent-booking'), |
| 84 |
'placeholder' => __('Select Status', 'fluent-booking') |
| 85 |
], |
| 86 |
'subscription_status_info' => [ |
| 87 |
'type' => 'html', |
| 88 |
'info' => '<b>' . __('An Automated double-optin email will be sent for new subscribers', 'fluent-booking') . '</b>', |
| 89 |
'dependency' => [ |
| 90 |
'depends_on' => 'subscription_status', |
| 91 |
'operator' => '=', |
| 92 |
'value' => 'pending' |
| 93 |
] |
| 94 |
] |
| 95 |
] |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
public function handle($funnel, $originalArgs) |
| 100 |
{ |
| 101 |
$booking = $originalArgs[0]; |
| 102 |
|
| 103 |
$willProcess = $this->isProcessable($funnel, $booking); |
| 104 |
|
| 105 |
$willProcess = apply_filters('fluentcrm_funnel_will_process_' . $this->triggerName, $willProcess, $funnel, $originalArgs); |
| 106 |
|
| 107 |
if (!$willProcess) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$subscriberData = array_filter([ |
| 112 |
'first_name' => $booking->first_name, |
| 113 |
'last_name' => $booking->last_name, |
| 114 |
'email' => $booking->email, |
| 115 |
'phone' => $booking->phone, |
| 116 |
'user_id' => $booking->person_user_id, |
| 117 |
'timezone' => $booking->person_time_zone, |
| 118 |
'status' => Arr::get($funnel->settings, 'subscription_status'), |
| 119 |
]); |
| 120 |
|
| 121 |
(new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [ |
| 122 |
'source_trigger_name' => $this->triggerName, |
| 123 |
'source_ref_id' => $booking->id |
| 124 |
]); |
| 125 |
} |
| 126 |
|
| 127 |
private function isProcessable($funnel, $booking) |
| 128 |
{ |
| 129 |
$slotId = Arr::get($funnel->settings, 'event_id'); |
| 130 |
|
| 131 |
if ($slotId != $booking->event_id) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
$subscriber = FunnelHelper::getSubscriber($booking->email); |
| 136 |
|
| 137 |
if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) { |
| 138 |
|
| 139 |
$runMultiple = Arr::get($funnel->conditions, 'run_only_one') == 'no'; |
| 140 |
|
| 141 |
if ($runMultiple) { |
| 142 |
FunnelHelper::removeSubscribersFromFunnel($funnel->id, [$subscriber->id]); |
| 143 |
} |
| 144 |
|
| 145 |
return $runMultiple; |
| 146 |
} |
| 147 |
|
| 148 |
return true; |
| 149 |
} |
| 150 |
} |
| 151 |
|