"use strict";
/**
* =====================================================================================================================
* includes/__js/wpbc/wpbc.js
* =====================================================================================================================
*/
/**
* Deep Clone of object or array
*
* @param obj
* @returns {any}
*/
function wpbc_clone_obj( obj ){
return JSON.parse( JSON.stringify( obj ) );
}
/**
* Main _wpbc JS object
*/
var _wpbc = (function ( obj, $) {
// Secure parameters for Ajax ------------------------------------------------------------------------------------
var p_secure = obj.security_obj = obj.security_obj || {
user_id: 0,
nonce : '',
locale : ''
};
obj.set_secure_param = function ( param_key, param_val ) {
p_secure[ param_key ] = param_val;
};
obj.get_secure_param = function ( param_key ) {
return p_secure[ param_key ];
};
// Calendars ----------------------------------------------------------------------------------------------------
var p_calendars = obj.calendars_obj = obj.calendars_obj || {
// sort : "booking_id",
// sort_type : "DESC",
// page_num : 1,
// page_items_count: 10,
// create_date : "",
// keyword : "",
// source : ""
};
/**
* Check if calendar for specific booking resource defined :: true | false
*
* @param {string|int} resource_id
* @returns {boolean}
*/
obj.calendar__is_defined = function ( resource_id ) {
return ('undefined' !== typeof( p_calendars[ 'calendar_' + resource_id ] ) );
};
/**
* Create Calendar initializing
*
* @param {string|int} resource_id
*/
obj.calendar__init = function ( resource_id ) {
p_calendars[ 'calendar_' + resource_id ] = {};
p_calendars[ 'calendar_' + resource_id ][ 'id' ] = resource_id;
p_calendars[ 'calendar_' + resource_id ][ 'pending_days_selectable' ] = false;
};
/**
* Check if the type of this property is INT
* @param property_name
* @returns {boolean}
*/
obj.calendar__is_prop_int = function ( property_name ) { // FixIn: 9.9.0.29.
var p_calendar_int_properties = ['dynamic__days_min', 'dynamic__days_max', 'fixed__days_num'];
var is_include = p_calendar_int_properties.includes( property_name );
return is_include;
};
/**
* Set params for all calendars
*
* @param {object} calendars_obj Object { calendar_1: {} }
* calendar_3: {}, ... }
*/
obj.calendars_all__set = function ( calendars_obj ) {
p_calendars = calendars_obj;
};
/**
* Get bookings in all calendars
*
* @returns {object|{}}
*/
obj.calendars_all__get = function () {
return p_calendars;
};
/**
* Get calendar object :: { id: 1, … }
*
* @param {string|int} resource_id '2'
* @returns {object|boolean} { id: 2 ,… }
*/
obj.calendar__get_parameters = function ( resource_id ) {
if ( obj.calendar__is_defined( resource_id ) ){
return p_calendars[ 'calendar_' + resource_id ];
} else {
return false;
}
};
/**
* Set calendar object :: { dates: Object { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, … }
*
* if calendar object not defined, then it's will be defined and ID set
* if calendar exist, then system set as new or overwrite only properties from calendar_property_obj parameter, but other properties will be existed and not overwrite, like 'id'
*
* @param {string|int} resource_id '2'
* @param {object} calendar_property_obj { dates: Object { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, … } }
* @param {boolean} is_complete_overwrite if 'true' (default: 'false'), then only overwrite or add new properties in calendar_property_obj
* @returns {*}
*
* Examples:
*
* Common usage in PHP:
* echo " _wpbc.calendar__set( " .intval( $resource_id ) . ", { 'dates': " . wp_json_encode( $availability_per_days_arr ) . " } );";
*/
obj.calendar__set_parameters = function ( resource_id, calendar_property_obj, is_complete_overwrite = false ) {
if ( (!obj.calendar__is_defined( resource_id )) || (true === is_complete_overwrite) ){
obj.calendar__init( resource_id );
}
for ( var prop_name in calendar_property_obj ){
p_calendars[ 'calendar_' + resource_id ][ prop_name ] = calendar_property_obj[ prop_name ];
}
return p_calendars[ 'calendar_' + resource_id ];
};
/**
* Set property to calendar
* @param resource_id "1"
* @param prop_name name of property
* @param prop_value value of property
* @returns {*} calendar object
*/
obj.calendar__set_param_value = function ( resource_id, prop_name, prop_value ) {
if ( (!obj.calendar__is_defined( resource_id )) ){
obj.calendar__init( resource_id );
}
p_calendars[ 'calendar_' + resource_id ][ prop_name ] = prop_value;
return p_calendars[ 'calendar_' + resource_id ];
};
/**
* Get calendar property value :: mixed | null
*
* @param {string|int} resource_id '1'
* @param {string} prop_name 'selection_mode'
* @returns {*|null} mixed | null
*/
obj.calendar__get_param_value = function( resource_id, prop_name ){
if (
( obj.calendar__is_defined( resource_id ) )
&& ( 'undefined' !== typeof ( p_calendars[ 'calendar_' + resource_id ][ prop_name ] ) )
){
// FixIn: 9.9.0.29.
if ( obj.calendar__is_prop_int( prop_name ) ){
p_calendars[ 'calendar_' + resource_id ][ prop_name ] = parseInt( p_calendars[ 'calendar_' + resource_id ][ prop_name ] );
}
return p_calendars[ 'calendar_' + resource_id ][ prop_name ];
}
return null; // If some property not defined, then null;
};
// -----------------------------------------------------------------------------------------------------------------
// Bookings ----------------------------------------------------------------------------------------------------
var p_bookings = obj.bookings_obj = obj.bookings_obj || {
// calendar_1: Object {
// id: 1
// , dates: Object { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, …
// }
};
/**
* Check if bookings for specific booking resource defined :: true | false
*
* @param {string|int} resource_id
* @returns {boolean}
*/
obj.bookings_in_calendar__is_defined = function ( resource_id ) {
return ('undefined' !== typeof( p_bookings[ 'calendar_' + resource_id ] ) );
};
/**
* Get bookings calendar object :: { id: 1 , dates: Object { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, … }
*
* @param {string|int} resource_id '2'
* @returns {object|boolean} { id: 2 , dates: Object { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, … }
*/
obj.bookings_in_calendar__get = function( resource_id ){
if ( obj.bookings_in_calendar__is_defined( resource_id ) ){
return p_bookings[ 'calendar_' + resource_id ];
} else {
return false;
}
};
/**
* Set bookings calendar object :: { dates: Object { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, … }
*
* if calendar object not defined, then it's will be defined and ID set
* if calendar exist, then system set as new or overwrite only properties from calendar_obj parameter, but other properties will be existed and not overwrite, like 'id'
*
* @param {string|int} resource_id '2'
* @param {object} calendar_obj { dates: Object { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, … } }
* @returns {*}
*
* Examples:
*
* Common usage in PHP:
* echo " _wpbc.bookings_in_calendar__set( " .intval( $resource_id ) . ", { 'dates': " . wp_json_encode( $availability_per_days_arr ) . " } );";
*/
obj.bookings_in_calendar__set = function( resource_id, calendar_obj ){
if ( ! obj.bookings_in_calendar__is_defined( resource_id ) ){
p_bookings[ 'calendar_' + resource_id ] = {};
p_bookings[ 'calendar_' + resource_id ][ 'id' ] = resource_id;
}
for ( var prop_name in calendar_obj ){
p_bookings[ 'calendar_' + resource_id ][ prop_name ] = calendar_obj[ prop_name ];
}
return p_bookings[ 'calendar_' + resource_id ];
};
// Dates
/**
* Get bookings data for ALL Dates in calendar :: false | { "2023-07-22": {…}, "2023-07-23": {…}, … }
*
* @param {string|int} resource_id '1'
* @returns {object|boolean} false | Object {
"2023-07-24": Object { ['summary']['status_for_day']: "available", day_availability: 1, max_capacity: 1, … }
"2023-07-26": Object { ['summary']['status_for_day']: "full_day_booking", ['summary']['status_for_bookings']: "pending", day_availability: 0, … }
"2023-07-29": Object { ['summary']['status_for_day']: "resource_availability", day_availability: 0, max_capacity: 1, … }
"2023-07-30": {…}, "2023-07-31": {…}, …
}
*/
obj.bookings_in_calendar__get_dates = function( resource_id){
if (
( obj.bookings_in_calendar__is_defined( resource_id ) )
&& ( 'undefined' !== typeof ( p_bookings[ 'calendar_' + resource_id ][ 'dates' ] ) )
){
return p_bookings[ 'calendar_' + resource_id ][ 'dates' ];
}
return false; // If some property not defined, then false;
};
/**
* Set bookings dates in calendar object :: { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, … }
*
* if calendar object not defined, then it's will be defined and 'id', 'dates' set
* if calendar exist, then system add a new or overwrite only dates from dates_obj parameter,
* but other dates not from parameter dates_obj will be existed and not overwrite.
*
* @param {string|int} resource_id '2'
* @param {object} dates_obj { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, … }
* @param {boolean} is_complete_overwrite if false, then only overwrite or add dates from dates_obj
* @returns {*}
*
* Examples:
* _wpbc.bookings_in_calendar__set_dates( resource_id, { "2023-07-21": {…}, "2023-07-22": {…}, … } ); <- overwrite ALL dates
* _wpbc.bookings_in_calendar__set_dates( resource_id, { "2023-07-22": {…} }, false ); <- add or overwrite only "2023-07-22": {}
*
* Common usage in PHP:
* echo " _wpbc.bookings_in_calendar__set_dates( " . intval( $resource_id ) . ", " . wp_json_encode( $availability_per_days_arr ) . " ); ";
*/
obj.bookings_in_calendar__set_dates = function( resource_id, dates_obj , is_complete_overwrite = true ){
if ( !obj.bookings_in_calendar__is_defined( resource_id ) ){
obj.bookings_in_calendar__set( resource_id, { 'dates': {} } );
}
if ( 'undefined' === typeof (p_bookings[ 'calendar_' + resource_id ][ 'dates' ]) ){
p_bookings[ 'calendar_' + resource_id ][ 'dates' ] = {}
}
if (is_complete_overwrite){
// Complete overwrite all booking dates
p_bookings[ 'calendar_' + resource_id ][ 'dates' ] = dates_obj;
} else {
// Add only new or overwrite exist booking dates from parameter. Booking dates not from parameter will be without chnanges
for ( var prop_name in dates_obj ){
p_bookings[ 'calendar_' + resource_id ]['dates'][ prop_name ] = dates_obj[ prop_name ];
}
}
return p_bookings[ 'calendar_' + resource_id ];
};
/**
* Get bookings data for specific date in calendar :: false | { day_availability: 1, ... }
*
* @param {string|int} resource_id '1'
* @param {string} sql_class_day '2023-07-21'
* @returns {object|boolean} false | {
day_availability: 4
max_capacity: 4 // >= Business Large
2: Object { is_day_unavailable: false, _day_status: "available" }
10: Object { is_day_unavailable: false, _day_status: "available" } // >= Business Large ...
11: Object { is_day_unavailable: false, _day_status: "available" }
12: Object { is_day_unavailable: false, _day_status: "available" }
}
*/
obj.bookings_in_calendar__get_for_date = function( resource_id, sql_class_day ){
if (
( obj.bookings_in_calendar__is_defined( resource_id ) )
&& ( 'undefined' !== typeof ( p_bookings[ 'calendar_' + resource_id ][ 'dates' ] ) )
&& ( 'undefined' !== typeof ( p_bookings[ 'calendar_' + resource_id ][ 'dates' ][ sql_class_day ] ) )
){
return p_bookings[ 'calendar_' + resource_id ][ 'dates' ][ sql_class_day ];
}
return false; // If some property not defined, then false;
};
// Any PARAMS in bookings
/**
* Set property to booking
* @param resource_id "1"
* @param prop_name name of property
* @param prop_value value of property
* @returns {*} booking object
*/
obj.booking__set_param_value = function ( resource_id, prop_name, prop_value ) {
if ( ! obj.bookings_in_calendar__is_defined( resource_id ) ){
p_bookings[ 'calendar_' + resource_id ] = {};
p_bookings[ 'calendar_' + resource_id ][ 'id' ] = resource_id;
}
p_bookings[ 'calendar_' + resource_id ][ prop_name ] = prop_value;
return p_bookings[ 'calendar_' + resource_id ];
};
/**
* Get booking property value :: mixed | null
*
* @param {string|int} resource_id '1'
* @param {string} prop_name 'selection_mode'
* @returns {*|null} mixed | null
*/
obj.booking__get_param_value = function( resource_id, prop_name ){
if (
( obj.bookings_in_calendar__is_defined( resource_id ) )
&& ( 'undefined' !== typeof ( p_bookings[ 'calendar_' + resource_id ][ prop_name ] ) )
){
return p_bookings[ 'calendar_' + resource_id ][ prop_name ];
}
return null; // If some property not defined, then null;
};
/**
* Set bookings for all calendars
*
* @param {object} calendars_obj Object { calendar_1: { id: 1, dates: Object { "2023-07-22": {…}, "2023-07-23": {…}, "2023-07-24": {…}, … } }
* calendar_3: {}, ... }
*/
obj.bookings_in_calendars__set_all = function ( calendars_obj ) {
p_bookings = calendars_obj;
};
/**
* Get bookings in all calendars
*
* @returns {object|{}}
*/
obj.bookings_in_calendars__get_all = function () {
return p_bookings;
};
// -----------------------------------------------------------------------------------------------------------------
// Seasons ----------------------------------------------------------------------------------------------------
var p_seasons = obj.seasons_obj = obj.seasons_obj || {
// calendar_1: Object {
// id: 1
// , dates: Object { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, …
// }
};
/**
* Add season names for dates in calendar object :: { "2023-07-21": [ 'wpbc_season_september_2023', 'wpbc_season_september_2024' ], "2023-07-22": [...], ... }
*
*
* @param {string|int} resource_id '2'
* @param {object} dates_obj { "2023-07-21": {…}, "2023-07-22": {…}, "2023-07-23": {…}, … }
* @param {boolean} is_complete_overwrite if false, then only add dates from dates_obj
* @returns {*}
*
* Examples:
* _wpbc.seasons__set( resource_id, { "2023-07-21": [ 'wpbc_season_september_2023', 'wpbc_season_september_2024' ], "2023-07-22": [...], ... } );
*/
obj.seasons__set = function( resource_id, dates_obj , is_complete_overwrite = false ){
if ( 'undefined' === typeof (p_seasons[ 'calendar_' + resource_id ]) ){
p_seasons[ 'calendar_' + resource_id ] = {};
}
if ( is_complete_overwrite ){
// Complete overwrite all season dates
p_seasons[ 'calendar_' + resource_id ] = dates_obj;
} else {
// Add only new or overwrite exist booking dates from parameter. Booking dates not from parameter will be without chnanges
for ( var prop_name in dates_obj ){
if ( 'undefined' === typeof (p_seasons[ 'calendar_' + resource_id ][ prop_name ]) ){
p_seasons[ 'calendar_' + resource_id ][ prop_name ] = [];
}
for ( var season_name_key in dates_obj[ prop_name ] ){
p_seasons[ 'calendar_' + resource_id ][ prop_name ].push( dates_obj[ prop_name ][ season_name_key ] );
}
}
}
return p_seasons[ 'calendar_' + resource_id ];
};
/**
* Get bookings data for specific date in calendar :: [] | [ 'wpbc_season_september_2023', 'wpbc_season_september_2024' ]
*
* @param {string|int} resource_id '1'
* @param {string} sql_class_day '2023-07-21'
* @returns {object|boolean} [] | [ 'wpbc_season_september_2023', 'wpbc_season_september_2024' ]
*/
obj.seasons__get_for_date = function( resource_id, sql_class_day ){
if (
( 'undefined' !== typeof ( p_seasons[ 'calendar_' + resource_id ] ) )
&& ( 'undefined' !== typeof ( p_seasons[ 'calendar_' + resource_id ][ sql_class_day ] ) )
){
return p_seasons[ 'calendar_' + resource_id ][ sql_class_day ];
}
return []; // If not defined, then [];
};
// Other parameters ------------------------------------------------------------------------------------
var p_other = obj.other_obj = obj.other_obj || { };
obj.set_other_param = function ( param_key, param_val ) {
p_other[ param_key ] = param_val;
};
obj.get_other_param = function ( param_key ) {
return p_other[ param_key ];
};
/**
* Get all other params
*
* @returns {object|{}}
*/
obj.get_other_param__all = function () {
return p_other;
};
// Messages ------------------------------------------------------------------------------------
var p_messages = obj.messages_obj = obj.messages_obj || { };
obj.set_message = function ( param_key, param_val ) {
p_messages[ param_key ] = param_val;
};
obj.get_message = function ( param_key ) {
return p_messages[ param_key ];
};
/**
* Get all other params
*
* @returns {object|{}}
*/
obj.get_messages__all = function () {
return p_messages;
};
// -----------------------------------------------------------------------------------------------------------------
return obj;
}( _wpbc || {}, jQuery ));