PluginProbe
ShiftController Employee Shift Scheduling / 4.9.96
ShiftController Employee Shift Scheduling v4.9.96
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 / app / query.php

query.php in ShiftController Employee Shift Scheduling 4.9.96, at sh4/app/query.php

543 lines 14.6 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_App_IQuery
3 {
4 public function findUserByEmployee( SH4_Employees_Model $employee );
5 public function findEmployeeByUser( HC3_Users_Model $user );
6 public function findAllUsersWithEmployee();
7
8 public function findManagersForCalendar( SH4_Calendars_Model $calendar );
9 public function findViewersForCalendar( SH4_Calendars_Model $calendar );
10
11 public function findCalendarsManagedByUser( HC3_Users_Model $user );
12 public function findCalendarsViewedByUser( HC3_Users_Model $user );
13
14 public function filterEmployeesForCalendar( array $employeeList, SH4_Calendars_Model $calendar );
15 public function findEmployeesForCalendar( SH4_Calendars_Model $calendar );
16 public function findCalendarsForEmployee( SH4_Employees_Model $employee );
17
18 public function findShiftTypesForCalendar( SH4_Calendars_Model $calendar );
19
20 public function filterShiftsForUser( HC3_Users_Model $user, array $shifts );
21 public function findCalendarsForChange( SH4_Shifts_Model $model );
22 }
23
24 class SH4_App_Query implements SH4_App_IQuery
25 {
26 public $self, $settings, $permission, $shiftTypesQuery, $calendarsQuery, $employeesQuery, $cp, $t, $usersQuery, $crudFactory;
27
28 public $repoEmployee = array();
29 public $repoActiveEmployee = array();
30 public $repoActiveCalendar = array();
31 public $repoShiftType = array();
32 public $repoUser = array();
33
34 public $repoEmployeeUser = array();
35 public $repoUserEmployee = array();
36
37 public function __construct(
38 HC3_Settings $settings,
39 HC3_IPermission $permission,
40
41 SH4_ShiftTypes_Query $shiftTypesQuery,
42 SH4_Calendars_Query $calendarsQuery,
43 SH4_Employees_Query $employeesQuery,
44 SH4_Calendars_Permissions $cp,
45
46 HC3_Time $t,
47 HC3_Users_Query $usersQuery,
48 HC3_CrudFactory $crudFactory,
49 HC3_Hooks $hooks
50 )
51 {
52 $this->settings = $hooks->wrap( $settings );
53 $this->permission = $hooks->wrap( $permission );
54 $this->crudFactory = $hooks->wrap( $crudFactory );
55 $this->t = $t;
56
57 $this->shiftTypesQuery = $hooks->wrap( $shiftTypesQuery );
58 $this->employeesQuery = $hooks->wrap( $employeesQuery );
59 $this->calendarsQuery = $hooks->wrap( $calendarsQuery );
60 $this->usersQuery = $hooks->wrap( $usersQuery );
61 $this->cp = $hooks->wrap( $cp );
62
63 $this->self = $hooks->wrap( $this );
64
65 // employee
66 $this->repoActiveEmployee = $this->employeesQuery->findActive();
67 $this->repoEmployee = $this->repoActiveEmployee;
68
69 // calendar
70 $this->repoActiveCalendar = $this->calendarsQuery->findActive();
71
72 // shift type
73 $this->repoShiftType = $this->shiftTypesQuery->findAll();
74
75 // employee-user
76 global $wpdb;
77 $sql = 'SELECT post_id AS employee_id, meta_value AS user_id FROM ' . $wpdb->postmeta . ' wpostmeta' . ' JOIN ' . $wpdb->posts . ' wposts ON wposts.ID = wpostmeta.post_id WHERE wpostmeta.meta_key="user_id" AND wposts.post_type="sh4_employee"';
78 $res = $wpdb->get_results( $sql, ARRAY_A );
79 foreach( $res as $e ){
80 if (!isset($e['user_id'])) {
81 continue;
82 }
83 if( ! $e['employee_id'] ) continue;
84 if( ! $e['user_id'] ) continue;
85 $this->repoEmployeeUser[ $e['employee_id'] ] = $e['user_id'];
86 $this->repoUserEmployee[ $e['user_id'] ?? '' ] = $e['employee_id'];
87 }
88
89 if( $this->repoUserEmployee ){
90 $this->repoUser = $this->usersQuery->findManyById( array_keys($this->repoUserEmployee) );
91 }
92 }
93
94 public function findCalendarsForChange( SH4_Shifts_Model $shift )
95 {
96 $employee = $shift->getEmployee();
97
98 // find current shift type
99 $startInDay = $shift->getStartInDay();
100 $endInDay = $shift->getEndInDay();
101 $shiftKey = $startInDay . '-' . $endInDay;
102
103 $calendar = $shift->getCalendar();
104 $calendarId = $calendar->getId();
105
106 $needMultiDay = $shift->isMultiDay();
107
108 $ret = $this->self->findCalendarsForEmployee( $employee );
109 unset( $ret[$calendarId] );
110
111 $ids = array_keys( $ret );
112 foreach( $ids as $id ){
113 if( $calendar->isTimeoff() && (! $ret[$id]->isTimeoff() ) ){
114 unset( $ret[$id] );
115 continue;
116 }
117 if( $calendar->isShift() && (! $ret[$id]->isShift() ) ){
118 unset( $ret[$id] );
119 continue;
120 }
121
122 $gotShiftTypes = FALSE;
123 $thisShiftTypes = $this->self->findShiftTypesForCalendar( $ret[$id] );
124 foreach( $thisShiftTypes as $shiftType ){
125 $range = $shiftType->getRange();
126
127 // multiday?
128 if( $needMultiDay ){
129 if( SH4_ShiftTypes_Model::RANGE_HOURS == $range ){
130 continue;
131 }
132
133 // if this allowed days cover this duration
134 $shiftEnd = $shift->getEnd();
135 $this->t->setDateTimeDb( $shift->getStart() );
136 $minEnd = $this->t->setDateTimeDb( $shift->getStart() )->modify( '+' . $shiftType->getStart() . ' days' )->formatDateTimeDb();
137 if( $minEnd > $shiftEnd ){
138 continue;
139 }
140 $maxEnd = $this->t->setDateTimeDb( $shift->getStart() )->modify( '+' . ($shiftType->getEnd() + 1) . ' days' )->formatDateTimeDb();
141 if( $maxEnd < $shiftEnd ){
142 continue;
143 }
144
145 $gotShiftTypes = TRUE;
146 break;
147 }
148 else {
149 if( SH4_ShiftTypes_Model::RANGE_DAYS == $range ){
150 continue;
151 }
152
153 $thisStart = $shiftType->getStart();
154 $thisEnd = $shiftType->getEnd();
155
156 // custom time
157 if( (NULL === $thisStart) && (NULL === $thisEnd) ){
158 $gotShiftTypes = TRUE;
159 break;
160 }
161
162 $thisKey = $thisStart . '-' . $thisEnd;
163 if( $thisKey == $shiftKey ){
164 $gotShiftTypes = TRUE;
165 break;
166 }
167 }
168 }
169
170 if( ! $gotShiftTypes ){
171 unset( $ret[$id] );
172 continue;
173 }
174 }
175
176 return $ret;
177 }
178
179 public function filterShiftsForUser( HC3_Users_Model $user, array $return )
180 {
181 $currentUserId = $user->getId();
182
183 if( ! $currentUserId ){
184 $return = array();
185 return $return;
186 }
187
188 $calendarsAsManager = $this->self->findCalendarsManagedByUser( $user );
189 $calendarsAsViewer = $this->self->findCalendarsViewedByUser( $user );
190
191 $calendarsAsEmployee = array();
192 $meEmployee = $this->self->findEmployeeByUser( $user );
193 if( $meEmployee ){
194 $meEmployeeId = $meEmployee->getId();
195 $calendarsAsEmployee = $this->self->findCalendarsForEmployee( $meEmployee );
196 }
197
198 $ids = array_keys( $return );
199
200 foreach( $ids as $id ){
201 $shift = $return[$id];
202
203 $shiftCalendar = $shift->getCalendar();
204 $shiftCalendarId = $shiftCalendar->getId();
205 $shiftEmployee = $shift->getEmployee();
206 $shiftEmployeeId = $shiftEmployee->getId();
207
208 if( isset($calendarsAsManager[$shiftCalendarId]) ){
209 continue;
210 }
211
212 if( isset($calendarsAsViewer[$shiftCalendarId]) ){
213 continue;
214 }
215
216 if( isset($calendarsAsEmployee[$shiftCalendarId]) ){
217 if( $shiftEmployeeId == $meEmployeeId ){
218 if( $shift->isPublished() ){
219 $perm = 'employee_view_own_publish';
220 }
221 else {
222 $perm = 'employee_view_own_draft';
223 }
224 }
225 else {
226 if( $shift->isOpen() ){
227 if( $shift->isPublished() ){
228 $perm = 'employee_view_open_publish';
229 }
230 else {
231 $perm = 'employee_view_open_draft';
232 }
233 }
234 else {
235 if( $shift->isPublished() ){
236 $perm = 'employee_view_others_publish';
237 }
238 else {
239 $perm = 'employee_view_others_draft';
240 }
241 }
242 }
243
244 if( $this->cp->get($shiftCalendar, $perm) ){
245 continue;
246 }
247 }
248
249 if( $shift->isOpen() ){
250 if( $shift->isPublished() ){
251 $perm = 'visitor_view_open_publish';
252 }
253 else {
254 $perm = 'visitor_view_open_draft';
255 }
256 }
257 else {
258 if( $shift->isPublished() ){
259 $perm = 'visitor_view_others_publish';
260 }
261 else {
262 $perm = 'visitor_view_others_draft';
263 }
264 }
265
266 if( $this->cp->get($shiftCalendar, $perm) ){
267 continue;
268 }
269
270 unset( $return[$id] );
271 }
272
273 return $return;
274 }
275
276 public function filterCalendarsManagedByUser( array $ret, HC3_Users_Model $user )
277 {
278 $isAdmin = $this->permission->isAdmin( $user );
279 if( $isAdmin ){
280 return $ret;
281 }
282
283 $userId = $user->getId();
284 foreach( $ret as $calendar ){
285 $calendarId = $calendar->getId();
286 $managers = $this->self->findManagersForCalendar( $calendar );
287 if( ! isset($managers[$userId])){
288 unset($ret[$calendarId]);
289 }
290 }
291
292 return $ret;
293 }
294
295 public function findCalendarsManagedByUser( HC3_Users_Model $user )
296 {
297 // $ret = $this->calendarsQuery->findActive();
298 // $ret = $this->self->filterCalendarsManagedByUser( $ret, $user );
299 $ret = $this->self->filterCalendarsManagedByUser( $this->repoActiveCalendar, $user );
300 return $ret;
301 }
302
303 public function filterCalendarsViewedByUser( array $ret, HC3_Users_Model $user )
304 {
305 $isAdmin = $this->permission->isAdmin( $user );
306 if( $isAdmin ){
307 return $ret;
308 }
309
310 $userId = $user->getId();
311 foreach( $ret as $calendar ){
312 $calendarId = $calendar->getId();
313 $viewers = $this->self->findViewersForCalendar( $calendar );
314 if( ! isset($viewers[$userId])){
315 unset($ret[$calendarId]);
316 }
317 }
318
319 return $ret;
320 }
321
322 public function findCalendarsViewedByUser( HC3_Users_Model $user )
323 {
324 // $ret = $this->calendarsQuery->findActive();
325 // $ret = $this->self->filterCalendarsViewedByUser( $ret, $user );
326 $ret = $this->self->filterCalendarsViewedByUser( $this->repoActiveCalendar, $user );
327 return $ret;
328 }
329
330 public function findManagersForCalendar( SH4_Calendars_Model $calendar )
331 {
332 $return = $this->permission->findAdmins();
333
334 $calendarId = $calendar->getId();
335 $settingName = 'calendar_' . $calendarId . '_manager';
336
337 $usersIds = $this->settings->get( $settingName, TRUE );
338 if( $usersIds ){
339 $moreReturn = $this->usersQuery->findManyById( $usersIds );
340 foreach( $moreReturn as $id => $user ){
341 if( ! array_key_exists($id, $return) ){
342 $return[ $id ] = $user;
343 }
344 }
345 }
346
347 return $return;
348 }
349
350 public function findViewersForCalendar( SH4_Calendars_Model $calendar )
351 {
352 // $return = $this->permission->findAdmins();
353 $return = array();
354
355 $calendarId = $calendar->getId();
356 $settingName = 'calendar_' . $calendarId . '_viewer';
357
358 $usersIds = $this->settings->get( $settingName, TRUE );
359 if( $usersIds ){
360 $moreReturn = $this->usersQuery->findManyById( $usersIds );
361 foreach( $moreReturn as $id => $user ){
362 if( ! array_key_exists($id, $return) ){
363 $return[ $id ] = $user;
364 }
365 }
366 }
367
368 return $return;
369 }
370
371 public function filterEmployeesForCalendar( array $employeeList, SH4_Calendars_Model $calendar )
372 {
373 $ret = array();
374
375 $calendarId = $calendar->getId();
376 $settingName = 'calendar_' . $calendarId . '_employee';
377 $employeeIds = $this->settings->get( $settingName, true );
378
379 if( ! $employeeIds ){
380 return $ret;
381 }
382
383 $ret = $employeeList;
384
385 $ids = array_keys( $ret );
386 foreach( $ids as $id ){
387 if( ! in_array($id, $employeeIds) ){
388 unset( $ret[$id] );
389 }
390 }
391
392 return $ret;
393 }
394
395 public function findEmployeesForCalendar( SH4_Calendars_Model $calendar )
396 {
397 $ret = array();
398
399 $calendarId = $calendar->getId();
400 $settingName = 'calendar_' . $calendarId . '_employee';
401
402 $listEmployeeId = $this->settings->get( $settingName, true );
403 if( $listEmployeeId ){
404 $ret = array_intersect_key( $this->repoActiveEmployee, array_combine($listEmployeeId, $listEmployeeId) );
405 // $ret = $this->employeesQuery->findManyActiveById( $listEmployeeId );
406 }
407
408 return $ret;
409 }
410
411 public function findShiftTypesForCalendar( SH4_Calendars_Model $calendar )
412 {
413 $ret = array();
414
415 $calendarId = $calendar->getId();
416 $settingName = 'calendar_' . $calendarId . '_shifttype';
417
418 $listShiftTypeId = $this->settings->get( $settingName, true );
419 if( $listShiftTypeId ){
420 // $ret = $this->shiftTypesQuery->findManyById( $listShiftTypeId );
421 $ret = array_intersect_key( $this->repoShiftType, array_combine($listShiftTypeId, $listShiftTypeId) );
422 }
423
424 return $ret;
425 }
426
427 public function filterCalendarsForEmployee( array $ret, SH4_Employees_Model $employee )
428 {
429 $employeeId = $employee->getId();
430
431 $calendarIds = array_keys($ret);
432 foreach( $calendarIds as $calendarId ){
433 $settingName = 'calendar_' . $calendarId . '_employee';
434 $employeesIds = $this->settings->get( $settingName, TRUE );
435 if( ! in_array($employeeId, $employeesIds) ){
436 unset( $ret[$calendarId] );
437 }
438 }
439
440 return $ret;
441 }
442
443 public function findCalendarsForEmployee( SH4_Employees_Model $employee )
444 {
445 // $ret = $this->calendarsQuery->findActive();
446 // $ret = $this->self->filterCalendarsForEmployee( $ret, $employee );
447 $ret = $this->self->filterCalendarsForEmployee( $this->repoActiveCalendar, $employee );
448 return $ret;
449 }
450
451 public function findAllUsersWithEmployee()
452 {
453 $ret = array();
454
455 $usersIds = array_keys( $this->repoUserEmployee );
456
457 // $crud = $this->crudFactory->make('employee');
458
459 // $args = array();
460 // $results = $crud->read( $args );
461 // if( ! $results ){
462 // return $ret;
463 // }
464
465 // $usersIds = array();
466 // foreach( $results as $r ){
467 // $userId = array_key_exists('user_id', $r) ? $r['user_id'] : NULL;
468 // if( ! $userId ){
469 // continue;
470 // }
471 // $usersIds[ $userId ] = $userId;
472 // }
473
474 if( ! $usersIds ){
475 return $ret;
476 }
477
478 $ret = $this->usersQuery->findManyById( $usersIds );
479 return $ret;
480 }
481
482 public function findUserByEmployee( SH4_Employees_Model $employee )
483 {
484 $ret = null;
485
486 $employeeId = $employee->getId();
487 if( ! $employeeId ){
488 return $ret;
489 }
490
491 $userId = isset( $this->repoEmployeeUser[$employeeId] ) ? $this->repoEmployeeUser[$employeeId] : null;
492
493 // $crud = $this->crudFactory->make('employee');
494 // $args = array();
495 // $args[] = array('id', '=', $employeeId );
496 // $results = $crud->read( $args );
497
498 // if( ! $results ){
499 // return $ret;
500 // }
501
502 // $results = array_shift( $results );
503 // $userId = array_key_exists('user_id', $results) ? $results['user_id'] : null;
504
505 if( ! $userId ){
506 return $ret;
507 }
508
509 // $ret = $this->usersQuery->findById( $userId );
510
511 if( ! isset($this->repoUser[$userId]) ){
512 $this->repoUser[$userId] = $this->usersQuery->findById( $userId );
513 }
514 $ret = $this->repoUser[$userId];
515
516 return $ret;
517 }
518
519 public function findEmployeeByUserId( $userId )
520 {
521 $ret = null;
522 if( ! $userId ){
523 return $ret;
524 }
525
526 $employeeId = isset( $this->repoUserEmployee[$userId] ) ? $this->repoUserEmployee[$userId] : null;
527 if( ! $employeeId ){
528 return $ret;
529 }
530
531 if( ! isset($this->repoEmployee[$employeeId]) ){
532 $this->repoEmployee[$employeeId] = $this->employeesQuery->findById( $employeeId );
533 }
534 $ret = $this->repoEmployee[$employeeId];
535
536 return $ret;
537 }
538
539 public function findEmployeeByUser( HC3_Users_Model $user )
540 {
541 return $this->self->findEmployeeByUserId( $user->getId() );
542 }
543 }