PluginProbe
ShiftController Employee Shift Scheduling / 4.9.24
ShiftController Employee Shift Scheduling v4.9.24
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 / new / acl.php

acl.php in ShiftController Employee Shift Scheduling 4.9.24, at sh4/new/acl.php

744 lines 19.2 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 interface SH4_New_IAcl
3 {
4 public function checkDraft( $calendarId, $typeId, $dateString, $employeeString );
5 public function checkPublish( $calendarId, $typeId, $dateString, $employeeString );
6
7 public function checkNewTimeoff( $params = array() );
8 public function checkNewAvailability( $params = array() );
9 public function checkNewShift( $params = array() );
10 public function checkNew( $params = array() );
11
12 public function check( $calendarId, $typeId, $employeeId, $date );
13 public function findAllCombos();
14 }
15
16 class SH4_New_Acl implements SH4_New_IAcl
17 {
18 public function __construct(
19 HC3_Hooks $hooks,
20 HC3_Time $t,
21
22 SH4_App_Query $appQuery,
23 SH4_Calendars_Permissions $calendarsPermissions,
24 SH4_ShiftTypes_Query $shiftTypesQuery,
25
26 SH4_Shifts_Availability $availability,
27 SH4_New_Query $newQuery,
28 SH4_Calendars_Query $calendarsQuery,
29 HC3_Auth $auth,
30 HC3_IPermission $permission
31 )
32 {
33 $this->self = $hooks->wrap( $this );
34 $this->t = $t;
35
36 $this->availability = $hooks->wrap( $availability );
37 $this->newQuery = $hooks->wrap( $newQuery );
38 $this->appQuery = $hooks->wrap( $appQuery );
39 $this->auth = $hooks->wrap( $auth );
40 $this->permission = $hooks->wrap( $permission );
41 $this->calendarsPermissions = $hooks->wrap( $calendarsPermissions );
42
43 $this->calendarsQuery = $hooks->wrap( $calendarsQuery );
44 $this->shiftTypesQuery = $hooks->wrap( $shiftTypesQuery );
45 }
46
47 public function checkCreate( $calendarId, $typeId, $dateString, $employeeString )
48 {
49 $return = FALSE;
50
51 $return = $this->self->checkDraft( $calendarId, $typeId, $dateString, $employeeString );
52 if( $return ){
53 return $return;
54 }
55
56 $return = $this->self->checkPublish( $calendarId, $typeId, $dateString, $employeeString );
57 return $return;
58 }
59
60 public function checkDraft( $calendarId, $typeId, $dateString, $employeeString )
61 {
62 $return = FALSE;
63
64 $employeesIds = HC3_Functions::unglueArray( $employeeString );
65 $dates = HC3_Functions::unglueArray( $dateString );
66
67 foreach( $dates as $date ){
68 foreach( $employeesIds as $employeeId ){
69 $pos = strpos($employeeId, SH4_NEW_OPEN_SEPARATOR);
70 if( $pos ){
71 $employeeId = substr($employeeId, 0, $pos);
72 }
73
74 $preCheck = $this->self->check( $calendarId, $typeId, $employeeId, $date );
75 if( ! $preCheck ){
76 return $return;
77 }
78 }
79 }
80
81 $currentUser = $this->auth->getCurrentUser();
82 $currentUserId = $currentUser->getId();
83 if( ! $currentUserId ){
84 return $return;
85 }
86
87 if( $this->permission->isAdmin($currentUser) ){
88 $return = TRUE;
89 return $return;
90 }
91
92 $calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser );
93 if( isset($calendarsAsManager[$calendarId]) ){
94 $return = TRUE;
95 return $return;
96 }
97
98 $calendarsAsEmployee = array();
99 $meEmployee = $this->appQuery->findEmployeeByUser( $currentUser );
100 if( $meEmployee ){
101 $employeeCalendars = $this->appQuery->findCalendarsForEmployee( $meEmployee );
102 foreach( $employeeCalendars as $thisCalendar ){
103 $thisCalendarId = $thisCalendar->getId();
104
105 if( $this->calendarsPermissions->get($thisCalendar, 'employee_create_own_draft') ){
106 $calendarsAsEmployee[ $thisCalendarId ] = $thisCalendar;
107 }
108 }
109 }
110
111 if( $calendarId != 'x' ){
112 if( isset($calendarsAsEmployee[$calendarId]) ){
113 $return = TRUE;
114 }
115 }
116 else {
117 if( $calendarsAsEmployee ){
118 $return = TRUE;
119 }
120 }
121
122 if( $return ){
123 // check if can create shifts with conflicts
124 // if( ! $this->calendarsPermissions->get($thisCalendar, 'employee_create_own_conflicts') ){
125 // $return = FALSE;
126 // }
127 }
128
129 return $return;
130 }
131
132 public function checkPublish( $calendarId, $typeId, $dateString, $employeeString )
133 {
134 $return = FALSE;
135 $employeesIds = HC3_Functions::unglueArray( $employeeString );
136 $dates = HC3_Functions::unglueArray( $dateString );
137
138 foreach( $dates as $date ){
139 foreach( $employeesIds as $employeeId ){
140 $pos = strpos($employeeId, SH4_NEW_OPEN_SEPARATOR);
141 if( $pos ){
142 $employeeId = substr($employeeId, 0, $pos);
143 }
144
145 $preCheck = $this->self->check( $calendarId, $typeId, $employeeId, $date );
146 if( ! $preCheck ){
147 $return = FALSE;
148 return $return;
149 }
150 }
151 }
152
153 $currentUser = $this->auth->getCurrentUser();
154 $currentUserId = $currentUser->getId();
155 if( ! $currentUserId ){
156 return $return;
157 }
158
159 if( $this->permission->isAdmin($currentUser) ){
160 $return = TRUE;
161 return $return;
162 }
163
164 $calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser );
165 if( isset($calendarsAsManager[$calendarId]) ){
166 $return = TRUE;
167 return $return;
168 }
169
170 $calendarsAsEmployee = array();
171 $meEmployee = $this->appQuery->findEmployeeByUser( $currentUser );
172 if( $meEmployee ){
173 $employeeCalendars = $this->appQuery->findCalendarsForEmployee( $meEmployee );
174 foreach( $employeeCalendars as $thisCalendar ){
175 $thisCalendarId = $thisCalendar->getId();
176
177 $checkPerms = array( 'employee_create_own_publish' );
178 foreach( $checkPerms as $perm ){
179 if( $this->calendarsPermissions->get($thisCalendar, $perm) ){
180 $calendarsAsEmployee[ $thisCalendarId ] = $thisCalendar;
181 break;
182 }
183 }
184 }
185 }
186
187 if( $calendarId != 'x' ){
188 if( isset($calendarsAsEmployee[$calendarId]) ){
189 $return = TRUE;
190 }
191 }
192 else {
193 if( $calendarsAsEmployee ){
194 $return = TRUE;
195 }
196 }
197
198 return $return;
199 }
200
201 public function checkNew( $params = array() )
202 {
203 $return = FALSE;
204
205 $currentUser = $this->auth->getCurrentUser();
206 $currentUserId = $currentUser->getId();
207 if( ! $currentUserId ){
208 return $return;
209 }
210
211 if( $this->permission->isAdmin($currentUser) ){
212 $return = TRUE;
213 return $return;
214 }
215
216 if( array_key_exists('date', $params) ){
217 $checkDate = $params['date'];
218
219 $today = $this->t->setNow()->formatDateDb();
220
221 if( $checkDate < $today ){
222 $calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser );
223
224 if( array_key_exists('calendar', $params) ){
225 $calendarId = $params['calendar'];
226 if( ! isset($calendarsAsManager[$calendarId]) ){
227 return $return;
228 }
229 }
230 else {
231 if( ! $calendarsAsManager ){
232 return $return;
233 }
234 }
235 }
236 }
237
238 $useCache = TRUE;
239 static $cache = array();
240 $cacheParams = $params;
241 unset( $cacheParams['date'] );
242 $paramsString = http_build_query($cacheParams, NULL, ',');
243
244 if( $useCache ){
245 if( isset($cache[$paramsString]) ){
246 // echo "USE CACHE '$paramsString'<br>";
247 $return = $cache[$paramsString];
248 return $return;
249 }
250 }
251
252 if( ! $return ){
253 $return = $this->self->checkNewShift( $params );
254 }
255
256 if( ! $return ){
257 $return = $this->self->checkNewTimeoff( $params );
258 }
259
260 if( ! $return ){
261 $return = $this->self->checkNewAvailability( $params );
262 }
263
264 $cache[$paramsString] = $return;
265
266 return $return;
267 }
268
269 public function checkNewTimeoff( $params = array() )
270 {
271 $return = FALSE;
272
273 // echo "CHECKING TIMEOFF";
274 // _print_r( $params );
275
276 $calendars = $this->_findCalendars( $params );
277 if( $calendars === FALSE ){
278 $return = FALSE;
279 return $return;
280 }
281
282 $employeeIds = array('x');
283 if( array_key_exists('employee', $params) ){
284 $employeeIds = HC3_Functions::unglueArray( $params['employee'] );
285 }
286
287 foreach( $calendars as $calendar ){
288 if( ! $calendar->isTimeoff() ){
289 continue;
290 }
291
292 $thisCalendarId = $calendar->getId();
293
294 $thisReturn = TRUE;
295 foreach( $employeeIds as $employeeId ){
296 $pos = strpos($employeeId, SH4_NEW_OPEN_SEPARATOR);
297 if( $pos ){
298 $employeeId = substr($employeeId, 0, $pos);
299 }
300
301 $thisThisReturn = $this->self->check( $thisCalendarId, 'x', $employeeId );
302 if( ! $thisThisReturn ){
303 $thisReturn = FALSE;
304 break;
305 }
306 }
307
308 if( $thisReturn ){
309 $return = TRUE;
310 break;
311 }
312 }
313
314 return $return;
315 }
316
317 public function checkNewAvailability( $params = array() )
318 {
319 if( ! $this->availability->hasAvailability() ){
320 $return = FALSE;
321 return $return;
322 }
323
324 $return = FALSE;
325
326 $calendars = $this->_findCalendars( $params );
327 if( $calendars === FALSE ){
328 $return = FALSE;
329 return $return;
330 }
331
332 $employeeIds = array('x');
333 if( array_key_exists('employee', $params) ){
334 $employeeIds = HC3_Functions::unglueArray( $params['employee'] );
335 }
336
337 foreach( $calendars as $calendar ){
338 if( ! $calendar->isAvailability() ){
339 continue;
340 }
341
342 $thisCalendarId = $calendar->getId();
343
344 $thisReturn = TRUE;
345 foreach( $employeeIds as $employeeId ){
346 $pos = strpos($employeeId, SH4_NEW_OPEN_SEPARATOR);
347 if( $pos ){
348 $employeeId = substr($employeeId, 0, $pos);
349 }
350
351 $thisThisReturn = $this->self->check( $thisCalendarId, 'x', $employeeId );
352 if( ! $thisThisReturn ){
353 $thisReturn = FALSE;
354 break;
355 }
356 }
357
358 if( $thisReturn ){
359 $return = TRUE;
360 break;
361 }
362 }
363
364 return $return;
365 }
366
367 public function checkNewShift( $params = array() )
368 {
369 // static $count = 0;
370 // $count++;
371
372 // echo __METHOD__ . ': ' . $count . '<br>';
373 // _print_r( $params );
374
375 $return = FALSE;
376
377 $calendars = $this->_findCalendars( $params );
378 if( $calendars === FALSE ){
379 $return = FALSE;
380 return $return;
381 }
382
383 $employeeIds = array('x');
384 if( array_key_exists('employee', $params) ){
385 $employeeIds = HC3_Functions::unglueArray( $params['employee'] );
386 }
387
388 foreach( $calendars as $calendar ){
389 if( ! $calendar->isShift() ){
390 continue;
391 }
392
393 $thisCalendarId = $calendar->getId();
394 $thisReturn = TRUE;
395 foreach( $employeeIds as $employeeId ){
396 $pos = strpos($employeeId, SH4_NEW_OPEN_SEPARATOR);
397 if( $pos ){
398 $employeeId = substr($employeeId, 0, $pos);
399 }
400
401 $thisThisReturn = $this->self->check( $thisCalendarId, 'x', $employeeId );
402 if( ! $thisThisReturn ){
403 $thisReturn = FALSE;
404 break;
405 }
406 }
407
408 if( $thisReturn ){
409 $return = TRUE;
410 break;
411 }
412 }
413
414 return $return;
415 }
416
417 protected function _findCalendars( $params = array() )
418 {
419 $return = $this->newQuery->findAllCalendars();
420
421 $employees = array();
422 if( array_key_exists('employee', $params) ){
423 $allEmployees = $this->newQuery->findAllEmployees();
424
425 $employeeId = $params['employee'];
426 $employeeIds = HC3_Functions::unglueArray( $employeeId );
427 foreach( $employeeIds as $employeeId ){
428 $pos = strpos($employeeId, SH4_NEW_OPEN_SEPARATOR);
429 if( $pos ){
430 $employeeId = substr($employeeId, 0, $pos);
431 }
432
433 if( array_key_exists($employeeId, $allEmployees) ){
434 $employees[] = $allEmployees[$employeeId];
435 }
436 else {
437 $return = FALSE;
438 return $return;
439 }
440 }
441 }
442
443 if( $employees ){
444 foreach( $employees as $employee ){
445 $filter = $this->appQuery->findCalendarsForEmployee( $employee );
446
447 $filterIds = array_keys( $filter );
448 $ids = array_keys($return);
449 foreach( $ids as $id ){
450 if( ! in_array($id, $filterIds) ){
451 unset( $return[$id] );
452 }
453 }
454 }
455 }
456
457 if( array_key_exists('calendar', $params) ){
458 $ok = FALSE;
459
460 $suppliedCalendarIds = is_array($params['calendar']) ? $params['calendar'] : array($params['calendar']);
461 foreach( $suppliedCalendarIds as $suppliedCalendarId ){
462 if( isset($return[$suppliedCalendarId]) ){
463 $ok = TRUE;
464 break;
465 }
466 }
467
468 if( ! $ok ){
469 $return = FALSE;
470 return $return;
471 }
472 }
473
474 return $return;
475 }
476
477 public function check( $calendarId, $typeId, $employeeId, $dateString = NULL )
478 {
479 $return = FALSE;
480 $today = $this->t->setNow()->formatDateDb();
481
482 $currentUser = $this->auth->getCurrentUser();
483 $currentUserId = $currentUser->getId();
484 if( ! $currentUserId ){
485 return $return;
486 }
487
488 if( $this->permission->isAdmin($currentUser) ){
489 $return = TRUE;
490 return $return;
491 }
492
493 $pos = strpos($employeeId, SH4_NEW_OPEN_SEPARATOR);
494 if( $pos ){
495 $employeeId = substr($employeeId, 0, $pos);
496 }
497
498 $calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser );
499
500 $calendarsAsEmployee = array();
501 $meEmployee = $this->appQuery->findEmployeeByUser( $currentUser );
502 if( $meEmployee ){
503 $meEmployeeId = $meEmployee->getId();
504 $employeeCalendars = $this->appQuery->findCalendarsForEmployee( $meEmployee );
505 foreach( $employeeCalendars as $thisCalendar ){
506 $thisCalendarId = $thisCalendar->getId();
507 $checkPerms = array( 'employee_create_own_draft', 'employee_create_own_publish' );
508 foreach( $checkPerms as $perm ){
509 if( $this->calendarsPermissions->get($thisCalendar, $perm) ){
510 $calendarsAsEmployee[ $thisCalendarId ] = $thisCalendar;
511 break;
512 }
513 }
514 }
515 }
516
517 if( ($calendarId == 'x') && ($typeId =='x') && ($employeeId == 'x') ){
518 if( $calendarsAsEmployee OR $calendarsAsManager ){
519 $return = TRUE;
520 }
521 return $return;
522 }
523
524 // check calendar
525 if( $calendarId != 'x' ){
526 if( ! (isset($calendarsAsManager[$calendarId]) OR isset($calendarsAsEmployee[$calendarId])) ){
527 return $return;
528 }
529 }
530
531 // check shift type
532 if( $typeId != 'x' ){
533 if( $calendarId == 'x' ){
534 return $return;
535 }
536
537 $calendar = $this->calendarsQuery->findActiveById( $calendarId );
538 if( ! $calendar ){
539 return $return;
540 }
541
542 $calendarShiftTypes = $this->appQuery->findShiftTypesForCalendar( $calendar );
543
544 // custom time
545 $checkTypeId = $typeId;
546 if( strpos($typeId, '-') ){
547 $checkTypeId = 0;
548 }
549 if( ! isset($calendarShiftTypes[$checkTypeId]) ){
550 return $return;
551 }
552 }
553
554 // check employee
555 if( $employeeId != 'x' ){
556 if( $calendarsAsManager ){
557 foreach( $calendarsAsManager as $calendar ){
558 $calendarEmployees = $this->appQuery->findEmployeesForCalendar( $calendar );
559 if( isset($calendarEmployees[$employeeId]) ){
560 $return = TRUE;
561 return $return;
562 }
563 }
564 }
565
566 // echo "CHECK $calendarId, $typeId, $employeeId, $date<br>";
567 // echo "ME EMPLOYEE $meEmployeeId VS $employeeId<br>";
568
569 if( $meEmployee && ($meEmployeeId == $employeeId) && $calendarsAsEmployee ){
570 if( $dateString ){
571 $checkDates = HC3_Functions::unglueArray( $dateString );
572 foreach( $checkDates as $checkDate ){
573 if( $checkDate < $today ){
574 $return = FALSE;
575 return $return;
576 }
577 }
578 }
579
580 $return = TRUE;
581 }
582
583 return $return;
584 }
585
586 if( $calendarId != 'x' ){
587 if( isset($calendarsAsManager[$calendarId]) OR isset($calendarsAsEmployee[$calendarId]) ){
588 $return = TRUE;
589 return $return;
590 }
591 }
592
593 // echo "CALENDAR ID = $calendarId, TYPE Id = $typeId, EMPLOYEE ID = $employeeId<br>";
594 return $return;
595 }
596
597 public function findAllCombos()
598 {
599 $ret = array();
600
601 $currentUser = $this->auth->getCurrentUser();
602 $currentUserId = $currentUser->getId();
603 if( ! $currentUserId ){
604 return $ret;
605 }
606
607 $calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser );
608 foreach( $calendarsAsManager as $calendar ){
609 $calendarEmployees = $this->appQuery->findEmployeesForCalendar( $calendar );
610 foreach( $calendarEmployees as $employee ){
611 $retId = $calendar->getId() . '-' . $employee->getId();
612 $ret[ $retId ] = $retId;
613 $retId = 0 . '-' . $employee->getId();
614 $ret[ $retId ] = $retId;
615 }
616 }
617
618 $meEmployee = $this->appQuery->findEmployeeByUser( $currentUser );
619
620 if( $meEmployee ){
621 $calendarsAsEmployee = array();
622 $employeeCalendars = $this->appQuery->findCalendarsForEmployee( $meEmployee );
623 foreach( $employeeCalendars as $thisCalendar ){
624 $thisCalendarId = $thisCalendar->getId();
625 if( $this->calendarsPermissions->get($thisCalendar, 'employee_create_own_draft') ){
626 $calendarsAsEmployee[ $thisCalendarId ] = $thisCalendar;
627 }
628 elseif( $this->calendarsPermissions->get($thisCalendar, 'employee_create_own_publish') ){
629 $calendarsAsEmployee[ $thisCalendarId ] = $thisCalendar;
630 }
631 }
632
633 foreach( $calendarsAsEmployee as $calendar ){
634 $retId = $calendar->getId() . '-' . $meEmployee->getId();
635 $ret[ $retId ] = $retId;
636 }
637 }
638
639 return $ret;
640 }
641
642 public function findAllCombosTimeoff()
643 {
644 $ret = array();
645
646 $currentUser = $this->auth->getCurrentUser();
647 $currentUserId = $currentUser->getId();
648 if( ! $currentUserId ){
649 return $ret;
650 }
651
652 $calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser );
653 foreach( $calendarsAsManager as $calendar ){
654 if( ! $calendar->isTimeoff() ){
655 continue;
656 }
657 $calendarEmployees = $this->appQuery->findEmployeesForCalendar( $calendar );
658 foreach( $calendarEmployees as $employee ){
659 $retId = $calendar->getId() . '-' . $employee->getId();
660 $ret[ $retId ] = $retId;
661 $retId = 0 . '-' . $employee->getId();
662 $ret[ $retId ] = $retId;
663 }
664 }
665
666 $meEmployee = $this->appQuery->findEmployeeByUser( $currentUser );
667
668 if( $meEmployee ){
669 $calendarsAsEmployee = array();
670 $employeeCalendars = $this->appQuery->findCalendarsForEmployee( $meEmployee );
671 foreach( $employeeCalendars as $calendar ){
672 if( ! $calendar->isTimeoff() ){
673 continue;
674 }
675 $thisCalendarId = $calendar->getId();
676 if( $this->calendarsPermissions->get($calendar, 'employee_create_own_draft') ){
677 $calendarsAsEmployee[ $thisCalendarId ] = $calendar;
678 }
679 elseif( $this->calendarsPermissions->get($calendar, 'employee_create_own_publish') ){
680 $calendarsAsEmployee[ $thisCalendarId ] = $calendar;
681 }
682 }
683
684 foreach( $calendarsAsEmployee as $calendar ){
685 $retId = $calendar->getId() . '-' . $meEmployee->getId();
686 $ret[ $retId ] = $retId;
687 }
688 }
689
690 return $ret;
691 }
692
693 public function findAllCombosShift()
694 {
695 $ret = array();
696
697 $currentUser = $this->auth->getCurrentUser();
698 $currentUserId = $currentUser->getId();
699 if( ! $currentUserId ){
700 return $ret;
701 }
702
703 $calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser );
704 foreach( $calendarsAsManager as $calendar ){
705 if( ! $calendar->isShift() ){
706 continue;
707 }
708
709 $calendarEmployees = $this->appQuery->findEmployeesForCalendar( $calendar );
710 foreach( $calendarEmployees as $employee ){
711 $retId = $calendar->getId() . '-' . $employee->getId();
712 $ret[ $retId ] = $retId;
713 $retId = 0 . '-' . $employee->getId();
714 $ret[ $retId ] = $retId;
715 }
716 }
717
718 $meEmployee = $this->appQuery->findEmployeeByUser( $currentUser );
719
720 if( $meEmployee ){
721 $calendarsAsEmployee = array();
722 $employeeCalendars = $this->appQuery->findCalendarsForEmployee( $meEmployee );
723 foreach( $employeeCalendars as $calendar ){
724 if( ! $calendar->isShift() ){
725 continue;
726 }
727 $thisCalendarId = $calendar->getId();
728 if( $this->calendarsPermissions->get($calendar, 'employee_create_own_draft') ){
729 $calendarsAsEmployee[ $thisCalendarId ] = $calendar;
730 }
731 elseif( $this->calendarsPermissions->get($calendar, 'employee_create_own_publish') ){
732 $calendarsAsEmployee[ $thisCalendarId ] = $calendar;
733 }
734 }
735
736 foreach( $calendarsAsEmployee as $calendar ){
737 $retId = $calendar->getId() . '-' . $meEmployee->getId();
738 $ret[ $retId ] = $retId;
739 }
740 }
741
742 return $ret;
743 }
744 }