PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.2
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.2
5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / helpers / events_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 1 year ago agent_helper.php 1 year ago auth_helper.php 9 months ago blocks_helper.php 1 year ago booking_helper.php 1 year ago bricks_helper.php 1 year ago bundles_helper.php 1 year ago calendar_helper.php 9 months ago carts_helper.php 1 year ago connector_helper.php 1 year ago csv_helper.php 9 months ago customer_helper.php 9 months ago customer_import_helper.php 9 months ago database_helper.php 9 months ago debug_helper.php 1 year ago defaults_helper.php 1 year ago elementor_helper.php 1 year ago email_helper.php 9 months ago encrypt_helper.php 1 year ago events_helper.php 1 year ago form_helper.php 9 months ago icalendar_helper.php 1 year ago image_helper.php 1 year ago invoices_helper.php 9 months ago license_helper.php 1 year ago location_helper.php 1 year ago marketing_systems_helper.php 1 year ago meeting_systems_helper.php 1 year ago menu_helper.php 9 months ago meta_helper.php 1 year ago migrations_helper.php 1 year ago money_helper.php 1 year ago notifications_helper.php 9 months ago order_intent_helper.php 9 months ago orders_helper.php 1 year ago otp_helper.php 9 months ago pages_helper.php 1 year ago params_helper.php 1 year ago payments_helper.php 1 year ago price_breakdown_helper.php 1 year ago process_jobs_helper.php 1 year ago processes_helper.php 1 year ago replacer_helper.php 1 year ago resource_helper.php 1 year ago roles_helper.php 9 months ago router_helper.php 1 year ago service_helper.php 1 year ago sessions_helper.php 1 year ago settings_helper.php 9 months ago short_links_systems_helper.php 9 months ago shortcodes_helper.php 9 months ago sms_helper.php 1 year ago steps_helper.php 9 months ago stripe_connect_helper.php 9 months ago styles_helper.php 9 months ago support_topics_helper.php 1 year ago time_helper.php 9 months ago timeline_helper.php 9 months ago transaction_helper.php 1 year ago transaction_intent_helper.php 1 year ago util_helper.php 9 months ago version_specific_updates_helper.php 9 months ago whatsapp_helper.php 1 year ago work_periods_helper.php 1 year ago wp_datetime.php 1 year ago wp_user_helper.php 1 year ago
events_helper.php
358 lines
1 <?php
2 /*
3 * Copyright (c) 2022 LatePoint LLC. All rights reserved.
4 */
5
6
7 class OsEventsHelper {
8
9 public static function week_grid(OsWpDateTime $calendar_start, OsWpDateTime $calendar_end, OsWpDateTime $target_date, array $services_with_resources, array $filter = []){
10 $now_datetime = OsTimeHelper::now_datetime_object();
11 $html = '';
12
13
14 $start_time = 540;
15 $end_time = 1080;
16
17 $min_service_duration = false;
18
19 $day_slots = [];
20 for ($day_date = clone $calendar_start; $day_date <= $calendar_end; $day_date->modify('+1 day')) {
21 $day_slots[$day_date->format('Y-m-d')] = [];
22 foreach ($services_with_resources as $service_resources) {
23 if (empty($service_resources['resources'][$day_date->format('Y-m-d')])) continue;
24 $clean_periods = [];
25 foreach ($service_resources['resources'][$day_date->format('Y-m-d')] as $resource) {
26 if (!empty($filter['agent_id']) && $resource->agent_id != $filter['agent_id']) continue;
27 if (!empty($filter['service_id']) && $resource->service_id != $filter['service_id']) continue;
28 if (!empty($filter['location_id']) && $resource->location_id != $filter['location_id']) continue;
29 foreach ($resource->work_time_periods as $time_period) {
30 $clean_periods[] = $time_period;
31 }
32 }
33 $merged_periods = \LatePoint\Misc\TimePeriod::merge_periods($clean_periods);
34
35
36 foreach ($merged_periods as $time_period) {
37 if(($time_period->end_time - $time_period->start_time) > 0){
38 $min_service_duration = $min_service_duration ? min($min_service_duration, ($time_period->end_time - $time_period->start_time)) : $time_period->end_time - $time_period->start_time;
39 }
40 $service_box_data = ['model' => $service_resources['model'], 'time_period' => $time_period];
41 $day_slots[$day_date->format('Y-m-d')][$time_period->start_time][] = $service_box_data;
42 $start_time = min($start_time, $time_period->start_time);
43 $end_time = max($end_time, $time_period->end_time);
44 }
45 }
46 ksort($day_slots[$day_date->format('Y-m-d')]);
47 }
48
49 $html.= '<div class="latepoint-calendar-week">';
50
51 // make it start a little earlier so that there is some space between day labels and first bookings
52 $start_time = $start_time - 60;
53
54 // make sure timeline starts at hour marks
55 if($start_time % 60 != 0) $start_time = $start_time - ($start_time % 60);
56 $day_duration = $end_time - $start_time;
57
58 $hour_height_css = '';
59 if($min_service_duration && $min_service_duration < 60){
60 $default_hour_height = 44;
61 $hour_height = ceil(60 / $min_service_duration) * $default_hour_height;
62 $hour_height_css = 'style="height: '.$hour_height.'px"';
63 }
64
65 for ($day_date = clone $calendar_start; $day_date <= $calendar_end; $day_date->modify('+1 day')) {
66
67
68
69 $is_today = ($day_date->format('Y-m-d') == $now_datetime->format('Y-m-d'));
70 $is_day_in_past = ($day_date->format('Y-m-d') < $now_datetime->format('Y-m-d'));
71 $is_target_month = ($day_date->format('m') == $target_date->format('m'));
72 $is_next_month = ($day_date->format('m') > $target_date->format('m'));
73 $is_prev_month = ($day_date->format('m') < $target_date->format('m'));
74
75
76 $day_class = '';
77 if ($is_today) $day_class.= ' os-today';
78 if ($is_day_in_past) $day_class.= ' os-day-passed';
79 if ($is_target_month) $day_class.= ' os-month-current';
80 if ($is_next_month) $day_class.= ' os-month-next';
81 if ($is_prev_month) $day_class.= ' os-month-prev';
82 $day_class.= ' day-weekday-' . $day_date->format('N');
83
84 $html.= '<div class="le-day-wrapper ' . $day_class . '">';
85
86
87 $html.= '<div class="le-day-weekday-wrapper">';
88 $html.= '<div class="le-day-weekday">'.OsBookingHelper::get_weekday_name_by_number($day_date->format('N')).'</div>';
89 if ($day_date->format('d') == '1') $html.= '<div class="os-day-month">' . OsUtilHelper::get_month_name_by_number($day_date->format('n')) . '</div>';
90 $html.= '<div class="le-day-number"
91 data-os-params="' . OsUtilHelper::build_os_params(['target_date_string' => $day_date->format('Y-m-d'), 'filter' => $filter]) . '"
92 data-os-output-target="lightbox"
93 data-os-action="' . OsRouterHelper::build_route_name('events', 'events_day_view') . '">' . $day_date->format('j') .
94 '</div>';
95 $html.= '</div>';
96
97 $html.= '<div class="le-day-schedule-wrapper">';
98 $html.='<div class="day-schedule-timeslots">';
99 for($i = $start_time; $i<$end_time; $i+= 60){
100 $html.='<div class="day-schedule-timeslot-wrapper">';
101 $html.='<div class="day-schedule-timeslot" '.$hour_height_css.'><div class="day-schedule-timeslot-value">'.OsTimeHelper::minutes_to_hours_and_minutes($i, null, true, true).'</div></div>';
102 $html.='</div>';
103
104 }
105 foreach($day_slots[$day_date->format('Y-m-d')] as $day_slot_services){
106 foreach($day_slot_services as $day_slot_service){
107 $slot_duration = $day_slot_service['time_period']->end_time - $day_slot_service['time_period']->start_time;
108 $start_position = ($day_slot_service['time_period']->start_time - $start_time) / $day_duration * 100;
109 $height = $slot_duration / $day_duration * 100;
110 $extra_attrs = 'style="top: '.$start_position.'%; height: '.$height.'%"';
111 $html.= self::event_service_box_html($day_slot_service['model'], $day_slot_service['time_period'], $filter, $day_date, $extra_attrs);
112 }
113 }
114 $html.= '</div>';
115 $html.= '</div>';
116 $html.= '</div>';
117 }
118 $html.= '</div>';
119 return $html;
120 }
121
122 public static function month_grid(OsWpDateTime $calendar_start, OsWpDateTime $calendar_end, OsWpDateTime $target_date, array $services_with_resources, array $filter = []){
123 $now_datetime = OsTimeHelper::now_datetime_object();
124 $html = '';
125 $html.= '<div class="latepoint-calendar-month">';
126
127 $weekdays = OsBookingHelper::get_weekdays_arr();
128 foreach ($weekdays as $weekday_number => $weekday_name) {
129 $html.= '<div class="le-weekday weekday-' . ($weekday_number + 1) . '">' . $weekday_name . '</div>';
130 }
131
132 for ($day_date = clone $calendar_start; $day_date <= $calendar_end; $day_date->modify('+1 day')) {
133 $is_today = ($day_date->format('Y-m-d') == $now_datetime->format('Y-m-d'));
134 $is_day_in_past = ($day_date->format('Y-m-d') < $now_datetime->format('Y-m-d'));
135 $is_target_month = ($day_date->format('m') == $target_date->format('m'));
136 $is_next_month = ($day_date->format('m') > $target_date->format('m'));
137 $is_prev_month = ($day_date->format('m') < $target_date->format('m'));
138
139 $day_class = '';
140 if ($is_today) $day_class.= ' os-today';
141 if ($is_day_in_past) $day_class.= ' os-day-passed';
142 if ($is_target_month) $day_class.= ' os-month-current';
143 if ($is_next_month) $day_class.= ' os-month-next';
144 if ($is_prev_month) $day_class.= ' os-month-prev';
145 $day_class.= ' day-weekday-' . $day_date->format('N');
146
147 $html.= '<div class="le-day-wrapper ' . $day_class . '">';
148
149 if ($day_date->format('d') == '1') $html.= '<div class="os-day-month">' . OsUtilHelper::get_month_name_by_number($day_date->format('n')) . '</div>';
150 $html.= '<div class="le-day-number"
151 data-os-params="' . OsUtilHelper::build_os_params(['target_date_string' => $day_date->format('Y-m-d'), 'filter' => $filter]) . '"
152 data-os-output-target="lightbox"
153 data-os-action="' . OsRouterHelper::build_route_name('events', 'events_day_view') . '">' . $day_date->format('j') .
154 '</div>';
155 $day_services_html = [];
156 foreach ($services_with_resources as $service_resources) {
157 if (empty($service_resources['resources'][$day_date->format('Y-m-d')])) continue;
158 $clean_periods = [];
159 foreach ($service_resources['resources'][$day_date->format('Y-m-d')] as $resource) {
160 if(!empty($filter['agent_id']) && $resource->agent_id != $filter['agent_id']) continue;
161 if(!empty($filter['service_id']) && $resource->service_id != $filter['service_id']) continue;
162 if(!empty($filter['location_id']) && $resource->location_id != $filter['location_id']) continue;
163 foreach ($resource->work_time_periods as $time_period) {
164 $clean_periods[] = $time_period;
165 }
166 }
167 $merged_periods = \LatePoint\Misc\TimePeriod::merge_periods($clean_periods);
168 foreach ($merged_periods as $time_period) {
169 $day_services_html[$time_period->start_time][] = self::event_service_box_html($service_resources['model'], $time_period, $filter, $day_date);
170 }
171 }
172 ksort($day_services_html);
173 $daily_count = 0;
174 foreach ($day_services_html as $day_service_html) {
175 if($daily_count >= 4) {
176 // translators: %d is the number of services
177 $more = sprintf(__('+ %d more', 'latepoint'), count($day_services_html) - 4);
178 $html.= '<div class="le-more-services"
179 data-os-params="' . esc_attr(OsUtilHelper::build_os_params(['target_date_string' => $day_date->format('Y-m-d'), 'filter' => $filter])) . '"
180 data-os-output-target="lightbox"
181 data-os-action="' . esc_attr(OsRouterHelper::build_route_name('events', 'events_day_view')) . '">'.esc_html($more).'</div>';
182 break;
183 }
184 $daily_count++;
185 $html.= implode('', $day_service_html);
186 }
187 $html.= '</div>';
188 }
189 $html.= '</div>';
190 return $html;
191 }
192
193 public static function event_service_box_html(OsServiceModel $service, \LatePoint\Misc\TimePeriod $time_period, array $filter, OsWpDateTime $start_date, string $extra_attrs = ''){
194 $service_box_html = '';
195 $selected_attributes = '';
196
197 if(!empty($filter['agent_id'])) $selected_attributes.= ' data-selected-agent="'.esc_attr($filter['agent_id']).'" ';
198 if(!empty($filter['location_id'])) $selected_attributes.= ' data-selected-location="'.esc_attr($filter['location_id']).'" ';
199 if(!empty($filter['show_agents'])) $selected_attributes.= ' data-show-agents="'.esc_attr($filter['show_agents']).'" ';
200 if(!empty($filter['show_locations'])) $selected_attributes.= ' data-show-locations="'.esc_attr($filter['show_locations']).'" ';
201
202
203 if(OsEventsHelper::is_service_slot_passed($service, $start_date, $time_period)){
204 $service_box_html.= '<div class="le-service-wrapper is-passed" '.$extra_attrs.'><div class="is-passed-message">'.esc_html__('This event has passed', 'latepoint').'</div>';
205 }else{
206 $start_time_attr = (($time_period->end_time - $time_period->start_time) > $service->duration) ? '' : 'data-selected-start-time="' . esc_attr($time_period->start_time) . '"';
207 $service_box_html.= '<div class="le-service-wrapper os_trigger_booking" '.$extra_attrs.' '.$selected_attributes.'
208 data-selected-service="' . esc_attr($service->id) . '"
209 data-selected-start-date="' . esc_attr($start_date->format('Y-m-d')) . '"
210 '.$start_time_attr.'
211 data-hide-side-panel="yes">';
212 }
213 $service_box_html.= '<div class="le-service-inner">';
214 $service_box_html.= '<div class="le-color-elem" style="background-color: ' . esc_attr($service->bg_color) . '"></div>';
215 $service_box_html.= '<div class="le-service-name">' . esc_html($service->name) . '</div>';
216 $service_box_html.= '<div class="le-service-time-period">' . OsTimeHelper::minutes_to_hours_and_minutes($time_period->start_time) . ' - ' . OsTimeHelper::minutes_to_hours_and_minutes($time_period->end_time) . '</div>';
217 $service_box_html.= '</div>';
218 $service_box_html.= '</div>';
219 return $service_box_html;
220 }
221
222 public static function events_grid(OsWpDateTime $target_date, array $filter = [], $range_type = 'week', array $restrictions = []): string {
223 $html = '';
224
225 switch($range_type){
226 case 'week':
227 # Get bounds for a month of a targeted day
228 $calendar_start = clone $target_date;
229 // set monday as start of the week
230 $start_of_the_week_day = 1;
231 // figure out what week day is target and then find start and end of that week
232 $shift = $calendar_start->format('N') - $start_of_the_week_day;
233 if($shift) $calendar_start->modify('-'.$shift.' days');
234 $calendar_end = clone $calendar_start;
235 $calendar_end->modify('+6 days');
236
237 $next_period = clone $calendar_start;
238 $next_period->modify('+7 days');
239
240 $prev_period = clone $calendar_start;
241 $prev_period->modify('-7 days');
242 break;
243 case 'month':
244 # Get bounds for a month of a targeted day
245 $calendar_start = clone $target_date;
246 $calendar_start->modify('first day of this month');
247 $calendar_end = clone $target_date;
248 $calendar_end->modify('last day of this month');
249
250 $next_period = clone $target_date;
251 $next_period->modify('first day of next month');
252
253 $prev_period = clone $target_date;
254 $prev_period->modify('first day of previous month');
255
256
257 $weekday_for_first_day_of_month = $calendar_start->format('N') - 1;
258 $weekday_for_last_day_of_month = $calendar_end->format('N') - 1;
259
260
261 if ($weekday_for_first_day_of_month > 0) {
262 $calendar_start->modify('-' . $weekday_for_first_day_of_month . ' days');
263 }
264
265 if ($weekday_for_last_day_of_month < 6) {
266 $days_to_add = 6 - $weekday_for_last_day_of_month;
267 $calendar_end->modify('+' . $days_to_add . ' days');
268 }
269 break;
270 }
271
272
273 $now_datetime = OsTimeHelper::now_datetime_object();
274
275
276 $services = new OsServiceModel();
277 if(!empty($restrictions['show_services'])) $services->where(['id' => explode(',', $restrictions['show_services'])]);
278 $services = $services->should_be_active()->get_results_as_models();
279
280 $services_with_resources = [];
281
282 foreach ($services as $service) {
283 $services_with_resources['service_' . $service->id] = ['model' => $service, 'work_periods' => []];
284 $booking_request = new \LatePoint\Misc\BookingRequest([
285 'service_id' => $service->id
286 ]);
287 $services_with_resources['service_' . $service->id]['resources'] = OsResourceHelper::get_resources_grouped_by_day($booking_request, $calendar_start, $calendar_end);
288 }
289
290 $clean_filter = [];
291 foreach($filter as $filter_key => $filter_value){
292 if(!empty($filter_value)) $clean_filter[$filter_key] = $filter_value;
293 }
294
295 $filter_class = empty($clean_filter) ? '' : 'show-filters';
296
297 $clean_filter = array_merge($clean_filter, $restrictions);
298 $agent_restriction = !empty($restrictions['show_agents']) ? explode(',', $restrictions['show_agents']) : [];
299 $service_restriction = !empty($restrictions['show_services']) ? explode(',', $restrictions['show_services']) : [];
300
301 $html.= '<div class="latepoint-calendar-wrapper '.$filter_class.'" data-route-name="' . OsRouterHelper::build_route_name('events', 'load_calendar_events') . '" data-target-date="' . $target_date->format('Y-m-d') . '">';
302 $html.= '<div class="latepoint-calendar-controls-wrapper">';
303 $html.= OsFormHelper::hidden_field('restrictions', wp_json_encode($restrictions));
304 $html.= '<div class="latepoint-calendar-controls">';
305 if($range_type == 'week'){
306 if($calendar_start->format('M') == $calendar_end->format('M')){
307 $html.= '<div class="le-week">' . $calendar_start->format('M j') . ' - ' . $calendar_end->format('j') .'</div>';
308 }else{
309 $html.= '<div class="le-week">' . $calendar_start->format('M j') . ' - ' . $calendar_end->format('M j') .'</div>';
310 }
311 }
312 if($range_type == 'month'){
313 $html.= '<div class="le-month">' . $target_date->format('F') . '</div>';
314 }
315 $html.= '<div class="le-range-selector">'.OsFormHelper::select_field('calendar_range_type', false, ['week' => 'Weekly', 'month' => 'Monthly'], $range_type).'</div>';
316 $html.= '<div class="le-filter le-filter-trigger"><i class="latepoint-icon latepoint-icon-ui-47"></i><span>' . __('Filters', 'latepoint') . '</span></div>';
317 $html.= OsFormHelper::hidden_field('target_date_string', $target_date->format('Y-m-d'));
318 $html.= '<div class="le-navigation-wrapper">';
319 $html.= '<div class="le-navigation">
320 <div class="le-navigation-button le-navigation-trigger" data-target-date="' . $prev_period->format('Y-m-d') . '"><i class="latepoint-icon latepoint-icon-arrow-left"></i></div>
321 <div class="le-today le-navigation-trigger" data-target-date="' . $now_datetime->format('Y-m-d') . '">' . __('Today', 'latepoint') . '</div>
322 <div class="le-navigation-button le-navigation-trigger" data-target-date="' . $next_period->format('Y-m-d') . '"><i class="latepoint-icon latepoint-icon-arrow-right"></i></div>
323 </div>';
324 $html.= '</div>';
325 $html.= '</div>';
326 $html.= '<div class="latepoint-calendar-filters">';
327 $html.= '<div class="le-filters-label">'.__('Show:', 'latepoint').'</div>';
328 $html.= OsFormHelper::select_field('filter[agent_id]', false, array_merge([['value' => '', 'label' => __('All Agents', 'latepoint')]], OsAgentHelper::get_agents_list(false, $agent_restriction)), $clean_filter['agent_id'] ?? '');
329 $html.= OsFormHelper::select_field('filter[service_id]', false, array_merge([['value' => '', 'label' => __('All Services', 'latepoint')]], OsServiceHelper::get_services_list(false, $service_restriction)), $clean_filter['service_id'] ?? '');
330 $html.= '</div>';
331 $html.= '</div>';
332
333 switch($range_type){
334 case 'week':
335 $html.= self::week_grid($calendar_start, $calendar_end, $target_date, $services_with_resources, $clean_filter);
336 break;
337 case 'month':
338 $html.= self::month_grid($calendar_start, $calendar_end, $target_date, $services_with_resources, $clean_filter);
339 break;
340 }
341
342 $html.= '</div>';
343 return $html;
344 }
345
346 private static function is_service_slot_passed(OsServiceModel $service, OsWpDateTime $start_date, \LatePoint\Misc\TimePeriod $time_period) :bool {
347 $now_datetime = OsTimeHelper::now_datetime_object();
348 $is_day_in_past = ($start_date->format('Y-m-d') < $now_datetime->format('Y-m-d'));
349 $is_today = ($start_date->format('Y-m-d') == $now_datetime->format('Y-m-d'));
350
351 try{
352 $is_passed = ($is_day_in_past || $is_today && (OsWpDateTime::os_createFromFormat(LATEPOINT_DATETIME_DB_FORMAT, $start_date->format('Y-m-d').' '.OsTimeHelper::minutes_to_army_hours_and_minutes($time_period->end_time - $service->duration).':00') < $now_datetime));
353 }catch(Exception $e){
354 $is_passed = false;
355 }
356 return $is_passed;
357 }
358 }