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
250 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'] ) ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | $key = sanitize_text_field( $this->params['key'] ); |
| 25 | $data = OsBookingHelper::get_booking_id_and_manage_ability_by_key( $key ); |
| 26 | if ( empty( $data ) ) { |
| 27 | return; |
| 28 | } |
| 29 | $booking = new OsBookingModel( $data['booking_id'] ); |
| 30 | if ( $booking->id ) { |
| 31 | $this->key = $key; |
| 32 | $this->booking = $booking; |
| 33 | $this->key_for = $data['for']; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | function __construct() { |
| 38 | parent::__construct(); |
| 39 | $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'manage_booking_by_key/'; |
| 40 | |
| 41 | $this->action_access['public'] = array_merge( $this->action_access['public'], [ |
| 42 | 'show', |
| 43 | 'print_booking_info', |
| 44 | 'ical_download', |
| 45 | 'change_status', |
| 46 | 'request_cancellation', |
| 47 | 'process_reschedule_request', |
| 48 | 'request_reschedule_calendar', |
| 49 | ] ); |
| 50 | |
| 51 | $this->set_booking_by_key(); |
| 52 | |
| 53 | } |
| 54 | |
| 55 | |
| 56 | function show() { |
| 57 | if ( empty( $this->booking->id ) ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | $this->vars['key'] = $this->key; |
| 63 | $this->vars['for'] = $this->key_for; |
| 64 | $this->vars['viewer'] = $this->key_for == 'agent' ? 'agent' : 'customer'; |
| 65 | $this->vars['booking'] = $this->booking; |
| 66 | $this->vars['order'] = $this->booking->get_order(); |
| 67 | $this->vars['order_manage_key'] = OsMetaHelper::get_order_meta_by_key( 'key_to_manage_for_' . $this->key_for, $this->booking->get_order()->id ); |
| 68 | |
| 69 | $this->vars['timezone_name'] = $this->key_for == 'agent' ? OsTimeHelper::get_wp_timezone_name() : $this->booking->customer->get_selected_timezone_name(); |
| 70 | |
| 71 | if ( $this->get_return_format() == 'json' ) { |
| 72 | $this->set_layout( 'none' ); |
| 73 | $response_html = $this->format_render_return( __FUNCTION__ ); |
| 74 | $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ) ); |
| 75 | } else { |
| 76 | $this->set_layout( 'clean' ); |
| 77 | $content = $this->format_render_return( __FUNCTION__ ); |
| 78 | echo $content; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | function ical_download() { |
| 84 | if ( empty( $this->booking->id ) ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | header( 'Content-Type: text/calendar; charset=utf-8' ); |
| 89 | header( 'Content-Disposition: attachment; filename=booking_' . $this->booking->id . '.ics' ); |
| 90 | |
| 91 | echo OsBookingHelper::generate_ical_event_string( $this->booking ); |
| 92 | } |
| 93 | |
| 94 | function print_booking_info() { |
| 95 | if ( empty( $this->booking->id ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $this->vars['booking'] = $this->booking; |
| 100 | $this->vars['order'] = $this->booking->get_order(); |
| 101 | $this->vars['customer'] = $this->booking->customer; |
| 102 | $this->set_layout( 'print' ); |
| 103 | $content = $this->format_render_return( __FUNCTION__, [], [], true ); |
| 104 | echo $content; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | function process_reschedule_request() { |
| 109 | if ( empty( $this->booking->id ) || empty( $this->params['start_date'] ) || empty( $this->params['start_time'] ) ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | $allowed = ( $this->key_for == 'agent' ) ? true : OsCustomerHelper::can_reschedule_booking( $this->booking ); |
| 114 | if ( $allowed ) { |
| 115 | $old_booking = clone $this->booking; |
| 116 | $this->booking->start_date = $this->params['start_date']; |
| 117 | $this->booking->start_time = $this->params['start_time']; |
| 118 | |
| 119 | $this->booking->convert_start_datetime_into_server_timezone($this->params['timezone_name'], ($this->key_for == 'customer')); |
| 120 | |
| 121 | if ( $this->booking->is_start_date_and_time_set() ) { |
| 122 | $this->booking->calculate_end_date_and_time(); |
| 123 | $this->booking->set_utc_datetimes(); |
| 124 | } |
| 125 | |
| 126 | // check if booking time is still available |
| 127 | if ( ! OsBookingHelper::is_booking_request_available( \LatePoint\Misc\BookingRequest::create_from_booking_model( $this->booking ), [ 'exclude_booking_ids' => [ $this->booking->id ] ] ) ) { |
| 128 | $response_html = __( 'Unfortunately the selected time slot is not available anymore, please select another timeslot.', 'latepoint' ); |
| 129 | $status = LATEPOINT_STATUS_ERROR; |
| 130 | } else { |
| 131 | // customer rescheduled, perform actions |
| 132 | if ( $this->key_for == 'customer' ) { |
| 133 | if ( OsSettingsHelper::is_on( 'change_status_on_customer_reschedule' ) ) { |
| 134 | $allowed_statuses = OsBookingHelper::get_statuses_list(); |
| 135 | if ( isset( $allowed_statuses[ OsSettingsHelper::get_settings_value( 'status_to_set_after_customer_reschedule' ) ] ) ) { |
| 136 | $this->booking->status = OsSettingsHelper::get_settings_value( 'status_to_set_after_customer_reschedule' ); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | if ( $this->booking->save() ) { |
| 141 | /** |
| 142 | * Booking is updated |
| 143 | * |
| 144 | * @param {OsBookingModel} $this->>booking Updated instance of booking model |
| 145 | * @param {OsBookingModel} $old_booking Instance of booking model before it was updated |
| 146 | * |
| 147 | * @since 4.9.0 |
| 148 | * @hook latepoint_booking_updated |
| 149 | * |
| 150 | */ |
| 151 | do_action( 'latepoint_booking_updated', $this->booking, $old_booking ); |
| 152 | $this->vars['booking'] = $this->booking; |
| 153 | $this->vars['timezone_name'] = ( $this->key_for == 'agent' ) ? OsTimeHelper::get_wp_timezone_name() : $this->booking->customer->get_selected_timezone_name(); |
| 154 | $status = LATEPOINT_STATUS_SUCCESS; |
| 155 | $this->vars['viewer'] = $this->key_for == 'agent' ? 'agent' : 'customer'; |
| 156 | $this->set_layout( 'none' ); |
| 157 | $response_html = $this->format_render_return( __FUNCTION__, [], [], true ); |
| 158 | } else { |
| 159 | OsDebugHelper::log( 'Error rescheduling appointment', 'booking_reschedule_error', $this->booking->get_error_messages() ); |
| 160 | $response_html = __( 'Error! Please try again later', 'latepoint' ); |
| 161 | $status = LATEPOINT_STATUS_ERROR; |
| 162 | } |
| 163 | } |
| 164 | } else { |
| 165 | $status = LATEPOINT_STATUS_ERROR; |
| 166 | $response_html = __( 'Error! LKDFU343', 'latepoint' ); |
| 167 | } |
| 168 | |
| 169 | if ( $this->get_return_format() == 'json' ) { |
| 170 | $this->send_json( array( 'status' => $status, 'message' => $response_html ) ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | function request_reschedule_calendar() { |
| 175 | if ( empty( $this->booking->id ) ) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | $allowed = ( $this->key_for == 'agent' ) ? true : OsCustomerHelper::can_reschedule_booking( $this->booking ); |
| 180 | |
| 181 | if ( $allowed ) { |
| 182 | if($this->key_for == 'customer'){ |
| 183 | // change timezone for customer to the one supplied, because we can't change it using the default change timezone method as we don't have a logged in customer here |
| 184 | $timezone_name = sanitize_text_field($this->params['timezone_name'] ?? $this->booking->customer->get_selected_timezone_name()); |
| 185 | OsMetaHelper::save_customer_meta_by_key( 'timezone_name', $timezone_name, $this->booking->customer_id ); |
| 186 | } |
| 187 | $this->vars['booking'] = $this->booking; |
| 188 | $this->vars['key'] = $this->key; |
| 189 | $this->vars['calendar_start_date'] = ! empty( $this->params['calendar_start_date'] ) ? new OsWpDateTime( $this->params['calendar_start_date'] ) : new OsWpDateTime( 'today' ); |
| 190 | $timezone_name = ( $this->key_for == 'agent' ) ? ($this->params['timezone_name'] ?? OsTimeHelper::get_wp_timezone_name()) : $this->booking->customer->get_selected_timezone_name(); |
| 191 | $this->vars['timezone_name'] = $timezone_name; |
| 192 | |
| 193 | $this->set_layout( 'none' ); |
| 194 | $response_html = $this->format_render_return( __FUNCTION__, [], [], true ); |
| 195 | } else { |
| 196 | $status = LATEPOINT_STATUS_ERROR; |
| 197 | $response_html = __( 'Reschedule is not allowed', 'latepoint' ); |
| 198 | } |
| 199 | if ( $this->get_return_format() == 'json' ) { |
| 200 | $this->send_json( array( 'status' => $status, 'message' => $response_html ) ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | function request_cancellation() { |
| 205 | if ( empty( $this->booking->id ) ) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | if ( OsCustomerHelper::can_cancel_booking( $this->booking ) ) { |
| 210 | if ( $this->booking->update_status( LATEPOINT_BOOKING_STATUS_CANCELLED ) ) { |
| 211 | $status = LATEPOINT_STATUS_SUCCESS; |
| 212 | $response_html = __( 'Appointment Status Updated', 'latepoint' ); |
| 213 | } else { |
| 214 | $status = LATEPOINT_STATUS_ERROR; |
| 215 | $response_html = __( 'Error Updating Booking Status!', 'latepoint' ) . ' ' . implode( ',', $this->booking->get_error_messages() ); |
| 216 | } |
| 217 | } else { |
| 218 | $status = LATEPOINT_STATUS_ERROR; |
| 219 | $response_html = __( 'Not allowed to cancel', 'latepoint' ); |
| 220 | } |
| 221 | if ( $this->get_return_format() == 'json' ) { |
| 222 | $this->send_json( array( 'status' => $status, 'message' => $response_html ) ); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | function change_status() { |
| 227 | // only agent key can cancel |
| 228 | if ( $this->key_for != 'agent' || empty( $this->booking->id ) || empty( $this->params['status'] ) ) { |
| 229 | return; |
| 230 | } |
| 231 | $statuses = OsBookingHelper::get_statuses_list(); |
| 232 | if ( ! isset( $statuses[ $this->params['status'] ] ) ) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | if ( $this->booking->update_status( $this->params['status'] ) ) { |
| 238 | $status = LATEPOINT_STATUS_SUCCESS; |
| 239 | $response_html = __( 'Appointment Status Updated', 'latepoint' ); |
| 240 | } else { |
| 241 | $status = LATEPOINT_STATUS_ERROR; |
| 242 | $response_html = __( 'Error Updating Booking Status!', 'latepoint' ) . ' ' . implode( ',', $this->booking->get_error_messages() ); |
| 243 | } |
| 244 | |
| 245 | if ( $this->get_return_format() == 'json' ) { |
| 246 | $this->send_json( array( 'status' => $status, 'message' => $response_html ) ); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | endif; |