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
calendars_controller.php
263 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | |
| 6 | |
| 7 | if ( ! class_exists( 'OsCalendarsController' ) ) : |
| 8 | |
| 9 | |
| 10 | class OsCalendarsController extends OsController { |
| 11 | |
| 12 | private $booking; |
| 13 | |
| 14 | function __construct() { |
| 15 | parent::__construct(); |
| 16 | $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'calendars/'; |
| 17 | $this->vars['page_header'] = OsMenuHelper::get_menu_items_by_id( 'calendar' ); |
| 18 | $this->vars['breadcrumbs'][] = array( 'label' => __( 'Appointments', 'latepoint' ), 'link' => OsRouterHelper::build_link( [ 'calendars', 'pending_approval' ] ) ); |
| 19 | } |
| 20 | |
| 21 | |
| 22 | public function view() { |
| 23 | $this->vars['page_header'] = ''; |
| 24 | |
| 25 | $today_date = new OsWpDateTime( 'today' ); |
| 26 | |
| 27 | |
| 28 | $services = ( new OsServiceModel() )->filter_allowed_records()->should_be_active()->get_results_as_models(); |
| 29 | $locations = ( new OsLocationModel() )->filter_allowed_records()->should_be_active()->get_results_as_models(); |
| 30 | $agents = ( new OsAgentModel() )->filter_allowed_records()->should_be_active()->get_results_as_models(); |
| 31 | |
| 32 | // extract groups from services |
| 33 | $service_categories = new OsServiceCategoryModel(); |
| 34 | $service_categories = $service_categories->get_results_as_models(); |
| 35 | $categorized_services_list = []; |
| 36 | // uncategorized |
| 37 | $categorized_services_list['category_none'] = [ 'name' => __( 'Uncategorized', 'latepoint' ), 'items' => [] ]; |
| 38 | |
| 39 | if ( $service_categories ) { |
| 40 | foreach ( $service_categories as $service_category ) { |
| 41 | $categorized_services_list[ 'category_' . $service_category->id ] = [ 'name' => $service_category->name, 'items' => [] ]; |
| 42 | } |
| 43 | } |
| 44 | foreach ( $services as $service ) { |
| 45 | if ( $service->category_id ) { |
| 46 | $categorized_services_list[ 'category_' . $service->category_id ]['items'][] = $service; |
| 47 | } else { |
| 48 | $categorized_services_list['category_none']['items'][] = $service; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | $this->vars['categorized_services_list'] = $categorized_services_list; |
| 53 | |
| 54 | $default_calendar_settings = [ |
| 55 | 'view' => OsAuthHelper::get_current_user()->get_wp_user_meta( 'latepoint_calendar_view', 'day' ), |
| 56 | 'target_date_string' => $today_date->format( 'Y-m-d' ), |
| 57 | 'show_service_ids' => OsUtilHelper::get_array_of_ids_from_array_of_models( $services ), |
| 58 | 'show_agent_ids' => OsUtilHelper::get_array_of_ids_from_array_of_models( $agents ), |
| 59 | 'show_location_ids' => OsUtilHelper::get_array_of_ids_from_array_of_models( $locations ), |
| 60 | 'overlay_service_availability' => LATEPOINT_VALUE_OFF, |
| 61 | 'availability_service_id' => '', |
| 62 | 'selected_agent_id' => '' // used for weekly calendar only |
| 63 | ]; |
| 64 | $calendar_settings = ! empty( $this->params['calendar_settings'] ) ? array_merge( $default_calendar_settings, $this->params['calendar_settings'] ) : $default_calendar_settings; |
| 65 | if ( $default_calendar_settings['view'] != $calendar_settings['view'] ) { |
| 66 | // update default view for this user |
| 67 | OsAuthHelper::get_current_user()->update_wp_user_meta( 'latepoint_calendar_view', $calendar_settings['view'] ); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | if ( $agents ) { |
| 72 | // show only agents that offer services and locations that are selected to be shown |
| 73 | $connected_agent_ids = OsConnectorHelper::get_connected_object_ids( 'agent_id', [ |
| 74 | 'service_id' => $calendar_settings['show_service_ids'], |
| 75 | 'location_id' => $calendar_settings['show_location_ids'] |
| 76 | ] ); |
| 77 | $connected_agents = []; |
| 78 | $connected_show_agents_ids = []; |
| 79 | foreach ( $agents as $agent ) { |
| 80 | if ( ! in_array( $agent->id, $connected_agent_ids ) || ! in_array( $agent->id, $calendar_settings['show_agent_ids'] ) ) { |
| 81 | continue; |
| 82 | } |
| 83 | $connected_agents[] = $agent; |
| 84 | $connected_show_agents_ids[] = $agent->id; |
| 85 | } |
| 86 | $calendar_settings['show_agent_ids'] = $connected_show_agents_ids; |
| 87 | $agents = $connected_agents; |
| 88 | if ( $agents && ( empty( $calendar_settings['selected_agent_id'] ) || ! in_array( $calendar_settings['selected_agent_id'], $connected_show_agents_ids ) ) ) { |
| 89 | $calendar_settings['selected_agent_id'] = $agents[0]->id; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // CHECK IF BOOKABLE RESOURCES EXIST |
| 94 | if ( empty( $services ) || empty( $agents ) || empty( $locations ) ) { |
| 95 | if ( $this->get_return_format() == 'json' ) { |
| 96 | $response_html = $this->render( $this->views_folder . 'missing_resources.php', 'none' ); |
| 97 | $this->send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html ] ); |
| 98 | } else { |
| 99 | $this->format_render( 'missing_resources' ); |
| 100 | exit(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | $selected_service_for_availability = false; |
| 105 | if ( $calendar_settings['overlay_service_availability'] == LATEPOINT_VALUE_ON && ! empty( $calendar_settings['availability_service_id'] ) ) { |
| 106 | foreach ( $services as $service ) { |
| 107 | if ( $service->id == $calendar_settings['availability_service_id'] ) { |
| 108 | $selected_service_for_availability = clone $service; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | if ( $calendar_settings['view'] == 'list' ) { |
| 116 | $per_page = OsSettingsHelper::get_number_of_records_per_page(); |
| 117 | $bookings = new OsBookingModel(); |
| 118 | $bookings = $bookings->get_upcoming_bookings( $calendar_settings['show_agent_ids'], false, $calendar_settings['show_service_ids'], $calendar_settings['show_location_ids'], $per_page ); |
| 119 | $this->vars['bookings'] = $bookings; |
| 120 | |
| 121 | $calendar_start = ( new OsWpDateTime( 'now' ) ); |
| 122 | $calendar_end = false; |
| 123 | |
| 124 | $top_date_label = OsUtilHelper::translate_months( $calendar_start->format( 'F' ), false ); |
| 125 | } else { |
| 126 | switch ( $calendar_settings['view'] ) { |
| 127 | case 'day': |
| 128 | $calendar_start = new OsWpDateTime( $calendar_settings['target_date_string'] ); |
| 129 | $calendar_end = new OsWpDateTime( $calendar_settings['target_date_string'] ); |
| 130 | $this->vars['day_view_calendar_min_height'] = OsSettingsHelper::get_day_calendar_min_height(); |
| 131 | $prev_target_date = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( '-1 day' ); |
| 132 | $next_target_date = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( '+1 day' ); |
| 133 | $top_date_label = OsUtilHelper::translate_months( $calendar_start->format( 'F j' ), false ); |
| 134 | break; |
| 135 | case 'week': |
| 136 | $calendar_start = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( 'monday this week' ); |
| 137 | $calendar_end = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( 'sunday this week' ); |
| 138 | $top_date_label = OsUtilHelper::translate_months( $calendar_start->format( 'F' ), false ) . ' ' . $calendar_start->format( 'j' ) . ' - ' . OsUtilHelper::translate_months( $calendar_end->format( 'F' ), false ) . ' ' . $calendar_end->format( 'j' ); |
| 139 | $prev_target_date = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( '-7 days' ); |
| 140 | $next_target_date = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( '+7 days' ); |
| 141 | break; |
| 142 | case 'month': |
| 143 | $calendar_start = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( 'first day of this month' ); |
| 144 | $calendar_end = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( 'last day of this month' ); |
| 145 | $top_date_label = OsUtilHelper::translate_months( $calendar_start->format( 'F' ), false ); |
| 146 | $prev_target_date = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( 'first day of previous month' ); |
| 147 | $next_target_date = ( new OsWpDateTime( $calendar_settings['target_date_string'] ) )->modify( 'first day of next month' ); |
| 148 | break; |
| 149 | } |
| 150 | $booking_request = new \LatePoint\Misc\BookingRequest(); |
| 151 | $booking_request->agent_id = $calendar_settings['show_agent_ids']; |
| 152 | $booking_request->service_id = $calendar_settings['show_service_ids']; |
| 153 | $booking_request->location_id = $calendar_settings['show_location_ids']; |
| 154 | |
| 155 | if ( $selected_service_for_availability ) { |
| 156 | $booking_request->service_id = $selected_service_for_availability->id; |
| 157 | $booking_request->duration = $selected_service_for_availability->duration; // TODO add capacity and duration select box and POST params if multiple durations in a service |
| 158 | $timeblock_interval = $selected_service_for_availability->get_timeblock_interval(); |
| 159 | } else { |
| 160 | $timeblock_interval = OsSettingsHelper::get_default_timeblock_interval(); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | $resources = OsResourceHelper::get_resources_grouped_by_day( $booking_request, $calendar_start, $calendar_end ); |
| 165 | |
| 166 | // if user wants to overlay availability for a specific service - we need to create a separate set of resources |
| 167 | // for the work boundaries, since the original one is only querying that service and not all other "shown" services, |
| 168 | // we want to show start and work time for all shown services, not just the selected one for the availability overlay |
| 169 | // we need to check if there is a single "shown" service - in this case we don't need a separate call for work boundaries |
| 170 | if ( count( $calendar_settings['show_service_ids'] ) > 1 && $selected_service_for_availability ) { |
| 171 | $booking_request_for_shown_services = clone $booking_request; |
| 172 | $booking_request_for_shown_services->service_id = $calendar_settings['show_service_ids']; |
| 173 | $resources_for_work_boundaries = OsResourceHelper::get_resources_grouped_by_day( $booking_request_for_shown_services, $calendar_start, $calendar_end ); |
| 174 | $work_boundaries = OsResourceHelper::get_work_boundaries_for_groups_of_resources( $resources_for_work_boundaries ); |
| 175 | } else { |
| 176 | $work_boundaries = OsResourceHelper::get_work_boundaries_for_groups_of_resources( $resources ); |
| 177 | } |
| 178 | |
| 179 | $work_time_periods_grouped_by_date_and_agent = []; |
| 180 | $bookings_grouped_by_date_and_agent = []; |
| 181 | foreach ( $agents as $agent ) { |
| 182 | for ( $day_date = clone $calendar_start; $day_date <= $calendar_end; $day_date->modify( '+1 day' ) ) { |
| 183 | // fill in all days and agents bookings with blanks |
| 184 | $bookings_grouped_by_date_and_agent[ $day_date->format( 'Y-m-d' ) ][ $agent->id ] = []; |
| 185 | $work_time_periods_grouped_by_date_and_agent[ $day_date->format( 'Y-m-d' ) ][ $agent->id ] = []; |
| 186 | $work_boundaries_grouped_by_date_and_agent[ $day_date->format( 'Y-m-d' ) ][ $agent->id ] = OsResourceHelper::get_work_boundaries_for_groups_of_resources( [ $resources[ $day_date->format( 'Y-m-d' ) ] ] ); |
| 187 | } |
| 188 | } |
| 189 | $filter = new \LatePoint\Misc\Filter( [ |
| 190 | 'date_from' => $calendar_start->format( 'Y-m-d' ), |
| 191 | 'date_to' => $calendar_end->format( 'Y-m-d' ), |
| 192 | 'service_id' => $calendar_settings['show_service_ids'], |
| 193 | 'agent_id' => $calendar_settings['show_agent_ids'], |
| 194 | 'location_id' => $calendar_settings['show_location_ids'], |
| 195 | 'statuses' => OsCalendarHelper::get_booking_statuses_to_display_on_calendar() |
| 196 | ] ); |
| 197 | $filter = OsRolesHelper::filter_allowed_records_from_arguments_or_filter( $filter ); |
| 198 | |
| 199 | // loop bookings to fill in array |
| 200 | $bookings = OsBookingHelper::get_bookings( $filter, true ); |
| 201 | foreach ( $bookings as $booking ) { |
| 202 | $bookings_grouped_by_date_and_agent[ $booking->start_date ][ $booking->agent_id ][] = $booking; |
| 203 | } |
| 204 | |
| 205 | // loop resources to fill in work periods array |
| 206 | foreach ( $resources as $day => $daily_resources ) { |
| 207 | foreach ( $daily_resources as $daily_resource ) { |
| 208 | $work_time_periods_grouped_by_date_and_agent[ $day ][ $daily_resource->agent_id ] = array_merge( $work_time_periods_grouped_by_date_and_agent[ $day ][ $daily_resource->agent_id ], $daily_resource->work_time_periods ); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | $this->vars['bookings_grouped_by_date_and_agent'] = $bookings_grouped_by_date_and_agent; |
| 213 | $this->vars['work_boundaries_grouped_by_date_and_agent'] = $work_boundaries_grouped_by_date_and_agent; |
| 214 | $this->vars['timeblock_interval'] = $timeblock_interval; |
| 215 | |
| 216 | $this->vars['booking_request'] = $booking_request; |
| 217 | $this->vars['work_time_periods_grouped_by_date_and_agent'] = $work_time_periods_grouped_by_date_and_agent; |
| 218 | $this->vars['prev_target_date'] = $prev_target_date; |
| 219 | $this->vars['next_target_date'] = $next_target_date; |
| 220 | $this->vars['resources'] = $resources; |
| 221 | $this->vars['work_boundaries'] = $work_boundaries; |
| 222 | $this->vars['work_total_minutes'] = $work_boundaries->end_time - $work_boundaries->start_time; |
| 223 | } |
| 224 | |
| 225 | $top_date_year = !empty($calendar_start) ? $calendar_start->format( 'Y' ) : ''; |
| 226 | |
| 227 | $this->vars['calendar_start'] = $calendar_start; |
| 228 | $this->vars['calendar_end'] = $calendar_end; |
| 229 | |
| 230 | $this->vars['date_format'] = OsSettingsHelper::get_readable_date_format( true ); |
| 231 | |
| 232 | $this->vars['target_date'] = new OsWpDateTime( $calendar_settings['target_date_string'] ); |
| 233 | $this->vars['today_date'] = $today_date; |
| 234 | $this->vars['top_date_label'] = $top_date_label; |
| 235 | |
| 236 | $this->vars['agents'] = $agents; |
| 237 | $this->vars['services'] = $services; |
| 238 | $this->vars['locations'] = $locations; |
| 239 | |
| 240 | $this->vars['calendar_settings'] = $calendar_settings; |
| 241 | |
| 242 | if ( $this->get_return_format() == 'json' ) { |
| 243 | $response_html = $this->render( $this->views_folder . 'scopes/_' . $calendar_settings['view'], 'none' ); |
| 244 | $this->send_json( [ 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => $response_html, 'top_date_label' => $top_date_label, 'top_date_year' => $top_date_year] ); |
| 245 | } else { |
| 246 | $this->format_render( __FUNCTION__ ); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
| 251 | public function load_monthly_calendar_days_only() { |
| 252 | $target_date = new OsWpDateTime( $this->params['target_date_string'] ); |
| 253 | $this->vars['target_date'] = $target_date; |
| 254 | |
| 255 | $this->set_layout( 'none' ); |
| 256 | $this->format_render( __FUNCTION__ ); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | } |
| 262 | |
| 263 | endif; |