PluginProbe
ShiftController Employee Shift Scheduling / 4.9.87
ShiftController Employee Shift Scheduling v4.9.87
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.87, at sh4/app/query.php

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