PluginProbe
Booking Calendar / 11.0
Booking Calendar v11.0
11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 All 203 releases
booking / includes / _capacity / get_times_fields.php

get_times_fields.php in Booking Calendar 11.0, at includes/_capacity/get_times_fields.php

397 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly // FixIn: 9.8.0.4.
4
5
6 /**
7 * Get Time Slots from booking form for Each date: [ '2023-10-25' => [ '12:20 - 12:55', '13:00 - 14:00' ], '2023-10-26' => [ ... ], ... ]
8 *
9 * @param $resource_id // Resource ID (used in case of MultiUser version for getting specific form for regular users)
10 * @param $custom_form_name // Form name for getting time slots configuration
11 * @param $start_date_unix_timestamp // Unix Timestamp to start dates count
12 * @param $days_count // Int number of how many dates to check
13 *
14 * @return array [ '2023-10-25' => [ '12:20 - 12:55', '13:00 - 14:00' ], '2023-10-26' => [ ... ], ... ]
15 *
16 * Example:
17 * $timeslots_arr = wpbc_get_times_fields_configuration(
18 * 1,
19 * 'my_custom_form_name',
20 * strtotime( gmdate( 'Y-m-d 00:00:00', strtotime( 'now' ) ) ),
21 * 365
22 * );
23 */
24 function wpbc_get_times_fields_configuration__by_dates( $resource_id, $custom_form_name, $start_date_unix_timestamp, $days_count ){
25
26 // Get booking form configuration CONTENT.
27 $booking_form_fields = wpbc_get__booking_form_fields__configuration( $resource_id, $custom_form_name );
28 // FixIn: 10.15.8.2.
29 // In BFB mode use the same resolved form source as the front-end renderer. The legacy loader above stays as a fallback.
30 if ( function_exists( 'wpbc_get__booking_form_pair__for_field_names_listing' ) ) {
31
32 $booking_form_pair = wpbc_get__booking_form_pair__for_field_names_listing( $resource_id, $custom_form_name );
33
34 if (
35 is_array( $booking_form_pair )
36 && isset( $booking_form_pair['form'] )
37 && ( '' !== trim( (string) $booking_form_pair['form'] ) )
38 ) {
39 $booking_form_fields = (string) $booking_form_pair['form'];
40 }
41 }
42
43 // -----------------------------------------------------------------------------------------------------------------
44 // Get conditional sections, if exist
45 // -----------------------------------------------------------------------------------------------------------------
46 /**
47 * result=[ weekday-condition=[ 0=[
48 * type = "weekday"
49 * value = "*"
50 * options = false
51 * content = "\r\n Default: [selectbox rangetime "10:00 - 11:00" "11:00 - 12:00" "12:00 - 13:00" "13:00 - 14:00" "14:00 - 15:00" "15:00 - 16:00" "16:00 - 17:00" "17:00 - 18:00"]\r\n"
52 * structure = "[condition name="weekday-condition" type="weekday" value="*"]\r\n Default: [selectbox rangetime "10:00 - 11:00" "11:00 - 12:00" "12:00 - 13:00" "13:00 - 14:00" "14:00 - 15:00" "15:00 - 16:00" "16:00 - 17:00" "17:00 - 18:00"]\r\n[/condition]"
53 * 1 = [
54 * type = "weekday"
55 * value = "1,2"
56 * options = false
57 * content = " \r\n Monday, Tuesday: [selectbox rangetime "10:00 - 12:00" "12:00 - 14:00"]\r\n"
58 * structure = "[condition name="weekday-condition" type="weekday" value="1,2"] \r\n Monday, Tuesday: [selectbox rangetime "10:00 - 12:00" "12:00 - 14:00"]\r\n[/condition]"
59 * 2 = [ ... ]
60 */
61 $cached__sql_data__season_filters_arr = array();
62 if ( function_exists( 'wpbc_conditions_form__get_sections' ) ) {
63
64 $conditions = wpbc_conditions_form__get_sections( $booking_form_fields );
65
66 if ( ! empty( $conditions ) ) {
67
68 // ["High season", "Weekend season"]
69 $season_names_arr = wpbc_conditions_get_season_titles_arr( $booking_form_fields );
70
71 // Load all seasons from DB to cache them - for having only one SQL request to DB, if needed
72 $cached__sql_data__season_filters_arr = wpbc_db_get_season_data_arr__for_these_season_names( $season_names_arr );
73 }
74
75 } else {
76 $conditions = array();
77 }
78 // -----------------------------------------------------------------------------------------------------------------
79
80
81 // -----------------------------------------------------------------------------------------------------------------
82 // Get Time Slots [ "10:30 - 10:45", "11:00 - 11:15", "11:30 - 12:00", ... ] if NO conditional sections - time slot the same for all dates. Get them once here.
83 // -----------------------------------------------------------------------------------------------------------------
84 if ( empty( $conditions ) ) {
85 $time_slots_arr = wpbc_get_timeslots__from_content( $booking_form_fields );
86 } else {
87 $time_slots_arr = array();
88 }
89 // -----------------------------------------------------------------------------------------------------------------
90
91
92 // -----------------------------------------------------------------------------------------------------------------
93 // Here Go for each DATE
94 // -----------------------------------------------------------------------------------------------------------------
95 $dates_times_fields_arr = array();
96 for ( $day_num = 0; $day_num <= $days_count; $day_num ++ ) {
97
98 $my_day_tag = gmdate( 'Y-m-d', $start_date_unix_timestamp ); // '2023-07-19'
99
100 if ( ( ! empty( $conditions ) ) && ( function_exists( 'wpbc_conditions_form__get_section__depend_from_date' ) ) ){
101
102 // Get booking form conditional section depends on DATE
103 $section_content = wpbc_conditions_form__get_section__depend_from_date( $conditions, $my_day_tag , $cached__sql_data__season_filters_arr );
104
105 // Get Time Slots [ "10:30 - 10:45", "11:00 - 11:15", "11:30 - 12:00", ... ]
106 $time_slots_in_section_arr = wpbc_get_timeslots__from_content( $section_content );
107
108 $dates_times_fields_arr[ $my_day_tag ] = $time_slots_in_section_arr;
109 } else {
110 $dates_times_fields_arr[ $my_day_tag ] = $time_slots_arr;
111 }
112
113 // DEBUG: $dates_times_fields_arr[$my_day_tag] = array( '00:00 - 24:00', '10:00 - 12:00', '12:00 - 14:00', '14:00 - 16:00', '16:00 - 18:00', '18:00 - 20:00' , '09:01 - 09:02' );
114
115 $start_date_unix_timestamp += 24 * 60 * 60; // Go next day + 1 day
116 }
117
118 return $dates_times_fields_arr;
119 }
120
121
122
123 /**
124 * Get Time-Slots (CONVERT READABLE to SECONDS) seconds range array for the specified date from array of dates, where each date include readable time-ranges array
125 *
126 * @param $dates_arr__with__readable_time_slots_arr = [
127 * 2023-10-25 = [],
128 * 2023-10-26 = [
129 * 0 => '00:00 - 24:00',
130 * 1 => '10:00 - 12:00',
131 * 2 => '12:00 - 14:00',
132 * 3 => '14:00 - 16:00',
133 * 4 => '16:00 - 18:00',
134 * 5 => '18:00 - 20:00',
135 * 6 => '09:01 - 09:02',
136 * ],
137 * 2023-10-27 = [], ...
138 * ]
139 * @param $my_day_tag = '2023-10-25'
140 *
141 * @return array|string[] IF ( $my_day_tag == '2023-10-25' ) => []
142 * OR ( $my_day_tag == '2023-10-26' ) => [
143 * 0 => '1 - 86392',
144 * 1 => '36011 - 43192',
145 * 2 => '43211 - 50392',
146 * 3 => '50411 - 57592',
147 * 4 => '57611 - 64792',
148 * 5 => '64811 - 71992',
149 * 6 => '32471 - 32512',
150 * ]
151 */
152 function wpbc_get__time_range_in_seconds_arr__for_date( $dates_arr__with__readable_time_slots_arr, $my_day_tag ) {
153
154 if ( isset( $dates_arr__with__readable_time_slots_arr[ $my_day_tag ] ) ) {
155
156
157 $bookingform__available_timeslots_in_sec__arr = array_map(
158 function ( $time_slot_readable ) {
159 return wpbc_convert__readable_time_slot__to__time_range_in_seconds( $time_slot_readable );
160 }
161 , $dates_arr__with__readable_time_slots_arr[ $my_day_tag ] );
162
163 return $bookingform__available_timeslots_in_sec__arr;
164 }
165
166 return array();
167 }
168
169
170 // =====================================================================================================================
171 // Get Time-Slots from CONTENT (booking form configuration)
172 // =====================================================================================================================
173
174 /**
175 * Get Time-Slots ["10:30 - 10:45", "11:00 - 11:15", "11:30 - 12:00", ... ] time slot values from booking form content ( or part of booking form configuration)
176 *
177 * @param string $content '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
178 *
179 * @return array
180 */
181 function wpbc_get_timeslots__from_content( $content ){
182
183 $time_slots_arr =array();
184
185 // == RANGE TIME == [ '10:00 - 12:00', '12:00 - 14:00' ... ] -------------------------------------------------
186 if ( false !== strpos( $content, ' rangetime ' ) ) {
187
188 $time_slots_arr = wpbc_get_timeslots__for_rangetime( $content );
189
190 } else {
191
192 // == START TIME | END TIME == --------------------------------------------------------------------------------
193 if (
194 ( false !== strpos( $content, ' starttime ' ) ) // Important to have empty space at the beginning, because it will search for [selectbox starttime and skip this [starttime]
195 && ( false !== strpos( $content, ' endtime ' ) ) // Important to have empty space at the beginning, because it will search for [selectbox endtime and skip this [endtime]
196 ){
197 // Now we need to create the smallest time intervals ( 60 sec. ) for each start / end times.
198
199 // == START TIME == [ '10:00 - 10:01', '12:00 - 12:01' ... ]
200 $start_time_slots_arr = wpbc_get_timeslots__for_starttime( $content , 60 );
201 foreach ( $start_time_slots_arr as $ts ) {
202 $time_slots_arr[] = $ts;
203 }
204
205 // == END TIME == [ '09:59 - 10:00', '11:59 - 12:00' ... ]
206 $end_time_slots_arr = wpbc_get_timeslots__for_endtime( $content , 60 );
207 foreach ( $end_time_slots_arr as $ts ) {
208 $time_slots_arr[] = $ts;
209 }
210 }
211
212 // == START TIME | DURATION TIME == ---------------------------------------------------------------------------
213 if (
214 ( false !== strpos( $content, ' starttime ' ) ) // Important to have empty space at the beginning, because it will search for [selectbox starttime and skip this [starttime]
215 && ( false !== strpos( $content, ' durationtime ' ) )
216 ){
217 // 900 -> e.g.: '00:15'
218 $min_time_duration = wpbc_get__min_time_duration__in_seconds( $content );
219
220 if ( false !== $min_time_duration ) {
221
222 // == START TIME == [ '10:00 - 10:15', '12:00 - 12:15' ... ]
223 $start_time_slots_arr = wpbc_get_timeslots__for_starttime( $content, $min_time_duration );
224 foreach ( $start_time_slots_arr as $ts ) {
225 $time_slots_arr[] = $ts;
226 }
227 }
228
229 }
230 }
231
232 return $time_slots_arr;
233 }
234
235
236 /**
237 * Get Time-Slots == RANGE TIME == [ '10:00 - 12:00', '12:00 - 14:00' ... ] from provided booking form configuration content
238 *
239 * @param $bookingform_configuration_content - '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
240 *
241 * @return array
242 *
243 * $time_slots_arr = [
244 * [0] => 00:00 - 24:00
245 * [1] => 10:00 - 12:00
246 * [2] => 12:00 - 14:00
247 * [3] => 14:00 - 16:00
248 * [4] => 16:00 - 18:00
249 * [5] => 18:00 - 20:00
250 * ]
251 * OR
252 * []
253 */
254 function wpbc_get_timeslots__for_rangetime( $bookingform_configuration_content ) {
255
256 $time_slots_arr = array();
257 $shortcode_values = wpbc_parse_form__get_first_shortcode_values( 'rangetime', $bookingform_configuration_content );
258
259 if ( ! empty( $shortcode_values ) ) {
260 foreach ( $shortcode_values as $timeslot_value ) {
261
262 if ( ! empty( $timeslot_value['value'] ) ) {
263 $time_slots_arr[] = $timeslot_value['value'];
264 }
265 }
266 }
267
268 return $time_slots_arr;
269 }
270
271
272 /**
273 * Get Time-Slots from == START TIME == [ '10:00 - 10:01', '12:00 - 12:01' ... ] searching in provided booking form configuration content
274 *
275 * @param string $bookingform_configuration_content '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
276 * @param int $time_shift_duration_in_seconds 60
277 *
278 * @return array
279 *
280 * $time_slots_arr = [
281 * [1] => 10:00 - 10:01
282 * [2] => 12:00 - 12:01
283 * [3] => 14:00 - 14:01
284 * [4] => 16:00 - 16:01
285 * [5] => 18:00 - 18:01
286 * ]
287 * OR
288 * []
289 */
290 function wpbc_get_timeslots__for_starttime( $bookingform_configuration_content , $time_shift_duration_in_seconds = 60 ) {
291
292 $time_slots_arr = array();
293
294 $shortcode_values = wpbc_parse_form__get_first_shortcode_values( 'starttime', $bookingform_configuration_content );
295 if ( ! empty( $shortcode_values ) ) {
296 foreach ( $shortcode_values as $timeslot_value ) {
297
298 if ( ! empty( $timeslot_value['value'] ) ) {
299 // '12:00:00' -> 43190
300 $time_in_seconds = wpbc_transform__24_hours_his__in__seconds( $timeslot_value['value'] . ':00' );
301
302 $time_in_seconds += $time_shift_duration_in_seconds;
303
304 // 43250 -> '12:01:00'
305 $time_his = wpbc_transform__seconds__in__24_hours_his( $time_in_seconds );
306 $time_his = substr( $time_his, 0, 5 );
307
308 // '12:00 - 12:01'
309 $time_slots_arr[] = $timeslot_value['value'] . ' - ' . $time_his;
310 }
311 }
312 }
313
314
315 return $time_slots_arr;
316 }
317
318
319 /**
320 * Get Time-Slots from == END TIME == [ '09:59 - 10:00', '11:59 - 12:00' ... ] searching in provided booking form configuration content
321 *
322 * @param string $bookingform_configuration_content '[calendar]... <p>Time: [select* rangetime "Full Day@@00:00 - 24:00" "10:00 AM - 12:00 PM@@10:00 - 12:00"] ...'
323 * @param int $time_shift_duration_in_seconds 60
324 *
325 * @return array
326 *
327 * $time_slots_arr = [
328 * [1] => 09:59 - 10:00
329 * [2] => 11:59 - 12:00
330 * [3] => 13:59 - 14:00
331 * [4] => 15:59 - 16:00
332 * [5] => 17:59 - 18:00
333 * ]
334 * OR
335 * []
336 */
337 function wpbc_get_timeslots__for_endtime( $bookingform_configuration_content , $time_shift_duration_in_seconds = 60 ) {
338
339 $time_slots_arr = array();
340
341 $shortcode_values = wpbc_parse_form__get_first_shortcode_values( 'endtime', $bookingform_configuration_content );
342 if ( ! empty( $shortcode_values ) ) {
343 foreach ( $shortcode_values as $timeslot_value ) {
344
345 if ( ! empty( $timeslot_value['value'] ) ) {
346 // '12:00:00' -> 43190
347 $time_in_seconds = wpbc_transform__24_hours_his__in__seconds( $timeslot_value['value'] . ':00' );
348
349 $time_in_seconds -= $time_shift_duration_in_seconds;
350
351 // 43130 -> '11:59:00'
352 $time_his = wpbc_transform__seconds__in__24_hours_his( $time_in_seconds );
353 $time_his = substr( $time_his, 0, 5 );
354
355 // '11:59 - 12:00'
356 $time_slots_arr[] = $time_his . ' - ' . $timeslot_value['value'];
357 }
358 }
359 }
360
361 return $time_slots_arr;
362 }
363
364
365 /**
366 * Get minimum duration of time ( from [selectbox durationtime ... ] ) or FALSE, if no such shortcode or no values.
367 *
368 * @param $bookingform_configuration_content
369 *
370 * @return false|int
371 */
372 function wpbc_get__min_time_duration__in_seconds( $bookingform_configuration_content ){
373
374 $min_time_duration = false;
375 $time_duration_in_seconds_arr = array();
376
377 $shortcode_values = wpbc_parse_form__get_first_shortcode_values( 'durationtime', $bookingform_configuration_content );
378 if ( ! empty( $shortcode_values ) ) {
379 foreach ( $shortcode_values as $timeslot_value ) {
380
381 if ( ! empty( $timeslot_value['value'] ) ) {
382 // '00:15' -> 900
383 $time_in_seconds = wpbc_transform__24_hours_his__in__seconds( $timeslot_value['value'] . ':00' );
384
385 // [ 900 , ... ]
386 $time_duration_in_seconds_arr[] = $time_in_seconds;
387 }
388 }
389 }
390
391 if ( ! empty( $time_duration_in_seconds_arr ) ) {
392 $min_time_duration = min( $time_duration_in_seconds_arr );
393 }
394
395 return $min_time_duration;
396 }
397