PluginProbe
ShiftController Employee Shift Scheduling / 4.9.92
ShiftController Employee Shift Scheduling v4.9.92
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.92, at sh4/api/api.php

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