PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / employee.php
vikappointments / admin / models Last commit date
apiban.php 4 years ago apilog.php 2 years ago apiplugin.php 4 years ago apiuser.php 2 years ago apiuseroptions.php 2 years ago backup.php 4 months ago caldays.php 1 month ago calendar.php 1 month ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 4 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 4 months ago conversion.php 4 years ago country.php 2 years ago coupon.php 2 years ago couponemployee.php 4 years ago coupongroup.php 2 years ago couponservice.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 4 months ago customf.php 2 years ago customfservice.php 4 years ago customizer.php 4 years ago empgroup.php 2 years ago employee.php 2 years ago empsettings.php 4 years ago file.php 4 years ago findreservation.php 1 month ago group.php 2 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langempgroup.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 2 years ago langoptiongroup.php 4 years ago langoptionvar.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 2 years ago langtaxrule.php 4 years ago location.php 2 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 2 years ago multiorder.php 1 month ago option.php 1 year ago optiongroup.php 2 years ago optionvar.php 1 year ago orderstatus.php 2 years ago package.php 2 years ago packageservice.php 4 years ago packgroup.php 2 years ago packorder.php 2 years ago packorderitem.php 1 month ago payment.php 2 years ago rate.php 2 years ago reportsemp.php 4 months ago reportsser.php 4 months ago reservation.php 1 month ago resoptassoc.php 2 years ago restriction.php 2 years ago review.php 3 years ago serempassoc.php 1 year ago seroptassoc.php 4 years ago serrateassoc.php 4 years ago serrestrassoc.php 4 years ago service.php 2 years ago state.php 2 years ago statswidget.php 2 years ago statuscode.php 2 years ago subscription.php 1 month ago subscrorder.php 1 month ago tag.php 4 years ago tax.php 2 years ago taxrule.php 2 years ago updateprogram.php 4 years ago usernote.php 2 years ago waitinglist.php 1 month ago webhook.php 1 year ago worktime.php 1 month ago
employee.php
547 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 VAPLoader::import('libraries.mvc.model');
15
16 /**
17 * VikAppointments employee model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelEmployee extends JModelVAP
22 {
23 /**
24 * A list of cached timezones.
25 *
26 * @var array
27 */
28 protected static $timezones = array();
29
30 /**
31 * Extend duplicate implementation to clone any related records
32 * stored within a separated table.
33 *
34 * @param mixed $ids Either the record ID or a list of records.
35 * @param mixed $src Specifies some values to be used while duplicating.
36 * @param array $ignore A list of columns to skip.
37 *
38 * @return mixed The ID of the records on success, false otherwise.
39 */
40 public function duplicate($ids, $src = array(), $ignore = array())
41 {
42 $new_ids = array();
43
44 // do not copy sync key
45 $ignore[] = 'synckey';
46
47 $dbo = JFactory::getDbo();
48
49 // get employee translation model
50 $langModel = JModelVAP::getInstance('langemployee');
51 // get service assoc model
52 $assocModel = JModelVAP::getInstance('serempassoc');
53 // get working times model
54 $wdModel = JModelVAP::getInstance('worktime');
55
56 foreach ($ids as $id_employee)
57 {
58 // start by duplicating the whole record
59 $new_id = parent::duplicate($id_employee, $src, $ignore);
60
61 if ($new_id)
62 {
63 $new_id = array_shift($new_id);
64
65 // register copied
66 $new_ids[] = $new_id;
67
68 // load any assigned translation
69 $q = $dbo->getQuery(true)
70 ->select($dbo->qn('id'))
71 ->from($dbo->qn('#__vikappointments_lang_employee'))
72 ->where($dbo->qn('id_employee') . ' = ' . (int) $id_employee);
73
74 $dbo->setQuery($q);
75
76 if ($duplicate = $dbo->loadColumn())
77 {
78 $lang_data = array();
79 $lang_data['id_employee'] = $new_id;
80
81 // duplicate languages by using the new employee ID
82 $langModel->duplicate($duplicate, $lang_data);
83 }
84
85 // load any assigned service
86 $q = $dbo->getQuery(true)
87 ->select($dbo->qn('id'))
88 ->from($dbo->qn('#__vikappointments_ser_emp_assoc'))
89 ->where($dbo->qn('id_employee') . ' = ' . (int) $id_employee);
90
91 $dbo->setQuery($q);
92
93 if ($duplicate = $dbo->loadColumn())
94 {
95 $assoc_data = array();
96 $assoc_data['id_employee'] = $new_id;
97
98 // duplicate services by using the new employee ID
99 $assocModel->duplicate($duplicate, $assoc_data);
100 }
101
102 // load any assigned working day (generic)
103 $q = $dbo->getQuery(true)
104 ->select($dbo->qn('id'))
105 ->from($dbo->qn('#__vikappointments_emp_worktime'))
106 ->where($dbo->qn('id_employee') . ' = ' . (int) $id_employee)
107 ->where($dbo->qn('id_service') . ' <= 0');
108
109 $dbo->setQuery($q);
110
111 if ($duplicate = $dbo->loadColumn())
112 {
113 $wd_data = array();
114 $wd_data['id_employee'] = $new_id;
115 // unset ID location, since it might be not
116 // accessible by the employee
117 $wd_data['id_location'] = -1;
118
119 // duplicate services by using the new employee ID
120 $wdModel->duplicate($duplicate, $wd_data);
121 }
122 }
123 }
124
125 return $new_ids;
126 }
127
128 /**
129 * Extend delete implementation to delete any related records
130 * stored within a separated table.
131 *
132 * @param mixed $ids Either the record ID or a list of records.
133 *
134 * @return boolean True on success, false otherwise.
135 */
136 public function delete($ids)
137 {
138 // only int values are accepted
139 $ids = array_map('intval', (array) $ids);
140
141 // invoke parent first
142 if (!parent::delete($ids))
143 {
144 // nothing to delete
145 return false;
146 }
147
148 $dbo = JFactory::getDbo();
149
150 // load any assigned translation
151 $q = $dbo->getQuery(true)
152 ->select($dbo->qn('id'))
153 ->from($dbo->qn('#__vikappointments_lang_employee'))
154 ->where($dbo->qn('id_employee') . ' IN (' . implode(',', $ids) . ')' );
155
156 $dbo->setQuery($q);
157
158 if ($lang_ids = $dbo->loadColumn())
159 {
160 // get translation model
161 $model = JModelVAP::getInstance('langemployee');
162 // delete assigned translations
163 $model->delete($lang_ids);
164 }
165
166 // load any assigned services
167 $q = $dbo->getQuery(true)
168 ->select($dbo->qn('id'))
169 ->from($dbo->qn('#__vikappointments_ser_emp_assoc'))
170 ->where($dbo->qn('id_employee') . ' IN (' . implode(',', $ids) . ')' );
171
172 $dbo->setQuery($q);
173
174 if ($assoc_ids = $dbo->loadColumn())
175 {
176 // get assoc model
177 $model = JModelVAP::getInstance('serempassoc');
178 // delete assigned services
179 $model->delete($assoc_ids);
180 }
181
182 // load any assigned working times
183 $q = $dbo->getQuery(true)
184 ->select($dbo->qn('id'))
185 ->from($dbo->qn('#__vikappointments_emp_worktime'))
186 ->where($dbo->qn('id_employee') . ' IN (' . implode(',', $ids) . ')' );
187
188 $dbo->setQuery($q);
189
190 if ($worktime_ids = $dbo->loadColumn())
191 {
192 // get working times model
193 $model = JModelVAP::getInstance('worktime');
194 // delete assigned working times
195 $model->delete($worktime_ids);
196 }
197
198 // load any assigned locations
199 $q = $dbo->getQuery(true)
200 ->select($dbo->qn('id'))
201 ->from($dbo->qn('#__vikappointments_employee_location'))
202 ->where($dbo->qn('id_employee') . ' IN (' . implode(',', $ids) . ')' );
203
204 $dbo->setQuery($q);
205
206 if ($location_ids = $dbo->loadColumn())
207 {
208 // get locations model
209 $model = JModelVAP::getInstance('location');
210 // delete assigned locations
211 $model->delete($location_ids);
212 }
213
214 // load any employee-coupon relation
215 $q = $dbo->getQuery(true)
216 ->select($dbo->qn('id'))
217 ->from($dbo->qn('#__vikappointments_coupon_employee_assoc'))
218 ->where($dbo->qn('id_employee') . ' IN (' . implode(',', $ids) . ')' );
219
220 $dbo->setQuery($q);
221
222 if ($assoc_ids = $dbo->loadColumn())
223 {
224 // get model
225 $model = JModelVAP::getInstance('couponemployee');
226 // delete relations
227 $model->delete($assoc_ids);
228 }
229
230 // load any employee-payment relation
231 $q = $dbo->getQuery(true)
232 ->select($dbo->qn('id'))
233 ->from($dbo->qn('#__vikappointments_gpayments'))
234 ->where($dbo->qn('id_employee') . ' IN (' . implode(',', $ids) . ')' );
235
236 $dbo->setQuery($q);
237
238 if ($assoc_ids = $dbo->loadColumn())
239 {
240 // get model
241 $model = JModelVAP::getInstance('payment');
242 // delete relations
243 $model->delete($assoc_ids);
244 }
245
246 // load any employee-custom field relation
247 $q = $dbo->getQuery(true)
248 ->select($dbo->qn('id'))
249 ->from($dbo->qn('#__vikappointments_custfields'))
250 ->where($dbo->qn('id_employee') . ' IN (' . implode(',', $ids) . ')' );
251
252 $dbo->setQuery($q);
253
254 if ($assoc_ids = $dbo->loadColumn())
255 {
256 // get model
257 $model = JModelVAP::getInstance('customf');
258 // delete relations
259 $model->delete($assoc_ids);
260 }
261
262 // load any employee settings
263 $q = $dbo->getQuery(true)
264 ->select($dbo->qn('id'))
265 ->from($dbo->qn('#__vikappointments_employee_settings'))
266 ->where($dbo->qn('id_employee') . ' IN (' . implode(',', $ids) . ')' );
267
268 $dbo->setQuery($q);
269
270 if ($assoc_ids = $dbo->loadColumn())
271 {
272 // get model
273 $model = JModelVAP::getInstance('empsettings');
274 // delete relations
275 $model->delete($assoc_ids);
276 }
277
278 return true;
279 }
280
281 /**
282 * Creates a new column within the employees database table.
283 *
284 * @param string $name The field name.
285 * @param string $type The field type.
286 *
287 * @return boolean True on success, false otherwise.
288 *
289 * @throws Exception
290 */
291 public function createColumn($name, $type = 'text')
292 {
293 $dbo = JFactory::getDbo();
294
295 /**
296 * Use a text type instead of a varchar for textarea fields,
297 * which might be used as editors.
298 *
299 * @since 1.6.3
300 * @since 1.7.4 Accept up to 1024 chars for files custom fields.
301 */
302 switch ($type)
303 {
304 case 'textarea':
305 $type = 'text';
306 break;
307
308 case 'file':
309 $type = 'varchar(1024)';
310 break;
311
312 default:
313 $type = 'varchar(128)';
314 }
315
316 $q = "ALTER TABLE `#__vikappointments_employee` ADD COLUMN `field_{$name}` $type DEFAULT NULL";
317
318 try
319 {
320 // attempt to alter the table
321 $dbo->setQuery($q);
322 $dbo->execute();
323 }
324 catch (Exception $e)
325 {
326 /**
327 * An error occurred, register it
328 *
329 * @since 1.6.2
330 */
331 $this->setError(JText::sprintf('VAPCFFORMNAMEALTER_ERROR', $q));
332
333 return false;
334 }
335
336 return true;
337 }
338
339 /**
340 * Removes an existing column from the employees database table.
341 *
342 * @param string $name The name of the column to remove.
343 *
344 * @return void
345 */
346 public function dropColumn($name)
347 {
348 $dbo = JFactory::getDbo();
349
350 $q = "ALTER TABLE `#__vikappointments_employee` DROP COLUMN `field_{$name}`";
351
352 try
353 {
354 // attempt to alter the table
355 $dbo->setQuery($q);
356 $dbo->execute();
357 }
358 catch (Exception $e)
359 {
360 /**
361 * Probably the column wasn't properly installed.
362 * Catch error to avoid breaking the flow.
363 *
364 * @since 1.6.2
365 */
366 }
367 }
368
369 /**
370 * Checks whether the employees database table already owns the
371 * specified column.
372 *
373 * @param string $name The name of the column to check.
374 *
375 * @return boolean True if existing, false otherwise.
376 */
377 public function hasColumn($name)
378 {
379 // get database table
380 $table = $this->getTable();
381
382 // check whether the table object owns the given
383 // column as property
384 return property_exists($table, 'field_' . $name);
385 }
386
387 /**
388 * Returns a list of services assigned to the specified employee.
389 *
390 * @param integer $id The employee ID.
391 * @param boolean $strict True to return all the services.
392 * False to obtain only the services listed in the front-end.
393 *
394 * @return array A list of services.
395 */
396 public function getServices($id, $strict = false)
397 {
398 $dbo = JFactory::getDbo();
399
400 $services = array();
401
402 $q = $dbo->getQuery(true)
403 ->select('a.*')
404 ->select($dbo->qn('s.id', 'id_service'))
405 ->select($dbo->qn('s.name'))
406 ->select($dbo->qn('s.color'))
407 ->from($dbo->qn('#__vikappointments_service', 's'))
408 ->leftjoin($dbo->qn('#__vikappointments_group', 'g') . ' ON ' . $dbo->qn('g.id') . ' = ' . $dbo->qn('s.id_group'))
409 ->leftjoin($dbo->qn('#__vikappointments_ser_emp_assoc', 'a') . ' ON ' . $dbo->qn('a.id_service') . ' = ' . $dbo->qn('s.id'))
410 ->where($dbo->qn('a.id_employee') . ' = ' . (int) $id);
411
412 if ($strict)
413 {
414 // do not show unpublished services
415 $q->where($dbo->qn('s.published') . ' = 1');
416 }
417 else
418 {
419 // show published services first
420 $q->order($dbo->qn('s.published') . ' DESC');
421 }
422
423 $q->order($dbo->qn('g.ordering') . ' ASC');
424 $q->order($dbo->qn('s.ordering') . ' ASC');
425
426 $dbo->setQuery($q);
427
428 foreach ($dbo->loadObjectList() as $service)
429 {
430 // use a different name for assoc ID
431 $service->id_assoc = $service->id;
432 // switch service ID to standard name
433 $service->id = $service->id_service;
434 unset($service->id_service);
435
436 $services[] = $service;
437 }
438
439 return $services;
440 }
441
442 /**
443 * Returns the timezone of the given employee.
444 *
445 * @param integer $id The employee ID.
446 *
447 * @return string The employee timezone.
448 */
449 public function getTimezone($id)
450 {
451 if (!isset(static::$timezones[$id]))
452 {
453 // use global timezone by default
454 static::$timezones[$id] = JFactory::getApplication()->get('offset', 'UTC');
455
456 // go ahead only in case an employee has been specified
457 if ($id)
458 {
459 $dbo = JFactory::getDbo();
460
461 $q = $dbo->getQuery(true)
462 ->select($dbo->qn('timezone'))
463 ->from($dbo->qn('#__vikappointments_employee'))
464 ->where($dbo->qn('id') . ' = ' . (int) $id);
465
466 $dbo->setQuery($q, 0, 1);
467 $tz = $dbo->loadResult();
468
469 if ($tz)
470 {
471 // use employee timezone
472 static::$timezones[$id] = $tz;
473 }
474 }
475 }
476
477 return static::$timezones[$id];
478 }
479
480 /**
481 * Helper method used to check whether the specified employee
482 * is active and can be accessed in the front-end.
483 *
484 * @param mixed $employee Either an object or and ID.
485 *
486 * @return boolean True if active, false otherwise.
487 */
488 public function isVisible($employee)
489 {
490 if (is_numeric($employee))
491 {
492 // get employee details
493 $employee = $this->getItem((int) $employee);
494 }
495 else
496 {
497 // treat as object
498 $employee = (object) $employee;
499 }
500
501 if (!$employee)
502 {
503 // missing employee
504 return false;
505 }
506
507 if (!$employee->listable)
508 {
509 // employee not visible in the front-end
510 return false;
511 }
512
513 if ($employee->active_to == 0)
514 {
515 // the employee activation is pending
516 return false;
517 }
518
519 if ($employee->active_to == 1 && $employee->active_to_date < JFactory::getDate())
520 {
521 // the employee was active but the license expired
522 return false;
523 }
524
525 /**
526 * This event can be used to apply additional conditions while checking whether
527 * the specified employee is listable or not. When this event is triggered, the
528 * system already validated the standard conditions and the employee is going
529 * to be listed into the website.
530 *
531 * @param object $employee The employee to check.
532 *
533 * @return boolean Return false to hide the employee.
534 *
535 * @since 1.7
536 */
537 if (VAPFactory::getEventDispatcher()->false('onCheckEmployeeVisibility', array($employee)))
538 {
539 // a plugin decided to hide the employee
540 return false;
541 }
542
543 // at this point, we do not need to look for a LIFETIME status...
544 return true;
545 }
546 }
547