| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services\Integrations\FluentCRM; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Booking; |
| 6 |
use FluentBooking\Framework\Support\Arr; |
| 7 |
use FluentBooking\App\Services\DateTimeHelper; |
| 8 |
use FluentCrm\App\Models\Subscriber; |
| 9 |
use FluentCrm\App\Services\Html\TableBuilder; |
| 10 |
|
| 11 |
class FluentCrmInit |
| 12 |
{ |
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$this->registerHooks(); |
| 16 |
$this->registerIntegrations(); |
| 17 |
|
| 18 |
// Contextual SmartCodes |
| 19 |
(new CrmSmartCode())->register(); |
| 20 |
|
| 21 |
add_filter('fluent_crm_asset_listed_slugs', function ($lists) { |
| 22 |
$lists[] = 'fluent-booking'; |
| 23 |
return $lists; |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
public function registerIntegrations() |
| 28 |
{ |
| 29 |
$this->addAutomations(); |
| 30 |
} |
| 31 |
|
| 32 |
public function registerHooks() |
| 33 |
{ |
| 34 |
add_filter('fluentcrm_profile_sections', [$this, 'addProfileSection'], 10, 1); |
| 35 |
add_filter('fluencrm_profile_section_fluent_booking', [$this, 'getProfileSection'], 10, 2); |
| 36 |
} |
| 37 |
|
| 38 |
public function addAutomations() |
| 39 |
{ |
| 40 |
new NewBookingTrigger(); |
| 41 |
new CancelBookingTrigger(); |
| 42 |
new BookingCompletedTrigger(); |
| 43 |
new BookingRescheduledTrigger(); |
| 44 |
} |
| 45 |
|
| 46 |
public function addProfileSection($sections) |
| 47 |
{ |
| 48 |
$sections['booking'] = [ |
| 49 |
'name' => 'fluentcrm_profile_extended', |
| 50 |
'title' => __('Bookings', 'fluent-booking'), |
| 51 |
'handler' => 'route', |
| 52 |
'query' => [ |
| 53 |
'handler' => 'fluent_booking' |
| 54 |
], |
| 55 |
]; |
| 56 |
|
| 57 |
return $sections; |
| 58 |
} |
| 59 |
|
| 60 |
public function getProfileSection($sections, Subscriber $contact) |
| 61 |
{ |
| 62 |
if (!current_user_can('fcrm_read_contacts') && !current_user_can('manage_options')) { |
| 63 |
return $sections; |
| 64 |
} |
| 65 |
|
| 66 |
$sections['heading'] = __('Bookings (Fluent Booking)', 'fluent-booking'); |
| 67 |
|
| 68 |
$limit = (int) apply_filters('fluent_booking/crm_meetings_limit', 20); |
| 69 |
$limit = max(1, $limit); |
| 70 |
|
| 71 |
$email = strtolower(trim((string) $contact->email)); |
| 72 |
|
| 73 |
$base = Booking::where('email', $email); |
| 74 |
|
| 75 |
$total = (clone $base)->count(); |
| 76 |
|
| 77 |
$meetings = (clone $base) |
| 78 |
->with(['slot', 'calendar']) |
| 79 |
->orderBy('start_time', 'DESC') |
| 80 |
->limit($limit) |
| 81 |
->get(); |
| 82 |
|
| 83 |
$rows = []; |
| 84 |
$hostCache = []; |
| 85 |
foreach ($meetings as $meeting) { |
| 86 |
if (!$meeting->calendar || !$meeting->slot) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
$cid = $meeting->calendar->id; |
| 90 |
if (!isset($hostCache[$cid])) { |
| 91 |
$hostCache[$cid] = $meeting->calendar->getAuthorProfile(); |
| 92 |
} |
| 93 |
|
| 94 |
$rows[] = apply_filters('fluent_booking/crm_meeting_data', $this->mapMeetingRow($meeting, $hostCache[$cid]), $meeting); |
| 95 |
} |
| 96 |
|
| 97 |
$response = apply_filters('fluent_booking/crm_meetings_response', [ |
| 98 |
'total' => $total, |
| 99 |
'data' => $rows, |
| 100 |
'columns_config' => [ |
| 101 |
'id' => ['label' => __('ID', 'fluent-booking'), 'width' => '100px'], |
| 102 |
'title' => ['label' => __('Event', 'fluent-booking')], |
| 103 |
'status' => ['label' => __('Status', 'fluent-booking'), 'width' => '150px'], |
| 104 |
'meeting_at' => ['label' => __('Meeting At', 'fluent-booking'), 'width' => '200px'], |
| 105 |
'action' => ['label' => __('Action', 'fluent-booking'), 'width' => '100px'], |
| 106 |
], |
| 107 |
], $meetings, $contact); |
| 108 |
|
| 109 |
if (empty($response['data'])) { |
| 110 |
$sections['content_html'] = '<p style="padding:0 20px;">' . esc_html__('No scheduled meetings found for this contact.', 'fluent-booking') . '</p>'; |
| 111 |
return $sections; |
| 112 |
} |
| 113 |
|
| 114 |
$header = []; |
| 115 |
foreach ($response['columns_config'] as $key => $col) { |
| 116 |
$header[$key] = Arr::get($col, 'label', ucfirst($key)); |
| 117 |
} |
| 118 |
|
| 119 |
$table = new TableBuilder(); |
| 120 |
$table->setHeader($header); |
| 121 |
foreach ($response['data'] as $row) { |
| 122 |
$table->addRow($row); |
| 123 |
} |
| 124 |
|
| 125 |
$sections['content_html'] = $table->getHtml() . $this->getViewAllLink($contact, $total, $limit); |
| 126 |
|
| 127 |
return $sections; |
| 128 |
} |
| 129 |
|
| 130 |
private function mapMeetingRow($meeting, $host) |
| 131 |
{ |
| 132 |
$groupRef = $meeting->group_id ?: $meeting->id; |
| 133 |
|
| 134 |
return [ |
| 135 |
'id' => '#' . $groupRef, |
| 136 |
'title' => $this->getBookingTitle($meeting, $host), |
| 137 |
'status' => $meeting->status, |
| 138 |
'meeting_at' => $this->getFormattedTime($meeting), |
| 139 |
'action' => $this->getActionUrl($meeting), |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
private function getViewAllLink(Subscriber $contact, $total, $limit) |
| 144 |
{ |
| 145 |
if ($total <= $limit) { |
| 146 |
return ''; |
| 147 |
} |
| 148 |
|
| 149 |
$url = admin_url('admin.php?page=fluent-booking#/scheduled-events?email=' . rawurlencode($contact->email) . '&period=all&author=all'); |
| 150 |
|
| 151 |
return '<p class="fcal_crm_view_all"><a style="color:#2271b1;padding:0 12px;text-decoration:underline" href="' . esc_url($url) . '">' |
| 152 |
/* translators: %d: total number of meetings */ |
| 153 |
. sprintf(esc_html__('View all meetings (%d)', 'fluent-booking'), (int) $total) |
| 154 |
. '</a></p>'; |
| 155 |
} |
| 156 |
|
| 157 |
private function getActionUrl($meeting) |
| 158 |
{ |
| 159 |
$url = admin_url('admin.php?page=fluent-booking#/scheduled-events?booking_id=' . $meeting->id); |
| 160 |
|
| 161 |
return '<a target="_blank" href="' . esc_url($url) . '">' . esc_html__('View', 'fluent-booking') . '</a>'; |
| 162 |
} |
| 163 |
|
| 164 |
private function getFormattedTime($meeting) |
| 165 |
{ |
| 166 |
return DateTimeHelper::convertToTimeZone($meeting->start_time, 'utc', $meeting->calendar->author_timezone, 'j M Y, g:i A'); |
| 167 |
} |
| 168 |
|
| 169 |
private function getBookingTitle($meeting, $host = null) |
| 170 |
{ |
| 171 |
if ($host === null) { |
| 172 |
$host = $meeting->calendar->getAuthorProfile(); |
| 173 |
} |
| 174 |
|
| 175 |
return sprintf( |
| 176 |
/* translators: 1: meeting title, 2: host name */ |
| 177 |
__('%1$s with %2$s', 'fluent-booking'), |
| 178 |
(string) $meeting->slot->title, |
| 179 |
(string) $host['name'] |
| 180 |
); |
| 181 |
} |
| 182 |
} |
| 183 |
|