PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / controllers / employee.php
vikappointments / admin / controllers Last commit date
analytics.php 4 years ago apiban.php 4 years ago apilog.php 4 years ago apiplugin.php 4 years ago apiuser.php 4 years ago backup.php 4 years ago calendar.php 4 years ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 2 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 1 month ago conversion.php 1 year ago country.php 4 years ago coupon.php 4 years ago coupongroup.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 5 months ago customf.php 1 year ago dashboard.php 4 years ago emplocwdays.php 4 years ago employee.php 1 year ago emprates.php 4 years ago export.php 4 years ago exportres.php 4 years ago file.php 5 months ago findreservation.php 1 month ago group.php 4 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 4 years ago langoptiongroup.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 4 years ago location.php 4 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 4 years ago multiorder.php 4 years ago option.php 5 months ago optiongroup.php 4 years ago package.php 2 years ago packgroup.php 4 years ago packorder.php 1 year ago payment.php 4 years ago rate.php 4 years ago reportsemp.php 4 years ago reportsser.php 4 years ago reservation.php 1 month ago restriction.php 4 years ago review.php 4 years ago service.php 1 year ago serworkday.php 5 months ago state.php 4 years ago statuscode.php 4 years ago subscription.php 4 years ago subscrorder.php 4 years ago tag.php 4 years ago tax.php 4 years ago usernote.php 4 years ago waitinglist.php 4 years ago webhook.php 4 years ago wizard.php 1 year ago
employee.php
572 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.controllers.admin');
15
16 /**
17 * VikAppointments employee controller.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsControllerEmployee extends VAPControllerAdmin
22 {
23 /**
24 * Task used to access the creation page of a new record.
25 *
26 * @return boolean
27 */
28 public function add()
29 {
30 $app = JFactory::getApplication();
31 $user = JFactory::getUser();
32
33 $data = array();
34 $id_group = $app->input->getInt('id_group', 0);
35
36 if ($id_group > 0)
37 {
38 $data['id_group'] = $id_group;
39 }
40
41 // unset user state for being recovered again
42 $app->setUserState('vap.employee.data', $data);
43
44 // check user permissions
45 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.employees', 'com_vikappointments'))
46 {
47 // back to main list, not authorised to create records
48 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
49 $this->cancel();
50
51 return false;
52 }
53
54 $this->setRedirect('index.php?option=com_vikappointments&view=manageemployee');
55
56 return true;
57 }
58
59 /**
60 * Task used to access the management page of an existing record.
61 *
62 * @return boolean
63 */
64 public function edit()
65 {
66 $app = JFactory::getApplication();
67 $user = JFactory::getUser();
68
69 // unset user state for being recovered again
70 $app->setUserState('vap.employee.data', array());
71
72 // check user permissions
73 if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.employees', 'com_vikappointments'))
74 {
75 // back to main list, not authorised to edit records
76 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
77 $this->cancel();
78
79 return false;
80 }
81
82 $cid = $app->input->getUint('cid', array(0));
83
84 $this->setRedirect('index.php?option=com_vikappointments&view=manageemployee&cid[]=' . $cid[0]);
85
86 return true;
87 }
88
89 /**
90 * Task used to save the record data set in the request.
91 * After saving, the user is redirected to the main list.
92 *
93 * @return void
94 */
95 public function saveclose()
96 {
97 if ($this->save())
98 {
99 $this->cancel();
100 }
101 }
102
103 /**
104 * Task used to save the record data set in the request.
105 * After saving, the user is redirected to the creation
106 * page of a new record.
107 *
108 * @return void
109 */
110 public function savenew()
111 {
112 if ($this->save())
113 {
114 $this->setRedirect('index.php?option=com_vikappointments&task=employee.add');
115 }
116 }
117
118 /**
119 * Task used to save the record data as a copy of the current item.
120 * After saving, the user is redirected to the management
121 * page of the record that has been saved.
122 *
123 * @return void
124 */
125 public function savecopy()
126 {
127 $this->save(true);
128 }
129
130 /**
131 * Task used to save the record data set in the request.
132 * After saving, the user is redirected to the management
133 * page of the record that has been saved.
134 *
135 * @param boolean $copy True to save the record as a copy.
136 *
137 * @return boolean
138 */
139 public function save($copy = false)
140 {
141 $app = JFactory::getApplication();
142 $input = $app->input;
143 $user = JFactory::getUser();
144
145 /**
146 * Added token validation.
147 *
148 * @since 1.7
149 */
150 if (!JSession::checkToken())
151 {
152 // back to main list, missing CSRF-proof token
153 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
154 $this->cancel();
155
156 return false;
157 }
158
159 $args = array();
160 $args['firstname'] = $input->getString('firstname');
161 $args['lastname'] = $input->getString('lastname');
162 $args['nickname'] = $input->getString('nickname');
163 $args['alias'] = $input->getString('alias');
164 $args['email'] = $input->getString('email');
165 $args['notify'] = $input->getUint('notify', 0);
166 $args['showphone'] = $input->getUint('showphone', 0);
167 $args['showemail'] = $input->getUint('showemail', 0);
168 $args['quick_contact'] = $input->getUint('quick_contact', 0);
169 $args['listable'] = $input->getUint('listable', 0);
170 $args['phone'] = $input->getString('phone');
171 $args['note'] = JComponentHelper::filterText($input->getRaw('note'));
172 $args['image'] = $input->getString('image');
173 $args['synckey'] = $input->getString('synckey');
174 $args['ical_url'] = $input->getString('ical_url');
175 $args['id_group'] = $input->getUint('id_group', 0);
176 $args['jid'] = $input->getUint('jid', 0);
177 $args['active_to'] = $input->getString('active_to', '');
178 $args['timezone'] = $input->getString('timezone');
179 $args['id'] = $input->getUint('id', 0);
180
181 switch ($input->getString('active_to_type', ''))
182 {
183 case 'lifetime':
184 $args['active_to'] = -1;
185 break;
186
187 case 'pending':
188 $args['active_to'] = 0;
189 break;
190
191 default:
192 /**
193 * Convert timestamp from local timezone to UTC.
194 *
195 * @since 1.7
196 */
197 $args['active_to_date'] = VAPDateHelper::getSqlDateLocale($args['active_to']);
198 $args['active_to'] = 1;
199 }
200
201 if ($copy)
202 {
203 // unset ID to create a copy
204 $args['id'] = 0;
205 }
206
207 $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create');
208
209 // check user permissions
210 if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.employees', 'com_vikappointments'))
211 {
212 // back to main list, not authorised to create/edit records
213 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
214 $this->cancel();
215
216 return false;
217 }
218
219 /**
220 * Try to auto-create a new group before saving the employee.
221 *
222 * @since 1.7
223 */
224 if ($args['id_group'] == 0 && ($group_name = $input->getString('group_name')))
225 {
226 // make sure the user is authorised
227 if ($user->authorise('core.create', 'com_vikappointments') && $user->authorise('core.access.groups', 'com_vikappointments'))
228 {
229 $group = $this->getModel('empgroup');
230
231 // attempt to save group
232 $id_group = $group->save(array('name' => $group_name));
233
234 if ($id_group)
235 {
236 // overwrite the group ID
237 $args['id_group'] = $id_group;
238 }
239 }
240 }
241
242 // import custom fields requestor and loader (as dependency)
243 VAPLoader::import('libraries.customfields.requestor');
244
245 // get relevant custom fields only
246 $_cf = VAPCustomFieldsLoader::getInstance()
247 ->employees()
248 ->noRequiredCheckbox()
249 ->fetch();
250
251 // load custom fields from request
252 $cust_req = VAPCustomFieldsRequestor::loadForm($_cf, $tmp, $strict = false);
253
254 if (!empty($tmp['uploads']))
255 {
256 // inject uploads within the custom fields array
257 $cust_req = array_merge($cust_req, $tmp['uploads']);
258 }
259
260 // inject custom fields within the employee table
261 foreach ($cust_req as $k => $v)
262 {
263 $args['field_' . $k] = $v;
264 }
265
266 // get employee model
267 $employee = $this->getModel();
268
269 // try to save arguments
270 $id = $employee->save($args);
271
272 if (!$id)
273 {
274 // get string error
275 $error = $employee->getError(null, true);
276
277 // display error message
278 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
279
280 $url = 'index.php?option=com_vikappointments&view=manageemployee';
281
282 if ($args['id'])
283 {
284 $url .= '&cid[]=' . $args['id'];
285 }
286
287 // redirect to new/edit page
288 $this->setRedirect($url);
289
290 return false;
291 }
292
293 // get working days model
294 $wdModel = $this->getModel('worktime');
295
296 // delete working days only if we are not saving as copy
297 if (!$copy)
298 {
299 // load deleted working times
300 $wd_deleted = $input->get('wd_deleted', array(), 'uint');
301
302 // delete working times before save the other ones
303 $wdModel->delete($wd_deleted);
304 }
305
306 // load working times details
307 $wd_json = $input->get('wd_json', array(), 'array');
308
309 foreach ($wd_json as $json)
310 {
311 // obtain the list of grouped working times, if any
312 $list = (array) json_decode($json, true);
313
314 foreach ($list as $src)
315 {
316 // always specify the employee ID
317 $src['id_employee'] = $id;
318
319 if ($copy)
320 {
321 // unset ID to force creation
322 $src['id'] = 0;
323 }
324
325 // attempt to save the working time
326 $wdModel->save($src);
327 }
328 }
329
330 // display generic successful message
331 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
332
333 // redirect to edit page
334 $this->setRedirect('index.php?option=com_vikappointments&task=employee.edit&cid[]=' . $id);
335
336 return true;
337 }
338
339 /**
340 * Deletes a list of records set in the request.
341 *
342 * @return boolean
343 */
344 public function delete()
345 {
346 $app = JFactory::getApplication();
347 $user = JFactory::getUser();
348
349 /**
350 * Added token validation.
351 * Both GET and POST are supported.
352 *
353 * @since 1.7
354 */
355 if (!JSession::checkToken() && !JSession::checkToken('get'))
356 {
357 // back to main list, missing CSRF-proof token
358 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
359 $this->cancel();
360
361 return false;
362 }
363
364 $cid = $app->input->get('cid', array(), 'uint');
365
366 // check user permissions
367 if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.employees', 'com_vikappointments'))
368 {
369 // back to main list, not authorised to delete records
370 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
371 $this->cancel();
372
373 return false;
374 }
375
376 // delete selected records
377 $this->getModel()->delete($cid);
378
379 // back to main list
380 $this->cancel();
381
382 return true;
383 }
384
385 /**
386 * Duplicates a list of records set in the request.
387 *
388 * @return boolean
389 */
390 public function duplicate()
391 {
392 $app = JFactory::getApplication();
393 $user = JFactory::getUser();
394
395 /**
396 * Added token validation.
397 * Both GET and POST are supported.
398 *
399 * @since 1.7
400 */
401 if (!JSession::checkToken() && !JSession::checkToken('get'))
402 {
403 // back to main list, missing CSRF-proof token
404 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
405 $this->cancel();
406
407 return false;
408 }
409
410 $cid = $app->input->get('cid', array(), 'uint');
411
412 // check user permissions
413 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.employees', 'com_vikappointments'))
414 {
415 // back to main list, not authorised to delete records
416 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
417 $this->cancel();
418
419 return false;
420 }
421
422 // duplicate selected records
423 $result = $this->getModel()->duplicate($cid);
424
425 /**
426 * @todo should we display how many records have been created?
427 */
428
429 // back to main list
430 $this->cancel();
431
432 return true;
433 }
434
435 /**
436 * Toggles the listable status of an employee.
437 *
438 * @return boolean
439 */
440 public function listable()
441 {
442 $app = JFactory::getApplication();
443 $user = JFactory::getUser();
444
445 /**
446 * Added token validation.
447 * Both GET and POST are supported.
448 *
449 * @since 1.7
450 */
451 if (!JSession::checkToken() && !JSession::checkToken('get'))
452 {
453 // back to main list, missing CSRF-proof token
454 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
455 $this->cancel();
456
457 return false;
458 }
459
460 $cid = $app->input->get('cid', array(), 'uint');
461 $task = $app->input->get('task', null);
462
463 $state = $app->input->get('state', 0, 'uint');
464
465 // check user permissions
466 if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.employees', 'com_vikappointments'))
467 {
468 // back to main list, not authorised to edit records
469 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
470 $this->cancel();
471
472 return false;
473 }
474
475 // change state of selected records
476 $this->getModel()->publish($cid, $state, 'listable');
477
478 // back to main list
479 $this->cancel();
480
481 return true;
482 }
483
484 /**
485 * Redirects the users to the main records list.
486 *
487 * @return void
488 */
489 public function cancel()
490 {
491 $input = JFactory::getApplication()->input;
492
493 // build URL by using a specific return view (if specified)
494 $url = 'index.php?option=com_vikappointments&view=' . $input->get('return', 'employees');
495
496 // look for tmpl in query string
497 if ($tmpl = $input->get('tmpl'))
498 {
499 // preserve given tmpl
500 $url .= '&tmpl=' . $tmpl;
501 }
502
503 $this->setRedirect($url);
504 }
505
506 /**
507 * AJAX end-point used to obtain all the services assigned to the given employee.
508 * The task expects the following parameters to be set in request.
509 *
510 * @param integer id_emp The employee ID.
511 *
512 * @return void
513 */
514 public function servicesajax()
515 {
516 $input = JFactory::getApplication()->input;
517
518 /**
519 * Added token validation.
520 *
521 * @since 1.7
522 */
523 if (!JSession::checkToken())
524 {
525 // missing CSRF-proof token
526 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
527 }
528
529 $id_emp = $input->getUint('id_emp', 0);
530
531 // get all assigned services
532 $services = $this->getModel()->getServices($id_emp);
533
534 // send resulting object to caller
535 $this->sendJSON($services);
536 }
537
538 /**
539 * AJAX end-point used to import the working times from an uploaded file.
540 *
541 * @return void
542 */
543 public function importworkdays()
544 {
545 VAPLoader::import('libraries.worktime.import.manager');
546
547 $input = JFactory::getApplication()->input;
548
549 $options = [];
550
551 if ($layout = $input->get('layout'))
552 {
553 // use the requested layout
554 $options['layout'] = $layout;
555 }
556
557 try
558 {
559 // import file
560 $results = VAPWorktimeImportManager::process('file', $options);
561 }
562 catch (Exception $e)
563 {
564 // an error occurred
565 UIErrorFactory::raiseError($e->getCode(), $e->getMessage());
566 }
567
568 // send result to caller
569 $this->sendJSON($results);
570 }
571 }
572