activities_controller.php
1 month ago
auth_controller.php
3 months ago
booking_form_settings_controller.php
3 months ago
bookings_controller.php
12 hours ago
calendars_controller.php
3 months ago
carts_controller.php
12 hours ago
controller.php
3 months ago
customer_cabinet_controller.php
2 months ago
customers_controller.php
12 hours ago
dashboard_controller.php
2 months ago
default_agent_controller.php
3 months ago
events_controller.php
3 months ago
form_fields_controller.php
1 week ago
integrations_controller.php
3 months ago
invoices_controller.php
12 hours ago
manage_booking_by_key_controller.php
3 months ago
manage_order_by_key_controller.php
3 months ago
notifications_controller.php
3 months ago
orders_controller.php
12 hours ago
pro_controller.php
2 weeks ago
process_jobs_controller.php
3 months ago
processes_controller.php
1 month ago
razorpay_connect_controller.php
1 week ago
search_controller.php
3 months ago
services_controller.php
3 months ago
settings_controller.php
2 months ago
steps_controller.php
2 weeks ago
stripe_connect_controller.php
1 week ago
support_topics_controller.php
3 months ago
todos_controller.php
3 months ago
transactions_controller.php
12 hours ago
wizard_controller.php
1 week ago
services_controller.php
310 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | |
| 6 | |
| 7 | if ( ! class_exists( 'OsServicesController' ) ) : |
| 8 | |
| 9 | |
| 10 | class OsServicesController extends OsController { |
| 11 | |
| 12 | |
| 13 | function __construct() { |
| 14 | parent::__construct(); |
| 15 | |
| 16 | $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'services/'; |
| 17 | $this->vars['page_header'] = OsMenuHelper::get_menu_items_by_id( 'services' ); |
| 18 | $this->vars['pre_page_header'] = OsMenuHelper::get_label_by_id( 'services' ); |
| 19 | $this->vars['breadcrumbs'][] = array( |
| 20 | 'label' => __( 'Services', 'latepoint' ), |
| 21 | 'link' => OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'services', 'index' ) ), |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | /* |
| 26 | Edit service |
| 27 | */ |
| 28 | |
| 29 | public function edit_form() { |
| 30 | $service_id = $this->params['id']; |
| 31 | |
| 32 | $this->vars['pre_page_header'] = ''; |
| 33 | $this->vars['page_header'] = __( 'Edit Service', 'latepoint' ); |
| 34 | $this->vars['breadcrumbs'][] = array( |
| 35 | 'label' => __( 'Edit Service', 'latepoint' ), |
| 36 | 'link' => false, |
| 37 | ); |
| 38 | |
| 39 | |
| 40 | $service = new OsServiceModel( $service_id ); |
| 41 | $service_categories = new OsServiceCategoryModel(); |
| 42 | $agents = new OsAgentModel(); |
| 43 | $locations = new OsLocationModel(); |
| 44 | |
| 45 | |
| 46 | $this->vars['service'] = $service; |
| 47 | $this->vars['service_categories_for_select'] = $service_categories->index_for_select(); |
| 48 | $this->vars['agents'] = $agents->get_results_as_models(); |
| 49 | $this->vars['locations'] = $locations->get_results_as_models(); |
| 50 | |
| 51 | $custom_work_periods = OsWorkPeriodsHelper::get_work_periods( |
| 52 | new \LatePoint\Misc\Filter( |
| 53 | [ |
| 54 | 'service_id' => $service_id, |
| 55 | 'exact_match' => true, |
| 56 | ] |
| 57 | ), |
| 58 | true |
| 59 | ); |
| 60 | $this->vars['custom_work_periods'] = $custom_work_periods; |
| 61 | $this->vars['is_custom_schedule'] = ( $custom_work_periods && ( count( $custom_work_periods ) > 0 ) ); |
| 62 | |
| 63 | $this->format_render( __FUNCTION__ ); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /* |
| 68 | New service form |
| 69 | */ |
| 70 | |
| 71 | public function new_form() { |
| 72 | $this->vars['pre_page_header'] = ''; |
| 73 | $this->vars['page_header'] = __( 'New Service', 'latepoint' ); |
| 74 | $this->vars['breadcrumbs'][] = array( |
| 75 | 'label' => __( 'Create New Service', 'latepoint' ), |
| 76 | 'link' => false, |
| 77 | ); |
| 78 | |
| 79 | |
| 80 | $service = new OsServiceModel(); |
| 81 | $service_categories = new OsServiceCategoryModel(); |
| 82 | $agents = new OsAgentModel(); |
| 83 | $locations = new OsLocationModel(); |
| 84 | |
| 85 | |
| 86 | $service->bg_color = $service->generate_new_bg_color(); |
| 87 | |
| 88 | if ( isset( $this->params['service_category_id'] ) ) { |
| 89 | $service->category_id = $this->params['service_category_id']; |
| 90 | } |
| 91 | |
| 92 | $this->vars['service'] = $service; |
| 93 | $this->vars['service_categories_for_select'] = $service_categories->index_for_select(); |
| 94 | $this->vars['agents'] = $agents->get_results_as_models(); |
| 95 | $this->vars['locations'] = $locations->get_results_as_models(); |
| 96 | |
| 97 | $this->vars['custom_work_periods'] = []; |
| 98 | $this->vars['is_custom_schedule'] = false; |
| 99 | |
| 100 | |
| 101 | $this->format_render( __FUNCTION__ ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /* |
| 106 | Index of services |
| 107 | */ |
| 108 | |
| 109 | public function index() { |
| 110 | $service_categories = new OsServiceCategoryModel(); |
| 111 | $service_categories = $service_categories->order_by( 'order_number asc' )->filter_allowed_records()->get_results_as_models(); |
| 112 | |
| 113 | |
| 114 | $this->vars['service_categories'] = $service_categories; |
| 115 | |
| 116 | $uncategorized_services = new OsServiceModel(); |
| 117 | $this->vars['uncategorized_services'] = $uncategorized_services->filter_allowed_records()->should_be_active()->where( |
| 118 | array( |
| 119 | 'category_id' => [ |
| 120 | 'OR' => [ |
| 121 | 0, |
| 122 | 'IS NULL', |
| 123 | ], |
| 124 | ], |
| 125 | ) |
| 126 | )->order_by( 'order_number asc' )->get_results_as_models(); |
| 127 | |
| 128 | $disabled_services = new OsServiceModel(); |
| 129 | $this->vars['disabled_services'] = $disabled_services->filter_allowed_records()->where( [ 'status' => LATEPOINT_SERVICE_STATUS_DISABLED ] )->get_results_as_models(); |
| 130 | |
| 131 | $this->format_render( __FUNCTION__ ); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | Create service |
| 137 | */ |
| 138 | |
| 139 | public function create() { |
| 140 | $this->update(); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /* |
| 145 | Update service |
| 146 | */ |
| 147 | |
| 148 | public function update() { |
| 149 | $is_new_record = ( isset( $this->params['service']['id'] ) && $this->params['service']['id'] ) ? false : true; |
| 150 | |
| 151 | $this->check_nonce( $is_new_record ? 'new_service' : 'edit_service_' . $this->params['service']['id'] ); |
| 152 | $service = new OsServiceModel(); |
| 153 | $service->set_data( $this->params['service'] ); |
| 154 | $extra_response_vars = array(); |
| 155 | |
| 156 | $this->params['service']['durations'] = isset( $this->params['service']['durations'] ) ? $this->params['service']['durations'] : []; |
| 157 | $this->params['service']['agents'] = isset( $this->params['service']['agents'] ) ? $this->params['service']['agents'] : []; |
| 158 | |
| 159 | if ( $service->save() && $service->save_durations( $this->params['service']['durations'] ) && $service->save_agents_and_locations( $this->params['service']['agents'] ) ) { |
| 160 | if ( $is_new_record ) { |
| 161 | $response_html = __( 'Service Created. ID:', 'latepoint' ) . $service->id; |
| 162 | OsActivitiesHelper::create_activity( |
| 163 | array( |
| 164 | 'code' => 'service_created', |
| 165 | 'service_id' => $service->id, |
| 166 | ) |
| 167 | ); |
| 168 | } else { |
| 169 | $response_html = __( 'Service Updated. ID:', 'latepoint' ) . $service->id; |
| 170 | OsActivitiesHelper::create_activity( |
| 171 | array( |
| 172 | 'code' => 'service_updated', |
| 173 | 'service_id' => $service->id, |
| 174 | ) |
| 175 | ); |
| 176 | } |
| 177 | $status = LATEPOINT_STATUS_SUCCESS; |
| 178 | // save schedules |
| 179 | if ( $this->params['is_custom_schedule'] == 'on' ) { |
| 180 | $service->save_custom_schedule( $this->params['work_periods'] ); |
| 181 | } elseif ( $this->params['is_custom_schedule'] == 'off' ) { |
| 182 | $service->delete_custom_schedule(); |
| 183 | } |
| 184 | $extra_response_vars['record_id'] = $service->id; |
| 185 | do_action( 'latepoint_service_saved', $service, $is_new_record, $this->params['service'] ); |
| 186 | } else { |
| 187 | $response_html = $service->get_error_messages(); |
| 188 | $status = LATEPOINT_STATUS_ERROR; |
| 189 | } |
| 190 | if ( $this->get_return_format() == 'json' ) { |
| 191 | $this->send_json( |
| 192 | array( |
| 193 | 'status' => $status, |
| 194 | 'message' => $response_html, |
| 195 | ) + $extra_response_vars |
| 196 | ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /* |
| 202 | Delete service |
| 203 | */ |
| 204 | |
| 205 | public function destroy() { |
| 206 | if ( filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) { |
| 207 | $this->check_nonce( 'destroy_service_' . $this->params['id'] ); |
| 208 | $service = new OsServiceModel( $this->params['id'] ); |
| 209 | if ( $service->delete() ) { |
| 210 | $status = LATEPOINT_STATUS_SUCCESS; |
| 211 | $response_html = __( 'Service Removed', 'latepoint' ); |
| 212 | } else { |
| 213 | $status = LATEPOINT_STATUS_ERROR; |
| 214 | $response_html = __( 'Error Removing Service', 'latepoint' ); |
| 215 | } |
| 216 | } else { |
| 217 | $status = LATEPOINT_STATUS_ERROR; |
| 218 | $response_html = __( 'Error Removing Service', 'latepoint' ); |
| 219 | } |
| 220 | |
| 221 | if ( $this->get_return_format() == 'json' ) { |
| 222 | $this->send_json( |
| 223 | array( |
| 224 | 'status' => $status, |
| 225 | 'message' => $response_html, |
| 226 | ) |
| 227 | ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | public function duplicate() { |
| 232 | if ( filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) { |
| 233 | $this->check_nonce( 'duplicate_service_' . $this->params['id'] ); |
| 234 | |
| 235 | $original_service = new OsServiceModel( $this->params['id'] ); |
| 236 | $cloned_service = clone $original_service; |
| 237 | $cloned_service->id = null; // reset ID to create a new record |
| 238 | $cloned_service->name = $cloned_service->name . ' - ' . __( 'Copy', 'latepoint' ); |
| 239 | |
| 240 | if ( $cloned_service->save() ) { |
| 241 | $status = LATEPOINT_STATUS_SUCCESS; |
| 242 | $response_html = OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'services', 'edit_form' ), array( 'id' => $cloned_service->id ) ); |
| 243 | OsActivitiesHelper::create_activity( |
| 244 | array( |
| 245 | 'code' => 'service_created', |
| 246 | 'service_id' => $cloned_service->id, |
| 247 | ) |
| 248 | ); |
| 249 | |
| 250 | $work_periods = new OsWorkPeriodModel(); |
| 251 | $work_periods = $work_periods->where( [ 'service_id' => $original_service->id ] )->get_results_as_models(); |
| 252 | foreach ( $work_periods as $work_period ) { |
| 253 | $new_period = clone $work_period; |
| 254 | $new_period->id = null; // reset ID to create a new record |
| 255 | $new_period->service_id = $cloned_service->id; // set new service ID |
| 256 | $new_period->save(); |
| 257 | } |
| 258 | |
| 259 | $connection_model = new OsConnectorModel(); |
| 260 | $connectors = $connection_model->where( [ 'service_id' => $original_service->id ] )->get_results_as_models(); |
| 261 | |
| 262 | foreach ( $connectors as $connector ) { |
| 263 | $new_connector = clone $connector; |
| 264 | $new_connector->id = null; |
| 265 | $new_connector->service_id = $cloned_service->id; // set new location ID |
| 266 | $new_connector->save(); |
| 267 | } |
| 268 | |
| 269 | $meta = new OsServiceMetaModel(); |
| 270 | $meta_items = $meta->where( [ 'object_id' => $original_service->id ] ) |
| 271 | ->where_not_in( 'meta_key', [ 'woocommerce_product_id', 'surecart_product_id' ] ) |
| 272 | ->get_results_as_models(); |
| 273 | foreach ( $meta_items as $meta_item ) { |
| 274 | $new_meta_item = clone $meta_item; |
| 275 | $new_meta_item->id = null; |
| 276 | $new_meta_item->object_id = $cloned_service->id; |
| 277 | $new_meta_item->save(); |
| 278 | } |
| 279 | |
| 280 | #available in pro version only |
| 281 | if ( class_exists( 'OsServiceExtraConnectorModel' ) ) { |
| 282 | $extras = new OsServiceExtraConnectorModel(); |
| 283 | $extras = $extras->where( [ 'service_id' => $original_service->id ] )->get_results_as_models(); |
| 284 | foreach ( $extras as $extra ) { |
| 285 | $new_extra = clone $extra; |
| 286 | $new_extra->id = null; |
| 287 | $new_extra->service_id = $cloned_service->id; |
| 288 | $new_extra->save(); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } else { |
| 293 | $status = LATEPOINT_STATUS_ERROR; |
| 294 | $response_html = __( 'Error Creating Service', 'latepoint' ); |
| 295 | } |
| 296 | |
| 297 | if ( $this->get_return_format() == 'json' ) { |
| 298 | $this->send_json( |
| 299 | array( |
| 300 | 'status' => $status, |
| 301 | 'message' => $response_html, |
| 302 | ) |
| 303 | ); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | |
| 309 | endif; |
| 310 |