From 828b42fc3470097b5053ee932b4b35b0333850d4 Mon Sep 17 00:00:00 2001 From: Milan Chaudhary Date: Wed, 14 Jun 2023 16:52:40 +0545 Subject: [PATCH 1/9] Add - Evf slot booking option --- assets/js/admin/form-builder.js | 17 +++++++ includes/class-evf-form-task.php | 51 +++++++++++++++++++ includes/fields/class-evf-field-date-time.php | 29 +++++++++++ 3 files changed, 97 insertions(+) diff --git a/assets/js/admin/form-builder.js b/assets/js/admin/form-builder.js index c917ac6d3..787af3d91 100644 --- a/assets/js/admin/form-builder.js +++ b/assets/js/admin/form-builder.js @@ -973,7 +973,24 @@ $( '#everest-forms-field-option-' + id ).removeClass( 'everest-forms-confirm-enabled' ).addClass( 'everest-forms-confirm-disabled' ); } }); + // Real-time updates for slot booking + $builder.on('change', '.everest-forms-field-option-row-slot_booking_advanced input', function(event) { + if($(this).is(":checked")) { + disable_past_date = $(document).find('.everest-forms-field-option-row-date_format .everest-forms-past-date-disable-format input'); + if(disable_past_date.is(":not(:checked)")) { + disable_past_date.attr("checked", true); + } + disable_past_date.attr("disabled", true); + } else { + disable_past_date.attr("disabled", false); + } + }); + if($('.everest-forms-field-option-row-slot_booking_advanced input').is(":checked")) { + disable_past_date = $(document).find('.everest-forms-field-option-row-date_format .everest-forms-past-date-disable-format input'); + disable_past_date.attr("disabled", true); + disable_past_date.attr("checked", true); + } // Real-time updates for "Placeholder" field option. $builder.on( 'input', '.everest-forms-field-option-row-placeholder input', function(e) { var $this = $( this ), diff --git a/includes/class-evf-form-task.php b/includes/class-evf-form-task.php index 6a21e9296..ede627a46 100644 --- a/includes/class-evf-form-task.php +++ b/includes/class-evf-form-task.php @@ -61,6 +61,7 @@ class EVF_Form_Task { public function __construct() { add_action( 'wp', array( $this, 'listen_task' ) ); add_filter( 'everest_forms_field_properties', array( $this, 'load_previous_field_value' ), 99, 3 ); + add_action( 'everest_forms_complete_entry_save', array( $this, 'update_slot_booking_value' ), 10, 5 ); } /** @@ -1039,6 +1040,56 @@ public function entry_save( $fields, $entry, $form_id, $form_data = array() ) { return $this->entry_id; } + /** + * Insert or update the slot booking data. + * + * @param int $entry_id Entry id. + * @param array $fields List of form fields. + * @param array $entry User submitted data. + * @param int $form_id Form ID. + * @param array $form_data Prepared form settings. + */ + public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, $form_data ) { + global $wpdb; + $new_slot_booking = array(); + $new_slot_booking_field_meta_key_list = array(); + + foreach ( $form_data['form_fields'] as $field ) { + if ( ( 'date-time' === $field['type'] ) && isset( $field['slot_booking_advanced'] ) && evf_string_to_bool( $field['slot_booking_advanced'] ) ) { + $new_slot_booking_field_meta_key_list[] = $field['meta-key']; + } + } + + if ( ! empty( $new_slot_booking_field_meta_key_list ) ) { + foreach ( $fields as $value ) { + if ( in_array( $value['meta_key'], $new_slot_booking_field_meta_key_list, true ) ) { + $new_slot_booking [] = $value['value']; + } + } + + if ( ! empty( $new_slot_booking ) ) { + $get_booked_slot = get_option( 'evf_booked_slot', array() ); + + $new_booked_slot = array( $form_id => $new_slot_booking ); + if ( empty( $get_booked_slot ) ) { + $all_booked_slot = maybe_serialize( $new_booked_slot ); + } else { + $unserialized_booked_slot = maybe_unserialize( $get_booked_slot ); + + if ( array_key_exists( $form_id, $unserialized_booked_slot ) ) { + $booked_slot = $unserialized_booked_slot[ $form_id ]; + $booked_slot = array_merge( (array) $booked_slot, $new_slot_booking ); + $new_booked_slot = array( $form_id => $booked_slot ); + } + + $all_booked_slot = maybe_serialize( array_replace( $unserialized_booked_slot, $new_booked_slot ) ); + } + + update_option( 'evf_booked_slot', $all_booked_slot ); + } + } + } + /** * Load Previous Field Value. * diff --git a/includes/fields/class-evf-field-date-time.php b/includes/fields/class-evf-field-date-time.php index 334cf80eb..46217e0dd 100644 --- a/includes/fields/class-evf-field-date-time.php +++ b/includes/fields/class-evf-field-date-time.php @@ -38,6 +38,7 @@ public function __construct() { 'advanced-options' => array( 'field_options' => array( 'placeholder', + 'slot_booking', 'datetime_options', 'label_hide', 'css', @@ -136,6 +137,34 @@ public function choose_style( $field ) { $this->field_element( 'row', $field, $args ); } + /** + * Use date/time field as slot booking advanced field options. + * + * @since 1.4.9 + * @param array $field Field Data. + */ + public function slot_booking( $field ) { + + $slot_booking_toggle = $this->field_element( + 'checkbox', + $field, + array( + 'slug' => 'slot_booking_advanced', + 'desc' => esc_html__( 'Slot Booking', 'everest-forms' ), + 'value' => isset( $field['slot_booking_advanced'] ) ? $field['slot_booking_advanced'] : false, + 'tooltip' => esc_html__( 'Enable to use date/time field as slot booking.', 'everest-forms' ), + 'class' => 'evf-date-format', + 'default' => false, + ), + false + ); + + $args = array( + 'slug' => 'slot_booking_advanced', + 'content' => $slot_booking_toggle, + ); + $this->field_element( 'row', $field, $args ); + } /** * Date and time advanced field options. * From 2dcddb5cb348e46d56cc5b45b7581436db0ffadd Mon Sep 17 00:00:00 2001 From: Milan Chaudhary Date: Wed, 21 Jun 2023 23:40:19 +0545 Subject: [PATCH 2/9] Add - Slot booking for both date and time --- assets/js/admin/form-builder.js | 13 +- assets/js/frontend/everest-forms.js | 34 +++++ includes/class-evf-ajax.php | 122 ++++++++++++++++++ includes/class-evf-form-task.php | 69 +++++++++- includes/class-evf-frontend-scripts.php | 1 + includes/fields/class-evf-field-date-time.php | 6 + 6 files changed, 234 insertions(+), 11 deletions(-) diff --git a/assets/js/admin/form-builder.js b/assets/js/admin/form-builder.js index 787af3d91..b24544851 100644 --- a/assets/js/admin/form-builder.js +++ b/assets/js/admin/form-builder.js @@ -559,6 +559,12 @@ var id = $( this ).attr( 'data-field-id' ); EVFPanelBuilder.dateSettingToggler( id, $('#everest-forms-field-option-' + id + '-datetime_style' ).val() ); } ); + + if($('.everest-forms-field-option-row-slot_booking_advanced input').is(":checked")) { + disable_past_date = $(document).find('.everest-forms-past-date-disable-format input'); + disable_past_date.attr("disabled", true); + disable_past_date.attr("checked", true); + } }, /** @@ -976,7 +982,7 @@ // Real-time updates for slot booking $builder.on('change', '.everest-forms-field-option-row-slot_booking_advanced input', function(event) { if($(this).is(":checked")) { - disable_past_date = $(document).find('.everest-forms-field-option-row-date_format .everest-forms-past-date-disable-format input'); + disable_past_date = $(document).find('.everest-forms-past-date-disable-format input'); if(disable_past_date.is(":not(:checked)")) { disable_past_date.attr("checked", true); } @@ -986,11 +992,6 @@ } }); - if($('.everest-forms-field-option-row-slot_booking_advanced input').is(":checked")) { - disable_past_date = $(document).find('.everest-forms-field-option-row-date_format .everest-forms-past-date-disable-format input'); - disable_past_date.attr("disabled", true); - disable_past_date.attr("checked", true); - } // Real-time updates for "Placeholder" field option. $builder.on( 'input', '.everest-forms-field-option-row-placeholder input', function(e) { var $this = $( this ), diff --git a/assets/js/frontend/everest-forms.js b/assets/js/frontend/everest-forms.js index 82677ccf5..a87b76e74 100644 --- a/assets/js/frontend/everest-forms.js +++ b/assets/js/frontend/everest-forms.js @@ -185,6 +185,40 @@ jQuery( function ( $ ) { } } ); } + $(document).find(".evf-field-date-time input").on('change', function (event) { + var slotBooking = $(this).data('slot-booking'), + targetLabel = $(this).parent().find('label'); + if(slotBooking === 1) { + var dataTimeValue = $(this).val(), + dateFormat = $(this).data('date-format'), + dateTimeFormat = $(this).data('date-time'), + mode = $(this).data('mode'), + form_id = $(this).data('form-id'), + data = {'action':'everest_forms_slot_booking', 'data-time-value':dataTimeValue, 'data-format': dateFormat, 'data-time-format': dateTimeFormat, 'mode': mode, 'form-id': form_id, 'security': everest_forms_params.everest_forms_slot_booking}; + + $.ajax({ + url:everest_forms_params.ajax_url, + data: data, + type: 'POST', + beforeSend: function () { + var submitButton = $(document).find('.evf-submit-container button'); + $(submitButton).prop('disabled', true); + }, + success: function (res) { + if(res.success === true) { + var message = res.data.message; + $(targetLabel).append(''); + var submitButton = $(document).find('.evf-submit-container button'); + $(submitButton).prop('disabled', true); + } else { + var submitButton = $(document).find('.evf-submit-container button'); + $(submitButton).prop('disabled', false); + } + } + }); + } + }) + }, init_datedropdown: function () { //Dropdown logic here diff --git a/includes/class-evf-ajax.php b/includes/class-evf-ajax.php index bf473c6e1..0a2a7fd9f 100644 --- a/includes/class-evf-ajax.php +++ b/includes/class-evf-ajax.php @@ -101,6 +101,7 @@ public static function add_ajax_events() { 'ajax_form_submission' => true, 'send_test_email' => false, 'locate_form_action' => false, + 'slot_booking' => true, ); foreach ( $ajax_events as $ajax_event => $nopriv ) { @@ -863,6 +864,127 @@ public static function locate_form_action() { ); } } + /** + * Slot booking. + */ + public static function slot_booking() { + try { + check_ajax_referer( 'everest_forms_slot_booking_nonce', 'security' ); + $datetime_value = isset( $_POST['data-time-value'] ) ? sanitize_text_field( wp_unslash( $_POST['data-time-value'] ) ) : ''; + $datetime_format = isset( $_POST['data-time-format'] ) ? sanitize_text_field( wp_unslash( $_POST['data-time-format'] ) ) : ''; + $date_format = isset( $_POST['data-format'] ) ? sanitize_text_field( wp_unslash( $_POST['data-format'] ) ) : ''; + $mode = isset( $_POST['mode'] ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : ''; + $form_id = isset( $_POST['form-id'] ) ? sanitize_text_field( wp_unslash( $_POST['form-id'] ) ) : ''; + $datetime_arr = array(); + switch ( $datetime_format ) { + case 'time': + $current_date = gmdate( 'Y-m-d' ); + $datetime_start = "$current_date $datetime_value"; + $date_time = new DateTime( $datetime_start ); + $date_time->modify( '+1 hour' ); + + $datetime_end = $date_time->format( 'Y-m-d H:i' ); + $datetime_arr[] = array( $datetime_start, $datetime_end ); + break; + case 'date': + if ( 'range' === $mode ) { + $selected_dates = explode( ' to ', $datetime_value ); + if ( 1 >= count( $selected_dates ) ) { + wp_send_json_error( + array( + 'message' => __( 'Please selected Proper range.', 'everest-forms' ), + ) + ); + } + $datetime_start = "$selected_dates[0] 00:00"; + $date_time = new DateTime( $selected_dates[1] ); + $date_time->modify( '+23 hour' ); + $datetime_end = $date_time->format( 'Y-m-d H:i' ); + array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); + } else { + $selected_dates = explode( ', ', $datetime_value ); + + foreach ( $selected_dates as $selected_date ) { + $datetime_start = "$selected_date 00:00"; + $date_time = new DateTime( $datetime_start ); + $date_time->modify( '+23 hour' ); + + $datetime_end = $date_time->format( 'Y-m-d H:i' ); + array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); + } + } + break; + case 'date-time': + if ( 'range' === $mode ) { + $selected_dates = explode( ' to ', $datetime_value ); + if ( 1 >= count( $selected_dates ) ) { + wp_send_json_error( + array( + 'message' => __( 'Please selected Proper range.', 'everest-forms' ), + ) + ); + } + $datetime_start = $selected_dates[0]; + $datetime_end = $selected_dates[1]; + array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); + } else { + $selected_dates = explode( ', ', $datetime_value ); + + foreach ( $selected_dates as $selected_date ) { + $datetime_start = $selected_date; + $date_time = new DateTime( $datetime_start ); + $date_time->modify( '+1 hour' ); + + $datetime_end = $date_time->format( 'Y-m-d H:i' ); + array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); + } + } + break; + } + + if ( empty( $datetime_arr ) ) { + wp_send_json_error( + array( + 'message' => __( 'Please select atleast one date time.', 'everest-forms' ), + ) + ); + } + $booked_slot = maybe_unserialize( get_option( 'evf_booked_slot', '' ) ); + $is_booked = false; + if ( ! empty( $booked_slot ) && array_key_exists( $form_id, $booked_slot ) ) { + foreach ( $datetime_arr as $arr ) { + + foreach ( $booked_slot[ $form_id ] as $slot ) { + if ( $arr[0] >= $slot[0] && $arr[1] <= $slot[1] ) { + $is_booked = true; + break; + } elseif ( $arr[0] >= $slot[0] && $arr[0] < $slot[1] && $arr[1] >= $slot[1] ) { + $is_booked = true; + break; + } + } + } + } + if ( $is_booked ) { + wp_send_json_success( + array( + 'message' => __( 'This slot is already booked. Please choose other slot', 'everest-forms' ), + ) + ); + } + wp_send_json_error( + array( + 'message' => __( 'This slot is not booked.', 'everest-forms' ), + ) + ); + } catch ( Exception $e ) { + wp_send_json_error( + array( + 'message' => $e->getMessage(), + ) + ); + } + } } EVF_AJAX::init(); diff --git a/includes/class-evf-form-task.php b/includes/class-evf-form-task.php index ede627a46..9b7f2daeb 100644 --- a/includes/class-evf-form-task.php +++ b/includes/class-evf-form-task.php @@ -1053,17 +1053,76 @@ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, global $wpdb; $new_slot_booking = array(); $new_slot_booking_field_meta_key_list = array(); - + $data_format = array(); foreach ( $form_data['form_fields'] as $field ) { if ( ( 'date-time' === $field['type'] ) && isset( $field['slot_booking_advanced'] ) && evf_string_to_bool( $field['slot_booking_advanced'] ) ) { - $new_slot_booking_field_meta_key_list[] = $field['meta-key']; + $new_slot_booking_field_meta_key_list[ $field['meta-key'] ] = array( $field['datetime_format'], $field['date_format'] ); + $mode = $field['date_mode']; } } if ( ! empty( $new_slot_booking_field_meta_key_list ) ) { - foreach ( $fields as $value ) { - if ( in_array( $value['meta_key'], $new_slot_booking_field_meta_key_list, true ) ) { - $new_slot_booking [] = $value['value']; + foreach ( $fields as $key => $value ) { + if ( array_key_exists( $value['meta_key'], $new_slot_booking_field_meta_key_list ) ) { + $new_value = $value['value']; + $datetime_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][0]; + $date_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][1]; + $datetime_arr = array(); + switch ( $datetime_format ) { + case 'time': + $current_date = gmdate( 'Y-m-d' ); + $datetime_start = "$current_date $new_value"; + $date_time = new DateTime( $datetime_start ); + $date_time->modify( '+1 hour' ); + + $datetime_end = $date_time->format( 'Y-m-d H:i' ); + $new_slot_booking[] = array( $datetime_start, $datetime_end ); + break; + case 'date': + if ( 'range' === $mode ) { + $selected_dates = explode( 'to ', $new_value ); + if ( 2 === count( $selected_dates ) ) { + $datetime_start = "$selected_dates[0] 00:00"; + $date_time = new DateTime( $selected_dates[1] ); + $date_time->modify( '+23 hour' ); + $datetime_end = $date_time->format( 'Y-m-d H:i' ); + array_push( $new_slot_booking, array( $datetime_start, $datetime_end ) ); + } + } else { + $selected_dates = explode( ', ', $new_value ); + + foreach ( $selected_dates as $selected_date ) { + $datetime_start = "$selected_date 00:00"; + $date_time = new DateTime( $datetime_start ); + $date_time->modify( '+23 hour' ); + + $datetime_end = $date_time->format( 'Y-m-d H:i' ); + array_push( $new_slot_booking, array( $datetime_start, $datetime_end ) ); + } + } + break; + case 'date-time': + if ( 'range' === $mode ) { + $selected_dates = explode( ' to ', $new_value ); + if ( 2 === count( $selected_dates ) ) { + $datetime_start = $selected_dates[0]; + $datetime_end = $selected_dates[1]; + array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); + } + } else { + $selected_dates = explode( ', ', $new_value ); + + foreach ( $selected_dates as $selected_date ) { + $datetime_start = $selected_date; + $date_time = new DateTime( $datetime_start ); + $date_time->modify( '+1 hour' ); + + $datetime_end = $date_time->format( 'Y-m-d H:i' ); + array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); + } + } + break; + } } } diff --git a/includes/class-evf-frontend-scripts.php b/includes/class-evf-frontend-scripts.php index 37c30c061..8fab7079f 100644 --- a/includes/class-evf-frontend-scripts.php +++ b/includes/class-evf-frontend-scripts.php @@ -286,6 +286,7 @@ private static function get_script_data( $handle ) { 'submit' => esc_html__( 'Submit', 'everest-forms' ), 'disable_user_details' => get_option( 'everest_forms_disable_user_details' ), 'everest_forms_data_save' => wp_create_nonce( 'everest_forms_data_save_nonce' ), + 'everest_forms_slot_booking' => wp_create_nonce( 'everest_forms_slot_booking_nonce' ), 'i18n_messages_required' => get_option( 'everest_forms_required_validation' ), 'i18n_messages_url' => get_option( 'everest_forms_url_validation' ), 'i18n_messages_email' => get_option( 'everest_forms_email_validation' ), diff --git a/includes/fields/class-evf-field-date-time.php b/includes/fields/class-evf-field-date-time.php index 46217e0dd..cc99cdcb2 100644 --- a/includes/fields/class-evf-field-date-time.php +++ b/includes/fields/class-evf-field-date-time.php @@ -718,6 +718,12 @@ public function field_properties( $properties, $field, $form_data ) { $properties['inputs']['primary']['attr']['data-disable-dates'] = esc_attr( $field['disable_dates'] ); } + // Input primary: Booked slot. + if ( isset( $field['slot_booking_advanced'] ) && evf_string_to_bool( $field['slot_booking_advanced'] ) ) { + $properties['inputs']['primary']['attr']['data-slot-booking'] = esc_attr( $field['slot_booking_advanced'] ); + $properties['inputs']['primary']['attr']['data-form-id'] = esc_attr( $form_data['id'] ); + } + // Input primary: data-date-time. if ( ! empty( $field['datetime_format'] ) ) { $properties['inputs']['primary']['attr']['data-date-time'] = esc_attr( $field['datetime_format'] ); From fbec7ab575894aeb4d539a32640a36dcc9dee939 Mon Sep 17 00:00:00 2001 From: Milan Chaudhary Date: Thu, 22 Jun 2023 13:58:48 +0545 Subject: [PATCH 3/9] Add - Date time slot booking for all date time mode and format --- assets/js/admin/form-builder.js | 7 ++++--- assets/js/frontend/everest-forms.js | 9 +++++++-- includes/class-evf-ajax.php | 14 +++++++++----- includes/class-evf-form-task.php | 16 ++++++++++------ includes/fields/class-evf-field-date-time.php | 3 ++- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/assets/js/admin/form-builder.js b/assets/js/admin/form-builder.js index b24544851..0bf27d887 100644 --- a/assets/js/admin/form-builder.js +++ b/assets/js/admin/form-builder.js @@ -561,9 +561,10 @@ } ); if($('.everest-forms-field-option-row-slot_booking_advanced input').is(":checked")) { + //checked and hide past dates. disable_past_date = $(document).find('.everest-forms-past-date-disable-format input'); - disable_past_date.attr("disabled", true); disable_past_date.attr("checked", true); + disable_past_date.parent().hide(); } }, @@ -986,9 +987,9 @@ if(disable_past_date.is(":not(:checked)")) { disable_past_date.attr("checked", true); } - disable_past_date.attr("disabled", true); + disable_past_date.parent().hide(); } else { - disable_past_date.attr("disabled", false); + disable_past_date.parent().show(); } }); diff --git a/assets/js/frontend/everest-forms.js b/assets/js/frontend/everest-forms.js index a87b76e74..e234edf29 100644 --- a/assets/js/frontend/everest-forms.js +++ b/assets/js/frontend/everest-forms.js @@ -187,14 +187,16 @@ jQuery( function ( $ ) { } $(document).find(".evf-field-date-time input").on('change', function (event) { var slotBooking = $(this).data('slot-booking'), - targetLabel = $(this).parent().find('label'); + targetLabel = $(this).parent(), + errorLabel = $(this).parent().find('.evf-error'); if(slotBooking === 1) { var dataTimeValue = $(this).val(), dateFormat = $(this).data('date-format'), dateTimeFormat = $(this).data('date-time'), mode = $(this).data('mode'), form_id = $(this).data('form-id'), - data = {'action':'everest_forms_slot_booking', 'data-time-value':dataTimeValue, 'data-format': dateFormat, 'data-time-format': dateTimeFormat, 'mode': mode, 'form-id': form_id, 'security': everest_forms_params.everest_forms_slot_booking}; + time_interval = $(this).data('time-interval'), + data = {'action':'everest_forms_slot_booking', 'data-time-value':dataTimeValue, 'data-format': dateFormat, 'data-time-format': dateTimeFormat, 'mode': mode, 'form-id': form_id, 'time-interval':time_interval, 'security': everest_forms_params.everest_forms_slot_booking}; $.ajax({ url:everest_forms_params.ajax_url, @@ -205,6 +207,9 @@ jQuery( function ( $ ) { $(submitButton).prop('disabled', true); }, success: function (res) { + if($(errorLabel).length) { + $(errorLabel).remove(); + } if(res.success === true) { var message = res.data.message; $(targetLabel).append(''); diff --git a/includes/class-evf-ajax.php b/includes/class-evf-ajax.php index 0a2a7fd9f..f35500a41 100644 --- a/includes/class-evf-ajax.php +++ b/includes/class-evf-ajax.php @@ -875,13 +875,15 @@ public static function slot_booking() { $date_format = isset( $_POST['data-format'] ) ? sanitize_text_field( wp_unslash( $_POST['data-format'] ) ) : ''; $mode = isset( $_POST['mode'] ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : ''; $form_id = isset( $_POST['form-id'] ) ? sanitize_text_field( wp_unslash( $_POST['form-id'] ) ) : ''; + $time_interval = isset( $_POST['time-interval'] ) ? sanitize_text_field( wp_unslash( $_POST['time-interval'] ) ) : ''; $datetime_arr = array(); switch ( $datetime_format ) { case 'time': $current_date = gmdate( 'Y-m-d' ); + $datetime_value = gmdate( 'H:i', strtotime( $datetime_value ) ); $datetime_start = "$current_date $datetime_value"; $date_time = new DateTime( $datetime_start ); - $date_time->modify( '+1 hour' ); + $date_time->modify( "+$time_interval minute" ); $datetime_end = $date_time->format( 'Y-m-d H:i' ); $datetime_arr[] = array( $datetime_start, $datetime_end ); @@ -897,6 +899,7 @@ public static function slot_booking() { ); } $datetime_start = "$selected_dates[0] 00:00"; + $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $datetime_start ) ); $date_time = new DateTime( $selected_dates[1] ); $date_time->modify( '+23 hour' ); $datetime_end = $date_time->format( 'Y-m-d H:i' ); @@ -906,6 +909,7 @@ public static function slot_booking() { foreach ( $selected_dates as $selected_date ) { $datetime_start = "$selected_date 00:00"; + $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $datetime_start ) ); $date_time = new DateTime( $datetime_start ); $date_time->modify( '+23 hour' ); @@ -924,16 +928,16 @@ public static function slot_booking() { ) ); } - $datetime_start = $selected_dates[0]; - $datetime_end = $selected_dates[1]; + $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $selected_dates[0] ) ); + $datetime_end = gmdate( 'Y-m-d H:i', strtotime( $selected_dates[1] ) ); array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); } else { $selected_dates = explode( ', ', $datetime_value ); foreach ( $selected_dates as $selected_date ) { - $datetime_start = $selected_date; + $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $selected_date ) ); $date_time = new DateTime( $datetime_start ); - $date_time->modify( '+1 hour' ); + $date_time->modify( "+$time_interval minute" ); $datetime_end = $date_time->format( 'Y-m-d H:i' ); array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); diff --git a/includes/class-evf-form-task.php b/includes/class-evf-form-task.php index 9b7f2daeb..1a1b828e2 100644 --- a/includes/class-evf-form-task.php +++ b/includes/class-evf-form-task.php @@ -1057,7 +1057,8 @@ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, foreach ( $form_data['form_fields'] as $field ) { if ( ( 'date-time' === $field['type'] ) && isset( $field['slot_booking_advanced'] ) && evf_string_to_bool( $field['slot_booking_advanced'] ) ) { $new_slot_booking_field_meta_key_list[ $field['meta-key'] ] = array( $field['datetime_format'], $field['date_format'] ); - $mode = $field['date_mode']; + $mode = $field['date_mode']; + $time_interval = $field['time_interval']; } } @@ -1071,9 +1072,10 @@ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, switch ( $datetime_format ) { case 'time': $current_date = gmdate( 'Y-m-d' ); + $new_value = gmdate( 'H:i', strtotime( $new_value ) ); $datetime_start = "$current_date $new_value"; $date_time = new DateTime( $datetime_start ); - $date_time->modify( '+1 hour' ); + $date_time->modify( "+$time_interval minute" ); $datetime_end = $date_time->format( 'Y-m-d H:i' ); $new_slot_booking[] = array( $datetime_start, $datetime_end ); @@ -1083,6 +1085,7 @@ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, $selected_dates = explode( 'to ', $new_value ); if ( 2 === count( $selected_dates ) ) { $datetime_start = "$selected_dates[0] 00:00"; + $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $datetime_start ) ); $date_time = new DateTime( $selected_dates[1] ); $date_time->modify( '+23 hour' ); $datetime_end = $date_time->format( 'Y-m-d H:i' ); @@ -1093,6 +1096,7 @@ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, foreach ( $selected_dates as $selected_date ) { $datetime_start = "$selected_date 00:00"; + $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $datetime_start ) ); $date_time = new DateTime( $datetime_start ); $date_time->modify( '+23 hour' ); @@ -1107,18 +1111,18 @@ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, if ( 2 === count( $selected_dates ) ) { $datetime_start = $selected_dates[0]; $datetime_end = $selected_dates[1]; - array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); + array_push( $new_slot_booking, array( $datetime_start, $datetime_end ) ); } } else { $selected_dates = explode( ', ', $new_value ); foreach ( $selected_dates as $selected_date ) { - $datetime_start = $selected_date; + $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $selected_date ) ); $date_time = new DateTime( $datetime_start ); - $date_time->modify( '+1 hour' ); + $date_time->modify( "+$time_interval minute" ); $datetime_end = $date_time->format( 'Y-m-d H:i' ); - array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); + array_push( $new_slot_booking, array( $datetime_start, $datetime_end ) ); } } break; diff --git a/includes/fields/class-evf-field-date-time.php b/includes/fields/class-evf-field-date-time.php index cc99cdcb2..c610bde53 100644 --- a/includes/fields/class-evf-field-date-time.php +++ b/includes/fields/class-evf-field-date-time.php @@ -153,7 +153,7 @@ public function slot_booking( $field ) { 'desc' => esc_html__( 'Slot Booking', 'everest-forms' ), 'value' => isset( $field['slot_booking_advanced'] ) ? $field['slot_booking_advanced'] : false, 'tooltip' => esc_html__( 'Enable to use date/time field as slot booking.', 'everest-forms' ), - 'class' => 'evf-date-format', + 'class' => 'slot-booking-advanced ', 'default' => false, ), false @@ -539,6 +539,7 @@ public function datetime_options( $field ) { 'options' => array( '15' => esc_html__( '15 minutes', 'everest-forms' ), '30' => esc_html__( '30 minutes', 'everest-forms' ), + '60' => esc_html__( '1 hour', 'everest-forms' ), ), ), false From fbce172e4f5ed60bfe3ae404afc42dcd66808a4f Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 12 Jul 2023 13:50:56 +0545 Subject: [PATCH 4/9] code refactore --- includes/class-evf-ajax.php | 73 +------------------- includes/class-evf-form-task.php | 112 ++++++++----------------------- includes/evf-core-functions.php | 71 ++++++++++++++++++++ 3 files changed, 101 insertions(+), 155 deletions(-) diff --git a/includes/class-evf-ajax.php b/includes/class-evf-ajax.php index f35500a41..5b3c33828 100644 --- a/includes/class-evf-ajax.php +++ b/includes/class-evf-ajax.php @@ -876,80 +876,13 @@ public static function slot_booking() { $mode = isset( $_POST['mode'] ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : ''; $form_id = isset( $_POST['form-id'] ) ? sanitize_text_field( wp_unslash( $_POST['form-id'] ) ) : ''; $time_interval = isset( $_POST['time-interval'] ) ? sanitize_text_field( wp_unslash( $_POST['time-interval'] ) ) : ''; - $datetime_arr = array(); - switch ( $datetime_format ) { - case 'time': - $current_date = gmdate( 'Y-m-d' ); - $datetime_value = gmdate( 'H:i', strtotime( $datetime_value ) ); - $datetime_start = "$current_date $datetime_value"; - $date_time = new DateTime( $datetime_start ); - $date_time->modify( "+$time_interval minute" ); - - $datetime_end = $date_time->format( 'Y-m-d H:i' ); - $datetime_arr[] = array( $datetime_start, $datetime_end ); - break; - case 'date': - if ( 'range' === $mode ) { - $selected_dates = explode( ' to ', $datetime_value ); - if ( 1 >= count( $selected_dates ) ) { - wp_send_json_error( - array( - 'message' => __( 'Please selected Proper range.', 'everest-forms' ), - ) - ); - } - $datetime_start = "$selected_dates[0] 00:00"; - $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $datetime_start ) ); - $date_time = new DateTime( $selected_dates[1] ); - $date_time->modify( '+23 hour' ); - $datetime_end = $date_time->format( 'Y-m-d H:i' ); - array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); - } else { - $selected_dates = explode( ', ', $datetime_value ); - - foreach ( $selected_dates as $selected_date ) { - $datetime_start = "$selected_date 00:00"; - $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $datetime_start ) ); - $date_time = new DateTime( $datetime_start ); - $date_time->modify( '+23 hour' ); - - $datetime_end = $date_time->format( 'Y-m-d H:i' ); - array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); - } - } - break; - case 'date-time': - if ( 'range' === $mode ) { - $selected_dates = explode( ' to ', $datetime_value ); - if ( 1 >= count( $selected_dates ) ) { - wp_send_json_error( - array( - 'message' => __( 'Please selected Proper range.', 'everest-forms' ), - ) - ); - } - $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $selected_dates[0] ) ); - $datetime_end = gmdate( 'Y-m-d H:i', strtotime( $selected_dates[1] ) ); - array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); - } else { - $selected_dates = explode( ', ', $datetime_value ); - - foreach ( $selected_dates as $selected_date ) { - $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $selected_date ) ); - $date_time = new DateTime( $datetime_start ); - $date_time->modify( "+$time_interval minute" ); - - $datetime_end = $date_time->format( 'Y-m-d H:i' ); - array_push( $datetime_arr, array( $datetime_start, $datetime_end ) ); - } - } - break; - } + $datetime_arr = parse_datetime_values( $datetime_value, $datetime_format, $date_format, $mode, $time_interval ); + lg($datetime_arr); if ( empty( $datetime_arr ) ) { wp_send_json_error( array( - 'message' => __( 'Please select atleast one date time.', 'everest-forms' ), + 'message' => __( 'Please select at least one date time.', 'everest-forms' ), ) ); } diff --git a/includes/class-evf-form-task.php b/includes/class-evf-form-task.php index 1a1b828e2..e378c1b87 100644 --- a/includes/class-evf-form-task.php +++ b/includes/class-evf-form-task.php @@ -1050,107 +1050,49 @@ public function entry_save( $fields, $entry, $form_id, $form_data = array() ) { * @param array $form_data Prepared form settings. */ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, $form_data ) { - global $wpdb; $new_slot_booking = array(); $new_slot_booking_field_meta_key_list = array(); - $data_format = array(); + $time_interval = 0; foreach ( $form_data['form_fields'] as $field ) { if ( ( 'date-time' === $field['type'] ) && isset( $field['slot_booking_advanced'] ) && evf_string_to_bool( $field['slot_booking_advanced'] ) ) { - $new_slot_booking_field_meta_key_list[ $field['meta-key'] ] = array( $field['datetime_format'], $field['date_format'] ); - $mode = $field['date_mode']; + $new_slot_booking_field_meta_key_list[ $field['meta-key'] ] = array( + $field['datetime_format'], + $field['date_format'], + ); $time_interval = $field['time_interval']; } } - if ( ! empty( $new_slot_booking_field_meta_key_list ) ) { - foreach ( $fields as $key => $value ) { - if ( array_key_exists( $value['meta_key'], $new_slot_booking_field_meta_key_list ) ) { - $new_value = $value['value']; - $datetime_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][0]; - $date_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][1]; - $datetime_arr = array(); - switch ( $datetime_format ) { - case 'time': - $current_date = gmdate( 'Y-m-d' ); - $new_value = gmdate( 'H:i', strtotime( $new_value ) ); - $datetime_start = "$current_date $new_value"; - $date_time = new DateTime( $datetime_start ); - $date_time->modify( "+$time_interval minute" ); - - $datetime_end = $date_time->format( 'Y-m-d H:i' ); - $new_slot_booking[] = array( $datetime_start, $datetime_end ); - break; - case 'date': - if ( 'range' === $mode ) { - $selected_dates = explode( 'to ', $new_value ); - if ( 2 === count( $selected_dates ) ) { - $datetime_start = "$selected_dates[0] 00:00"; - $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $datetime_start ) ); - $date_time = new DateTime( $selected_dates[1] ); - $date_time->modify( '+23 hour' ); - $datetime_end = $date_time->format( 'Y-m-d H:i' ); - array_push( $new_slot_booking, array( $datetime_start, $datetime_end ) ); - } - } else { - $selected_dates = explode( ', ', $new_value ); - - foreach ( $selected_dates as $selected_date ) { - $datetime_start = "$selected_date 00:00"; - $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $datetime_start ) ); - $date_time = new DateTime( $datetime_start ); - $date_time->modify( '+23 hour' ); - - $datetime_end = $date_time->format( 'Y-m-d H:i' ); - array_push( $new_slot_booking, array( $datetime_start, $datetime_end ) ); - } - } - break; - case 'date-time': - if ( 'range' === $mode ) { - $selected_dates = explode( ' to ', $new_value ); - if ( 2 === count( $selected_dates ) ) { - $datetime_start = $selected_dates[0]; - $datetime_end = $selected_dates[1]; - array_push( $new_slot_booking, array( $datetime_start, $datetime_end ) ); - } - } else { - $selected_dates = explode( ', ', $new_value ); - - foreach ( $selected_dates as $selected_date ) { - $datetime_start = gmdate( 'Y-m-d H:i', strtotime( $selected_date ) ); - $date_time = new DateTime( $datetime_start ); - $date_time->modify( "+$time_interval minute" ); - - $datetime_end = $date_time->format( 'Y-m-d H:i' ); - array_push( $new_slot_booking, array( $datetime_start, $datetime_end ) ); - } - } - break; - } - } + foreach ( $fields as $key => $value ) { + if ( array_key_exists( $value['meta_key'], $new_slot_booking_field_meta_key_list ) ) { + $new_value = $value['value']; + $datetime_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][0]; + $date_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][1]; + $datetime_arr = parse_datetime_values( $new_value, $datetime_format, $date_format, $mode, $time_interval ); } + } - if ( ! empty( $new_slot_booking ) ) { - $get_booked_slot = get_option( 'evf_booked_slot', array() ); - - $new_booked_slot = array( $form_id => $new_slot_booking ); - if ( empty( $get_booked_slot ) ) { - $all_booked_slot = maybe_serialize( $new_booked_slot ); - } else { - $unserialized_booked_slot = maybe_unserialize( $get_booked_slot ); + if ( ! empty( $new_slot_booking ) ) { + $get_booked_slot = get_option( 'evf_booked_slot', array() ); + $new_booked_slot = array( $form_id => $new_slot_booking ); - if ( array_key_exists( $form_id, $unserialized_booked_slot ) ) { - $booked_slot = $unserialized_booked_slot[ $form_id ]; - $booked_slot = array_merge( (array) $booked_slot, $new_slot_booking ); - $new_booked_slot = array( $form_id => $booked_slot ); - } + if ( empty( $get_booked_slot ) ) { + $all_booked_slot = maybe_serialize( $new_booked_slot ); + } else { + $unserialized_booked_slot = maybe_unserialize( $get_booked_slot ); - $all_booked_slot = maybe_serialize( array_replace( $unserialized_booked_slot, $new_booked_slot ) ); + if ( array_key_exists( $form_id, $unserialized_booked_slot ) ) { + $booked_slot = $unserialized_booked_slot[ $form_id ]; + $booked_slot = array_merge( (array) $booked_slot, $new_slot_booking ); + $new_booked_slot = array( $form_id => $booked_slot ); } - update_option( 'evf_booked_slot', $all_booked_slot ); + $all_booked_slot = maybe_serialize( array_replace( $unserialized_booked_slot, $new_booked_slot ) ); } + + update_option( 'evf_booked_slot', $all_booked_slot ); } + } /** diff --git a/includes/evf-core-functions.php b/includes/evf-core-functions.php index 56240c12f..1967ece43 100644 --- a/includes/evf-core-functions.php +++ b/includes/evf-core-functions.php @@ -4635,4 +4635,75 @@ function evf_file_get_contents( $file ) { return; } +/** +* Parses datetime values based on the provided format and mode. +* +* @param string $datetime_value The datetime value to parse. +* @param string $datetime_format The format of the datetime value. +* @param string $date_format The format of the date. +* @param string $mode The mode of the datetime field, +* @param int $time_interval The time interval in minutes. +* @return array +*/ +function parse_datetime_values( $datetime_value, $datetime_format, $date_format, $mode, $time_interval ) { + $datetime_arr = array(); + + switch ($datetime_format) { + case 'time': + $current_date = gmdate('Y-m-d'); + $datetime_value = gmdate('H:i', strtotime($datetime_value)); + $datetime_start = "$current_date $datetime_value"; + $date_time = new DateTime($datetime_start); + $date_time->modify("+$time_interval minute"); + $datetime_end = $date_time->format('Y-m-d H:i'); + $datetime_arr[] = array($datetime_start, $datetime_end); + break; + case 'date': + if ('range' === $mode) { + $selected_dates = explode(' to ', $datetime_value); + if (count($selected_dates) >= 2) { + $datetime_start = "$selected_dates[0] 00:00"; + $datetime_start = gmdate('Y-m-d H:i', strtotime($datetime_start)); + $date_time = new DateTime($selected_dates[1]); + $date_time->modify('+23 hour'); + $datetime_end = $date_time->format('Y-m-d H:i'); + $datetime_arr[] = array($datetime_start, $datetime_end); + } + } else { + $selected_dates = explode(', ', $datetime_value); + + foreach ($selected_dates as $selected_date) { + $datetime_start = "$selected_date 00:00"; + $datetime_start = gmdate('Y-m-d H:i', strtotime($datetime_start)); + $date_time = new DateTime($datetime_start); + $date_time->modify('+23 hour'); + + $datetime_end = $date_time->format('Y-m-d H:i'); + $datetime_arr[] = array($datetime_start, $datetime_end); + } + } + break; + case 'date-time': + if ('range' === $mode) { + $selected_dates = explode(' to ', $datetime_value); + if (count($selected_dates) >= 2) { + $datetime_start = gmdate('Y-m-d H:i', strtotime($selected_dates[0])); + $datetime_end = gmdate('Y-m-d H:i', strtotime($selected_dates[1])); + $datetime_arr[] = array($datetime_start, $datetime_end); + } + } else { + $selected_dates = explode(', ', $datetime_value); + + foreach ($selected_dates as $selected_date) { + $datetime_start = gmdate('Y-m-d H:i', strtotime($selected_date)); + $date_time = new DateTime($datetime_start); + $date_time->modify("+$time_interval minute"); + $datetime_end = $date_time->format('Y-m-d H:i'); + $datetime_arr[] = array($datetime_start, $datetime_end); + } + } + break; + } + return $datetime_arr; +} From 2027a8378568ab93755a819612bde38c338bfc2f Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 12 Jul 2023 13:52:14 +0545 Subject: [PATCH 5/9] Code cleanup --- includes/class-evf-ajax.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-evf-ajax.php b/includes/class-evf-ajax.php index 5b3c33828..4e89cb6aa 100644 --- a/includes/class-evf-ajax.php +++ b/includes/class-evf-ajax.php @@ -877,7 +877,7 @@ public static function slot_booking() { $form_id = isset( $_POST['form-id'] ) ? sanitize_text_field( wp_unslash( $_POST['form-id'] ) ) : ''; $time_interval = isset( $_POST['time-interval'] ) ? sanitize_text_field( wp_unslash( $_POST['time-interval'] ) ) : ''; $datetime_arr = parse_datetime_values( $datetime_value, $datetime_format, $date_format, $mode, $time_interval ); - lg($datetime_arr); + if ( empty( $datetime_arr ) ) { wp_send_json_error( From 958db638226f3f3dc73ab73cfddc27727d4f0f34 Mon Sep 17 00:00:00 2001 From: Milan Chaudhary Date: Thu, 13 Jul 2023 12:25:38 +0545 Subject: [PATCH 6/9] Fix - Backend validation --- assets/css/admin.scss | 5 +- assets/js/admin/form-builder.js | 4 +- includes/abstracts/class-evf-form-fields.php | 30 ++++++++++++ includes/class-evf-form-task.php | 9 ++-- includes/fields/class-evf-field-date-time.php | 48 +++++++------------ 5 files changed, 56 insertions(+), 40 deletions(-) diff --git a/assets/css/admin.scss b/assets/css/admin.scss index a37c89cb4..de2465117 100644 --- a/assets/css/admin.scss +++ b/assets/css/admin.scss @@ -2161,7 +2161,7 @@ p.search-box { } &.upgrade-modal, - &.enable-stripe-model, + &.enable-stripe-model, &.enable-authorize-net-model { opacity: 0.45; } @@ -2275,7 +2275,8 @@ p.search-box { .everest-forms-current-date-format, .everest-forms-past-date-disable-format, - .everest-forms-min-max-date-format { + .everest-forms-min-max-date-format, + .everest-forms-slot-booking { margin-bottom: 14px; } diff --git a/assets/js/admin/form-builder.js b/assets/js/admin/form-builder.js index 0bf27d887..4797da531 100644 --- a/assets/js/admin/form-builder.js +++ b/assets/js/admin/form-builder.js @@ -560,7 +560,7 @@ EVFPanelBuilder.dateSettingToggler( id, $('#everest-forms-field-option-' + id + '-datetime_style' ).val() ); } ); - if($('.everest-forms-field-option-row-slot_booking_advanced input').is(":checked")) { + if($('.everest-forms-slot-booking input').is(":checked")) { //checked and hide past dates. disable_past_date = $(document).find('.everest-forms-past-date-disable-format input'); disable_past_date.attr("checked", true); @@ -981,7 +981,7 @@ } }); // Real-time updates for slot booking - $builder.on('change', '.everest-forms-field-option-row-slot_booking_advanced input', function(event) { + $builder.on('change', '.everest-forms-slot-booking input', function(event) { if($(this).is(":checked")) { disable_past_date = $(document).find('.everest-forms-past-date-disable-format input'); if(disable_past_date.is(":not(:checked)")) { diff --git a/includes/abstracts/class-evf-form-fields.php b/includes/abstracts/class-evf-form-fields.php index 7f9d7d088..ada1053d9 100644 --- a/includes/abstracts/class-evf-form-fields.php +++ b/includes/abstracts/class-evf-form-fields.php @@ -2422,6 +2422,36 @@ public function validate( $field_id, $field_submit, $form_data ) { } } break; + case 'date-time': + $slot_booking = isset( $form_data['form_fields'][ $field_id ]['slot_booking_advanced'] ) ? $form_data['form_fields'][ $field_id ]['slot_booking_advanced'] : ''; + if ( $slot_booking ) { + $datetime_format = isset( $form_data['form_fields'][ $field_id ]['datetime_format'] ) ? $form_data['form_fields'][ $field_id ]['datetime_format'] : ''; + $date_format = isset( $form_data['form_fields'][ $field_id ]['date_format'] ) ? $form_data['form_fields'][ $field_id ]['date_format'] : ''; + $mode = isset( $form_data['form_fields'][ $field_id ]['date_mode'] ) ? $form_data['form_fields'][ $field_id ]['date_mode'] : ''; + $time_interval = isset( $form_data['form_fields'][ $field_id ]['time_interval'] ) ? $form_data['form_fields'][ $field_id ]['time_interval'] : ''; + $datetime_arr = parse_datetime_values( $field_submit, $datetime_format, $date_format, $mode, $time_interval ); + $booked_slot = maybe_unserialize( get_option( 'evf_booked_slot', '' ) ); + $form_id = $form_data['id']; + $is_booked = false; + if ( ! empty( $booked_slot ) && array_key_exists( $form_id, $booked_slot ) ) { + foreach ( $datetime_arr as $arr ) { + + foreach ( $booked_slot[ $form_id ] as $slot ) { + if ( $arr[0] >= $slot[0] && $arr[1] <= $slot[1] ) { + $is_booked = true; + break; + } elseif ( $arr[0] >= $slot[0] && $arr[0] < $slot[1] && $arr[1] >= $slot[1] ) { + $is_booked = true; + break; + } + } + } + } + if ( $is_booked ) { + $validation_text = get_option( 'evf_' . $field_type . '_validation', esc_html__( 'This slot is already booked. Please choose other slot.', 'everest-forms' ) ); + } + } + break; } if ( isset( $validation_text ) ) { diff --git a/includes/class-evf-form-task.php b/includes/class-evf-form-task.php index e378c1b87..d08e245d5 100644 --- a/includes/class-evf-form-task.php +++ b/includes/class-evf-form-task.php @@ -1050,7 +1050,6 @@ public function entry_save( $fields, $entry, $form_id, $form_data = array() ) { * @param array $form_data Prepared form settings. */ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, $form_data ) { - $new_slot_booking = array(); $new_slot_booking_field_meta_key_list = array(); $time_interval = 0; foreach ( $form_data['form_fields'] as $field ) { @@ -1058,6 +1057,7 @@ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, $new_slot_booking_field_meta_key_list[ $field['meta-key'] ] = array( $field['datetime_format'], $field['date_format'], + $field['date_mode'], ); $time_interval = $field['time_interval']; } @@ -1068,13 +1068,14 @@ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, $new_value = $value['value']; $datetime_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][0]; $date_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][1]; + $mode = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][2]; $datetime_arr = parse_datetime_values( $new_value, $datetime_format, $date_format, $mode, $time_interval ); } } - if ( ! empty( $new_slot_booking ) ) { + if ( ! empty( $datetime_arr ) ) { $get_booked_slot = get_option( 'evf_booked_slot', array() ); - $new_booked_slot = array( $form_id => $new_slot_booking ); + $new_booked_slot = array( $form_id => $datetime_arr ); if ( empty( $get_booked_slot ) ) { $all_booked_slot = maybe_serialize( $new_booked_slot ); @@ -1083,7 +1084,7 @@ public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, if ( array_key_exists( $form_id, $unserialized_booked_slot ) ) { $booked_slot = $unserialized_booked_slot[ $form_id ]; - $booked_slot = array_merge( (array) $booked_slot, $new_slot_booking ); + $booked_slot = array_merge( (array) $booked_slot, $datetime_arr ); $new_booked_slot = array( $form_id => $booked_slot ); } diff --git a/includes/fields/class-evf-field-date-time.php b/includes/fields/class-evf-field-date-time.php index c610bde53..9882964b0 100644 --- a/includes/fields/class-evf-field-date-time.php +++ b/includes/fields/class-evf-field-date-time.php @@ -38,7 +38,6 @@ public function __construct() { 'advanced-options' => array( 'field_options' => array( 'placeholder', - 'slot_booking', 'datetime_options', 'label_hide', 'css', @@ -137,34 +136,6 @@ public function choose_style( $field ) { $this->field_element( 'row', $field, $args ); } - /** - * Use date/time field as slot booking advanced field options. - * - * @since 1.4.9 - * @param array $field Field Data. - */ - public function slot_booking( $field ) { - - $slot_booking_toggle = $this->field_element( - 'checkbox', - $field, - array( - 'slug' => 'slot_booking_advanced', - 'desc' => esc_html__( 'Slot Booking', 'everest-forms' ), - 'value' => isset( $field['slot_booking_advanced'] ) ? $field['slot_booking_advanced'] : false, - 'tooltip' => esc_html__( 'Enable to use date/time field as slot booking.', 'everest-forms' ), - 'class' => 'slot-booking-advanced ', - 'default' => false, - ), - false - ); - - $args = array( - 'slug' => 'slot_booking_advanced', - 'content' => $slot_booking_toggle, - ); - $this->field_element( 'row', $field, $args ); - } /** * Date and time advanced field options. * @@ -404,6 +375,20 @@ public function datetime_options( $field ) { false ); + $slot_booking_toggle = $this->field_element( + 'checkbox', + $field, + array( + 'slug' => 'slot_booking_advanced', + 'desc' => esc_html__( 'Slot Booking', 'everest-forms' ), + 'value' => isset( $field['slot_booking_advanced'] ) ? $field['slot_booking_advanced'] : false, + 'tooltip' => esc_html__( 'Enable to use date/time field as slot booking.', 'everest-forms' ), + 'class' => 'slot-booking-advanced ', + 'default' => false, + ), + false + ); + $min_date_label = $this->field_element( 'label', $field, @@ -508,7 +493,7 @@ public function datetime_options( $field ) { $args = array( 'slug' => 'date_format', - 'content' => $date_format_label . $date_format_select . $disable_dates_label . $disable_dates . $date_localization_label . $date_localization_select . $date_timezone_label . $date_timezone_select . '
' . $current_date_mode . '
' . $current_date_default . '
' . $enable_past_date_disable . '
' . $enable_min_max . '
' . $set_date_range . '
' . $min_date_label . $min_date . $max_date_label . $max_date . '
' . $min_date_range_level . $min_date_range . $max_date_range_label . $max_date_range . '
', + 'content' => $date_format_label . $date_format_select . $disable_dates_label . $disable_dates . $date_localization_label . $date_localization_select . $date_timezone_label . $date_timezone_select . '
' . $current_date_mode . '
' . $current_date_default . '
' . $enable_past_date_disable . '
' . $slot_booking_toggle . '
' . $enable_min_max . '
' . $set_date_range . '
' . $min_date_label . $min_date . $max_date_label . $max_date . '
' . $min_date_range_level . $min_date_range . $max_date_range_label . $max_date_range . '
', ); $this->field_element( 'row', $field, $args ); @@ -646,8 +631,7 @@ public function datetime_options( $field ) { ), false ); - - $args = array( + $args = array( 'slug' => 'time_interval_format', 'content' => $time_format_label . $time_interval_select . $time_format_select . $enable_min_max_time . $select_min_time . $min_time_select . $select_max_time . $max_time_select, ); From 213b6d087d2f6aafda3e3e4b41a8c352dba3e93c Mon Sep 17 00:00:00 2001 From: Milan Chaudhary Date: Mon, 17 Jul 2023 10:02:06 +0545 Subject: [PATCH 7/9] Fix - PHPcs --- includes/class-evf-ajax.php | 1 - includes/evf-core-functions.php | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-evf-ajax.php b/includes/class-evf-ajax.php index 4e89cb6aa..6842e5aa7 100644 --- a/includes/class-evf-ajax.php +++ b/includes/class-evf-ajax.php @@ -877,7 +877,6 @@ public static function slot_booking() { $form_id = isset( $_POST['form-id'] ) ? sanitize_text_field( wp_unslash( $_POST['form-id'] ) ) : ''; $time_interval = isset( $_POST['time-interval'] ) ? sanitize_text_field( wp_unslash( $_POST['time-interval'] ) ) : ''; $datetime_arr = parse_datetime_values( $datetime_value, $datetime_format, $date_format, $mode, $time_interval ); - if ( empty( $datetime_arr ) ) { wp_send_json_error( diff --git a/includes/evf-core-functions.php b/includes/evf-core-functions.php index 1967ece43..0d40533f1 100644 --- a/includes/evf-core-functions.php +++ b/includes/evf-core-functions.php @@ -1128,6 +1128,7 @@ function evf_get_random_string( $length = 10 ) { * Get all forms. * * @param bool $skip_disabled_entries True to skip disabled entries. + * @param bool $check_disable_storing_entry_info Check disable storing entry. * @return array of form data. */ function evf_get_all_forms( $skip_disabled_entries = false, $check_disable_storing_entry_info = true ) { @@ -4620,7 +4621,7 @@ function evf_file_get_contents( $file ) { if ( $file ) { $local_file = preg_replace( '/\\\\|\/\//', '/', plugin_dir_path( EVF_PLUGIN_FILE ) . $file ); $response = file_get_contents($local_file); - if( $response ){ + if ( $response ) { return $response; } global $wp_filesystem; From 2b09aed9aceb54bdb303991c84c307fc1d9e9e84 Mon Sep 17 00:00:00 2001 From: Milan Chaudhary Date: Mon, 17 Jul 2023 13:59:19 +0545 Subject: [PATCH 8/9] Fix - Disabled past date issue --- assets/js/admin/form-builder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/admin/form-builder.js b/assets/js/admin/form-builder.js index 4797da531..95e2d7229 100644 --- a/assets/js/admin/form-builder.js +++ b/assets/js/admin/form-builder.js @@ -985,7 +985,7 @@ if($(this).is(":checked")) { disable_past_date = $(document).find('.everest-forms-past-date-disable-format input'); if(disable_past_date.is(":not(:checked)")) { - disable_past_date.attr("checked", true); + disable_past_date.prop("checked", true); } disable_past_date.parent().hide(); } else { From 60371d2f78780afa0b70919320f8e0a08985c94b Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 17 Jul 2023 14:51:16 +0545 Subject: [PATCH 9/9] Changelog updated --- changelog.txt | 3 ++- readme.txt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index f8a9ab215..073b1c108 100644 --- a/changelog.txt +++ b/changelog.txt @@ -3,7 +3,8 @@ - 2.0.2 - xx-xx-2023 * Feature - Regex validation for field. * Feature - Custom CSS and JS for form. -* Enhancement - Upgrade to the pro improve for dragging a field +* Enhancement - Upgrade to the pro improve for dragging a field. +* Enhancement - Date time slot booking. * Fix - Word count issue with Cyrillic text. - 2.0.1 - 14-06-2023 diff --git a/readme.txt b/readme.txt index bd4fb26d8..7baed319d 100644 --- a/readme.txt +++ b/readme.txt @@ -420,7 +420,8 @@ Yes you can! Join in on our [GitHub repository](https://github.com/wpeverest/eve - 2.0.2 - xx-xx-2023 * Feature - Regex validation for field. * Feature - Custom CSS and JS for form. -* Enhancement - Upgrade to the pro improve for dragging a field +* Enhancement - Upgrade to the pro improve for dragging a field. +* Enhancement - Date time slot booking. * Fix - Word count issue with Cyrillic text.