PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.11
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.11
5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / models / agent_model.php
latepoint / lib / models Last commit date
activity_model.php 3 months ago agent_meta_model.php 3 months ago agent_model.php 3 months ago booking_meta_model.php 3 months ago booking_model.php 3 months ago bundle_meta_model.php 3 months ago bundle_model.php 3 months ago cart_item_model.php 3 months ago cart_meta_model.php 3 months ago cart_model.php 3 months ago connector_model.php 3 months ago customer_meta_model.php 3 months ago customer_model.php 3 months ago invoice_model.php 3 months ago join_bundles_services_model.php 3 months ago location_category_model.php 3 months ago location_model.php 3 months ago meta_model.php 3 months ago model.php 3 months ago off_period_model.php 3 months ago order_intent_meta_model.php 3 months ago order_intent_model.php 3 months ago order_item_model.php 3 months ago order_meta_model.php 3 months ago order_model.php 3 months ago otp_model.php 3 months ago payment_request_model.php 3 months ago process_job_model.php 3 months ago process_model.php 3 months ago recurrence_model.php 3 months ago service_category_model.php 3 months ago service_meta_model.php 3 months ago service_model.php 3 months ago session_model.php 3 months ago settings_model.php 3 months ago step_settings_model.php 3 months ago transaction_intent_model.php 3 months ago transaction_model.php 3 months ago transaction_refund_model.php 3 months ago work_period_model.php 3 months ago
agent_model.php
480 lines
1 <?php
2
3 /**
4 * @property string $full_name
5 */
6 class OsAgentModel extends OsModel {
7 public $id,
8 $first_name = '',
9 $last_name = '',
10 $display_name,
11 $email,
12 $phone,
13 $password,
14 $avatar_image_id,
15 $bio_image_id,
16 $is_custom_price = false,
17 $is_custom_hours = false,
18 $is_custom_duration = false,
19 $custom_hours,
20 $wp_user_id,
21 $title,
22 $bio,
23 $features,
24 $extra_emails,
25 $extra_phones,
26 $status,
27 $meta_class = 'OsAgentMetaModel',
28 $updated_at,
29 $created_at,
30 $services_agents_table_name;
31
32 function __construct( $id = false ) {
33 parent::__construct();
34 $this->table_name = LATEPOINT_TABLE_AGENTS;
35 $this->services_agents_table_name = LATEPOINT_TABLE_AGENTS_SERVICES;
36 $this->nice_names = array(
37 'first_name' => __( 'First Name', 'latepoint' ),
38 'password' => __( 'Password', 'latepoint' ),
39 'email' => __( 'Email Address', 'latepoint' ),
40 'wp_user_id' => __( 'Connected WordPress User', 'latepoint' ),
41 'last_name' => __( 'Last Name', 'latepoint' ),
42 );
43
44 if ( $id ) {
45 $this->load_by_id( $id );
46 }
47 }
48
49
50 public function get_initials() {
51 return mb_substr( $this->first_name, 0, 1 ) . mb_substr( $this->last_name, 0, 1 );
52 }
53
54 public function get_edit_link() {
55 return OsRouterHelper::build_link( [ 'agents', 'edit_form' ], [ 'id' => $this->id ] );
56 }
57
58 public function generate_data_vars(): array {
59 return [
60 'id' => $this->id,
61 'full_name' => $this->full_name,
62 'email' => $this->email,
63 'phone' => $this->phone,
64 ];
65 }
66
67 protected function params_to_save( $role = 'admin' ) {
68 $params_to_save = array(
69 'id',
70 'first_name',
71 'last_name',
72 'display_name',
73 'email',
74 'phone',
75 'password',
76 'wp_user_id',
77 'bio_image_id',
78 'title',
79 'bio',
80 'features',
81 'status',
82 'extra_emails',
83 'extra_phones',
84 'avatar_image_id',
85 'custom_hours',
86 );
87 return $params_to_save;
88 }
89
90 protected function allowed_params( $role = 'admin' ) {
91 $allowed_params = array(
92 'id',
93 'first_name',
94 'last_name',
95 'display_name',
96 'email',
97 'phone',
98 'password',
99 'wp_user_id',
100 'bio_image_id',
101 'title',
102 'bio',
103 'features',
104 'extra_emails',
105 'extra_phones',
106 'status',
107 'avatar_image_id',
108 'custom_hours',
109 );
110 return $allowed_params;
111 }
112
113
114 protected function properties_to_validate() {
115 $validations = array(
116 'email' => array( 'presence' ),
117 'wp_user_id' => array( 'uniqueness' ),
118 );
119 return $validations;
120 }
121
122
123 public function count_number_of_connected_locations( $service_id = false ) {
124 if ( $this->is_new_record() ) {
125 return 0;
126 }
127 $args = [ 'agent_id' => $this->id ];
128 if ( $service_id ) {
129 $args['service_id'] = $service_id;
130 }
131 return OsConnectorHelper::count_connections( $args, 'location_id' );
132 }
133
134 public function delete_meta_by_key( $meta_key ) {
135 if ( $this->is_new_record() ) {
136 return false;
137 }
138
139 $meta = new OsAgentMetaModel();
140 return $meta->delete_by_key( $meta_key, $this->id );
141 }
142
143 public function get_meta_by_key( $meta_key, $default = false ) {
144 if ( $this->is_new_record() ) {
145 return $default;
146 }
147
148 $meta = new OsAgentMetaModel();
149 return $meta->get_by_key( $meta_key, $this->id, $default );
150 }
151
152 public function save_meta_by_key( $meta_key, $meta_value ) {
153 if ( $this->is_new_record() ) {
154 return false;
155 }
156
157 $meta = new OsAgentMetaModel();
158 return $meta->save_by_key( $meta_key, $meta_value, $this->id );
159 }
160
161 protected function set_defaults() {
162 if ( empty( $this->status ) ) {
163 $this->status = LATEPOINT_AGENT_STATUS_ACTIVE;
164 }
165 }
166
167 public function has_location( $location_id ) {
168 return OsConnectorHelper::has_connection(
169 [
170 'agent_id' => $this->id,
171 'location_id' => $location_id,
172 ]
173 );
174 }
175
176
177 protected function get_total_future_bookings() {
178 $bookings = new OsBookingModel();
179 $total = $bookings->where( [ 'agent_id' => $this->id ] )->should_be_in_future()->count();
180 return $total;
181 }
182
183 protected function get_total_synced_future_bookings() {
184 $bookings = new OsBookingModel();
185 $total = $bookings->where(
186 [
187 'agent_id' => $this->id,
188 LATEPOINT_TABLE_BOOKING_META . '.meta_key' => 'google_calendar_event_id',
189 ]
190 )
191 ->join( LATEPOINT_TABLE_BOOKING_META, [ 'object_id' => LATEPOINT_TABLE_BOOKINGS . '.id' ] )
192 ->should_be_in_future()
193 ->count();
194 return $total;
195 }
196
197 protected function get_future_bookings( $limit = false ) {
198 $bookings = new OsBookingModel();
199 if ( $limit ) {
200 $bookings = $bookings->set_limit( $limit );
201 }
202 return $bookings->order_by( 'start_date, start_time asc' )->where( [ 'agent_id' => $this->id ] )->should_be_in_future()->get_results_as_models();
203 }
204
205 public function should_be_active() {
206 return $this->where( [ 'status !=' => LATEPOINT_AGENT_STATUS_DISABLED ] );
207 }
208
209 public function has_service_and_location( $service_id, $location_id ) {
210 if ( $this->is_new_record() ) {
211 return false;
212 }
213 return OsConnectorHelper::has_connection(
214 [
215 'location_id' => $location_id,
216 'agent_id' => $this->id,
217 'service_id' => $service_id,
218 ]
219 );
220 }
221
222 public function get_features_arr() {
223 $features_arr = [];
224 if ( ! empty( $this->features ) ) {
225 $features = json_decode( $this->features, true );
226 if ( ! empty( $features ) ) {
227 foreach ( $features as $feature ) {
228 if ( $feature['value'] && $feature['label'] ) {
229 $features_arr[] = $feature;
230 }
231 }
232 }
233 }
234 return $features_arr;
235 }
236
237 public function save_custom_schedule( $work_periods ) {
238 foreach ( $work_periods as &$work_period ) {
239 $work_period['agent_id'] = $this->id;
240 }
241 unset( $work_period );
242 OsWorkPeriodsHelper::save_work_periods( $work_periods );
243 }
244
245 public function delete_custom_schedule() {
246 $work_periods_model = new OsWorkPeriodModel();
247 $work_periods = $work_periods_model->where(
248 array(
249 'agent_id' => $this->id,
250 'service_id' => 0,
251 'location_id' => 0,
252 'custom_date' => 'IS NULL',
253 )
254 )->get_results_as_models();
255 if ( is_array( $work_periods ) ) {
256 foreach ( $work_periods as $work_period ) {
257 $work_period->delete();
258 }
259 }
260 }
261
262 public function has_service( $service_id ) {
263 foreach ( $this->services as $service ) {
264 if ( $service->id == $service_id ) {
265 return true;
266 }
267 }
268 return false;
269 }
270
271
272 public function save_services() {
273 foreach ( $this->services as $service ) {
274 $service_connection_row = $this->db->get_row( $this->db->prepare( 'SELECT id FROM ' . $this->services_agents_table_name . ' WHERE agent_id = %d AND service_id = %d', array( $this->id, $service->id ) ) );
275 if ( $service_connection_row ) {
276 $update_data = array(
277 'is_custom_hours' => $service->is_custom_hours,
278 'is_custom_price' => $service->is_custom_price,
279 'is_custom_duration' => $service->is_custom_duration,
280 );
281 $this->db->update( $this->services_agents_table_name, $update_data, array( 'id' => $service_connection_row->id ) );
282 } else {
283 $insert_data = array(
284 'service_id' => $service->id,
285 'agent_id' => $this->id,
286 'is_custom_hours' => $service->is_custom_hours,
287 'is_custom_price' => $service->is_custom_price,
288 'is_custom_duration' => $service->is_custom_duration,
289 );
290 if ( $this->db->insert( $this->services_agents_table_name, $insert_data ) ) {
291 $service_connection_row_id = $this->db->insert_id;
292 }
293 }
294 }
295 return true;
296 }
297
298
299 public function remove_services_by_ids( $ids_to_remove = array() ) {
300 if ( $ids_to_remove ) {
301 $query = $this->db->prepare( 'DELETE FROM %i WHERE agent_id = %d AND service_id IN ' . OsModel::where_in_array_to_string( $ids_to_remove ), [ $this->services_agents_table_name, $this->id ] );
302 $this->db->query( $query );
303 }
304 }
305
306
307 public function get_service_ids_to_remove( $new_services = array() ) {
308 $current_service_ids = $this->get_current_service_ids_from_db();
309 $new_service_ids = array();
310 foreach ( $new_services as $service ) {
311 if ( $service['connected'] == 'yes' ) {
312 $new_service_ids[] = $service['id'];
313 }
314 }
315 $service_ids_to_remove = array_diff( $current_service_ids, $new_service_ids );
316 return $service_ids_to_remove;
317 }
318
319
320 public function save_locations_and_services( $services ) {
321 if ( ! $services ) {
322 return true;
323 }
324 $connections_to_save = [];
325 $connections_to_remove = [];
326 foreach ( $services as $service_key => $locations ) {
327 $service_id = str_replace( 'service_', '', $service_key );
328 foreach ( $locations as $location_key => $location ) {
329 $location_id = str_replace( 'location_', '', $location_key );
330 $connection = [
331 'service_id' => $service_id,
332 'agent_id' => $this->id,
333 'location_id' => $location_id,
334 ];
335 if ( $location['connected'] == 'yes' ) {
336 $connections_to_save[] = $connection;
337 } else {
338 $connections_to_remove[] = $connection;
339 }
340 }
341 }
342 if ( ! empty( $connections_to_save ) ) {
343 foreach ( $connections_to_save as $connection_to_save ) {
344 OsConnectorHelper::save_connection( $connection_to_save );
345 }
346 }
347 if ( ! empty( $connections_to_remove ) ) {
348 foreach ( $connections_to_remove as $connection_to_remove ) {
349 OsConnectorHelper::remove_connection( $connection_to_remove );
350 }
351 }
352 return true;
353 }
354
355 public function set_features( $features ) {
356 $this->features = wp_json_encode( $features );
357 }
358
359 public function get_current_service_ids_from_db() {
360 $query = $this->db->prepare( 'SELECT service_id FROM ' . $this->services_agents_table_name . ' WHERE agent_id = %d', $this->id );
361 $service_rows = $this->db->get_results( $query );
362
363 $service_ids = array();
364
365 if ( $service_rows ) {
366 foreach ( $service_rows as $service_row ) {
367 $service_ids[] = $service_row->service_id;
368 }
369 }
370 return $service_ids;
371 }
372
373
374 public function get_current_service_ids() {
375 $service_ids = array();
376 foreach ( $this->services as $service ) {
377 $service_ids[] = $service->id;
378 }
379 return $service_ids;
380 }
381
382 public function set_services( $service_datas ) {
383 $this->services = array();
384
385 foreach ( $service_datas as $service_data ) {
386 if ( $service_data['connected'] == 'yes' ) {
387 $service = new OsserviceModel();
388 $service->id = $service_data['id'];
389 $service->is_custom_hours = $service_data['is_custom_hours'];
390 $service->is_custom_price = $service_data['is_custom_price'];
391 $service->is_custom_duration = $service_data['is_custom_duration'];
392 $this->services[] = $service;
393 }
394 }
395 return $this;
396 }
397
398 public function filter_allowed_records(): OsModel {
399 if ( ! OsRolesHelper::are_all_records_allowed( 'agent' ) ) {
400 $this->filter_where_conditions( [ 'id' => OsRolesHelper::get_allowed_records( 'agent' ) ] );
401 }
402 return $this;
403 }
404
405 public function get_services() {
406 if ( ! isset( $this->services ) ) {
407 $query = 'SELECT * FROM ' . $this->services_agents_table_name . ' WHERE agent_id = %d GROUP BY service_id';
408 $query_args = array( $this->id );
409 $services_rows = $this->get_query_results( $query, $query_args );
410
411 $this->services = array();
412
413 if ( $services_rows ) {
414 foreach ( $services_rows as $service_row ) {
415 $service = new OsServiceModel( $service_row->service_id );
416 $service->is_custom_hours = $service_row->is_custom_hours;
417 $service->is_custom_price = $service_row->is_custom_price;
418 $service->is_custom_duration = $service_row->is_custom_duration;
419 $this->services[] = $service;
420 }
421 }
422 }
423 return $this->services;
424 }
425
426
427 protected function before_create() {
428 if ( empty( $this->password ) ) {
429 $this->password = wp_hash_password( bin2hex( openssl_random_pseudo_bytes( 8 ) ) );
430 }
431 }
432
433 protected function before_save() {
434 }
435
436 public function get_full_name() {
437 $full_name = trim( join( ' ', array( $this->first_name, $this->last_name ) ) );
438 return empty( $full_name ) ? __( 'Agent', 'latepoint' ) : $full_name;
439 }
440
441 protected function get_name_for_front() {
442 if ( isset( $this->display_name ) && ! empty( $this->display_name ) ) {
443 return $this->display_name;
444 } else {
445 return $this->get_full_name();
446 }
447 }
448
449 public function get_avatar_url() {
450 return OsAgentHelper::get_avatar_url( $this );
451 }
452
453 public function get_avatar_image() {
454 return '<img src="' . $this->get_avatar_url() . '"/>';
455 }
456
457 public function get_bio_image_url() {
458 return OsAgentHelper::get_bio_image_url( $this );
459 }
460
461 public function get_bio_image() {
462 return '<img src="' . $this->get_bio_image_url() . '"/>';
463 }
464
465
466 public function delete( $id = false ) {
467 if ( ! $id && isset( $this->id ) ) {
468 $id = $this->id;
469 }
470 if ( $id && $this->db->delete( $this->table_name, array( 'id' => $id ), array( '%d' ) ) ) {
471 $this->db->delete( LATEPOINT_TABLE_AGENTS_SERVICES, array( 'agent_id' => $id ), array( '%d' ) );
472 $this->db->delete( LATEPOINT_TABLE_WORK_PERIODS, array( 'agent_id' => $id ), array( '%d' ) );
473 $this->db->delete( LATEPOINT_TABLE_AGENT_META, array( 'object_id' => $id ), array( '%d' ) );
474 return true;
475 } else {
476 return false;
477 }
478 }
479 }
480