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 / app / query.php

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

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