PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
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.26, at sh4/api/api.php

515 lines 14.4 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 class SH4_Api_Api
3 {
4 public function __construct(
5 HC3_Hooks $hooks,
6 HC3_Time $t,
7
8 HC3_IPermission $permission,
9 HC3_Translate $translate,
10
11 SH4_Employees_Query $employeesQuery,
12 SH4_Calendars_Query $calendarsQuery,
13 SH4_Shifts_Presenter $shiftsPresenter,
14
15 SH4_Shifts_Conflicts $conflicts,
16 // SH4_Shifts_Acl $shiftsAcl,
17 SH4_Shifts_Query $shiftsQuery,
18 SH4_Shifts_Command $shiftsCommand
19 )
20 {
21 $this->t = $t;
22 $this->translate = $translate;
23 $this->permission = $hooks->wrap( $permission );
24
25 $this->employeesQuery = $hooks->wrap( $employeesQuery );
26 $this->calendarsQuery = $hooks->wrap( $calendarsQuery );
27
28 $this->shiftsPresenter = $hooks->wrap( $shiftsPresenter );
29 $this->conflicts = $hooks->wrap($conflicts);
30 $this->shiftsQuery = $hooks->wrap( $shiftsQuery );
31 $this->shiftsCommand = $hooks->wrap( $shiftsCommand );
32 // $this->shiftsAcl = $hooks->wrap( $shiftsAcl );
33 $this->self = $hooks->wrap( $this );
34
35 add_filter( 'shiftcontroller4/api/shifts/get', array($this->self, 'shiftsGet'), 10, 1 );
36 add_filter( 'shiftcontroller4/api/shifts/getbyid', array($this->self, 'shiftsGetById'), 10, 1 );
37 add_filter( 'shiftcontroller4/api/shifts/create', array($this->self, 'shiftsCreate'), 10, 1 );
38 add_filter( 'shiftcontroller4/api/shifts/deletebyid', array($this->self, 'shiftsDeleteById'), 10, 1 );
39 add_filter( 'shiftcontroller4/api/shifts/updatebyid', array($this->self, 'shiftsUpdateById'), 10, 2 );
40 }
41
42 public function toArray( array $shifts )
43 {
44 $out = array();
45
46 foreach( $shifts as $shift ){
47 $thisOut = $this->shiftsPresenter->export( $shift, TRUE );
48
49 $keys = array_keys( $thisOut );
50 reset( $keys );
51 foreach( $keys as $k ){
52 $thisOut[$k] = $this->translate->translate( $thisOut[$k] );
53 }
54
55 $out[] = $thisOut;
56 }
57
58 return $out;
59 }
60
61 public function shiftsGet( $queryParams = array() )
62 {
63 $status = isset($queryParams['status_id']) ? $queryParams['status_id'] : null;
64
65 $calendarId = isset( $queryParams['calendar_id'] ) ? $queryParams['calendar_id'] : null;
66 $employeeId = isset( $queryParams['employee_id']) ? $queryParams['employee_id'] : null;
67
68 if( isset($queryParams['from']) ){
69 $fromParam = $queryParams['from'];
70 // datetime
71 if( strlen($fromParam) >= 12 ){
72 $start = substr( $fromParam, 0, 12 );
73 }
74 // date
75 else {
76 $fromDate = substr( $fromParam, 0, 8 );
77 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
78 }
79 }
80 else {
81 $fromDate = $this->t->setNow()->formatDateDb();
82 $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
83 }
84
85 if( isset($queryParams['to']) ){
86 $toParam = $queryParams['to'];
87 // datetime
88 if( strlen($toParam) >= 12 ){
89 $end = substr( $toParam, 0, 12 );
90 }
91 // date
92 else {
93 $toDate = substr( $toParam, 0, 8 );
94 $end = $this->t->setDateDb( $toDate )->modify('+1 day')->formatDateTimeDb();
95 }
96 }
97 else {
98 $end = $this->t->setDateTimeDb( $start )->modify('+1 year')->formatDateTimeDb();
99 }
100
101 // echo "$start - $end, $status<br>";
102
103 $this->shiftsQuery
104 ->setStart( $start )
105 ->setEnd( $end )
106 ;
107
108 $shifts = $this->shiftsQuery->find();
109
110 $default = array( 'status_id', 'calendar_id', 'employee_id', 'from', 'to' );
111 foreach( $default as $k ) unset( $queryParams[$k] );
112
113 // filter shifts
114 $ids = array_keys( $shifts );
115 foreach( $ids as $id ){
116 $shift = $shifts[$id];
117
118 if( $shift->getStart() >= $end ){
119 unset( $shifts[$id] );
120 continue;
121 }
122
123 if( $shift->getEnd() <= $start ){
124 unset( $shifts[$id] );
125 continue;
126 }
127
128 if( NULL !== $status ){
129 $shiftStatus = $shift->getStatus();
130 if( $shiftStatus != $status ){
131 unset( $shifts[$id] );
132 continue;
133 }
134 }
135
136 $shiftCalendar = $shift->getCalendar();
137 $shiftCalendarId = $shiftCalendar->getId();
138
139 $shiftEmployee = $shift->getEmployee();
140 $shiftEmployeeId = $shiftEmployee->getId();
141
142 if( NULL !== $calendarId ){
143 if( 't' == $calendarId ){
144 if( ! $shiftCalendar->isTimeoff() ){
145 unset( $shifts[$id] );
146 }
147 }
148 elseif( 's' == $calendarId ){
149 if( ! $shiftCalendar->isShift() ){
150 unset( $shifts[$id] );
151 }
152 }
153 elseif( 'x' != $calendarId ){
154 if( $shiftCalendarId != $calendarId ){
155 unset( $shifts[$id] );
156 }
157 }
158 }
159
160 if( (NULL !== $employeeId) && ('x' != $employeeId) ){
161 if( $shiftEmployeeId != $employeeId ){
162 unset( $shifts[$id] );
163 }
164 }
165
166 // check additional args if we have any
167 if( $queryParams ){
168 $a = $this->shiftsPresenter->export( $shift );
169
170 reset( $queryParams );
171 foreach( $queryParams as $k => $v ){
172 if( ! array_key_exists($k, $a) ){
173 unset( $shifts[$id] );
174 break;
175 }
176 else {
177 if( $a[$k] != $v ){
178 unset( $shifts[$id] );
179 break;
180 }
181 }
182 }
183 }
184 }
185
186 $ret = $this->self->toArray( $shifts );
187 return $ret;
188 }
189
190 public function shiftsGetById( $id )
191 {
192 $shift = $this->shiftsQuery->findById( $id );
193 if( ! $shift ){
194 return new WP_Error( 'no_shift', 'Not Found', array('status' => 404) );
195 }
196
197 $shifts = array( $shift );
198 $ret = $this->self->toArray( $shifts );
199 $ret= array_shift( $ret );
200
201 return $ret;
202 }
203
204 public function shiftsCreate( $values )
205 {
206 $errors = array();
207
208 $start = isset( $values['start'] ) ? $values['start'] : NULL;
209 if( NULL === $start ){
210 $errors['start'] = 'Required Field';
211 }
212
213 $start = trim( $start );
214 if( ! isset($errors['start']) ){
215 if( 12 !== strlen($start) ){
216 $errors['start'] = 'Format: YYYYMMDDHHMM';
217 }
218 }
219
220 $end = isset( $values['end'] ) ? $values['end'] : NULL;
221 if( NULL === $end ){
222 $errors['end'] = 'Required Field';
223 }
224
225 $end = trim( $end );
226 if( ! isset($errors['end']) ){
227 if( 12 !== strlen($end) ){
228 $errors['end'] = 'Format: YYYYMMDDHHMM';
229 }
230 }
231
232 $breakStart = isset( $values['break_start'] ) ? $values['break_start'] : NULL;
233 if( NULL !== $breakStart ){
234 $breakStart = trim( $breakStart );
235 if( ! isset($errors['break_start']) ){
236 if( 12 !== strlen($breakStart) ){
237 $errors['break_start'] = 'Format: YYYYMMDDHHMM';
238 }
239 }
240 }
241
242 $breakEnd = isset( $values['break_end'] ) ? $values['break_end'] : NULL;
243 if( NULL !== $breakEnd ){
244 $breakEnd = trim( $breakEnd );
245 if( ! isset($errors['break_end']) ){
246 if( 12 !== strlen($breakEnd) ){
247 $errors['break_end'] = 'Format: YYYYMMDDHHMM';
248 }
249 }
250 }
251
252 if( ! isset($values['calendar_id']) ){
253 $errors['calendar_id'] = 'Required Field';
254 }
255 else {
256 $calendarId = $values['calendar_id'];
257 $calendar = $this->calendarsQuery->findById( $calendarId );
258 if( ! $calendar ){
259 $errors['calendar'] = 'Not Found';
260 }
261 }
262
263 if( ! isset($values['employee_id']) ){
264 $errors['employee_id'] = 'Required Field';
265 }
266 else {
267 $employeeId = $values['employee_id'];
268 $employee = $this->employeesQuery->findById( $employeeId );
269 if( ! $employee ){
270 $errors['employee_id'] = 'Not Found';
271 }
272 }
273
274 $status = isset( $values['status_id'] ) ? $values['status_id'] : SH4_Shifts_Model::STATUS_DRAFT;
275 if( ! in_array($status, array(SH4_Shifts_Model::STATUS_DRAFT, SH4_Shifts_Model::STATUS_PUBLISH)) ){
276 $errors['status_id'] = 'Wrong Value';
277 }
278
279 if( $errors ){
280 $errorStr = array();
281 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
282 $errorStr = join( ', ', $errorStr );
283 return new WP_Error( 'error', $errorStr, array('status' => 500) );
284 }
285
286 $shift = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $employee, $breakStart, $breakEnd );
287
288 $allowConflict = isset( $values['conflict'] ) ? $values['conflict'] : FALSE;
289 if( ! $allowConflict ){
290 $conflicts = $this->conflicts->get( $shift );
291 if( $conflicts ){
292 $errors['conflict'] = 'Conflict';
293 }
294 }
295
296 if( $errors ){
297 $errorStr = array();
298 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
299 $errorStr = join( ', ', $errorStr );
300 return new WP_Error( 'error', $errorStr, array('status' => 500) );
301 }
302
303 $shiftId = $this->shiftsCommand->createNew( $shift );
304 $shift->setId( $shiftId );
305
306 // $token = $this->self->getToken( $request );
307 // $currentUser = $this->auth->getUserByToken( $token );
308
309 if( SH4_Shifts_Model::STATUS_PUBLISH == $status ){
310 // $can = $this->shiftsAcl->checkCreatePublished( $shiftId, $currentUser );
311 // if( ! $can ){
312 // $this->shiftsCommand->delete( $shift );
313 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
314 // }
315
316 $this->shiftsCommand->publish( $shift );
317 }
318
319 if( SH4_Shifts_Model::STATUS_DRAFT == $status ){
320 // $can = $this->shiftsAcl->checkCreateDraft( $shiftId, $currentUser );
321 // if( ! $can ){
322 // $this->shiftsCommand->delete( $shift );
323 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
324 // }
325
326 $this->shiftsCommand->draft( $shift );
327 }
328
329 return $shiftId;
330 }
331
332 public function shiftsDeleteById( $id )
333 {
334 $shift = $this->shiftsQuery->findById( $id );
335
336 if( ! $shift ){
337 return new WP_Error( 'no_shift', 'Not Found', array('status' => 404) );
338 }
339
340 // $admins = $this->permission->findAdmins();
341 // $currentUser = current( $admins );
342 // $can = $this->shiftsAcl->checkDelete( $shift->getId(), $currentUser );
343
344 // if( ! $can ){
345 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
346 // }
347
348 $this->shiftsCommand->delete( $shift );
349 return;
350 }
351
352 public function shiftsUpdateById( $id, $values )
353 {
354 $shift = $this->shiftsQuery->findById( $id );
355
356 if( ! $shift ){
357 return new WP_Error( 'no_shift', 'Not Found', array('status' => 404) );
358 }
359
360 $calendar = $shift->getCalendar();
361 $employee = $shift->getEmployee();
362 $start = $shift->getStart();
363 $end = $shift->getEnd();
364 $breakStart = $shift->getBreakStart();
365 $breakEnd = $shift->getBreakEnd();
366
367 // $admins = $this->permission->findAdmins();
368 // $currentUser = current( $admins );
369 // $can = $this->shiftsAcl->checkDelete( $shift->getId(), $currentUser );
370
371 // if( ! $can ){
372 // return new WP_Error( 'error', 'Access Denied', array('status' => 500) );
373 // }
374
375 $errors = array();
376
377 $newStart = NULL;
378 if( isset($values['start']) ){
379 $newStart = $values['start'];
380 $newStart = trim( $newStart );
381 if( ! isset($errors['start']) ){
382 if( 12 !== strlen($newStart) ){
383 $errors['start'] = 'Format: YYYYMMDDHHMM';
384 }
385 }
386
387 if( $newStart == $start ) $newStart = NULL;
388 }
389
390 $newEnd = NULL;
391 if( isset($values['end']) ){
392 $newEnd = $values['end'];
393 $newEnd = trim( $newEnd );
394 if( ! isset($errors['end']) ){
395 if( 12 !== strlen($newEnd) ){
396 $errors['end'] = 'Format: YYYYMMDDHHMM';
397 }
398 }
399
400 if( $newEnd == $end ) $newEnd = NULL;
401 }
402
403 $newBreakStart = NULL;
404 if( isset($values['break_start']) ){
405 $newBreakStart = $values['break_start'];
406 $newBreakStart = trim( $newBreakStart );
407 if( ! isset($errors['break_start']) ){
408 if( 12 !== strlen($newBreakStart) ){
409 $errors['break_start'] = 'Format: YYYYMMDDHHMM';
410 }
411 }
412
413 if( $newBreakStart == $breakStart ) $newBreakStart = NULL;
414 }
415
416 $newBreakEnd = NULL;
417 if( isset($values['break_end']) ){
418 $newBreakEnd = $values['break_end'];
419 $newBreakEnd = trim( $newBreakEnd );
420 if( ! isset($errors['break_end']) ){
421 if( 12 !== strlen($newBreakEnd) ){
422 $errors['break_end'] = 'Format: YYYYMMDDHHMM';
423 }
424 }
425
426 if( $newBreakEnd == $breakEnd ) $newBreakEnd = NULL;
427 }
428
429 $newCalendar = NULL;
430 if( isset($values['calendar_id']) ){
431 $calendarId = $values['calendar_id'];
432 $newCalendar = $this->calendarsQuery->findById( $calendarId );
433
434 if( ! $newCalendar ){
435 $errors['calendar'] = 'Not Found';
436 }
437
438 if( $newCalendar && $calendar && ($newCalendar->getId() == $calendar->getId()) ) $newCalendar = NULL;
439 }
440
441 $newEmployee = NULL;
442 if( isset($values['employee_id']) ){
443 $employeeId = $values['employee_id'];
444 $newEmployee = $this->employeesQuery->findById( $employeeId );
445 if( ! $newEmployee ){
446 $errors['employee_id'] = 'Not Found';
447 }
448 if( $newEmployee && $employee && ($newEmployee->getId() == $employee->getId()) ) $newEmployee = NULL;
449 }
450
451 $newStatus = NULL;
452 if( isset($values['status_id']) ){
453 $newStatus = $values['status_id'];
454 if( ! in_array($newStatus, array(SH4_Shifts_Model::STATUS_DRAFT, SH4_Shifts_Model::STATUS_PUBLISH)) ){
455 $errors['status_id'] = 'Wrong Value';
456 $newStatus = NULL;
457 }
458 }
459
460 if( $errors ){
461 $errorStr = array();
462 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
463 $errorStr = join( ', ', $errorStr );
464 return new WP_Error( 'error', $errorStr, array('status' => 500) );
465 }
466
467 // check conflicts
468 $allowConflict = isset( $values['conflict'] ) ? $values['conflict'] : FALSE;
469
470 if( (! $allowConflict) && ($newCalendar OR $newEmployee OR $newStart OR $newEnd) ){
471 $calendar = $newCalendar ? $newCalendar : $shift->getCalendar();
472 $employee = $newEmployee ? $newEmployee : $shift->getEmployee();
473 $start = $newStart ? $newStart : $shift->getStart();
474 $end = $newEnd ? $newEnd : $shift->getEnd();
475 $breakStart = $newBreakStart ? $newBreakStart : $shift->getBreakStart();
476 $breakEnd = $newBreakEnd ? $newBreakEnd : $shift->getBreakEnd();
477
478 $newShift = new SH4_Shifts_Model( $id, $calendar, $start, $end, $employee, $breakStart, $breakEnd );
479
480 $conflicts = $this->conflicts->get( $newShift );
481 if( $conflicts ){
482 $errors['conflict'] = 'Conflict';
483 }
484
485 if( $errors ){
486 $errorStr = array();
487 foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
488 $errorStr = join( ', ', $errorStr );
489 return new WP_Error( 'error', $errorStr, array('status' => 500) );
490 }
491 }
492
493 if( $newCalendar ){
494 $this->shiftsCommand->changeCalendar( $shift, $newCalendar );
495 }
496
497 if( $newEmployee ){
498 $this->shiftsCommand->changeEmployee( $shift, $newEmployee );
499 }
500
501 if( $newStart OR $newEnd OR $newBreakStart OR $newBreakEnd ){
502 $this->shiftsCommand->reschedule( $shift, $start, $end, $breakStart, $breakEnd );
503 }
504
505 if( $newStatus && (SH4_Shifts_Model::STATUS_PUBLISH == $newStatus) && ( ! $shift->isPublished() ) ){
506 $this->shiftsCommand->publish( $shift );
507 }
508
509 if( $newStatus && (SH4_Shifts_Model::STATUS_DRAFT == $newStatus) && ( ! $shift->isDraft() ) ){
510 $this->shiftsCommand->unpublish( $shift );
511 }
512
513 return;
514 }
515 }