PluginProbe
ShiftController Employee Shift Scheduling / 4.9.61
ShiftController Employee Shift Scheduling v4.9.61
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.61, at sh4/api/api.php

643 lines 17.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_Calendars_Query $calendarsQuery,
17 SH4_Shifts_Presenter $shiftsPresenter,
18
19 SH4_App_Query $appQuery,
20 SH4_Employees_Presenter $employeePresenter,
21
22 SH4_Shifts_Conflicts $conflicts,
23 // SH4_Shifts_Acl $shiftsAcl,
24 SH4_Shifts_Query $shiftsQuery,
25 SH4_Shifts_Command $shiftsCommand
26 )
27 {
28 $this->t = $t;
29 $this->translate = $translate;
30 $this->permission = $hooks->wrap( $permission );
31
32 $this->employeesQuery = $hooks->wrap( $employeesQuery );
33 $this->calendarsQuery = $hooks->wrap( $calendarsQuery );
34 $this->employeePresenter = $hooks->wrap( $employeePresenter );
35 $this->appQuery = $hooks->wrap( $appQuery );
36
37 $this->shiftsPresenter = $hooks->wrap( $shiftsPresenter );
38 $this->conflicts = $hooks->wrap($conflicts);
39 $this->shiftsQuery = $hooks->wrap( $shiftsQuery );
40 $this->shiftsCommand = $hooks->wrap( $shiftsCommand );
41 // $this->shiftsAcl = $hooks->wrap( $shiftsAcl );
42 $this->self = $hooks->wrap( $this );
43
44 add_filter( 'shiftcontroller4/api/shifts/get', array($this->self, 'shiftsGet'), 10, 1 );
45 add_filter( 'shiftcontroller4/api/shifts/getbyid', array($this->self, 'shiftsGetById'), 10, 1 );
46 add_filter( 'shiftcontroller4/api/shifts/create', array($this->self, 'shiftsCreate'), 10, 1 );
47 add_filter( 'shiftcontroller4/api/shifts/deletebyid', array($this->self, 'shiftsDeleteById'), 10, 1 );
48 add_filter( 'shiftcontroller4/api/shifts/updatebyid', array($this->self, 'shiftsUpdateById'), 10, 2 );
49 }
50
51 public function toArrayEmployee( array $employeeList )
52 {
53 $out = array();
54
55 foreach( $employeeList as $employee ){
56 $thisOut = $this->employeePresenter->export( $employee, true );
57
58 $keys = array_keys( $thisOut );
59 reset( $keys );
60 foreach( $keys as $k ){
61 $thisOut[$k] = $this->translate->translate( $thisOut[$k] );
62 }
63
64 $out[] = $thisOut;
65 }
66
67 return $out;
68 }
69
70 public function toArray( array $shifts )
71 {
72 $out = array();
73
74 foreach( $shifts as $shift ){
75 $thisOut = $this->shiftsPresenter->export( $shift, 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 availableEmployeesGet( $queryParams = array() )
90 {
91 $calendarIdList = isset( $queryParams['calendar_id'] ) ? $queryParams['calendar_id'] : array();
92 if( ! is_array($calendarIdList) ) $calendarIdList = array( $calendarIdList );
93
94 if( isset($queryParams['from']) ){
95 $fromParam = $queryParams['from'];
96 // datetime
97 if( strlen($fromParam) >= 12 ){
98 $start = substr( $fromParam, 0, 12 );
99 }
100 // date
101 else {
102 $fromDate = substr( $fromParam, 0, 8 );
103 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
104 }
105 }
106 else {
107 $fromDate = $this->t->setNow()->formatDateDb();
108 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
109 }
110
111 if( isset($queryParams['to']) ){
112 $toParam = $queryParams['to'];
113 // datetime
114 if( strlen($toParam) >= 12 ){
115 $end = substr( $toParam, 0, 12 );
116 }
117 // date
118 else {
119 $toDate = substr( $toParam, 0, 8 );
120 $end = $this->t->setDateDb( $toDate )->modify('+1 day')->formatDateTimeDb();
121 }
122 }
123 else {
124 $end = $this->t->setDateTimeDb( $start )->modify('+1 day')->formatDateTimeDb();
125 }
126
127 // echo "$start - $end, $status<br>";
128 $employee2calendar = array();
129
130 $calendarList = $calendarIdList ? $this->calendarsQuery->findManyById( $calendarIdList ) : $this->calendarsQuery->findActive();
131
132 foreach( $calendarList as $calendar ){
133 // if no explicit calendars requested then leave only shifts
134 if( ! $calendarIdList ){
135 if( ! $calendar->isShift() ){
136 continue;
137 }
138 }
139
140 $calendarId = $calendar->getId();
141 $thisEmployees = $this->appQuery->findEmployeesForCalendar( $calendar );
142 foreach( array_keys($thisEmployees) as $employeeId ){
143 $employee2calendar[ $employeeId ][ $calendarId ] = $calendar;
144 }
145 }
146
147 $employees = array();
148 if( $employee2calendar ){
149 $employees = $this->employeesQuery->findManyActiveById( array_keys($employee2calendar) );
150 }
151 unset( $employees[0] );
152
153 foreach( array_keys($employees) as $employeeId ){
154 // no calendars at all
155 if( ! isset($employee2calendar[$employeeId]) ){
156 unset( $employees[$employeeId] );
157 continue;
158 }
159
160 $employee = $employees[ $employeeId ];
161 $thisCalendarList = $employee2calendar[ $employeeId ];
162 foreach( $thisCalendarList as $calendar ){
163 $testModel = new SH4_Shifts_Model( null, $calendar, $start, $end, $employee );
164 $conflicts = $this->conflicts->get( $testModel );
165 if( $conflicts ){
166 unset( $employees[$employeeId] );
167 break;
168 }
169 }
170 }
171
172 $ret = array();
173 $ret[ 'from' ] = $start;
174 $ret[ 'to' ] = $end;
175 $ret[ 'employees' ] = $this->self->toArrayEmployee( $employees );
176
177 return $ret;
178 }
179
180 public function shiftsGet( $queryParams = array() )
181 {
182 $status = isset($queryParams['status_id']) ? $queryParams['status_id'] : null;
183
184 $calendarId = isset( $queryParams['calendar_id'] ) ? $queryParams['calendar_id'] : null;
185 $employeeId = isset( $queryParams['employee_id']) ? $queryParams['employee_id'] : null;
186
187 if( isset($queryParams['from']) ){
188 $fromParam = $queryParams['from'];
189 // datetime
190 if( strlen($fromParam) >= 12 ){
191 $start = substr( $fromParam, 0, 12 );
192 }
193 // date
194 else {
195 $fromDate = substr( $fromParam, 0, 8 );
196 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
197 }
198 }
199 else {
200 $fromDate = $this->t->setNow()->formatDateDb();
201 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
202 }
203
204 if( isset($queryParams['to']) ){
205 $toParam = $queryParams['to'];
206 // datetime
207 if( strlen($toParam) >= 12 ){
208 $end = substr( $toParam, 0, 12 );
209 }
210 // date
211 else {
212 $toDate = substr( $toParam, 0, 8 );
213 $end = $this->t->setDateDb( $toDate )->modify('+1 day')->formatDateTimeDb();
214 }
215 }
216 else {
217 $end = $this->t->setDateTimeDb( $start )->modify('+1 year')->formatDateTimeDb();
218 }
219
220 // echo "$start - $end, $status<br>";
221
222 $this->shiftsQuery
223 ->setStart( $start )
224 ->setEnd( $end )
225 ;
226
227 $shifts = $this->shiftsQuery->find();
228
229 $default = array( 'status_id', 'calendar_id', 'employee_id', 'from', 'to', 'X-WP-ShiftController-AuthCode' );
230 foreach( $default as $k ) unset( $queryParams[$k] );
231
232 // filter shifts
233 $ids = array_keys( $shifts );
234 foreach( $ids as $id ){
235 $shift = $shifts[$id];
236
237 if( $shift->getStart() >= $end ){
238 unset( $shifts[$id] );
239 continue;
240 }
241
242 if( $shift->getEnd() <= $start ){
243 unset( $shifts[$id] );
244 continue;
245 }
246
247 if( NULL !== $status ){
248 $shiftStatus = $shift->getStatus();
249 if( $shiftStatus != $status ){
250 unset( $shifts[$id] );
251 continue;
252 }
253 }
254
255 $shiftCalendar = $shift->getCalendar();
256 $shiftCalendarId = $shiftCalendar->getId();
257
258 $shiftEmployee = $shift->getEmployee();
259 $shiftEmployeeId = $shiftEmployee->getId();
260
261 if( NULL !== $calendarId ){
262 if( 't' == $calendarId ){
263 if( ! $shiftCalendar->isTimeoff() ){
264 unset( $shifts[$id] );
265 }
266 }
267 elseif( 's' == $calendarId ){
268 if( ! $shiftCalendar->isShift() ){
269 unset( $shifts[$id] );
270 }
271 }
272 elseif( 'x' != $calendarId ){
273 if( $shiftCalendarId != $calendarId ){
274 unset( $shifts[$id] );
275 }
276 }
277 }
278
279 if( (NULL !== $employeeId) && ('x' != $employeeId) ){
280 if( $shiftEmployeeId != $employeeId ){
281 unset( $shifts[$id] );
282 }
283 }
284
285 // check additional args if we have any
286 if( $queryParams ){
287 $a = $this->shiftsPresenter->export( $shift );
288
289 reset( $queryParams );
290 foreach( $queryParams as $k => $v ){
291 if( ! array_key_exists($k, $a) ){
292 continue;
293 }
294
295 if( $a[$k] != $v ){
296 unset( $shifts[$id] );
297 break;
298 }
299
300 // if( ! array_key_exists($k, $a) ){
301 // unset( $shifts[$id] );
302 // break;
303 // }
304 // else {
305 // if( $a[$k] != $v ){
306 // unset( $shifts[$id] );
307 // break;
308 // }
309 // }
310 }
311 }
312 }
313
314 $ret = $this->self->toArray( $shifts );
315 return $ret;
316 }
317
318 public function shiftsGetById( $id )
319 {
320 $shift = $this->shiftsQuery->findById( $id );
321 if( ! $shift ){
322 return new WP_Error( 'no_shift', 'Not Found', array('status' => 404) );
323 }
324
325 $shifts = array( $shift );
326 $ret = $this->self->toArray( $shifts );
327 $ret= array_shift( $ret );
328
329 return $ret;
330 }
331
332 public function shiftsCreate( $values )
333 {
334 $errors = array();
335
336 $start = isset( $values['start'] ) ? $values['start'] : NULL;
337 if( NULL === $start ){
338 $errors['start'] = 'Required Field';
339 }
340
341 $start = trim( $start );
342 if( ! isset($errors['start']) ){
343 if( 12 !== strlen($start) ){
344 $errors['start'] = 'Format: YYYYMMDDHHMM';
345 }
346 }
347
348 $end = isset( $values['end'] ) ? $values['end'] : NULL;
349 if( NULL === $end ){
350 $errors['end'] = 'Required Field';
351 }
352
353 $end = trim( $end );
354 if( ! isset($errors['end']) ){
355 if( 12 !== strlen($end) ){
356 $errors['end'] = 'Format: YYYYMMDDHHMM';
357 }
358 }
359
360 $breakStart = isset( $values['break_start'] ) ? $values['break_start'] : NULL;
361 if( NULL !== $breakStart ){
362 $breakStart = trim( $breakStart );
363 if( ! isset($errors['break_start']) ){
364 if( 12 !== strlen($breakStart) ){
365 $errors['break_start'] = 'Format: YYYYMMDDHHMM';
366 }
367 }
368 }
369
370 $breakEnd = isset( $values['break_end'] ) ? $values['break_end'] : NULL;
371 if( NULL !== $breakEnd ){
372 $breakEnd = trim( $breakEnd );
373 if( ! isset($errors['break_end']) ){
374 if( 12 !== strlen($breakEnd) ){
375 $errors['break_end'] = 'Format: YYYYMMDDHHMM';
376 }
377 }
378 }
379
380 if( ! isset($values['calendar_id']) ){
381 $errors['calendar_id'] = 'Required Field';
382 }
383 else {
384 $calendarId = $values['calendar_id'];
385 $calendar = $this->calendarsQuery->findById( $calendarId );
386 if( ! $calendar ){
387 $errors['calendar'] = 'Not Found';
388 }
389 }
390
391 if( ! isset($values['employee_id']) ){
392 $errors['employee_id'] = 'Required Field';
393 }
394 else {
395 $employeeId = $values['employee_id'];
396 $employee = $this->employeesQuery->findById( $employeeId );
397 if( ! $employee ){
398 $errors['employee_id'] = 'Not Found';
399 }
400 }
401
402 $status = isset( $values['status_id'] ) ? $values['status_id'] : SH4_Shifts_Model::STATUS_DRAFT;
403 if( ! in_array($status, array(SH4_Shifts_Model::STATUS_DRAFT, SH4_Shifts_Model::STATUS_PUBLISH)) ){
404 $errors['status_id'] = 'Wrong Value';
405 }
406
407 if( $errors ){
408 $errorStr = array();
409 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
410 $errorStr = join( ', ', $errorStr );
411 return new WP_Error( 'error', $errorStr, array('status' => 500) );
412 }
413
414 $shift = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $employee, $breakStart, $breakEnd );
415
416 $allowConflict = isset( $values['conflict'] ) ? $values['conflict'] : FALSE;
417 if( ! $allowConflict ){
418 $conflicts = $this->conflicts->get( $shift );
419 if( $conflicts ){
420 $errors['conflict'] = 'Conflict';
421 }
422 }
423
424 if( $errors ){
425 $errorStr = array();
426 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
427 $errorStr = join( ', ', $errorStr );
428 return new WP_Error( 'error', $errorStr, array('status' => 500) );
429 }
430
431 $shiftId = $this->shiftsCommand->createNew( $shift );
432 $shift->setId( $shiftId );
433
434 // $token = $this->self->getToken( $request );
435 // $currentUser = $this->auth->getUserByToken( $token );
436
437 if( SH4_Shifts_Model::STATUS_PUBLISH == $status ){
438 // $can = $this->shiftsAcl->checkCreatePublished( $shiftId, $currentUser );
439 // if( ! $can ){
440 // $this->shiftsCommand->delete( $shift );
441 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
442 // }
443
444 $this->shiftsCommand->publish( $shift );
445 }
446
447 if( SH4_Shifts_Model::STATUS_DRAFT == $status ){
448 // $can = $this->shiftsAcl->checkCreateDraft( $shiftId, $currentUser );
449 // if( ! $can ){
450 // $this->shiftsCommand->delete( $shift );
451 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
452 // }
453
454 $this->shiftsCommand->draft( $shift );
455 }
456
457 return $shiftId;
458 }
459
460 public function shiftsDeleteById( $id )
461 {
462 $shift = $this->shiftsQuery->findById( $id );
463
464 if( ! $shift ){
465 return new WP_Error( 'no_shift', 'Not Found', array('status' => 404) );
466 }
467
468 // $admins = $this->permission->findAdmins();
469 // $currentUser = current( $admins );
470 // $can = $this->shiftsAcl->checkDelete( $shift->getId(), $currentUser );
471
472 // if( ! $can ){
473 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
474 // }
475
476 $this->shiftsCommand->delete( $shift );
477 return;
478 }
479
480 public function shiftsUpdateById( $id, $values )
481 {
482 $shift = $this->shiftsQuery->findById( $id );
483
484 if( ! $shift ){
485 return new WP_Error( 'no_shift', 'Not Found', array('status' => 404) );
486 }
487
488 $calendar = $shift->getCalendar();
489 $employee = $shift->getEmployee();
490 $start = $shift->getStart();
491 $end = $shift->getEnd();
492 $breakStart = $shift->getBreakStart();
493 $breakEnd = $shift->getBreakEnd();
494
495 // $admins = $this->permission->findAdmins();
496 // $currentUser = current( $admins );
497 // $can = $this->shiftsAcl->checkDelete( $shift->getId(), $currentUser );
498
499 // if( ! $can ){
500 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
501 // }
502
503 $errors = array();
504
505 $newStart = NULL;
506 if( isset($values['start']) ){
507 $newStart = $values['start'];
508 $newStart = trim( $newStart );
509 if( ! isset($errors['start']) ){
510 if( 12 !== strlen($newStart) ){
511 $errors['start'] = 'Format: YYYYMMDDHHMM';
512 }
513 }
514
515 if( $newStart == $start ) $newStart = NULL;
516 }
517
518 $newEnd = NULL;
519 if( isset($values['end']) ){
520 $newEnd = $values['end'];
521 $newEnd = trim( $newEnd );
522 if( ! isset($errors['end']) ){
523 if( 12 !== strlen($newEnd) ){
524 $errors['end'] = 'Format: YYYYMMDDHHMM';
525 }
526 }
527
528 if( $newEnd == $end ) $newEnd = NULL;
529 }
530
531 $newBreakStart = NULL;
532 if( isset($values['break_start']) ){
533 $newBreakStart = $values['break_start'];
534 $newBreakStart = trim( $newBreakStart );
535 if( ! isset($errors['break_start']) ){
536 if( 12 !== strlen($newBreakStart) ){
537 $errors['break_start'] = 'Format: YYYYMMDDHHMM';
538 }
539 }
540
541 if( $newBreakStart == $breakStart ) $newBreakStart = NULL;
542 }
543
544 $newBreakEnd = NULL;
545 if( isset($values['break_end']) ){
546 $newBreakEnd = $values['break_end'];
547 $newBreakEnd = trim( $newBreakEnd );
548 if( ! isset($errors['break_end']) ){
549 if( 12 !== strlen($newBreakEnd) ){
550 $errors['break_end'] = 'Format: YYYYMMDDHHMM';
551 }
552 }
553
554 if( $newBreakEnd == $breakEnd ) $newBreakEnd = NULL;
555 }
556
557 $newCalendar = NULL;
558 if( isset($values['calendar_id']) ){
559 $calendarId = $values['calendar_id'];
560 $newCalendar = $this->calendarsQuery->findById( $calendarId );
561
562 if( ! $newCalendar ){
563 $errors['calendar'] = 'Not Found';
564 }
565
566 if( $newCalendar && $calendar && ($newCalendar->getId() == $calendar->getId()) ) $newCalendar = NULL;
567 }
568
569 $newEmployee = NULL;
570 if( isset($values['employee_id']) ){
571 $employeeId = $values['employee_id'];
572 $newEmployee = $this->employeesQuery->findById( $employeeId );
573 if( ! $newEmployee ){
574 $errors['employee_id'] = 'Not Found';
575 }
576 if( $newEmployee && $employee && ($newEmployee->getId() == $employee->getId()) ) $newEmployee = NULL;
577 }
578
579 $newStatus = NULL;
580 if( isset($values['status_id']) ){
581 $newStatus = $values['status_id'];
582 if( ! in_array($newStatus, array(SH4_Shifts_Model::STATUS_DRAFT, SH4_Shifts_Model::STATUS_PUBLISH)) ){
583 $errors['status_id'] = 'Wrong Value';
584 $newStatus = NULL;
585 }
586 }
587
588 if( $errors ){
589 $errorStr = array();
590 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
591 $errorStr = join( ', ', $errorStr );
592 return new WP_Error( 'error', $errorStr, array('status' => 500) );
593 }
594
595 // check conflicts
596 $allowConflict = isset( $values['conflict'] ) ? $values['conflict'] : FALSE;
597
598 if( (! $allowConflict) && ($newCalendar OR $newEmployee OR $newStart OR $newEnd) ){
599 $calendar = $newCalendar ? $newCalendar : $shift->getCalendar();
600 $employee = $newEmployee ? $newEmployee : $shift->getEmployee();
601 $start = $newStart ? $newStart : $shift->getStart();
602 $end = $newEnd ? $newEnd : $shift->getEnd();
603 $breakStart = $newBreakStart ? $newBreakStart : $shift->getBreakStart();
604 $breakEnd = $newBreakEnd ? $newBreakEnd : $shift->getBreakEnd();
605
606 $newShift = new SH4_Shifts_Model( $id, $calendar, $start, $end, $employee, $breakStart, $breakEnd );
607
608 $conflicts = $this->conflicts->get( $newShift );
609 if( $conflicts ){
610 $errors['conflict'] = 'Conflict';
611 }
612
613 if( $errors ){
614 $errorStr = array();
615 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
616 $errorStr = join( ', ', $errorStr );
617 return new WP_Error( 'error', $errorStr, array('status' => 500) );
618 }
619 }
620
621 if( $newCalendar ){
622 $this->shiftsCommand->changeCalendar( $shift, $newCalendar );
623 }
624
625 if( $newEmployee ){
626 $this->shiftsCommand->changeEmployee( $shift, $newEmployee );
627 }
628
629 if( $newStart OR $newEnd OR $newBreakStart OR $newBreakEnd ){
630 $this->shiftsCommand->reschedule( $shift, $start, $end, $breakStart, $breakEnd );
631 }
632
633 if( $newStatus && (SH4_Shifts_Model::STATUS_PUBLISH == $newStatus) && ( ! $shift->isPublished() ) ){
634 $this->shiftsCommand->publish( $shift );
635 }
636
637 if( $newStatus && (SH4_Shifts_Model::STATUS_DRAFT == $newStatus) && ( ! $shift->isDraft() ) ){
638 $this->shiftsCommand->unpublish( $shift );
639 }
640
641 return;
642 }
643 }