activities_controller.php
1 year ago
auth_controller.php
1 year ago
booking_form_settings_controller.php
1 year ago
bookings_controller.php
1 year ago
calendars_controller.php
1 year ago
carts_controller.php
1 year ago
controller.php
1 year ago
customer_cabinet_controller.php
1 year ago
customers_controller.php
1 year ago
dashboard_controller.php
1 year ago
default_agent_controller.php
1 year ago
events_controller.php
1 year ago
form_fields_controller.php
1 year ago
integrations_controller.php
1 year 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
1 year ago
processes_controller.php
1 year ago
search_controller.php
1 year ago
services_controller.php
1 year ago
settings_controller.php
1 year ago
steps_controller.php
1 year ago
stripe_connect_controller.php
1 year 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
manage_booking_by_key_controller.php
215 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (c) 2023 LatePoint LLC. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; // Exit if accessed directly. |
| 8 | } |
| 9 | |
| 10 | |
| 11 | if ( ! class_exists( 'OsManageBookingByKeyController' ) ) : |
| 12 | |
| 13 | |
| 14 | class OsManageBookingByKeyController extends OsController { |
| 15 | private $booking; |
| 16 | private $key_for; |
| 17 | private $key = ''; |
| 18 | |
| 19 | private function set_booking_by_key(){ |
| 20 | if(empty($this->params['key'])) return; |
| 21 | |
| 22 | $key = sanitize_text_field( $this->params['key'] ); |
| 23 | $data = OsBookingHelper::get_booking_id_and_manage_ability_by_key($key); |
| 24 | if(empty($data)) return; |
| 25 | $booking = new OsBookingModel($data['booking_id']); |
| 26 | if($booking->id){ |
| 27 | $this->key = $key; |
| 28 | $this->booking = $booking; |
| 29 | $this->key_for = $data['for']; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | function __construct() { |
| 34 | parent::__construct(); |
| 35 | $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'manage_booking_by_key/'; |
| 36 | |
| 37 | $this->action_access['public'] = array_merge($this->action_access['public'], [ |
| 38 | 'show', |
| 39 | 'print_booking_info', |
| 40 | 'ical_download', |
| 41 | 'change_status', |
| 42 | 'request_cancellation', |
| 43 | 'process_reschedule_request', |
| 44 | 'request_reschedule_calendar',]); |
| 45 | |
| 46 | $this->set_booking_by_key(); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | |
| 51 | function show(){ |
| 52 | if(empty($this->booking->id)) return; |
| 53 | |
| 54 | |
| 55 | $this->vars['key'] = $this->key; |
| 56 | $this->vars['for'] = $this->key_for; |
| 57 | $this->vars['booking'] = $this->booking; |
| 58 | $this->vars['order'] = $this->booking->get_order(); |
| 59 | $this->vars['order_manage_key'] = OsMetaHelper::get_order_meta_by_key( 'key_to_manage_for_' . $this->key_for, $this->booking->get_order()->id ); |
| 60 | |
| 61 | $this->vars['timezone_name'] = $this->key_for == 'agent' ? OsTimeHelper::get_wp_timezone_name() : $this->booking->customer->get_selected_timezone_name(); |
| 62 | |
| 63 | if($this->get_return_format() == 'json'){ |
| 64 | $this->set_layout('none'); |
| 65 | $response_html = $this->format_render_return(__FUNCTION__); |
| 66 | $this->send_json(array('status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html)); |
| 67 | }else{ |
| 68 | $this->set_layout('clean'); |
| 69 | $content = $this->format_render_return(__FUNCTION__); |
| 70 | echo $content; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | function ical_download(){ |
| 76 | if(empty($this->booking->id)) return; |
| 77 | |
| 78 | header('Content-Type: text/calendar; charset=utf-8'); |
| 79 | header('Content-Disposition: attachment; filename=booking_'.$this->booking->id.'.ics'); |
| 80 | |
| 81 | echo OsBookingHelper::generate_ical_event_string($this->booking); |
| 82 | } |
| 83 | |
| 84 | function print_booking_info(){ |
| 85 | if(empty($this->booking->id)) return; |
| 86 | |
| 87 | $this->vars['booking'] = $this->booking; |
| 88 | $this->vars['order'] = $this->booking->get_order(); |
| 89 | $this->vars['customer'] = $this->booking->customer; |
| 90 | $this->set_layout('print'); |
| 91 | $content = $this->format_render_return(__FUNCTION__, [], [], true); |
| 92 | echo $content; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | function process_reschedule_request(){ |
| 98 | if(empty($this->booking->id) || empty($this->params['start_date']) || empty($this->params['start_time'])) return; |
| 99 | |
| 100 | $allowed = ($this->key_for == 'agent') ? true : OsCustomerHelper::can_reschedule_booking($this->booking); |
| 101 | if($allowed){ |
| 102 | $old_booking = clone $this->booking; |
| 103 | $this->booking->start_date = $this->params['start_date']; |
| 104 | $this->booking->start_time = $this->params['start_time']; |
| 105 | $this->booking->end_time = $this->booking->calculate_end_time(); |
| 106 | $this->booking->end_date = $this->booking->calculate_end_date(); |
| 107 | $this->booking->set_utc_datetimes(); |
| 108 | // check if booking time is still available |
| 109 | if (!OsBookingHelper::is_booking_request_available(\LatePoint\Misc\BookingRequest::create_from_booking_model($this->booking), ['exclude_booking_ids' => [$this->booking->id]])) { |
| 110 | $response_html = __('Unfortunately the selected time slot is not available anymore, please select another timeslot.', 'latepoint'); |
| 111 | $status = LATEPOINT_STATUS_ERROR; |
| 112 | }else{ |
| 113 | // customer rescheduled, perform actions |
| 114 | if($this->key_for == 'customer'){ |
| 115 | if(OsSettingsHelper::is_on('change_status_on_customer_reschedule')){ |
| 116 | $allowed_statuses = OsBookingHelper::get_statuses_list(); |
| 117 | if(isset($allowed_statuses[OsSettingsHelper::get_settings_value('status_to_set_after_customer_reschedule')])) $this->booking->status = OsSettingsHelper::get_settings_value('status_to_set_after_customer_reschedule'); |
| 118 | } |
| 119 | } |
| 120 | if($this->booking->save()){ |
| 121 | /** |
| 122 | * Booking is updated |
| 123 | * |
| 124 | * @since 4.9.0 |
| 125 | * @hook latepoint_booking_updated |
| 126 | * |
| 127 | * @param {OsBookingModel} $this->>booking Updated instance of booking model |
| 128 | * @param {OsBookingModel} $old_booking Instance of booking model before it was updated |
| 129 | */ |
| 130 | do_action('latepoint_booking_updated', $this->booking, $old_booking); |
| 131 | $this->vars['booking'] = $this->booking; |
| 132 | $this->vars['timezone_name'] = ($this->key_for == 'agent') ? OsTimeHelper::get_wp_timezone_name() : $this->booking->customer->get_selected_timezone_name(); |
| 133 | $status = LATEPOINT_STATUS_SUCCESS; |
| 134 | $this->set_layout('none'); |
| 135 | $response_html = $this->format_render_return(__FUNCTION__, [], [], true); |
| 136 | }else{ |
| 137 | OsDebugHelper::log('Error rescheduling appointment', 'booking_reschedule_error', $this->booking->get_error_messages()); |
| 138 | $response_html = __('Error! Please try again later', 'latepoint'); |
| 139 | $status = LATEPOINT_STATUS_ERROR; |
| 140 | } |
| 141 | } |
| 142 | }else{ |
| 143 | $status = LATEPOINT_STATUS_ERROR; |
| 144 | $response_html = __('Error! LKDFU343', 'latepoint'); |
| 145 | } |
| 146 | |
| 147 | if($this->get_return_format() == 'json'){ |
| 148 | $this->send_json(array('status' => $status, 'message' => $response_html)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | function request_reschedule_calendar(){ |
| 153 | if(empty($this->booking->id)) return; |
| 154 | |
| 155 | $allowed = ($this->key_for == 'agent') ? true : OsCustomerHelper::can_reschedule_booking($this->booking); |
| 156 | if($allowed){ |
| 157 | $this->vars['booking'] = $this->booking; |
| 158 | $this->vars['key'] = $this->key; |
| 159 | $this->vars['calendar_start_date'] = !empty($this->params['calendar_start_date']) ? new OsWpDateTime($this->params['calendar_start_date']) : new OsWpDateTime('today'); |
| 160 | $timezone_name = ($this->key_for == 'agent') ? OsTimeHelper::get_wp_timezone_name() : $this->booking->customer->get_selected_timezone_name(); |
| 161 | $this->vars['timeshift_minutes'] = OsTimeHelper::get_timezone_shift_in_minutes($timezone_name); |
| 162 | $this->vars['timezone_name'] = $timezone_name; |
| 163 | |
| 164 | $this->set_layout('none'); |
| 165 | $response_html = $this->format_render_return(__FUNCTION__, [], [], true); |
| 166 | }else{ |
| 167 | $status = LATEPOINT_STATUS_ERROR; |
| 168 | $response_html = __('Reschedule is not allowed', 'latepoint'); |
| 169 | } |
| 170 | if($this->get_return_format() == 'json'){ |
| 171 | $this->send_json(array('status' => $status, 'message' => $response_html)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | function request_cancellation(){ |
| 176 | if(empty($this->booking->id)) return; |
| 177 | |
| 178 | if(OsCustomerHelper::can_cancel_booking($this->booking)){ |
| 179 | if($this->booking->update_status(LATEPOINT_BOOKING_STATUS_CANCELLED)){ |
| 180 | $status = LATEPOINT_STATUS_SUCCESS; |
| 181 | $response_html = __('Appointment Status Updated', 'latepoint'); |
| 182 | }else{ |
| 183 | $status = LATEPOINT_STATUS_ERROR; |
| 184 | $response_html = __('Error Updating Booking Status!', 'latepoint').' '.implode(',', $this->booking->get_error_messages()); |
| 185 | } |
| 186 | }else{ |
| 187 | $status = LATEPOINT_STATUS_ERROR; |
| 188 | $response_html = __('Not allowed to cancel', 'latepoint'); |
| 189 | } |
| 190 | if($this->get_return_format() == 'json'){ |
| 191 | $this->send_json(array('status' => $status, 'message' => $response_html)); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | function change_status(){ |
| 196 | // only agent key can cancel |
| 197 | if($this->key_for != 'agent' || empty($this->booking->id) || empty($this->params['status'])) return; |
| 198 | $statuses = OsBookingHelper::get_statuses_list(); |
| 199 | if(!isset($statuses[$this->params['status']])) return; |
| 200 | |
| 201 | |
| 202 | if($this->booking->update_status($this->params['status'])){ |
| 203 | $status = LATEPOINT_STATUS_SUCCESS; |
| 204 | $response_html = __('Appointment Status Updated', 'latepoint'); |
| 205 | }else{ |
| 206 | $status = LATEPOINT_STATUS_ERROR; |
| 207 | $response_html = __('Error Updating Booking Status!', 'latepoint').' '.implode(',', $this->booking->get_error_messages()); |
| 208 | } |
| 209 | |
| 210 | if($this->get_return_format() == 'json'){ |
| 211 | $this->send_json(array('status' => $status, 'message' => $response_html)); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | endif; |