// Templates ===================================================================================================
/**
* Templates at footer of page
*
* @param $page string
*/
public function hook__page_footer_tmpl( $page ){
// page=wpbc&view_mode=vm_booking_listing
if ( 'wpbc-ajx_booking_availability' === $page ) { // from >> do_action( 'wpbc_hook_settings_page_footer', 'wpbc-ajx_booking_availability' ); as availability_page.php in bottom of content method
$this->template__main_page_content();
$this->template_toolbar_select_booking_resource();
$this->template_toolbar_select_month_number_in_row(); // FixIn: 10.8.1.5.
$this->template__widget_available_unavailable();
$this->template__widget_calendar_legend();
}
}
/**
* Template
*
* Help Tips:
*
*
*
* var template__var = wp.template( 'template_name_a' );
*
* jQuery( '.content' ).html( template__var( { 'test_key' => 'Data' } ) );
*
* @return void
*/
private function template__main_page_content() {
?>
#>
{{{ wpbc_widget__available_unavailable( data.ajx_cleaned_params ) }}}
{{{ wpbc_widget__calendar_legend( { } ) }}}
#>
';
return false;
}
/*
?><# console.log( ' == TEMPLATE PARAMS "wpbc_ajx_change_booking_resource" == ', data ); #>';
return false;
}
?> $el_id
, 'label' => '' . esc_html__( 'Booking resource', 'booking' ) . ':'
)
);
?>
//
// A J A X =====================================================================================================
/**
* Define HOOKs for start loading Ajax
*/
public function define_ajax_hook(){
// Ajax Handlers. Note. "locale_for_ajax" rechecked in wpbc-ajax.php
add_action( 'wp_ajax_' . 'WPBC_AJX_AVAILABILITY', array( $this, 'ajax_' . 'WPBC_AJX_AVAILABILITY' ) ); // Admin & Client (logged in usres)
// Ajax Handlers for actions
//add_action( 'wp_ajax_' . 'WPBC_AJX_BOOKING_ACTIONS', 'wpbc_ajax_' . 'WPBC_AJX_BOOKING_ACTIONS' );
// add_action( 'wp_ajax_nopriv_' . 'WPBC_AJX_BOOKING_LISTING', array( $this, 'ajax_' . 'WPBC_AJX_BOOKING_LISTING' ) ); // Client (not logged in)
}
/**
* Ajax - Get Listing Data and Response to JS script
*/
public function ajax_WPBC_AJX_AVAILABILITY() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['search_params'] ) || empty( $_POST['search_params'] ) ) { exit; }
// Security ----------------------------------------------------------------------------------------------- // in Ajax Post: 'nonce': wpbc_ajx_booking_listing.get_secure_param( 'nonce' ),
$action_name = 'wpbc_ajx_availability_ajx' . '_wpbcnonce';
$nonce_post_key = 'nonce';
$result_check = check_ajax_referer( $action_name, $nonce_post_key );
$user_id = ( isset( $_REQUEST['wpbc_ajx_user_id'] ) ) ? intval( $_REQUEST['wpbc_ajx_user_id'] ) : wpbc_get_current_user_id(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
/**
* SQL ---------------------------------------------------------------------------
*
* in Ajax Post: 'search_params': wpbc_ajx_booking_listing.search_get_all_params()
*
* Use prefix "search_params", if Ajax sent -
* $_REQUEST['search_params']['page_num'], $_REQUEST['search_params']['page_items_count'],..
*/
$user_request = new WPBC_AJX__REQUEST( array(
'db_option_name' => 'booking_availability_request_params',
'user_id' => $user_id,
'request_rules_structure' => WPBC_AJX__Availability::request_rules_structure()
)
);
$request_prefix = 'search_params';
$request_params = $user_request->get_sanitized__in_request__value_or_default( $request_prefix ); // NOT Direct: $_REQUEST['search_params']['resource_id']
//----------------------------------------------------------------------------------------------------------
// Get booking resources (sql)
$resources_arr = wpbc_ajx_get_all_booking_resources_arr(); /**
* Array ( [0] => Array ( [booking_type_id] => 1
[title] => Standard
[users] => 1
[import] =>
[export] =>
[cost] => 25
[default_form] => standard
[prioritet] => 0
[parent] => 0
[visitors] => 2
), ... */
$resources_arr_sorted = wpbc_ajx_get_sorted_booking_resources_arr( $resources_arr );
// FixIn: 10.3.0.7.
// Check if $request_params['resource_id'] exist in $resources_arr_sorted, because it possible situation, when some booking resources was deleted and we do not need to load old data.
$is_resource_exist = false;
foreach ( $resources_arr_sorted as $resource_index => $resource_value_arr ) {
if ( intval( $resource_value_arr['booking_type_id'] ) === intval( $request_params['resource_id'] ) ) {
$is_resource_exist = true;
break; // All good
}
}
if ( ! $is_resource_exist ) {
if ( count( $resources_arr_sorted ) > 0 ) {
$request_params['resource_id'] = intval( $resources_arr_sorted[0]['booking_type_id'] );
}
}
$data_arr = array();
$data_arr['ajx_after_action_message'] = '';
$data_arr['ajx_after_action_result'] = 1;
if ( 'set_availability' == $request_params['do_action'] ) {
// Dates -- update status in DB
$row_number = wpbc_availability__update_dates_status__sql( array(
'resource_id' => $request_params['resource_id'], // int
'prop_name' => 'date_status', // 'rate', 'allow_start_day_selection', 'allow_days_number_to_select', 'availability_count', ...
'prop_value' => $request_params['dates_availability'], // 'available', 'unavailable' , 'pending', 'approved'
'dates_selection' => $request_params['dates_selection'] // '2023-04-04 | 2023-04-07'
) );
if ( false !== $row_number ) {
if ( 'available' == $request_params['dates_availability'] ) {
/* translators: 1: ... */
$data_arr['ajx_after_action_message'] = sprintf( _n( '%1$s date has been set as %2$s available %3$s', '%1$s dates has been set as %2$s available %3$s', $row_number, 'booking' )
, '' . $row_number . ''
, '', '' );
} else {
/* translators: 1: ... */
$data_arr['ajx_after_action_message'] = sprintf( _n( '%1$s date has been set as %2$s unavailable %3$s', '%1$s dates has been set as %2$s unavailable %3$s', $row_number, 'booking' )
, '' . $row_number . ''
, '', '' );
}
$data_arr['ajx_after_action_result'] = ( $row_number > 0 ) ? 1 : 0;
}
}
if ( 'erase_availability' == $request_params['do_action'] ) {
//TODO: Make new fucntion here to delete all items from DB relative to this booking resource 2023-02-06 14:42
// Dates -- update status in DB
$row_number = wpbc_availability__delete_dates_status__sql( array(
'resource_id' => $request_params['resource_id'], // int
'prop_name' => 'date_status', // 'rate', 'allow_start_day_selection', 'allow_days_number_to_select', 'availability_count', ...
//'prop_value' => $request_params['dates_availability'], // 'available', 'unavailable' , 'pending', 'approved'
'dates_selection' => 'all' // '2023-04-04 | 2023-04-07'
) );
$data_arr['ajx_after_action_message'] = ( $row_number > 0 )
/* translators: 1: ... */
? sprintf( __( 'All %s unavailable dates has been removed', 'booking' ), '' . $row_number . '' )
: sprintf( __( 'No unavailable dates.', 'booking' ), $row_number );
$data_arr['ajx_after_action_result'] = ( $row_number > 0 ) ? 1 : 0;
}
// FixIn: 10.8.1.5.
if ( 'change_month_number_in_row' == $request_params['do_action'] ) {
// $request_params['calendar__view__months_in_row']; // Saving this parameter
/* translators: 1: ... */
$data_arr['ajx_after_action_message'] = sprintf( __( 'Set month number in a row as %s', 'booking' ), '' . $request_params['calendar__view__months_in_row'] . '' );
$data_arr['ajx_after_action_result'] = 1;
}
$data_arr['booked_dates'] = wpbc__sql__get_booked_dates( array(
'resource_id' => $request_params['resource_id']
) );
$data_arr['season_availability'] = wpbc__sql__get_season_availability( array(
'resource_id' => $request_params['resource_id']
) );
$data_arr['resource_unavailable_dates'] = wpbc_resource__get_unavailable_dates($request_params['resource_id']);
//----------------------------------------------------------------------------------------------------------
$data_arr['ajx_booking_resources'] = $resources_arr_sorted;
//----------------------------------------------------------------------------------------------------------
$data_arr['ajx_nonce_calendar'] = wp_nonce_field( 'CALCULATE_THE_COST', 'wpbc_nonce' . 'CALCULATE_THE_COST' . $request_params['resource_id'], true, false );
$data_arr['popover_hints'] = array();
$data_arr['popover_hints']['season_unavailable'] = '' . esc_html__( 'Season unavailable day', 'booking' ) . '
'
/* translators: 1: ... */
. sprintf( __( 'Change this date status at %1$sBooking Calendar %2$s Availability %3$s Season Availability page.', 'booking' ), '
', '>', '>' );
$data_arr['popover_hints']['weekdays_unavailable'] = '' . esc_html__( 'Unavailable week day', 'booking' ) . '
'
/* translators: 1: ... */
. sprintf( __( 'Change this date status at %1$sBooking Calendar %2$s Settings General page %3$s in "Availability" section.', 'booking' ), '
', '>', '
' );
$data_arr['popover_hints']['before_after_unavailable'] = '' . esc_html__( 'Unavailable day, depends on today day', 'booking' ) . '
'
/* translators: 1: ... */
. sprintf( __( 'Change this date status at %1$sBooking Calendar %2$s Settings General page %3$s in "Availability" section.', 'booking' ), '
', '>', '
' );
$data_arr['popover_hints']['toolbar_text'] = ''.
/* translators: 1: ... */
sprintf( __( '%1$sSelect days%2$s in calendar then select %3$sAvailable%4$s / %5$sUnavailable%6$s status and click %7$sApply%8$s availability button.', 'booking' )
, '', ' '
, ' ', ' '
, ' ', ' '
, ' ', ' '
)
.'';
/* translators: 1: ... */
$data_arr['popover_hints']['toolbar_text_available'] = sprintf( __( 'Set dates %1$s as %2$s available.', 'booking' )
, '_DATES_'
, '_HTML_'
);
/* translators: 1: ... */
$data_arr['popover_hints']['toolbar_text_unavailable'] = sprintf( __( 'Set dates %1$s as %2$s unavailable.', 'booking' )
, '_DATES_'
, '_HTML_'
);
// Clear here DATES selection in $request_params['dates_selection'] to not save such selection
if ( 'make_reset' === $request_params['do_action'] ) {
$is_reseted = $user_request->user_request_params__db_delete(); // Delete from DB
$request_params['do_action'] = $is_reseted ? 'reset_done' : 'reset_error';
} else {
$request_params_to_save = $request_params;
// Do not safe such elements
unset( $request_params_to_save['ui_clicked_element_id'] );
unset( $request_params_to_save['do_action'] );
unset( $request_params_to_save['dates_selection'] );
// Do not save "Do not change background color for partially booked days" option ! it must reflect from Booking > Settings General page and not from User options
unset( $request_params_to_save['calendar__timeslot_day_bg_as_available'] ); // FixIn: 9.5.5.4.
$is_success_update = $user_request->user_request_params__db_save( $request_params_to_save ); // Save to DB // - $request_params - serialized here automatically
}
//----------------------------------------------------------------------------------------------------------
// Send JSON. Its will make "wp_json_encode" - so pass only array, and This function call wp_die( '', '', array( 'response' => null, ) ) Pass JS OBJ: response_data in "jQuery.post( " function on success.
wp_send_json( array(
'ajx_data' => $data_arr,
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
'ajx_search_params' => $_REQUEST[ $request_prefix ], // $_REQUEST[ 'search_params' ]
'ajx_cleaned_params' => $request_params
) );
}
//
}
/**
* Just for loading CSS and JavaScript files
*/
if ( true ) {
$ajx_availability_loading = new WPBC_AJX__Availability;
$ajx_availability_loading->init_load_css_js_tpl();
$ajx_availability_loading->define_ajax_hook();
}