PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.4
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.4
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 11 months ago agent_meta_model.php 1 year ago agent_model.php 1 year ago booking_meta_model.php 1 year ago booking_model.php 11 months ago bundle_meta_model.php 11 months ago bundle_model.php 11 months ago cart_item_model.php 1 year ago cart_meta_model.php 1 year ago cart_model.php 1 year ago connector_model.php 1 year ago customer_meta_model.php 1 year ago customer_model.php 9 months ago invoice_model.php 1 year ago join_bundles_services_model.php 1 year ago location_category_model.php 9 months ago location_model.php 1 year ago meta_model.php 1 year ago model.php 9 months ago off_period_model.php 1 year ago order_intent_meta_model.php 1 year ago order_intent_model.php 9 months ago order_item_model.php 1 year ago order_meta_model.php 1 year ago order_model.php 11 months ago otp_model.php 9 months ago payment_request_model.php 1 year ago process_job_model.php 1 year ago process_model.php 1 year ago recurrence_model.php 1 year ago service_category_model.php 9 months ago service_meta_model.php 1 year ago service_model.php 9 months ago session_model.php 1 year ago settings_model.php 1 year ago step_settings_model.php 1 year ago transaction_intent_model.php 1 year ago transaction_model.php 1 year ago transaction_refund_model.php 1 year ago work_period_model.php 1 year ago
agent_model.php
415 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 if ($id) {
44 $this->load_by_id($id);
45 }
46 }
47
48
49 public function get_initials(){
50 return mb_substr($this->first_name,0,1).mb_substr($this->last_name,0,1);
51 }
52
53 public function get_edit_link(){
54 return OsRouterHelper::build_link(['agents', 'edit_form'], ['id' => $this->id] );
55 }
56
57 public function generate_data_vars(): array {
58 return [
59 'id' => $this->id,
60 'full_name' => $this->full_name,
61 'email' => $this->email,
62 'phone' => $this->phone
63 ];
64 }
65
66 protected function params_to_save($role = 'admin') {
67 $params_to_save = array('id',
68 'first_name',
69 'last_name',
70 'display_name',
71 'email',
72 'phone',
73 'password',
74 'wp_user_id',
75 'bio_image_id',
76 'title',
77 'bio',
78 'features',
79 'status',
80 'extra_emails',
81 'extra_phones',
82 'avatar_image_id',
83 'custom_hours');
84 return $params_to_save;
85 }
86
87 protected function allowed_params($role = 'admin') {
88 $allowed_params = array('id',
89 'first_name',
90 'last_name',
91 'display_name',
92 'email',
93 'phone',
94 'password',
95 'wp_user_id',
96 'bio_image_id',
97 'title',
98 'bio',
99 'features',
100 'extra_emails',
101 'extra_phones',
102 'status',
103 'avatar_image_id',
104 'custom_hours');
105 return $allowed_params;
106 }
107
108
109 protected function properties_to_validate() {
110 $validations = array(
111 'email' => array('presence'),
112 'wp_user_id' => array('uniqueness'),
113 );
114 return $validations;
115 }
116
117
118 public function count_number_of_connected_locations($service_id = false) {
119 if ($this->is_new_record()) return 0;
120 $args = ['agent_id' => $this->id];
121 if ($service_id) $args['service_id'] = $service_id;
122 return OsConnectorHelper::count_connections($args, 'location_id');
123 }
124
125 public function delete_meta_by_key($meta_key) {
126 if ($this->is_new_record()) return false;
127
128 $meta = new OsAgentMetaModel();
129 return $meta->delete_by_key($meta_key, $this->id);
130 }
131
132 public function get_meta_by_key($meta_key, $default = false) {
133 if ($this->is_new_record()) return $default;
134
135 $meta = new OsAgentMetaModel();
136 return $meta->get_by_key($meta_key, $this->id, $default);
137 }
138
139 public function save_meta_by_key($meta_key, $meta_value) {
140 if ($this->is_new_record()) return false;
141
142 $meta = new OsAgentMetaModel();
143 return $meta->save_by_key($meta_key, $meta_value, $this->id);
144 }
145
146 protected function set_defaults() {
147 if (empty($this->status)) $this->status = LATEPOINT_AGENT_STATUS_ACTIVE;
148 }
149
150 public function has_location($location_id) {
151 return OsConnectorHelper::has_connection(['agent_id' => $this->id, 'location_id' => $location_id]);
152 }
153
154
155 protected function get_total_future_bookings() {
156 $bookings = new OsBookingModel();
157 $total = $bookings->where(['agent_id' => $this->id])->should_be_in_future()->count();
158 return $total;
159 }
160
161 protected function get_total_synced_future_bookings() {
162 $bookings = new OsBookingModel();
163 $total = $bookings->where(['agent_id' => $this->id, LATEPOINT_TABLE_BOOKING_META.'.meta_key' => 'google_calendar_event_id'])
164 ->join(LATEPOINT_TABLE_BOOKING_META, ['object_id' => LATEPOINT_TABLE_BOOKINGS . '.id'])
165 ->should_be_in_future()
166 ->count();
167 return $total;
168 }
169
170 protected function get_future_bookings($limit = false) {
171 $bookings = new OsBookingModel();
172 if ($limit) {
173 $bookings = $bookings->set_limit($limit);
174 }
175 return $bookings->order_by('start_date, start_time asc')->where(['agent_id' => $this->id])->should_be_in_future()->get_results_as_models();
176 }
177
178 public function should_be_active() {
179 return $this->where(['status !=' => LATEPOINT_AGENT_STATUS_DISABLED]);
180 }
181
182 public function has_service_and_location($service_id, $location_id) {
183 if ($this->is_new_record()) return false;
184 return OsConnectorHelper::has_connection(['location_id' => $location_id, 'agent_id' => $this->id, 'service_id' => $service_id]);
185 }
186
187 public function get_features_arr() {
188 $features_arr = [];
189 if (!empty($this->features)) {
190 $features = json_decode($this->features, true);
191 if(!empty($features)){
192 foreach ($features as $feature) {
193 if ($feature['value'] && $feature['label']) $features_arr[] = $feature;
194 }
195 }
196 }
197 return $features_arr;
198 }
199
200 public function save_custom_schedule($work_periods) {
201 foreach ($work_periods as &$work_period) {
202 $work_period['agent_id'] = $this->id;
203 }
204 unset($work_period);
205 OsWorkPeriodsHelper::save_work_periods($work_periods);
206 }
207
208 public function delete_custom_schedule() {
209 $work_periods_model = new OsWorkPeriodModel();
210 $work_periods = $work_periods_model->where(array('agent_id' => $this->id, 'service_id' => 0, 'location_id' => 0, 'custom_date' => 'IS NULL'))->get_results_as_models();
211 if (is_array($work_periods)) {
212 foreach ($work_periods as $work_period) {
213 $work_period->delete();
214 }
215 }
216 }
217
218 public function has_service($service_id) {
219 foreach ($this->services as $service) {
220 if ($service->id == $service_id) return true;
221 }
222 return false;
223 }
224
225
226 public function save_services() {
227 foreach ($this->services as $service) {
228 $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)));
229 if ($service_connection_row) {
230 $update_data = array('is_custom_hours' => $service->is_custom_hours, 'is_custom_price' => $service->is_custom_price, 'is_custom_duration' => $service->is_custom_duration);
231 $this->db->update($this->services_agents_table_name, $update_data, array('id' => $service_connection_row->id));
232 } else {
233 $insert_data = array('service_id' => $service->id, 'agent_id' => $this->id, 'is_custom_hours' => $service->is_custom_hours, 'is_custom_price' => $service->is_custom_price, 'is_custom_duration' => $service->is_custom_duration);
234 if ($this->db->insert($this->services_agents_table_name, $insert_data)) {
235 $service_connection_row_id = $this->db->insert_id;
236 }
237 }
238 }
239 return true;
240 }
241
242
243 public function remove_services_by_ids($ids_to_remove = array()) {
244 if ($ids_to_remove) {
245 $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]);
246 $this->db->query($query);
247 }
248 }
249
250
251 public function get_service_ids_to_remove($new_services = array()) {
252 $current_service_ids = $this->get_current_service_ids_from_db();
253 $new_service_ids = array();
254 foreach ($new_services as $service) {
255 if ($service['connected'] == "yes") $new_service_ids[] = $service['id'];
256 }
257 $service_ids_to_remove = array_diff($current_service_ids, $new_service_ids);
258 return $service_ids_to_remove;
259 }
260
261
262 public function save_locations_and_services($services) {
263 if (!$services) return true;
264 $connections_to_save = [];
265 $connections_to_remove = [];
266 foreach ($services as $service_key => $locations) {
267 $service_id = str_replace('service_', '', $service_key);
268 foreach ($locations as $location_key => $location) {
269 $location_id = str_replace('location_', '', $location_key);
270 $connection = ['service_id' => $service_id, 'agent_id' => $this->id, 'location_id' => $location_id];
271 if ($location['connected'] == 'yes') {
272 $connections_to_save[] = $connection;
273 } else {
274 $connections_to_remove[] = $connection;
275 }
276 }
277 }
278 if (!empty($connections_to_save)) {
279 foreach ($connections_to_save as $connection_to_save) {
280 OsConnectorHelper::save_connection($connection_to_save);
281 }
282 }
283 if (!empty($connections_to_remove)) {
284 foreach ($connections_to_remove as $connection_to_remove) {
285 OsConnectorHelper::remove_connection($connection_to_remove);
286 }
287 }
288 return true;
289 }
290
291 public function set_features($features) {
292 $this->features = wp_json_encode($features);
293 }
294
295 public function get_current_service_ids_from_db() {
296 $query = $this->db->prepare('SELECT service_id FROM ' . $this->services_agents_table_name . ' WHERE agent_id = %d', $this->id);
297 $service_rows = $this->db->get_results($query);
298
299 $service_ids = array();
300
301 if ($service_rows) {
302 foreach ($service_rows as $service_row) {
303 $service_ids[] = $service_row->service_id;
304 }
305 }
306 return $service_ids;
307 }
308
309
310 public function get_current_service_ids() {
311 $service_ids = array();
312 foreach ($this->services as $service) {
313 $service_ids[] = $service->id;
314 }
315 return $service_ids;
316 }
317
318 public function set_services($service_datas) {
319 $this->services = array();
320
321 foreach ($service_datas as $service_data) {
322 if ($service_data['connected'] == "yes") {
323 $service = new OsserviceModel();
324 $service->id = $service_data['id'];
325 $service->is_custom_hours = $service_data['is_custom_hours'];
326 $service->is_custom_price = $service_data['is_custom_price'];
327 $service->is_custom_duration = $service_data['is_custom_duration'];
328 $this->services[] = $service;
329 }
330 }
331 return $this;
332 }
333
334 public function filter_allowed_records(): OsModel{
335 if(!OsRolesHelper::are_all_records_allowed('agent')){
336 $this->filter_where_conditions(['id' => OsRolesHelper::get_allowed_records('agent')]);
337 }
338 return $this;
339 }
340
341 public function get_services() {
342 if (!isset($this->services)) {
343 $query = 'SELECT * FROM ' . $this->services_agents_table_name . ' WHERE agent_id = %d GROUP BY service_id';
344 $query_args = array($this->id);
345 $services_rows = $this->get_query_results($query, $query_args);
346
347 $this->services = array();
348
349 if ($services_rows) {
350 foreach ($services_rows as $service_row) {
351 $service = new OsServiceModel($service_row->service_id);
352 $service->is_custom_hours = $service_row->is_custom_hours;
353 $service->is_custom_price = $service_row->is_custom_price;
354 $service->is_custom_duration = $service_row->is_custom_duration;
355 $this->services[] = $service;
356 }
357 }
358 }
359 return $this->services;
360 }
361
362
363 protected function before_create() {
364 if (empty($this->password)) $this->password = wp_hash_password(bin2hex(openssl_random_pseudo_bytes(8)));
365 }
366
367 protected function before_save() {
368 }
369
370 public function get_full_name() {
371 $full_name = trim(join(' ', array($this->first_name, $this->last_name)));
372 return empty($full_name) ? __('Agent', 'latepoint') : $full_name;
373 }
374
375 protected function get_name_for_front() {
376 if (isset($this->display_name) && !empty($this->display_name)) {
377 return $this->display_name;
378 } else {
379 return $this->get_full_name();
380 }
381 }
382
383 public function get_avatar_url() {
384 return OsAgentHelper::get_avatar_url($this);
385 }
386
387 public function get_avatar_image() {
388 return '<img src="' . $this->get_avatar_url() . '"/>';
389 }
390
391 public function get_bio_image_url() {
392 return OsAgentHelper::get_bio_image_url($this);
393 }
394
395 public function get_bio_image() {
396 return '<img src="' . $this->get_bio_image_url() . '"/>';
397 }
398
399
400 public function delete($id = false) {
401 if (!$id && isset($this->id)) {
402 $id = $this->id;
403 }
404 if ($id && $this->db->delete($this->table_name, array('id' => $id), array('%d'))) {
405 $this->db->delete(LATEPOINT_TABLE_AGENTS_SERVICES, array('agent_id' => $id), array('%d'));
406 $this->db->delete(LATEPOINT_TABLE_WORK_PERIODS, array('agent_id' => $id), array('%d'));
407 $this->db->delete(LATEPOINT_TABLE_AGENT_META, array('object_id' => $id), array('%d'));
408 return true;
409 } else {
410 return false;
411 }
412 }
413
414
415 }