| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services\Integrations\FluentCRM; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Booking; |
| 6 |
use FluentBooking\App\Services\DateTimeHelper; |
| 7 |
|
| 8 |
class FluentCrmInit |
| 9 |
{ |
| 10 |
public function __construct() |
| 11 |
{ |
| 12 |
$this->registerHooks(); |
| 13 |
$this->registerIntegrations(); |
| 14 |
|
| 15 |
// Contextual SmartCodes |
| 16 |
(new CrmSmartCode())->register(); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Register all the CRM integrations from here |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function registerIntegrations() |
| 24 |
{ |
| 25 |
$this->addContactMenuSection(); |
| 26 |
$this->addAutomations(); |
| 27 |
} |
| 28 |
|
| 29 |
public function registerHooks() |
| 30 |
{ |
| 31 |
add_filter('fluentcrm_profile_sections', [$this, 'addProfileSection'], 10, 1); |
| 32 |
add_filter('fluentcrm_get_form_submissions_fluent_booking', [$this, 'getScheduledMeetings'], 10, 2); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* load Assets for to Fluent CRM contact section |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function addContactMenuSection() |
| 40 |
{ |
| 41 |
add_action('fluent_crm/global_appjs_loaded', function () { |
| 42 |
wp_enqueue_script('fluent_booking_in_crm', FLUENT_BOOKING_URL . 'assets/admin/fluent-crm-in-calendar.js', [], FLUENT_BOOKING_ASSETS_VERSION, true); |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
public function addAutomations() |
| 47 |
{ |
| 48 |
new NewBookingTrigger(); |
| 49 |
new CancelBookingTrigger(); |
| 50 |
new BookingCompletedTrigger(); |
| 51 |
new BookingRescheduledTrigger(); |
| 52 |
} |
| 53 |
|
| 54 |
private function getSubscriberId($email) |
| 55 |
{ |
| 56 |
$contact = FluentCrmApi('contacts')->getContact($email); |
| 57 |
return $contact ? $contact->id : null; |
| 58 |
} |
| 59 |
|
| 60 |
public function addProfileSection($sections) |
| 61 |
{ |
| 62 |
$sections['booking'] = [ |
| 63 |
'name' => 'booking', |
| 64 |
'title' => __('Bookings', 'fluent-booking'), |
| 65 |
'handler' => 'route' |
| 66 |
]; |
| 67 |
|
| 68 |
return $sections; |
| 69 |
} |
| 70 |
|
| 71 |
private function getActionUrl($meeting) |
| 72 |
{ |
| 73 |
$url = admin_url('admin.php?page=fluent-booking#/scheduled-events?booking_id=' . $meeting->id); |
| 74 |
|
| 75 |
$link = '<a target="_blank" href="' . esc_url($url) . '">' . 'view' . '</a>'; |
| 76 |
|
| 77 |
return $link; |
| 78 |
} |
| 79 |
|
| 80 |
private function getFormattedTime($meeting) |
| 81 |
{ |
| 82 |
$formattedTime = DateTimeHelper::convertToTimeZone($meeting->start_time, 'utc', $meeting->calendar->author_timezone, 'j M Y, g:i A'); |
| 83 |
|
| 84 |
return $formattedTime; |
| 85 |
} |
| 86 |
|
| 87 |
private function getBookingTitle($meeting) |
| 88 |
{ |
| 89 |
$host = $meeting->calendar->getAuthorProfile(); |
| 90 |
$title = $meeting->slot->title . ' with ' . $host['name']; |
| 91 |
|
| 92 |
return $title; |
| 93 |
} |
| 94 |
|
| 95 |
public function getScheduledMeetings($data, $subsriber) |
| 96 |
{ |
| 97 |
$app = fluentCrm(); |
| 98 |
$page = intval($app->request->get('page', 1)); |
| 99 |
$perPage = intval($app->request->get('per_page', 10)); |
| 100 |
|
| 101 |
$meetings = Booking::with(['slot', 'calendar']) |
| 102 |
->where('email', $subsriber->email) |
| 103 |
->distinct('group_id') |
| 104 |
->orderBy('start_time', 'DESC') |
| 105 |
->paginate(); |
| 106 |
|
| 107 |
$formattedMeetings = []; |
| 108 |
|
| 109 |
foreach ($meetings->items() as $meeting) { |
| 110 |
if (!$meeting->calendar || !$meeting->slot) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
$formattedMeetings[] = [ |
| 115 |
'id' => '#' . $meeting->group_id, |
| 116 |
'title' => $this->getBookingTitle($meeting), |
| 117 |
'status' => $meeting->status, |
| 118 |
'meeting_at' => $this->getFormattedTime($meeting), |
| 119 |
'action' => $this->getActionUrl($meeting) |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
return [ |
| 124 |
'total' => $meetings->total(), |
| 125 |
'data' => $formattedMeetings, |
| 126 |
'columns_config' => [ |
| 127 |
'id' => [ |
| 128 |
'label' => __('ID', 'fluent-booking'), |
| 129 |
'width' => '100px' |
| 130 |
], |
| 131 |
'title' => [ |
| 132 |
'label' => __('Event', 'fluent-booking'), |
| 133 |
], |
| 134 |
'status' => [ |
| 135 |
'label' => __('Status', 'fluent-booking'), |
| 136 |
'width' => '150px' |
| 137 |
], |
| 138 |
'meeting_at' => [ |
| 139 |
'label' => __('Meeting At', 'fluent-booking'), |
| 140 |
'width' => '200px' |
| 141 |
], |
| 142 |
'action' => [ |
| 143 |
'label' => __('Action', 'fluent-booking'), |
| 144 |
'width' => '100px' |
| 145 |
] |
| 146 |
] |
| 147 |
]; |
| 148 |
} |
| 149 |
} |
| 150 |
|