activities_controller.php
11 months ago
auth_controller.php
9 months ago
booking_form_settings_controller.php
1 year ago
bookings_controller.php
1 year ago
calendars_controller.php
9 months ago
carts_controller.php
1 year ago
controller.php
9 months ago
customer_cabinet_controller.php
9 months ago
customers_controller.php
9 months ago
dashboard_controller.php
9 months ago
default_agent_controller.php
1 year ago
events_controller.php
1 year ago
form_fields_controller.php
9 months ago
integrations_controller.php
9 months ago
invoices_controller.php
1 year ago
manage_booking_by_key_controller.php
1 year ago
manage_order_by_key_controller.php
1 year ago
notifications_controller.php
1 year ago
orders_controller.php
1 year ago
pro_controller.php
1 year ago
process_jobs_controller.php
9 months ago
processes_controller.php
1 year ago
search_controller.php
1 year ago
services_controller.php
9 months ago
settings_controller.php
1 year ago
steps_controller.php
9 months ago
stripe_connect_controller.php
9 months ago
support_topics_controller.php
1 year ago
todos_controller.php
1 year ago
transactions_controller.php
1 year ago
wizard_controller.php
1 year ago
steps_controller.php
351 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | |
| 6 | |
| 7 | if ( ! class_exists( 'OsStepsController' ) ) : |
| 8 | |
| 9 | |
| 10 | class OsStepsController extends OsController { |
| 11 | |
| 12 | private $booking; |
| 13 | |
| 14 | function __construct() { |
| 15 | parent::__construct(); |
| 16 | $this->action_access['customer'] = array_merge( $this->action_access['customer'], [ 'start_from_order_intent' ] ); |
| 17 | $this->action_access['public'] = array_merge( $this->action_access['public'], [ |
| 18 | 'start', |
| 19 | 'start_instant', |
| 20 | 'load_step', |
| 21 | 'reload_booking_form_summary_panel', |
| 22 | 'check_order_intent_bookable', |
| 23 | 'load_datepicker_month' |
| 24 | ] ); |
| 25 | |
| 26 | $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'steps/'; |
| 27 | $this->vars['page_header'] = __( 'Appointments', 'latepoint' ); |
| 28 | $this->vars['breadcrumbs'][] = array( |
| 29 | 'label' => __( 'Appointments', 'latepoint' ), |
| 30 | 'link' => OsRouterHelper::build_link( [ |
| 31 | 'bookings', |
| 32 | 'pending_approval' |
| 33 | ] ) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | public function start_instant() { |
| 39 | $atts = []; |
| 40 | if(!empty($this->params['selected_agent'])) $atts['selected_agent'] = sanitize_text_field($this->params['selected_agent']); |
| 41 | if(!empty($this->params['selected_service'])) $atts['selected_service'] = sanitize_text_field($this->params['selected_service']); |
| 42 | if(!empty($this->params['selected_location'])) $atts['selected_location'] = sanitize_text_field($this->params['selected_location']); |
| 43 | |
| 44 | if(!empty($this->params['hide_side_panel']) && $this->params['hide_side_panel'] == 'yes') $atts['hide_side_panel'] = 'yes'; |
| 45 | if(!empty($this->params['hide_summary']) && $this->params['hide_summary'] == 'yes') $atts['hide_summary'] = 'yes'; |
| 46 | if(!empty($this->params['background_pattern'])) $this->vars['background_pattern'] = sanitize_text_field($this->params['background_pattern']); |
| 47 | |
| 48 | $this->vars['atts'] = $atts; |
| 49 | $this->set_layout( 'clean' ); |
| 50 | $this->format_render( __FUNCTION__ ); |
| 51 | } |
| 52 | |
| 53 | public function load_datepicker_month() { |
| 54 | OsStepsHelper::set_required_objects( $this->params ); |
| 55 | |
| 56 | $target_date = new OsWpDateTime( $this->params['target_date_string'] ); |
| 57 | $calendar_settings = [ |
| 58 | 'layout' => $this->params['calendar_layout'] ?? 'classic', |
| 59 | 'timezone_name' => $this->params['timezone_name'] ?? false, |
| 60 | ]; |
| 61 | |
| 62 | $calendar_settings['earliest_possible_booking'] = OsSettingsHelper::get_earliest_possible_booking_restriction( OsStepsHelper::$booking_object->service_id ?? false ); |
| 63 | $calendar_settings['latest_possible_booking'] = OsSettingsHelper::get_latest_possible_booking_restriction( OsStepsHelper::$booking_object->service_id ?? false ); |
| 64 | |
| 65 | $this->format_render( 'partials/_monthly_calendar_days', [ |
| 66 | 'target_date' => $target_date, |
| 67 | 'calendar_settings' => $calendar_settings, |
| 68 | 'booking_request' => \LatePoint\Misc\BookingRequest::create_from_booking_model( OsStepsHelper::$booking_object ) |
| 69 | ] ); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | public function check_order_intent_bookable() { |
| 74 | OsStepsHelper::set_required_objects( $this->params ); |
| 75 | if(OsStepsHelper::$cart_object->order_id){ |
| 76 | // already converted, so we are good |
| 77 | $this->send_json( [ |
| 78 | 'status' => LATEPOINT_STATUS_SUCCESS, |
| 79 | 'message' => __( 'Cart has already been converted to order', 'latepoint' ) |
| 80 | ] ); |
| 81 | } |
| 82 | $order_intent = OsOrderIntentHelper::create_or_update_order_intent( OsStepsHelper::$cart_object, OsStepsHelper::$restrictions, OsStepsHelper::$presets, '', OsStepsHelper::get_customer_object_id() ); |
| 83 | if ( $order_intent->is_bookable() ) { |
| 84 | $this->send_json( [ |
| 85 | 'status' => LATEPOINT_STATUS_SUCCESS, |
| 86 | 'message' => __( 'Order intent can be converted to order', 'latepoint' ) |
| 87 | ] ); |
| 88 | } else { |
| 89 | $this->send_json( [ |
| 90 | 'status' => LATEPOINT_STATUS_ERROR, |
| 91 | 'message' => __( 'Selected booking slot is not available anymore. Please pick a different time slot.', 'latepoint' ) |
| 92 | ] ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | function generate_timeslots_for_day(){ |
| 97 | OsStepsHelper::set_required_objects( $this->params ); |
| 98 | |
| 99 | |
| 100 | |
| 101 | wp_send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => '' ] ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | function reload_booking_form_summary_panel() { |
| 106 | OsStepsHelper::set_required_objects( $this->params ); |
| 107 | $this->vars['cart'] = OsStepsHelper::$cart_object; |
| 108 | |
| 109 | if ( OsStepsHelper::is_ready_for_summary() ) { |
| 110 | $this->vars['current_step_code'] = $this->params['current_step_code'] ?? ''; |
| 111 | $this->vars['booking'] = OsStepsHelper::$booking_object; |
| 112 | $this->vars['customer'] = OsStepsHelper::get_customer_object(); |
| 113 | $this->vars['active_cart_item'] = OsStepsHelper::$active_cart_item; |
| 114 | |
| 115 | if ( $this->get_return_format() == 'json' ) { |
| 116 | $response_html = $this->render( $this->views_folder . 'partials/_booking_form_summary_panel', 'none' ); |
| 117 | wp_send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ] ); |
| 118 | exit(); |
| 119 | } else { |
| 120 | echo $this->render( $this->views_folder . 'partials/_booking_form_summary_panel', $this->get_layout() ); |
| 121 | } |
| 122 | } else { |
| 123 | wp_send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => '' ] ); |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
| 128 | |
| 129 | public function start_from_order_intent() { |
| 130 | $order_intent = OsOrderIntentHelper::get_order_intent_by_intent_key($this->params['order_intent_key']); |
| 131 | |
| 132 | if ( !$order_intent->is_new_record() ) { |
| 133 | $step_codes_to_preload = []; |
| 134 | $steps = OsStepsHelper::get_steps(); |
| 135 | OsStepsHelper::set_required_objects($this->params); |
| 136 | |
| 137 | if ( $order_intent->order_id ) { |
| 138 | // if order is created - load it |
| 139 | OsStepsHelper::load_order_object( $order_intent->order_id ); |
| 140 | $current_step_code = 'confirmation'; |
| 141 | $current_step = $steps[ $current_step_code ]; |
| 142 | |
| 143 | $this->vars['price_breakdown_rows'] = OsStepsHelper::$order_object->generate_price_breakdown_rows(); |
| 144 | |
| 145 | $steps = [ 'confirmation' => $steps['confirmation'] ]; |
| 146 | |
| 147 | |
| 148 | } else { |
| 149 | |
| 150 | OsStepsHelper::set_cart_object_from_order_intent( $order_intent ); |
| 151 | $current_step_code = 'verify'; |
| 152 | $current_step = $steps[ $current_step_code ]; |
| 153 | |
| 154 | |
| 155 | foreach ( OsStepsHelper::$step_codes_in_order as $step_code ) { |
| 156 | if ( $step_code == $current_step_code ) { |
| 157 | break; |
| 158 | } else { |
| 159 | $step_codes_to_preload[] = $step_code; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $this->vars['price_breakdown_rows'] = OsStepsHelper::$cart_object->generate_price_breakdown_rows(); |
| 164 | // order exists - only load confirmation step |
| 165 | $this->vars['step_codes_to_preload'] = $step_codes_to_preload; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | $this->vars['cart'] = OsStepsHelper::$cart_object; |
| 170 | $this->vars['show_next_btn'] = OsStepsHelper::can_step_show_next_btn( $current_step_code ); |
| 171 | $this->vars['show_prev_btn'] = OsStepsHelper::can_step_show_prev_btn( $current_step_code ); |
| 172 | $this->vars['all_steps'] = OsStepsHelper::get_steps( true ); |
| 173 | $this->vars['steps'] = $steps; |
| 174 | $this->vars['current_step'] = $current_step; |
| 175 | |
| 176 | $this->vars['current_step_code'] = $current_step_code; |
| 177 | $this->vars['booking'] = OsStepsHelper::$booking_object; |
| 178 | $this->vars['customer'] = OsStepsHelper::get_customer_object(); |
| 179 | $this->vars['active_cart_item'] = OsStepsHelper::$active_cart_item; |
| 180 | $this->vars['restrictions'] = OsStepsHelper::$restrictions; |
| 181 | $this->vars['presets'] = OsStepsHelper::$presets; |
| 182 | $this->vars['booking_element_type'] = 'lightbox'; |
| 183 | $this->vars['booking_element_styles'] = []; |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | $this->set_layout( 'none' ); |
| 190 | |
| 191 | $this->format_render( 'start', array(), array( 'lightbox_class' => '' ) ); |
| 192 | } else { |
| 193 | $this->send_json( array( |
| 194 | 'status' => LATEPOINT_STATUS_ERROR, |
| 195 | 'message' => __( 'Invalid order intent key', 'latepoint' ) |
| 196 | ) ); |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |
| 201 | public function start( array $custom_restrictions = [], array $custom_presets = [], array $booking_element_styles = [], bool $output = true, string $booking_element_type = 'lightbox' ) { |
| 202 | $merged_params = $this->params; |
| 203 | |
| 204 | if ( ! empty( $custom_restrictions ) ) { |
| 205 | $merged_params['restrictions'] = $custom_restrictions; |
| 206 | } |
| 207 | if ( ! empty( $custom_presets ) ) { |
| 208 | $merged_params['presets'] = $custom_presets; |
| 209 | } |
| 210 | if( ! empty( $this->params['booking_element_type'] ) ) { |
| 211 | $booking_element_type = $this->params['booking_element_type']; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | if(!empty($merged_params['booking_element_styles'])){ |
| 216 | $booking_element_styles = array_merge( $booking_element_styles, $merged_params['booking_element_styles'] ); |
| 217 | } |
| 218 | |
| 219 | // set early to check if it's converted or should be emptied |
| 220 | OsStepsHelper::set_cart_object(); |
| 221 | if(!empty(OsStepsHelper::$cart_object->order_id)){ |
| 222 | OsCartsHelper::reset_cart(); |
| 223 | OsStepsHelper::set_cart_object(); |
| 224 | } |
| 225 | // clear cart if "shopping cart" feature is not enabled |
| 226 | if ( ! OsCartsHelper::can_checkout_multiple_items() ) OsStepsHelper::$cart_object->clear(); |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | OsStepsHelper::set_required_objects( $merged_params ); |
| 232 | |
| 233 | $steps = OsStepsHelper::get_steps(); |
| 234 | |
| 235 | $current_step_code = OsStepsHelper::get_step_codes_in_order()[0]; |
| 236 | |
| 237 | if ( OsStepsHelper::should_step_be_skipped( $current_step_code ) ) { |
| 238 | $current_step_code = OsStepsHelper::get_next_step_code( $current_step_code ); |
| 239 | } |
| 240 | // check if all booking steps have to be skipped, if so - it means the booking object is ready and we can add it to the cart |
| 241 | $ready_to_add_to_cart = true; |
| 242 | if(OsStepsHelper::$active_cart_item->is_booking()){ |
| 243 | foreach($steps as $step_code => $step_object) { |
| 244 | $step_main_parent_code = explode('__', $step_code); |
| 245 | if(!empty($step_main_parent_code[0]) && $step_main_parent_code[0] == 'booking'){ |
| 246 | $ready_to_add_to_cart = false; |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | // looks like item is ready to be added to cart (because all necessary steps/presets where applied in a trigger element), add it to cart |
| 252 | if($ready_to_add_to_cart){ |
| 253 | try{ |
| 254 | OsStepsHelper::add_current_item_to_cart(); |
| 255 | }catch(Exception $e){ |
| 256 | $this->vars['booking'] = OsStepsHelper::$booking_object; |
| 257 | if ( $output ) { |
| 258 | $this->format_render( 'preset_slot_not_available' ); |
| 259 | return false; |
| 260 | } else { |
| 261 | return $this->format_render_return( 'preset_slot_not_available' ); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | $current_step = $steps[ $current_step_code ]; |
| 266 | |
| 267 | $this->vars['cart'] = OsStepsHelper::$cart_object; |
| 268 | $this->vars['show_next_btn'] = OsStepsHelper::can_step_show_next_btn( $current_step->code ); |
| 269 | $this->vars['show_prev_btn'] = OsStepsHelper::can_step_show_prev_btn( $current_step->code ); |
| 270 | $this->vars['all_steps'] = OsStepsHelper::get_steps( true ); |
| 271 | $this->vars['steps'] = $steps; |
| 272 | $this->vars['current_step'] = $current_step; |
| 273 | |
| 274 | $this->vars['current_step_code'] = $current_step->code; |
| 275 | $this->vars['booking'] = OsStepsHelper::$booking_object; |
| 276 | $this->vars['customer'] = OsStepsHelper::get_customer_object(); |
| 277 | $this->vars['active_cart_item'] = OsStepsHelper::$active_cart_item; |
| 278 | $this->vars['restrictions'] = OsStepsHelper::$restrictions; |
| 279 | $this->vars['presets'] = OsStepsHelper::$presets; |
| 280 | $this->vars['booking_element_type'] = $booking_element_type; |
| 281 | $this->vars['booking_element_styles'] = $booking_element_styles; |
| 282 | $this->vars['timezone_name'] = OsTimeHelper::get_timezone_name_from_session(); |
| 283 | |
| 284 | $this->set_layout( 'none' ); |
| 285 | |
| 286 | |
| 287 | |
| 288 | if ( $output ) { |
| 289 | $this->format_render( __FUNCTION__, [], [ 'step' => $current_step->code ] ); |
| 290 | } else { |
| 291 | return $this->format_render_return( __FUNCTION__, [], [ 'step' => $current_step->code ] ); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | public function load_step() { |
| 297 | OsStepsHelper::set_required_objects( $this->params ); |
| 298 | |
| 299 | $current_step_code = OsStepsHelper::retrieve_step_code( $this->params['current_step_code'] ); |
| 300 | if ( empty( $current_step_code ) ) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | $step_direction = $this->params['step_direction'] ?? 'next'; |
| 305 | $step_code_to_load = false; |
| 306 | switch ( $step_direction ) { |
| 307 | case 'next': |
| 308 | /** |
| 309 | * Process step by code |
| 310 | * |
| 311 | * @param {string} $step_code step code that will be processed |
| 312 | * @param {OsBookingModel} $booking booking object |
| 313 | * @param {array} $params array of params |
| 314 | * |
| 315 | * @since 5.0.0 |
| 316 | * @hook latepoint_process_step |
| 317 | * |
| 318 | */ |
| 319 | do_action( 'latepoint_process_step', $current_step_code, OsStepsHelper::$booking_object, $this->params ); |
| 320 | $step_code_to_load = OsStepsHelper::get_next_step_code( $current_step_code ); |
| 321 | break; |
| 322 | case 'prev': |
| 323 | $step_code_to_load = OsStepsHelper::get_prev_step_code( $current_step_code ); |
| 324 | break; |
| 325 | case 'specific': |
| 326 | $step_code_to_load = OsStepsHelper::should_step_be_skipped( $current_step_code ) ? OsStepsHelper::get_next_step_code( $current_step_code ) : $current_step_code; |
| 327 | break; |
| 328 | } |
| 329 | if ( $step_code_to_load ) { |
| 330 | |
| 331 | /** |
| 332 | * Load step by code |
| 333 | * |
| 334 | * @param {string} $step_code step code to load |
| 335 | * @param {string} $type type of return (json) |
| 336 | * @param {array} $params array of params |
| 337 | * |
| 338 | * @since 5.0.0 |
| 339 | * @hook latepoint_load_step |
| 340 | * |
| 341 | */ |
| 342 | do_action( 'latepoint_load_step', $step_code_to_load, 'json', $this->params ); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | |
| 347 | } |
| 348 | |
| 349 | |
| 350 | endif; |
| 351 |