| 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( 'label' => __( 'Services', 'latepoint' ), 'link' => OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'services', 'index' ) ) ); |
| 20 |
} |
| 21 |
|
| 22 |
/* |
| 23 |
Edit service |
| 24 |
*/ |
| 25 |
|
| 26 |
public function edit_form() { |
| 27 |
$service_id = $this->params['id']; |
| 28 |
|
| 29 |
$this->vars['pre_page_header'] = ''; |
| 30 |
$this->vars['page_header'] = __( 'Edit Service', 'latepoint' ); |
| 31 |
$this->vars['breadcrumbs'][] = array( 'label' => __( 'Edit Service', 'latepoint' ), 'link' => false ); |
| 32 |
|
| 33 |
|
| 34 |
$service = new OsServiceModel( $service_id ); |
| 35 |
$service_categories = new OsServiceCategoryModel(); |
| 36 |
$agents = new OsAgentModel(); |
| 37 |
$locations = new OsLocationModel(); |
| 38 |
|
| 39 |
|
| 40 |
$this->vars['service'] = $service; |
| 41 |
$this->vars['service_categories_for_select'] = $service_categories->index_for_select(); |
| 42 |
$this->vars['agents'] = $agents->get_results_as_models(); |
| 43 |
$this->vars['locations'] = $locations->get_results_as_models(); |
| 44 |
|
| 45 |
$custom_work_periods = OsWorkPeriodsHelper::get_work_periods( new \LatePoint\Misc\Filter( [ 'service_id' => $service_id, 'exact_match' => true ] ), true ); |
| 46 |
$this->vars['custom_work_periods'] = $custom_work_periods; |
| 47 |
$this->vars['is_custom_schedule'] = ( $custom_work_periods && ( count( $custom_work_periods ) > 0 ) ); |
| 48 |
|
| 49 |
$this->format_render( __FUNCTION__ ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
/* |
| 54 |
New service form |
| 55 |
*/ |
| 56 |
|
| 57 |
public function new_form() { |
| 58 |
$this->vars['pre_page_header'] = ''; |
| 59 |
$this->vars['page_header'] = __( 'New Service', 'latepoint' ); |
| 60 |
$this->vars['breadcrumbs'][] = array( 'label' => __( 'Create New Service', 'latepoint' ), 'link' => false ); |
| 61 |
|
| 62 |
|
| 63 |
$service = new OsServiceModel(); |
| 64 |
$service_categories = new OsServiceCategoryModel(); |
| 65 |
$agents = new OsAgentModel(); |
| 66 |
$locations = new OsLocationModel(); |
| 67 |
|
| 68 |
|
| 69 |
$service->bg_color = $service->generate_new_bg_color(); |
| 70 |
|
| 71 |
if ( isset( $this->params['service_category_id'] ) ) { |
| 72 |
$service->category_id = $this->params['service_category_id']; |
| 73 |
} |
| 74 |
|
| 75 |
$this->vars['service'] = $service; |
| 76 |
$this->vars['service_categories_for_select'] = $service_categories->index_for_select(); |
| 77 |
$this->vars['agents'] = $agents->get_results_as_models(); |
| 78 |
$this->vars['locations'] = $locations->get_results_as_models(); |
| 79 |
|
| 80 |
$this->vars['custom_work_periods'] = []; |
| 81 |
$this->vars['is_custom_schedule'] = false; |
| 82 |
|
| 83 |
|
| 84 |
$this->format_render( __FUNCTION__ ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/* |
| 89 |
Index of services |
| 90 |
*/ |
| 91 |
|
| 92 |
public function index() { |
| 93 |
$service_categories = new OsServiceCategoryModel(); |
| 94 |
$service_categories = $service_categories->order_by( 'order_number asc' )->filter_allowed_records()->get_results_as_models(); |
| 95 |
|
| 96 |
|
| 97 |
$this->vars['service_categories'] = $service_categories; |
| 98 |
|
| 99 |
$uncategorized_services = new OsServiceModel(); |
| 100 |
$this->vars['uncategorized_services'] = $uncategorized_services->filter_allowed_records()->should_be_active()->where( array( |
| 101 |
'category_id' => [ |
| 102 |
'OR' => [ |
| 103 |
0, |
| 104 |
'IS NULL' |
| 105 |
] |
| 106 |
] |
| 107 |
) )->order_by( 'order_number asc' )->get_results_as_models(); |
| 108 |
|
| 109 |
$disabled_services = new OsServiceModel(); |
| 110 |
$this->vars['disabled_services'] = $disabled_services->filter_allowed_records()->where( [ 'status' => LATEPOINT_SERVICE_STATUS_DISABLED ] )->get_results_as_models(); |
| 111 |
|
| 112 |
$this->format_render( __FUNCTION__ ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/* |
| 117 |
Create service |
| 118 |
*/ |
| 119 |
|
| 120 |
public function create() { |
| 121 |
$this->update(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/* |
| 126 |
Update service |
| 127 |
*/ |
| 128 |
|
| 129 |
public function update() { |
| 130 |
$is_new_record = ( isset( $this->params['service']['id'] ) && $this->params['service']['id'] ) ? false : true; |
| 131 |
|
| 132 |
$this->check_nonce( $is_new_record ? 'new_service' : 'edit_service_' . $this->params['service']['id'] ); |
| 133 |
$service = new OsServiceModel(); |
| 134 |
$service->set_data( $this->params['service'] ); |
| 135 |
$extra_response_vars = array(); |
| 136 |
|
| 137 |
$this->params['service']['durations'] = isset( $this->params['service']['durations'] ) ? $this->params['service']['durations'] : []; |
| 138 |
$this->params['service']['agents'] = isset( $this->params['service']['agents'] ) ? $this->params['service']['agents'] : []; |
| 139 |
|
| 140 |
if ( $service->save() && $service->save_durations( $this->params['service']['durations'] ) && $service->save_agents_and_locations( $this->params['service']['agents'] ) ) { |
| 141 |
if ( $is_new_record ) { |
| 142 |
$response_html = __( 'Service Created. ID:', 'latepoint' ) . $service->id; |
| 143 |
OsActivitiesHelper::create_activity( array( 'code' => 'service_created', 'service_id' => $service->id ) ); |
| 144 |
} else { |
| 145 |
$response_html = __( 'Service Updated. ID:', 'latepoint' ) . $service->id; |
| 146 |
OsActivitiesHelper::create_activity( array( 'code' => 'service_updated', 'service_id' => $service->id ) ); |
| 147 |
} |
| 148 |
$status = LATEPOINT_STATUS_SUCCESS; |
| 149 |
// save schedules |
| 150 |
if ( $this->params['is_custom_schedule'] == 'on' ) { |
| 151 |
$service->save_custom_schedule( $this->params['work_periods'] ); |
| 152 |
} elseif ( $this->params['is_custom_schedule'] == 'off' ) { |
| 153 |
$service->delete_custom_schedule(); |
| 154 |
} |
| 155 |
$extra_response_vars['record_id'] = $service->id; |
| 156 |
do_action( 'latepoint_service_saved', $service, $is_new_record, $this->params['service'] ); |
| 157 |
} else { |
| 158 |
$response_html = $service->get_error_messages(); |
| 159 |
$status = LATEPOINT_STATUS_ERROR; |
| 160 |
} |
| 161 |
if ( $this->get_return_format() == 'json' ) { |
| 162 |
$this->send_json( array( 'status' => $status, 'message' => $response_html ) + $extra_response_vars ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/* |
| 168 |
Delete service |
| 169 |
*/ |
| 170 |
|
| 171 |
public function destroy() { |
| 172 |
if ( filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) { |
| 173 |
$this->check_nonce( 'destroy_service_' . $this->params['id'] ); |
| 174 |
$service = new OsServiceModel( $this->params['id'] ); |
| 175 |
if ( $service->delete() ) { |
| 176 |
$status = LATEPOINT_STATUS_SUCCESS; |
| 177 |
$response_html = __( 'Service Removed', 'latepoint' ); |
| 178 |
} else { |
| 179 |
$status = LATEPOINT_STATUS_ERROR; |
| 180 |
$response_html = __( 'Error Removing Service', 'latepoint' ); |
| 181 |
} |
| 182 |
} else { |
| 183 |
$status = LATEPOINT_STATUS_ERROR; |
| 184 |
$response_html = __( 'Error Removing Service', 'latepoint' ); |
| 185 |
} |
| 186 |
|
| 187 |
if ( $this->get_return_format() == 'json' ) { |
| 188 |
$this->send_json( array( 'status' => $status, 'message' => $response_html ) ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
public function duplicate( ) { |
| 193 |
if(filter_var($this->params['id'], FILTER_VALIDATE_INT)) { |
| 194 |
$this->check_nonce( 'duplicate_service_' . $this->params['id'] ); |
| 195 |
|
| 196 |
$original_service = new OsServiceModel( $this->params['id'] ); |
| 197 |
$cloned_service = clone $original_service; |
| 198 |
$cloned_service->id = null; // reset ID to create a new record |
| 199 |
$cloned_service->name = $cloned_service->name . ' - ' . __('Copy', 'latepoint'); |
| 200 |
|
| 201 |
if($cloned_service->save()){ |
| 202 |
$status = LATEPOINT_STATUS_SUCCESS; |
| 203 |
$response_html = OsRouterHelper::build_link( OsRouterHelper::build_route_name( 'services', 'edit_form' ), array('id' => $cloned_service->id) ); |
| 204 |
OsActivitiesHelper::create_activity( array( 'code' => 'service_created', 'service_id' => $cloned_service->id ) ); |
| 205 |
|
| 206 |
$work_periods = new OsWorkPeriodModel(); |
| 207 |
$work_periods = $work_periods->where(['service_id' => $original_service->id])->get_results_as_models(); |
| 208 |
foreach($work_periods as $work_period){ |
| 209 |
$new_period = clone $work_period; |
| 210 |
$new_period->id = null; // reset ID to create a new record |
| 211 |
$new_period->service_id = $cloned_service->id; // set new service ID |
| 212 |
$new_period->save(); |
| 213 |
} |
| 214 |
|
| 215 |
$connection_model = new OsConnectorModel(); |
| 216 |
$connectors = $connection_model->where(['service_id' => $original_service->id])->get_results_as_models(); |
| 217 |
|
| 218 |
foreach($connectors as $connector){ |
| 219 |
$new_connector = clone $connector; |
| 220 |
$new_connector->id = null; |
| 221 |
$new_connector->service_id = $cloned_service->id; // set new location ID |
| 222 |
$new_connector->save(); |
| 223 |
} |
| 224 |
|
| 225 |
$meta = new OsServiceMetaModel(); |
| 226 |
$meta_items = $meta->where(['object_id' => $original_service->id]) |
| 227 |
->where_not_in('meta_key', ['woocommerce_product_id', 'surecart_product_id']) |
| 228 |
->get_results_as_models(); |
| 229 |
foreach($meta_items as $meta_item){ |
| 230 |
$new_meta_item = clone $meta_item; |
| 231 |
$new_meta_item->id = null; |
| 232 |
$new_meta_item->object_id = $cloned_service->id; |
| 233 |
$new_meta_item->save(); |
| 234 |
} |
| 235 |
|
| 236 |
#available in pro version only |
| 237 |
if (class_exists('OsServiceExtraConnectorModel')) { |
| 238 |
$extras = new OsServiceExtraConnectorModel(); |
| 239 |
$extras = $extras->where(['service_id' => $original_service->id])->get_results_as_models(); |
| 240 |
foreach($extras as $extra){ |
| 241 |
$new_extra = clone $extra; |
| 242 |
$new_extra->id = null; |
| 243 |
$new_extra->service_id = $cloned_service->id; |
| 244 |
$new_extra->save(); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} else { |
| 249 |
$status = LATEPOINT_STATUS_ERROR; |
| 250 |
$response_html = __('Error Creating Service', 'latepoint'); |
| 251 |
} |
| 252 |
|
| 253 |
if($this->get_return_format() == 'json'){ |
| 254 |
$this->send_json(array('status' => $status, 'message' => $response_html)); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
endif; |