| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services\Integrations\FluentForms; |
| 4 |
|
| 5 |
|
| 6 |
use FluentBooking\App\App; |
| 7 |
use FluentBooking\App\Services\CalendarService; |
| 8 |
use FluentBooking\App\Services\BookingFieldService; |
| 9 |
use FluentBooking\Framework\Support\Arr; |
| 10 |
use FluentBooking\App\Services\Helper; |
| 11 |
use FluentBooking\App\Models\Booking; |
| 12 |
use FluentBooking\App\Models\Calendar; |
| 13 |
use FluentBooking\App\Models\CalendarSlot; |
| 14 |
use FluentBooking\App\Services\DateTimeHelper; |
| 15 |
use FluentBooking\App\Services\PermissionManager; |
| 16 |
use FluentBooking\App\Hooks\Handlers\FrontEndHandler; |
| 17 |
use FluentForm\App\Services\FormBuilder\BaseFieldManager; |
| 18 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 19 |
|
| 20 |
class BookingElement extends BaseFieldManager |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Wrapper class for repeat element |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $wrapperClass = 'fcal_booking_elem'; |
| 27 |
|
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
parent::__construct( |
| 31 |
'fcal_booking', |
| 32 |
'Calendar Booking', |
| 33 |
['booking', 'calendar'], |
| 34 |
'advanced' |
| 35 |
); |
| 36 |
|
| 37 |
add_filter('fluentform/response_render_fcal_booking', array($this, 'renderResponse'), 10, 3); |
| 38 |
add_filter('fluentform/select_group_component_ajax_options', array($this, 'getCalendarOptions')); |
| 39 |
|
| 40 |
add_action('fluentform/loading_editor_assets', function () { |
| 41 |
wp_enqueue_script('fluentcal_ff_editor_extended', FLUENT_BOOKING_URL . 'assets/admin/fluentform.js', [], '1.0.0', true); |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
function getComponent() |
| 46 |
{ |
| 47 |
return [ |
| 48 |
'index' => 20, |
| 49 |
'element' => 'fcal_booking', |
| 50 |
'attributes' => array( |
| 51 |
'name' => 'fcal_booking', |
| 52 |
'data-type' => 'fcal_booking' |
| 53 |
), |
| 54 |
'settings' => array( |
| 55 |
'label' => __('Select Appointment Date & Time', 'fluent-booking'), |
| 56 |
'admin_field_label' => '', |
| 57 |
'event_id' => '', |
| 58 |
'booking_calendar' => '', |
| 59 |
'conditional_logics' => array(), |
| 60 |
'container_class' => '', |
| 61 |
'cal_guest_fields' => [ |
| 62 |
'email_field' => '', |
| 63 |
'name_field' => '', |
| 64 |
'host_info' => 'show' |
| 65 |
], |
| 66 |
'validation_rules' => array( |
| 67 |
'required' => [ |
| 68 |
'value' => false, |
| 69 |
'message' => __('Appointment Date & Time is required', 'fluent-booking'), |
| 70 |
], |
| 71 |
), |
| 72 |
), |
| 73 |
'editor_options' => array( |
| 74 |
'title' => __('FluentBooking Field', 'fluent-booking'), |
| 75 |
'icon_class' => 'el-icon-date', |
| 76 |
'template' => 'inputCalendar' |
| 77 |
), |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
public function getGeneralEditorElements() |
| 82 |
{ |
| 83 |
return [ |
| 84 |
'booking_calendar', |
| 85 |
'label', |
| 86 |
'label_placement', |
| 87 |
'admin_field_label', |
| 88 |
'event_id', |
| 89 |
'cal_guest_fields', |
| 90 |
'validation_rules', |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
public function getAdvancedEditorElements() |
| 95 |
{ |
| 96 |
return [ |
| 97 |
'container_class', |
| 98 |
'name', |
| 99 |
'conditional_logics' |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
public function getEditorCustomizationSettings() |
| 104 |
{ |
| 105 |
return [ |
| 106 |
'event_id' => [ |
| 107 |
'template' => 'selectGroup', |
| 108 |
'label' => __('Select Calendar', 'fluent-booking'), |
| 109 |
], |
| 110 |
'cal_guest_fields' => [ |
| 111 |
'template' => 'CustomSettingsField', |
| 112 |
'label' => __('Guest Fields', 'fluent-booking'), |
| 113 |
'componentName' => 'FluentCalNameEmailChoiceComponent' |
| 114 |
], |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Compile and echo the html element |
| 120 |
* @param array $data [element data] |
| 121 |
* @param object $form [Form Object] |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function render($data, $form) |
| 125 |
{ |
| 126 |
$elementName = $data['element']; |
| 127 |
|
| 128 |
$data['attributes']['class'] = @trim('ff-el-form-control ' . Arr::get($data, 'attributes.class')); |
| 129 |
$data['attributes']['id'] = $this->makeElementId($data, $form); |
| 130 |
if ($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) { |
| 131 |
$data['attributes']['tabindex'] = $tabIndex; |
| 132 |
} |
| 133 |
|
| 134 |
$ariaRequired = 'false'; |
| 135 |
if (Arr::get($data, 'settings.validation_rules.required.value')) { |
| 136 |
$ariaRequired = 'true'; |
| 137 |
} |
| 138 |
|
| 139 |
$slot_id = (int)Arr::get($data, 'settings.event_id'); |
| 140 |
|
| 141 |
$calendarEvent = CalendarSlot::find($slot_id); |
| 142 |
|
| 143 |
if (!$calendarEvent || !$calendarEvent->calendar) { |
| 144 |
esc_html_e('Selected Calendar could not be found', 'fluent-booking'); |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
$isHostEnabled = Arr::get($data, 'settings.cal_guest_fields.host_info', 'hide') == 'show'; |
| 149 |
|
| 150 |
$showHostInfo = $isHostEnabled || Arr::isTrue($calendarEvent->settings, 'multi_duration.enabled'); |
| 151 |
|
| 152 |
[$localizeData, $element_id] = (new FluentFormInit())->getLocalizedData($calendarEvent, $data, $form); |
| 153 |
|
| 154 |
$localizeData['time_format'] = (Helper::getGlobalSettings())['time_format']; |
| 155 |
|
| 156 |
$assetUrl = App::getInstance('url.assets'); |
| 157 |
|
| 158 |
wp_enqueue_script( |
| 159 |
'fluentform-calendar-public', |
| 160 |
$assetUrl . 'public/js/fluentform.js', [], |
| 161 |
FLUENT_BOOKING_ASSETS_VERSION, true |
| 162 |
); |
| 163 |
|
| 164 |
if (BookingFieldService::hasPhoneNumberField($localizeData['form_fields'])) { |
| 165 |
wp_enqueue_script('fluent-booking-phone-field', $assetUrl . 'public/js/phone-field.js', [], FLUENT_BOOKING_ASSETS_VERSION, true); |
| 166 |
?> |
| 167 |
<style> |
| 168 |
.fcal_phone_wrapper .flag { |
| 169 |
background: url(<?php echo esc_url($assetUrl.'images/flags_responsive.png'); ?>) no-repeat; |
| 170 |
background-size: 100%; |
| 171 |
} |
| 172 |
</style> |
| 173 |
<?php |
| 174 |
} |
| 175 |
|
| 176 |
wp_localize_script('fluentform-calendar-public', 'fcal_public_vars_' . $element_id, $localizeData); |
| 177 |
|
| 178 |
wp_localize_script('fluentform-calendar-public', 'fluentCalendarPublicVars', |
| 179 |
(new FrontEndHandler())->getGlobalVars() |
| 180 |
); |
| 181 |
|
| 182 |
$calClass = 'fluentform_calendar_app'; |
| 183 |
|
| 184 |
if ($showHostInfo) { |
| 185 |
$calClass .= ' fcal_showing_host'; |
| 186 |
} else { |
| 187 |
$calClass .= ' fcal_not_showing_host'; |
| 188 |
} |
| 189 |
|
| 190 |
$elMarkup = '<div class="fcal_cal_wrap"><div class="' . esc_attr($calClass) . '" data-element_id="' . esc_attr($element_id) . '"></div></div>'; |
| 191 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 192 |
echo apply_filters('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $html is escaped before being passed in. |
| 193 |
} |
| 194 |
|
| 195 |
public function renderResponse($data, $field, $form_id) |
| 196 |
{ |
| 197 |
if (is_string($data)) { |
| 198 |
$data = json_decode($data, true); |
| 199 |
} |
| 200 |
|
| 201 |
if (!$data) { |
| 202 |
return ''; |
| 203 |
} |
| 204 |
|
| 205 |
$data = (array)$data; |
| 206 |
|
| 207 |
$text = Arr::get($data, 'start_time') . ' ( ' . Arr::get($data, 'timezone') . ' )'; |
| 208 |
|
| 209 |
if (defined('FLUENTFORM_RENDERING_ENTRY')) { |
| 210 |
|
| 211 |
$booking = Booking::find(Arr::get($data, 'booking_id')); |
| 212 |
|
| 213 |
if ($booking && $booking->calendar) { |
| 214 |
$calendar = $booking->calendar; |
| 215 |
$html = '<div class="ff_entry_table_wrapper"><table class="ff_entry_table_field ff-table">'; |
| 216 |
$html .= '<tr>'; |
| 217 |
$html .= '<th>' . __('Booking ID', 'fluent-booking') . '</th>'; |
| 218 |
$html .= '<td>' . $booking->id . ' <a href="' . Helper::getAppBaseUrl('scheduled-events?period=upcoming&booking_id=' . $booking->id) . '" target="_blank">View Booking</a></td>'; |
| 219 |
$html .= '</tr>'; |
| 220 |
$html .= '<tr>'; |
| 221 |
$html .= '<th>' . __('Booking Status', 'fluent-booking') . '</th>'; |
| 222 |
$html .= '<td>' . $booking->status . '</td>'; |
| 223 |
$html .= '</tr>'; |
| 224 |
$html .= '<tr>'; |
| 225 |
$html .= '<th>' . __('Date & Time', 'fluent-booking') . '</th>'; |
| 226 |
$html .= '<td>' . $booking->getFullBookingDateTimeText($calendar->author_timezone, true) . ' (' . $calendar->author_timezone . ')</td>'; |
| 227 |
$html .= '</tr>'; |
| 228 |
$html .= '<tr>'; |
| 229 |
$html .= '<th>' . __('Meeting Duration', 'fluent-booking') . '</th>'; |
| 230 |
$html .= '<td>' . $booking->slot_minutes . ' Minutes</td>'; |
| 231 |
$html .= '</tr>'; |
| 232 |
$html .= '<tr>'; |
| 233 |
$html .= '<th>' . __('Meeting Host', 'fluent-booking') . '</th>'; |
| 234 |
$html .= '<td>' . $calendar->title . '</td>'; |
| 235 |
$html .= '</tr>'; |
| 236 |
$html .= '</html></div>'; |
| 237 |
return $html; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
return $text; |
| 242 |
} |
| 243 |
|
| 244 |
protected function getResponseHtml($response, $fields, $columns) |
| 245 |
{ |
| 246 |
return __('HTML Response', 'fluent-booking'); |
| 247 |
} |
| 248 |
|
| 249 |
protected function getResponseAsText($response, $fields, $columns) |
| 250 |
{ |
| 251 |
return __('Text Response', 'fluent-booking'); |
| 252 |
} |
| 253 |
|
| 254 |
public function getCalendarOptions() |
| 255 |
{ |
| 256 |
return apply_filters('fluent_booking/ff_editor_calendar_options', CalendarService::getCalendarOptionsByHost()); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Build unique ID concatenating form id and name attribute |
| 261 |
* |
| 262 |
* @param array $data $form |
| 263 |
* |
| 264 |
* @return string for id value |
| 265 |
*/ |
| 266 |
protected function makeElementId($data, $form) |
| 267 |
{ |
| 268 |
if (isset($data['attributes']['name'])) { |
| 269 |
$formInstance = \FluentForm\App\Helpers\Helper::$formInstance; |
| 270 |
if (!empty($data['attributes']['id'])) { |
| 271 |
return $data['attributes']['id']; |
| 272 |
} |
| 273 |
$elementName = $data['attributes']['name']; |
| 274 |
$elementName = str_replace(['[', ']', ' '], '_', $elementName); |
| 275 |
|
| 276 |
$suffix = esc_attr($form->id); |
| 277 |
if ($formInstance > 1) { |
| 278 |
$suffix = $suffix . '_' . $formInstance; |
| 279 |
} |
| 280 |
|
| 281 |
$suffix .= '_' . $elementName; |
| 282 |
|
| 283 |
return 'ff_' . esc_attr($suffix); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
} |
| 288 |
|