PluginProbe
ShiftController Employee Shift Scheduling / 4.9.87
ShiftController Employee Shift Scheduling v4.9.87
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / sh4 / api / api.php

api.php in ShiftController Employee Shift Scheduling 4.9.87, at sh4/api/api.php

916 lines 25.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 #[AllowDynamicProperties]
3 class SH4_Api_Api
4 {
5 public $employeePresenter;
6 public $appQuery;
7
8 public function __construct(
9 HC3_Hooks $hooks,
10 HC3_Time $t,
11
12 HC3_IPermission $permission,
13 HC3_Translate $translate,
14
15 SH4_Employees_Query $employeesQuery,
16 SH4_Employees_Command $employeesCommand,
17 SH4_Calendars_Query $calendarsQuery,
18 SH4_Calendars_Presenter $calendarPresenter,
19 SH4_Shifts_Presenter $shiftsPresenter,
20
21 SH4_App_Query $appQuery,
22 SH4_App_Command $appCommand,
23 SH4_Employees_Presenter $employeePresenter,
24
25 SH4_Shifts_Conflicts $conflicts,
26 // SH4_Shifts_Acl $shiftsAcl,
27 SH4_Shifts_Query $shiftsQuery,
28 SH4_Shifts_Command $shiftsCommand
29 )
30 {
31 $this->t = $t;
32 $this->translate = $translate;
33 $this->permission = $hooks->wrap( $permission );
34
35 $this->employeesQuery = $hooks->wrap( $employeesQuery );
36 $this->employeesCommand = $hooks->wrap( $employeesCommand );
37 $this->calendarsQuery = $hooks->wrap( $calendarsQuery );
38 $this->calendarPresenter = $hooks->wrap( $calendarPresenter );
39 $this->employeePresenter = $hooks->wrap( $employeePresenter );
40 $this->appQuery = $hooks->wrap( $appQuery );
41 $this->appCommand = $hooks->wrap( $appCommand );
42
43 $this->shiftsPresenter = $hooks->wrap( $shiftsPresenter );
44 $this->conflicts = $hooks->wrap($conflicts);
45 $this->shiftsQuery = $hooks->wrap( $shiftsQuery );
46 $this->shiftsCommand = $hooks->wrap( $shiftsCommand );
47 // $this->shiftsAcl = $hooks->wrap( $shiftsAcl );
48 $this->self = $hooks->wrap( $this );
49
50 add_filter( 'shiftcontroller4/api/shifts/get', array($this->self, 'shiftsGet'), 10, 1 );
51 add_filter( 'shiftcontroller4/api/shifts/getbyid', array($this->self, 'shiftsGetById'), 10, 1 );
52 add_filter( 'shiftcontroller4/api/shifts/create', array($this->self, 'shiftsCreate'), 10, 1 );
53 add_filter( 'shiftcontroller4/api/shifts/deletebyid', array($this->self, 'shiftsDeleteById'), 10, 1 );
54 add_filter( 'shiftcontroller4/api/shifts/updatebyid', array($this->self, 'shiftsUpdateById'), 10, 2 );
55
56 add_filter( 'shiftcontroller4/api/employees/get', array($this->self, 'employeesGet'), 10, 1 );
57 add_filter( 'shiftcontroller4/api/employees/getbyid', array($this->self, 'employeesGetById'), 10, 1 );
58 add_filter( 'shiftcontroller4/api/employees/create', array($this->self, 'employeesCreate'), 10, 1 );
59 add_filter( 'shiftcontroller4/api/employees/getbyuserid', array($this->self, 'employeesGetByUserId'), 10, 1 );
60 add_filter( 'shiftcontroller4/api/employees/createbyuserid', array($this->self, 'employeesCreateByUserId'), 10, 1 );
61
62 add_filter( 'shiftcontroller4/api/calendars/get', array($this->self, 'calendarsGet'), 10, 1 );
63 add_filter( 'shiftcontroller4/api/employees/getbycalendarid', array($this->self, 'employeesGetByCalendarId'), 10, 1 );
64 add_filter( 'shiftcontroller4/api/employees/addtocalendar', array($this->self, 'employeesAddToCalendar'), 10, 2 );
65 add_filter( 'shiftcontroller4/api/employees/removefromcalendar', array($this->self, 'employeesRemoveFromCalendar'), 10, 2 );
66
67 add_filter( 'shiftcontroller4/api/calendars/getbyemployeeid', array($this->self, 'calendarsGetByEmployeeId'), 10, 1 );
68 }
69
70 public function toArrayEmployee( array $employeeList )
71 {
72 $out = array();
73
74 foreach( $employeeList as $employee ){
75 $thisOut = $this->employeePresenter->export( $employee, true );
76
77 $keys = array_keys( $thisOut );
78 reset( $keys );
79 foreach( $keys as $k ){
80 $thisOut[$k] = $this->translate->translate( $thisOut[$k] );
81 }
82
83 $out[] = $thisOut;
84 }
85
86 return $out;
87 }
88
89 public function toArrayCalendar( array $list )
90 {
91 $out = array();
92
93 foreach( $list as $e ){
94 $thisOut = $this->calendarPresenter->export( $e, true );
95
96 $keys = array_keys( $thisOut );
97 reset( $keys );
98 foreach( $keys as $k ){
99 $thisOut[$k] = $this->translate->translate( $thisOut[$k] );
100 }
101
102 $out[] = $thisOut;
103 }
104
105 return $out;
106 }
107
108 public function toArray( array $shifts )
109 {
110 $out = array();
111
112 foreach( $shifts as $shift ){
113 $thisOut = $this->shiftsPresenter->export( $shift, TRUE );
114
115 $keys = array_keys( $thisOut );
116 reset( $keys );
117 foreach( $keys as $k ){
118 $thisOut[$k] = $this->translate->translate( $thisOut[$k] );
119 }
120
121 $out[] = $thisOut;
122 }
123
124 return $out;
125 }
126
127 public function availableEmployeesGet( $queryParams = array() )
128 {
129 $calendarIdList = isset( $queryParams['calendar_id'] ) ? $queryParams['calendar_id'] : array();
130 if( ! is_array($calendarIdList) ) $calendarIdList = array( $calendarIdList );
131
132 if( isset($queryParams['from']) ){
133 $fromParam = $queryParams['from'];
134 // datetime
135 if( strlen($fromParam) >= 12 ){
136 $start = substr( $fromParam, 0, 12 );
137 }
138 // date
139 else {
140 $fromDate = substr( $fromParam, 0, 8 );
141 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
142 }
143 }
144 else {
145 $fromDate = $this->t->setNow()->formatDateDb();
146 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
147 }
148
149 if( isset($queryParams['to']) ){
150 $toParam = $queryParams['to'];
151 // datetime
152 if( strlen($toParam) >= 12 ){
153 $end = substr( $toParam, 0, 12 );
154 }
155 // date
156 else {
157 $toDate = substr( $toParam, 0, 8 );
158 $end = $this->t->setDateDb( $toDate )->modify('+1 day')->formatDateTimeDb();
159 }
160 }
161 else {
162 $end = $this->t->setDateTimeDb( $start )->modify('+1 day')->formatDateTimeDb();
163 }
164
165 // echo "$start - $end, $status<br>";
166 $employee2calendar = array();
167
168 $calendarList = $calendarIdList ? $this->calendarsQuery->findManyById( $calendarIdList ) : $this->calendarsQuery->findActive();
169
170 foreach( $calendarList as $calendar ){
171 // if no explicit calendars requested then leave only shifts
172 if( ! $calendarIdList ){
173 if( ! $calendar->isShift() ){
174 continue;
175 }
176 }
177
178 $calendarId = $calendar->getId();
179 $thisEmployees = $this->appQuery->findEmployeesForCalendar( $calendar );
180 foreach( array_keys($thisEmployees) as $employeeId ){
181 $employee2calendar[ $employeeId ][ $calendarId ] = $calendar;
182 }
183 }
184
185 $employees = array();
186 if( $employee2calendar ){
187 $employees = $this->employeesQuery->findManyActiveById( array_keys($employee2calendar) );
188 }
189 unset( $employees[0] );
190
191 foreach( array_keys($employees) as $employeeId ){
192 // no calendars at all
193 if( ! isset($employee2calendar[$employeeId]) ){
194 unset( $employees[$employeeId] );
195 continue;
196 }
197
198 $employee = $employees[ $employeeId ];
199 $thisCalendarList = $employee2calendar[ $employeeId ];
200 foreach( $thisCalendarList as $calendar ){
201 $testModel = new SH4_Shifts_Model( null, $calendar, $start, $end, $employee );
202 $conflicts = $this->conflicts->get( $testModel );
203 if( $conflicts ){
204 unset( $employees[$employeeId] );
205 break;
206 }
207 }
208 }
209
210 $ret = array();
211 $ret[ 'from' ] = $start;
212 $ret[ 'to' ] = $end;
213 $ret[ 'employees' ] = $this->self->toArrayEmployee( $employees );
214
215 return $ret;
216 }
217
218 public function shiftsGet( $queryParams = array() )
219 {
220 $status = isset($queryParams['status_id']) ? $queryParams['status_id'] : null;
221
222 $calendarId = isset( $queryParams['calendar_id'] ) ? $queryParams['calendar_id'] : null;
223 $employeeId = isset( $queryParams['employee_id']) ? $queryParams['employee_id'] : null;
224
225 if( isset($queryParams['from']) ){
226 $fromParam = $queryParams['from'];
227 // datetime
228 if( strlen($fromParam) >= 12 ){
229 $start = substr( $fromParam, 0, 12 );
230 }
231 // date
232 else {
233 $fromDate = substr( $fromParam, 0, 8 );
234 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
235 }
236 }
237 else {
238 $fromDate = $this->t->setNow()->formatDateDb();
239 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
240 }
241
242 if( isset($queryParams['to']) ){
243 $toParam = $queryParams['to'];
244 // datetime
245 if( strlen($toParam) >= 12 ){
246 $end = substr( $toParam, 0, 12 );
247 }
248 // date
249 else {
250 $toDate = substr( $toParam, 0, 8 );
251 $end = $this->t->setDateDb( $toDate )->modify('+1 day')->formatDateTimeDb();
252 }
253 }
254 else {
255 $end = $this->t->setDateTimeDb( $start )->modify('+1 year')->formatDateTimeDb();
256 }
257
258 // echo "$start - $end, $status<br>";
259
260 $this->shiftsQuery
261 ->setStart( $start )
262 ->setEnd( $end )
263 ;
264
265 $shifts = $this->shiftsQuery->find();
266
267 $default = array( 'status_id', 'calendar_id', 'employee_id', 'from', 'to', 'X-WP-ShiftController-AuthCode' );
268 foreach( $default as $k ) unset( $queryParams[$k] );
269
270 // filter shifts
271 $ids = array_keys( $shifts );
272 foreach( $ids as $id ){
273 $shift = $shifts[$id];
274
275 if( $shift->getStart() >= $end ){
276 unset( $shifts[$id] );
277 continue;
278 }
279
280 if( $shift->getEnd() <= $start ){
281 unset( $shifts[$id] );
282 continue;
283 }
284
285 if( NULL !== $status ){
286 $shiftStatus = $shift->getStatus();
287 if( $shiftStatus != $status ){
288 unset( $shifts[$id] );
289 continue;
290 }
291 }
292
293 $shiftCalendar = $shift->getCalendar();
294 $shiftCalendarId = $shiftCalendar->getId();
295
296 $shiftEmployee = $shift->getEmployee();
297 $shiftEmployeeId = $shiftEmployee->getId();
298
299 if( NULL !== $calendarId ){
300 if( 't' == $calendarId ){
301 if( ! $shiftCalendar->isTimeoff() ){
302 unset( $shifts[$id] );
303 }
304 }
305 elseif( 's' == $calendarId ){
306 if( ! $shiftCalendar->isShift() ){
307 unset( $shifts[$id] );
308 }
309 }
310 elseif( 'x' != $calendarId ){
311 if( $shiftCalendarId != $calendarId ){
312 unset( $shifts[$id] );
313 }
314 }
315 }
316
317 if( (NULL !== $employeeId) && ('x' != $employeeId) ){
318 if( $shiftEmployeeId != $employeeId ){
319 unset( $shifts[$id] );
320 }
321 }
322
323 // check additional args if we have any
324 if( $queryParams ){
325 $a = $this->shiftsPresenter->export( $shift );
326
327 reset( $queryParams );
328 foreach( $queryParams as $k => $v ){
329 if( ! array_key_exists($k, $a) ){
330 continue;
331 }
332
333 if( $a[$k] != $v ){
334 unset( $shifts[$id] );
335 break;
336 }
337
338 // if( ! array_key_exists($k, $a) ){
339 // unset( $shifts[$id] );
340 // break;
341 // }
342 // else {
343 // if( $a[$k] != $v ){
344 // unset( $shifts[$id] );
345 // break;
346 // }
347 // }
348 }
349 }
350 }
351
352 $ret = $this->self->toArray( $shifts );
353 return $ret;
354 }
355
356 public function shiftsGetById( $id )
357 {
358 $shift = $this->shiftsQuery->findById( $id );
359 if( ! $shift ){
360 return new WP_Error( 'no_shift', 'Not Found', array('status' => 404) );
361 }
362
363 $shifts = array( $shift );
364 $ret = $this->self->toArray( $shifts );
365 $ret= array_shift( $ret );
366
367 return $ret;
368 }
369
370 public function shiftsCreate( $values )
371 {
372 $errors = array();
373
374 $start = isset( $values['start'] ) ? $values['start'] : NULL;
375 if( NULL === $start ){
376 $errors['start'] = 'Required Field';
377 }
378
379 $start = trim( $start );
380 if( ! isset($errors['start']) ){
381 if( 12 !== strlen($start) ){
382 $errors['start'] = 'Format: YYYYMMDDHHMM';
383 }
384 }
385
386 $end = isset( $values['end'] ) ? $values['end'] : NULL;
387 if( NULL === $end ){
388 $errors['end'] = 'Required Field';
389 }
390
391 $end = trim( $end );
392 if( ! isset($errors['end']) ){
393 if( 12 !== strlen($end) ){
394 $errors['end'] = 'Format: YYYYMMDDHHMM';
395 }
396 }
397
398 $breakStart = isset( $values['break_start'] ) ? $values['break_start'] : NULL;
399 if( NULL !== $breakStart ){
400 $breakStart = trim( $breakStart );
401 if( ! isset($errors['break_start']) ){
402 if( 12 !== strlen($breakStart) ){
403 $errors['break_start'] = 'Format: YYYYMMDDHHMM';
404 }
405 }
406 }
407
408 $breakEnd = isset( $values['break_end'] ) ? $values['break_end'] : NULL;
409 if( NULL !== $breakEnd ){
410 $breakEnd = trim( $breakEnd );
411 if( ! isset($errors['break_end']) ){
412 if( 12 !== strlen($breakEnd) ){
413 $errors['break_end'] = 'Format: YYYYMMDDHHMM';
414 }
415 }
416 }
417
418 if( ! isset($values['calendar_id']) ){
419 $errors['calendar_id'] = 'Required Field';
420 }
421 else {
422 $calendarId = $values['calendar_id'];
423 $calendar = $this->calendarsQuery->findById( $calendarId );
424 if( ! $calendar ){
425 $errors['calendar'] = 'Not Found';
426 }
427 }
428
429 if( ! isset($values['employee_id']) ){
430 $errors['employee_id'] = 'Required Field';
431 }
432 else {
433 $employeeId = $values['employee_id'];
434 $employee = $this->employeesQuery->findById( $employeeId );
435 if( ! $employee ){
436 $errors['employee_id'] = 'Not Found';
437 }
438 }
439
440 $status = isset( $values['status_id'] ) ? $values['status_id'] : SH4_Shifts_Model::STATUS_DRAFT;
441 if( ! in_array($status, array(SH4_Shifts_Model::STATUS_DRAFT, SH4_Shifts_Model::STATUS_PUBLISH)) ){
442 $errors['status_id'] = 'Wrong Value';
443 }
444
445 if( $errors ){
446 $errorStr = array();
447 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
448 $errorStr = join( ', ', $errorStr );
449 return new WP_Error( 'error', $errorStr, array('status' => 500) );
450 }
451
452 $shift = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $employee, $breakStart, $breakEnd );
453
454 $allowConflict = isset( $values['conflict'] ) ? $values['conflict'] : FALSE;
455 if( ! $allowConflict ){
456 $conflicts = $this->conflicts->get( $shift );
457 if( $conflicts ){
458 $errors['conflict'] = 'Conflict';
459 }
460 }
461
462 if( $errors ){
463 $errorStr = array();
464 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
465 $errorStr = join( ', ', $errorStr );
466 return new WP_Error( 'error', $errorStr, array('status' => 500) );
467 }
468
469 $shiftId = $this->shiftsCommand->createNew( $shift );
470 $shift->setId( $shiftId );
471
472 // $token = $this->self->getToken( $request );
473 // $currentUser = $this->auth->getUserByToken( $token );
474
475 if( SH4_Shifts_Model::STATUS_PUBLISH == $status ){
476 // $can = $this->shiftsAcl->checkCreatePublished( $shiftId, $currentUser );
477 // if( ! $can ){
478 // $this->shiftsCommand->delete( $shift );
479 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
480 // }
481
482 $this->shiftsCommand->publish( $shift );
483 }
484
485 if( SH4_Shifts_Model::STATUS_DRAFT == $status ){
486 // $can = $this->shiftsAcl->checkCreateDraft( $shiftId, $currentUser );
487 // if( ! $can ){
488 // $this->shiftsCommand->delete( $shift );
489 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
490 // }
491
492 $this->shiftsCommand->draft( $shift );
493 }
494
495 return $shiftId;
496 }
497
498 public function shiftsDeleteById( $id )
499 {
500 $shift = $this->shiftsQuery->findById( $id );
501
502 if( ! $shift ){
503 return new WP_Error( 'no_shift', 'Not Found', array('status' => 404) );
504 }
505
506 // $admins = $this->permission->findAdmins();
507 // $currentUser = current( $admins );
508 // $can = $this->shiftsAcl->checkDelete( $shift->getId(), $currentUser );
509
510 // if( ! $can ){
511 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
512 // }
513
514 $this->shiftsCommand->delete( $shift );
515 return;
516 }
517
518 public function shiftsUpdateById( $id, $values )
519 {
520 $shift = $this->shiftsQuery->findById( $id );
521
522 if( ! $shift ){
523 return new WP_Error( 'no_shift', 'Not Found', array('status' => 404) );
524 }
525
526 $calendar = $shift->getCalendar();
527 $employee = $shift->getEmployee();
528 $start = $shift->getStart();
529 $end = $shift->getEnd();
530 $breakStart = $shift->getBreakStart();
531 $breakEnd = $shift->getBreakEnd();
532
533 // $admins = $this->permission->findAdmins();
534 // $currentUser = current( $admins );
535 // $can = $this->shiftsAcl->checkDelete( $shift->getId(), $currentUser );
536
537 // if( ! $can ){
538 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
539 // }
540
541 $errors = array();
542
543 $newStart = NULL;
544 if( isset($values['start']) ){
545 $newStart = $values['start'];
546 $newStart = trim( $newStart );
547 if( ! isset($errors['start']) ){
548 if( 12 !== strlen($newStart) ){
549 $errors['start'] = 'Format: YYYYMMDDHHMM';
550 }
551 }
552
553 if( $newStart == $start ) $newStart = NULL;
554 }
555
556 $newEnd = NULL;
557 if( isset($values['end']) ){
558 $newEnd = $values['end'];
559 $newEnd = trim( $newEnd );
560 if( ! isset($errors['end']) ){
561 if( 12 !== strlen($newEnd) ){
562 $errors['end'] = 'Format: YYYYMMDDHHMM';
563 }
564 }
565
566 if( $newEnd == $end ) $newEnd = NULL;
567 }
568
569 $newBreakStart = NULL;
570 if( isset($values['break_start']) ){
571 $newBreakStart = $values['break_start'];
572 $newBreakStart = trim( $newBreakStart );
573 if( ! isset($errors['break_start']) ){
574 if( 12 !== strlen($newBreakStart) ){
575 $errors['break_start'] = 'Format: YYYYMMDDHHMM';
576 }
577 }
578
579 if( $newBreakStart == $breakStart ) $newBreakStart = NULL;
580 }
581
582 $newBreakEnd = NULL;
583 if( isset($values['break_end']) ){
584 $newBreakEnd = $values['break_end'];
585 $newBreakEnd = trim( $newBreakEnd );
586 if( ! isset($errors['break_end']) ){
587 if( 12 !== strlen($newBreakEnd) ){
588 $errors['break_end'] = 'Format: YYYYMMDDHHMM';
589 }
590 }
591
592 if( $newBreakEnd == $breakEnd ) $newBreakEnd = NULL;
593 }
594
595 $newCalendar = NULL;
596 if( isset($values['calendar_id']) ){
597 $calendarId = $values['calendar_id'];
598 $newCalendar = $this->calendarsQuery->findById( $calendarId );
599
600 if( ! $newCalendar ){
601 $errors['calendar'] = 'Not Found';
602 }
603
604 if( $newCalendar && $calendar && ($newCalendar->getId() == $calendar->getId()) ) $newCalendar = NULL;
605 }
606
607 $newEmployee = NULL;
608 if( isset($values['employee_id']) ){
609 $employeeId = $values['employee_id'];
610 $newEmployee = $this->employeesQuery->findById( $employeeId );
611 if( ! $newEmployee ){
612 $errors['employee_id'] = 'Not Found';
613 }
614 if( $newEmployee && $employee && ($newEmployee->getId() == $employee->getId()) ) $newEmployee = NULL;
615 }
616
617 $newStatus = NULL;
618 if( isset($values['status_id']) ){
619 $newStatus = $values['status_id'];
620 if( ! in_array($newStatus, array(SH4_Shifts_Model::STATUS_DRAFT, SH4_Shifts_Model::STATUS_PUBLISH)) ){
621 $errors['status_id'] = 'Wrong Value';
622 $newStatus = NULL;
623 }
624 }
625
626 if( $errors ){
627 $errorStr = array();
628 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
629 $errorStr = join( ', ', $errorStr );
630 return new WP_Error( 'error', $errorStr, array('status' => 500) );
631 }
632
633 // check conflicts
634 $allowConflict = isset( $values['conflict'] ) ? $values['conflict'] : FALSE;
635
636 if( (! $allowConflict) && ($newCalendar OR $newEmployee OR $newStart OR $newEnd) ){
637 $calendar = $newCalendar ? $newCalendar : $shift->getCalendar();
638 $employee = $newEmployee ? $newEmployee : $shift->getEmployee();
639 $start = $newStart ? $newStart : $shift->getStart();
640 $end = $newEnd ? $newEnd : $shift->getEnd();
641 $breakStart = $newBreakStart ? $newBreakStart : $shift->getBreakStart();
642 $breakEnd = $newBreakEnd ? $newBreakEnd : $shift->getBreakEnd();
643
644 $newShift = new SH4_Shifts_Model( $id, $calendar, $start, $end, $employee, $breakStart, $breakEnd );
645
646 $conflicts = $this->conflicts->get( $newShift );
647 if( $conflicts ){
648 $errors['conflict'] = 'Conflict';
649 }
650
651 if( $errors ){
652 $errorStr = array();
653 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
654 $errorStr = join( ', ', $errorStr );
655 return new WP_Error( 'error', $errorStr, array('status' => 500) );
656 }
657 }
658
659 if( $newCalendar ){
660 $this->shiftsCommand->changeCalendar( $shift, $newCalendar );
661 }
662
663 if( $newEmployee ){
664 $this->shiftsCommand->changeEmployee( $shift, $newEmployee );
665 }
666
667 if( $newStart OR $newEnd OR $newBreakStart OR $newBreakEnd ){
668 $this->shiftsCommand->reschedule( $shift, $start, $end, $breakStart, $breakEnd );
669 }
670
671 if( $newStatus && (SH4_Shifts_Model::STATUS_PUBLISH == $newStatus) && ( ! $shift->isPublished() ) ){
672 $this->shiftsCommand->publish( $shift );
673 }
674
675 if( $newStatus && (SH4_Shifts_Model::STATUS_DRAFT == $newStatus) && ( ! $shift->isDraft() ) ){
676 $this->shiftsCommand->unpublish( $shift );
677 }
678
679 return;
680 }
681
682 public function employeesGet( $queryParams = array() )
683 {
684 $args = array();
685 $ret = $this->employeesQuery->read();
686 $ret = $this->self->toArrayEmployee( $ret );
687 return $ret;
688 }
689
690 public function calendarsGet( $queryParams = array() )
691 {
692 $args = array();
693 $ret = $this->calendarsQuery->read();
694 $ret = $this->self->toArrayCalendar( $ret );
695 return $ret;
696 }
697
698 public function employeesGetById( $id )
699 {
700 $args = array();
701 $ret = $this->employeesQuery->findById( $id );
702 if( ! $ret ){
703 return new WP_Error( 'no_employee', 'Not Found', array('status' => 404) );
704 }
705
706 $ret = [ $ret ];
707 $ret = $this->self->toArrayEmployee( $ret );
708 $ret = current( $ret );
709 return $ret;
710 }
711
712 public function employeesGetByUserId( $userId )
713 {
714 $employee = $this->appQuery->findEmployeeByUserId( $userId );
715 if( ! $employee ){
716 return new WP_Error( 'no_employee', 'Not Found', array('status' => 404) );
717 }
718
719 $employees = array( $employee );
720 $ret = $this->self->toArrayEmployee( $employees );
721 $ret = array_shift( $ret );
722
723 return $ret;
724 }
725
726 public function employeesGetByCalendarId( $calendarId )
727 {
728 $calendar = $this->calendarsQuery->findById( $calendarId );
729 if( ! $calendar ){
730 return new WP_Error( 'no_calendar', 'Not Found', array('status' => 404) );
731 }
732
733 $employees = $this->appQuery->findEmployeesForCalendar( $calendar );
734 $ret = $this->self->toArrayEmployee( $employees );
735
736 return $ret;
737 }
738
739 public function calendarsGetByEmployeeId( $employeeId )
740 {
741 $employee = $this->employeesQuery->findById( $employeeId );
742 if( ! $employee ){
743 return new WP_Error( 'no_employee', 'Not Found', array('status' => 404) );
744 }
745
746 $calendars = $this->appQuery->findCalendarsForEmployee( $employee );
747 $ret = $this->self->toArrayCalendar( $calendars );
748
749 return $ret;
750 }
751
752 /* returns employee id */
753 public function employeesCreate( $values )
754 {
755 $errors = array();
756
757 $userId = isset( $values['user_id'] ) ? $values['user_id'] : null;
758 $title = isset( $values['title'] ) ? $values['title'] : null;
759
760 if( $userId ){
761 // check if there's an employee linked to that user
762 $employee = $this->appQuery->findEmployeeByUserId( $userId );
763 if( $employee ){
764 return new WP_Error( 'user_occupied', 'The user is already linked to another employee', array('status' => 500) );
765 }
766
767 $wpUser = get_user_by( 'id', $userId );
768 if( ! $wpUser ){
769 return new WP_Error( 'no_user', 'User not found', array('status' => 404) );
770 }
771 }
772
773 if( (! $userId) && (null === $title) ){
774 $errors['title'] = 'Required Field';
775 }
776 $description = isset( $values['description'] ) ? $values['description'] : null;
777
778 if( $errors ){
779 $errorStr = array();
780 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
781 $errorStr = join( ', ', $errorStr );
782 return new WP_Error( 'error', $errorStr, array('status' => 500) );
783 }
784
785 try {
786 // create by user id
787 if( null === $title ){
788 $employeeId = $this->self->employeesCreateByUserId( $userId );
789 }
790 else {
791 $employeeId = $this->employeesCommand->create( $title, $description );
792 }
793 }
794 catch( HC3_ExceptionArray $e ){
795 $errors = $e->getErrors();
796 }
797
798 if( $errors ){
799 $errorStr = array();
800 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
801 $errorStr = join( ', ', $errorStr );
802 return new WP_Error( 'error', $errorStr, array('status' => 500) );
803 }
804
805 if( ! $userId ){
806 return $employeeId;
807 }
808
809 // link user to employee
810 if( null !== $title ){
811 $this->appCommand->linkEmployeeToUserById( $employeeId, $userId );
812 }
813
814 return $employeeId;
815 }
816
817 /* returns employee id */
818 public function employeesCreateByUserId( $userId )
819 {
820 $employee = $this->appQuery->findEmployeeByUserId( $userId );
821 if( $employee ){
822 $ret = $employee->getId();
823 return $ret;
824 }
825
826 $wpUser = get_user_by( 'id', $userId );
827 if( ! $wpUser ){
828 return new WP_Error( 'no_user', 'User not found', array('status' => 404) );
829 }
830
831 $title = $wpUser->display_name;
832 $description = null;
833
834 try {
835 $employeeId = $this->employeesCommand->create( $title, $description );
836 }
837 catch( HC3_ExceptionArray $e ){
838 $errors = $e->getErrors();
839 }
840
841 if( $errors ){
842 $errorStr = array();
843 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
844 $errorStr = join( ', ', $errorStr );
845 return new WP_Error( 'error', $errorStr, array('status' => 500) );
846 }
847
848 // link user to employee
849 $this->appCommand->linkEmployeeToUserById( $employeeId, $userId );
850
851 return $employeeId;
852 }
853
854 /* returns employee id */
855 public function employeesAddToCalendar( $employeeId, $calendarId )
856 {
857 $errors = array();
858
859 $employee = $this->employeesQuery->findById( $employeeId );
860 if( ! $employee ){
861 return new WP_Error( 'no_employee', 'Not found', array('status' => 404) );
862 }
863
864 $calendar = $this->calendarsQuery->findById( $calendarId );
865 if( ! $calendar ){
866 return new WP_Error( 'no_calendar', 'Not found', array('status' => 404) );
867 }
868
869 try {
870 $this->appCommand->addEmployeeToCalendar( $employee, $calendar );
871 }
872 catch( HC3_ExceptionArray $e ){
873 $errors = $e->getErrors();
874 }
875
876 if( $errors ){
877 $errorStr = array();
878 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
879 $errorStr = join( ', ', $errorStr );
880 return new WP_Error( 'error', $errorStr, array('status' => 500) );
881 }
882
883 return true;
884 }
885
886 public function employeesRemoveFromCalendar( $employeeId, $calendarId )
887 {
888 $errors = array();
889
890 $employee = $this->employeesQuery->findById( $employeeId );
891 if( ! $employee ){
892 return new WP_Error( 'no_employee', 'Not found', array('status' => 404) );
893 }
894
895 $calendar = $this->calendarsQuery->findById( $calendarId );
896 if( ! $calendar ){
897 return new WP_Error( 'no_calendar', 'Not found', array('status' => 404) );
898 }
899
900 try {
901 $this->appCommand->removeEmployeeFromCalendar( $employee, $calendar );
902 }
903 catch( HC3_ExceptionArray $e ){
904 $errors = $e->getErrors();
905 }
906
907 if( $errors ){
908 $errorStr = array();
909 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
910 $errorStr = join( ', ', $errorStr );
911 return new WP_Error( 'error', $errorStr, array('status' => 500) );
912 }
913
914 return true;
915 }
916 }