PluginProbe
ShiftController Employee Shift Scheduling / 4.9.85
ShiftController Employee Shift Scheduling v4.9.85
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 / schedule / html / view / common.php

common.php in ShiftController Employee Shift Scheduling 4.9.85, at sh4/schedule/html/view/common.php

1,142 lines 30.9 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_Schedule_Html_View_ICommon
3 {
4 public function getShifts();
5 public function getCalendars();
6 public function getEmployees();
7
8 public function findAllCalendars();
9 public function findAllEmployees();
10 public function filterShifts( $shifts );
11 public function filterViewTypes( $types );
12
13 public function renderReport( $shiftsDuration, $alignRight = TRUE );
14
15 public function renderDateLabelGrouped( $date );
16 }
17
18 #[AllowDynamicProperties]
19 class SH4_Schedule_Html_View_Common implements SH4_Schedule_Html_View_ICommon
20 {
21 public function __construct(
22 HC3_Hooks $hooks,
23
24 HC3_Auth $auth,
25 HC3_Settings $settings,
26
27 HC3_Ui $ui,
28 HC3_Request $request,
29 HC3_Time $t,
30
31 SH4_App_Query $appQuery,
32
33 SH4_Shifts_Conflicts $conflicts,
34
35 SH4_Calendars_Permissions $calendarsPermissions,
36 SH4_Employees_Query $employeesQuery,
37 SH4_Calendars_Query $calendarsQuery,
38 SH4_Shifts_Query $shiftsQuery
39 )
40 {
41 $this->self = $hooks->wrap( $this );
42
43 $this->auth = $auth;
44
45 $this->ui = $ui;
46 $this->t = $t;
47 $this->request = $request;
48 $this->settings = $settings;
49
50 $this->appQuery = $hooks->wrap( $appQuery );
51
52 $this->conflicts = $hooks->wrap( $conflicts );
53
54 $this->calendarsPermissions = $hooks->wrap( $calendarsPermissions );
55 $this->employeesQuery = $hooks->wrap( $employeesQuery );
56 $this->calendarsQuery = $hooks->wrap( $calendarsQuery );
57 $this->shiftsQuery = $hooks->wrap( $shiftsQuery );
58 }
59
60 public function dateLabel( $date )
61 {
62 $slug = $this->request->getSlug();
63 $today = $this->t->setNow()->formatDateDb();
64
65 $this->t->setDateDb( $date );
66
67 $weekdayView = $this->t->getWeekdayName();
68 $weekdayView = $this->ui->makeBlock( $weekdayView )->tag('muted')->tag('font-size', 1);
69
70 $ret = $this->t->getDay();
71 $ret = $this->ui->makeListInline( array($weekdayView, $ret) )
72 ->tag('align', 'center')
73 ;
74
75 return $ret;
76 }
77
78 public function renderDateLabel( $date )
79 {
80 $slug = $this->request->getSlug();
81 $today = $this->t->setNow()->formatDateDb();
82
83 $dateView = $this->self->dateLabel( $date );
84
85 $this->t->setDateDb( $date );
86 $weekLabel = $this->t->getWeekNo();
87 $weekLabel = '__Week__' . ' #' . $weekLabel;
88
89 $toDateParams = array();
90 $toDateParams['type'] = 'day';
91 $toDateParams['start'] = $date;
92 $toDate = array( $slug, $toDateParams );
93
94 $title = $this->t->formatDateWithWeekday() . ' ' . $weekLabel;
95
96 $dateView = $this->ui->makeAhref( $toDate, $dateView )
97 ->tag('print')
98 // ->addAttr( 'title', $title )
99 ->setAttr( 'title', $title )
100 ;
101
102 if( $date == $today ){
103 $dateView = $this->ui->makeBlock( $dateView )
104 ->tag('border')
105 ->tag('border-color', 'gray')
106 ;
107 }
108
109 $ret = $dateView;
110 return $ret;
111 }
112
113 public function dateLabelGrouped( $date )
114 {
115 $this->t->setDateDb( $date );
116
117 $dateView = array();
118 $weekdayView = $this->t->getWeekdayName();
119 $weekdayView = $this->ui->makeBlock( $weekdayView )->tag('muted')->tag('font-size', 1);
120 $dateView[] = $weekdayView;
121
122 $dayView = $this->t->getDay();
123 $dateView[] = $dayView;
124
125 $dateView = $this->ui->makeList( $dateView )
126 ->gutter( 0 )
127 ->tag('align', 'center')
128 ;
129
130 return $dateView;
131 }
132
133 public function renderDateLabelGrouped( $date )
134 {
135 $slug = $this->request->getSlug();
136 $today = $this->t->setNow()->formatDateDb();
137
138 $dateView = $this->self->dateLabelGrouped( $date );
139
140 $this->t->setDateDb( $date );
141 $weekLabel = $this->t->getWeekNo();
142 $weekLabel = '__Week__' . ' #' . $weekLabel;
143
144 $toDateParams = array();
145 $toDateParams['type'] = 'day';
146 $toDateParams['start'] = $date;
147 $toDate = array( $slug, $toDateParams );
148
149 $title = $this->t->formatDateWithWeekday() . ' ' . $weekLabel;
150
151 $dateView = $this->ui->makeAhref( $toDate, $dateView )
152 ->tag('print')
153 // ->addAttr( 'title', $title )
154 ->setAttr( 'title', $title )
155 ;
156
157 if( $date == $today ){
158 $dateView = $this->ui->makeBlock( $dateView )
159 ->tag('border')
160 ->tag('border-color', 'gray')
161 ;
162 }
163
164 $ret = $dateView;
165 return $ret;
166 }
167
168 public function employeeRowLabel( $employeeView, $employeeId, $range, $dates, $shifts )
169 {
170 return $employeeView;
171 }
172
173 public function renderReport( $shiftsDuration, $alignRight = true, $isVert = false )
174 {
175 $return = NULL;
176
177 $hide = $this->settings->get('datetime_hide_schedule_reports');
178 if( $hide ){
179 return $return;
180 }
181
182 $currentUser = $this->auth->getCurrentUser();
183 $currentUserId = $currentUser->getId();
184
185 if( ! $currentUserId ){
186 return $return;
187 }
188
189 $qty = $shiftsDuration->getQty();
190 $duration = $shiftsDuration->getDuration();
191
192 $qtyTimeoff = $shiftsDuration->getQtyTimeoff();
193 $durationTimeoff = $shiftsDuration->getDurationTimeoff();
194
195 if( $qtyTimeoff ){
196 $qty = $qty - $qtyTimeoff;
197 $duration = $duration - $durationTimeoff;
198 }
199
200 $return = array();
201 // $return[] = $this->ui->makeBlockInline( $shiftsDuration->getQty() )
202 // ->addAttr( 'title', '__Number Of Shifts__' )
203 // ;
204 $thisView = $shiftsDuration->formatDuration($duration);
205 $return[] = $this->ui->makeSpan( $thisView )
206 ->addAttr( 'title', '__Hours__' . ': ' . $thisView )
207 ;
208 $return[] = $this->ui->makeBlockInline( '(' . $qty . ')' )
209 ->addAttr( 'title', '__Shifts__' . ': ' . $qty )
210 ->tag('muted')
211 ->tag('font-size', 2)
212 ;
213
214 $return = $isVert ? $this->ui->makeList( $return )->gutter(1) : $this->ui->makeListInline( $return )->gutter(1);
215
216 if( $qtyTimeoff ){
217 $timeoffReturn = array();
218 $timeoffReturn[] = $this->ui->makeSpan( $shiftsDuration->formatDuration($durationTimeoff) )
219 ->addAttr( 'title', '__Hours__' )
220 ;
221 $timeoffReturn[] = $this->ui->makeBlockInline( '(' . $qtyTimeoff . ')' )
222 ->addAttr( 'title', '__Time Off__' )
223 ->tag('muted')
224 ->tag('font-size', 2)
225 ;
226 $timeoffReturn = $this->ui->makeListInline( $timeoffReturn )
227 ->gutter(1)
228 ;
229
230 $return = $isVert ? $this->ui->makeList( array($return, $timeoffReturn) )->gutter(1) : $this->ui->makeListInline( array($return, $timeoffReturn) )->gutter(1);
231 }
232
233 if( $isVert ){
234 $return = $this->ui->makeBlock( $return )
235 // ->tag('border')
236 ->tag('padding', 1 )
237 ;
238 }
239 else {
240 $return = $this->ui->makeBlockInline( $return )
241 ->tag('border')
242 ->tag('padding', 1 )
243 ;
244 }
245 if( $alignRight ){
246 $return = $this->ui->makeBlock( $return )
247 ->tag('align', 'right')
248 ;
249 }
250
251 return $return;
252 }
253
254 public function filterShifts( $return )
255 {
256 $currentUser = $this->auth->getCurrentUser();
257 $currentUserId = $currentUser->getId();
258
259 $allCalendars = $this->self->findAllCalendars();
260
261 $meEmployeeId = NULL;
262 $meEmployeeCalendars = array();
263
264 $meEmployee = $this->appQuery->findEmployeeByUser( $currentUser );
265 if( $meEmployee ){
266 $meEmployeeId = $meEmployee->getId();
267 $meEmployeeCalendars = $this->appQuery->filterCalendarsForEmployee( $allCalendars, $meEmployee );
268 }
269
270 $myManagedCalendars = array();
271 $myViewedCalendars = array();
272 if( $currentUserId ){
273 $myManagedCalendars = $this->appQuery->filterCalendarsManagedByUser( $allCalendars, $currentUser );
274 $myViewedCalendars = $this->appQuery->filterCalendarsViewedByUser( $allCalendars, $currentUser );
275 }
276
277 $ids = array_keys( $return );
278 foreach( $ids as $id ){
279 $shift = $return[$id];
280
281 $shiftCalendar = $shift->getCalendar();
282 $shiftCalendarId = $shiftCalendar->getId();
283 $shiftEmployee = $shift->getEmployee();
284 $shiftEmployeeId = $shiftEmployee->getId();
285
286 // is manager?
287 if( isset($myManagedCalendars[$shiftCalendarId]) ){
288 continue;
289 }
290
291 // is viewer?
292 if( isset($myViewedCalendars[$shiftCalendarId]) ){
293 continue;
294 }
295
296 // is visitor
297 if( ! $meEmployeeId ){
298 if( $shift->isOpen() ){
299 $permName = $shift->isPublished() ? 'visitor_view_open_publish' : 'visitor_view_open_draft';
300 }
301 else {
302 $permName = $shift->isPublished() ? 'visitor_view_others_publish' : 'visitor_view_others_draft';
303 }
304 $perm = $this->calendarsPermissions->get( $shiftCalendar, $permName );
305 if( ! $perm ){
306 unset( $return[$id] );
307 }
308 continue;
309 }
310
311 // is shift employee
312 if( $meEmployeeId == $shiftEmployeeId ){
313 $permName = $shift->isPublished() ? 'employee_view_own_publish' : 'employee_view_own_draft';
314 $perm = $this->calendarsPermissions->get( $shiftCalendar, $permName );
315 if( ! $perm ){
316 unset( $return[$id] );
317 }
318 continue;
319 }
320
321 // is other employee
322 if( $meEmployeeId != $shiftEmployeeId ){
323 // calendar employees
324 if( isset($meEmployeeCalendars[$shiftCalendarId]) ){
325 if( $shift->isOpen() ){
326 $permName = $shift->isPublished() ? 'employee_view_open_publish' : 'employee_view_open_draft';
327 }
328 else {
329 $permName = $shift->isPublished() ? 'employee_view_others_publish' : 'employee_view_others_draft';
330 }
331 }
332 // other employees
333 else {
334 if( $shift->isOpen() ){
335 $permName = $shift->isPublished() ? 'employee2_view_open_publish' : 'employee2_view_open_draft';
336 }
337 else {
338 $permName = $shift->isPublished() ? 'employee2_view_others_publish' : 'employee2_view_others_draft';
339 }
340 }
341
342 $perm = $this->calendarsPermissions->get( $shiftCalendar, $permName );
343 if( ! $perm ){
344 unset( $return[$id] );
345 }
346
347 continue;
348 }
349 }
350
351 // sort
352 uasort( $return, array($this, '_sortShifts') );
353
354 return $return;
355 }
356
357 public function _sortShifts( $a, $b )
358 {
359 $compare1 = $a->getStart();
360 $compare2 = $b->getStart();
361 if( $compare1 != $compare2 ){
362 return ( $compare1 > $compare2 ) ? 1 : -1;
363 }
364
365 $compare1 = $a->getEnd();
366 $compare2 = $b->getEnd();
367 if( $compare1 != $compare2 ){
368 return ( $compare1 > $compare2 ) ? 1 : -1;
369 }
370
371 $cal1 = $a->getCalendar();
372 $cal2 = $b->getCalendar();
373 if( ! ($cal1 && $cal2) ){
374 return 0;
375 }
376
377 $compare1 = $cal1->getSortOrder();
378 $compare2 = $cal2->getSortOrder();
379 if( $compare1 != $compare2 ){
380 return ( $compare1 > $compare2 ) ? 1 : -1;
381 }
382
383 $compare1 = $cal1->getTitle();
384 $compare2 = $cal2->getTitle();
385 if( $compare1 != $compare2 ){
386 return strcmp( $compare1, $compare2 );
387 }
388
389 $emp1 = $a->getEmployee();
390 $emp2 = $b->getEmployee();
391 if( ! ($emp1 && $emp2) ){
392 return 0;
393 }
394
395 $compare1 = $emp1->getSortOrder();
396 $compare2 = $emp2->getSortOrder();
397 if( $compare1 != $compare2 ){
398 return ( $compare1 > $compare2 ) ? 1 : -1;
399 }
400
401 $compare1 = $emp1->getTitle();
402 $compare2 = $emp2->getTitle();
403 if( $compare1 != $compare2 ){
404 return strcmp( $compare1, $compare2 );
405 }
406 }
407
408 public function filterViewTypes( $return )
409 {
410 $currentUser = $this->auth->getCurrentUser();
411 $currentUserId = $currentUser->getId();
412 if( ! $currentUserId ){
413 unset( $return['report'] );
414 }
415 return $return;
416 }
417
418 public function findAllEmployees()
419 {
420 // $return = $this->employeesQuery->findActive();
421 $return = $this->employeesQuery->findAll();
422
423 $currentUser = $this->auth->getCurrentUser();
424 $currentUserId = $currentUser->getId();
425
426 $employee = $this->appQuery->findEmployeeByUser( $currentUser );
427 $managedCalendars = $this->appQuery->findCalendarsManagedByUser( $currentUser );
428 $viewedCalendars = $this->appQuery->findCalendarsViewedByUser( $currentUser );
429
430 $allowed = array();
431 if( $employee ){
432 $allowed[ $employee->getId() ] = $employee;
433 }
434
435 $calendars = $this->self->findAllCalendars();
436 foreach( $calendars as $calendar ){
437 $thisCalendarId = $calendar->getId();
438
439 // $thisAllowed = $this->appQuery->findEmployeesForCalendar( $calendar );
440 $thisAllowed = $this->appQuery->filterEmployeesForCalendar( $return, $calendar );
441
442 if( isset($managedCalendars[$thisCalendarId]) ){
443 $allowed = $allowed + $thisAllowed;
444 continue;
445 }
446
447 if( isset($viewedCalendars[$thisCalendarId]) ){
448 $allowed = $allowed + $thisAllowed;
449 continue;
450 }
451
452 // OPEN?
453 if( isset($thisAllowed[0]) ){
454 $permNames = array();
455 $permNames[] = 'visitor_view_open_publish';
456 $permNames[] = 'visitor_view_open_draft';
457 if( $employee ){
458 $permNames[] = 'employee_view_open_publish';
459 $permNames[] = 'employee_view_open_draft';
460 }
461
462 reset( $permNames );
463 foreach( $permNames as $permName ){
464 $perm = $this->calendarsPermissions->get( $calendar, $permName );
465 if( $perm ){
466 $allowed[0] = $thisAllowed[0];
467 break;
468 }
469 }
470
471 unset( $thisAllowed[0] );
472 }
473
474 $permNames = array();
475 $permNames[] = 'visitor_view_others_publish';
476 $permNames[] = 'visitor_view_others_draft';
477 if( $employee ){
478 $permNames[] = 'employee_view_others_publish';
479 $permNames[] = 'employee_view_others_draft';
480 }
481
482 reset( $permNames );
483 foreach( $permNames as $permName ){
484 $perm = $this->calendarsPermissions->get( $calendar, $permName );
485 if( $perm ){
486 $allowed = $allowed + $thisAllowed;
487 break;
488 }
489 }
490
491 // PICKUP?
492 if( $employee ){
493 $permNames = array();
494 $permNames[] = 'employee_pickup_others';
495
496 reset( $permNames );
497 foreach( $permNames as $permName ){
498 $perm = $this->calendarsPermissions->get( $calendar, $permName );
499 if( $perm ){
500 $allowed = $allowed + $thisAllowed;
501 break;
502 }
503 }
504 }
505
506 // $allowed = $allowed + $thisReturn;
507 }
508
509 $ids = array_keys( $return );
510 foreach( $ids as $id ){
511 if( ! array_key_exists($id, $allowed) ){
512 unset( $return[$id] );
513 }
514 }
515
516 return $return;
517 }
518
519 public function findAllCalendars()
520 {
521 static $return = null;
522 if( null !== $return ){
523 return $return;
524 }
525
526 // $return = $this->calendarsQuery->findActive();
527 $return = $this->calendarsQuery->findAll();
528
529 $params = $this->request->getParams();
530 if( isset($params['calendar']) && $params['calendar'] ){
531 $paramCalendarIdList = $params['calendar'];
532 if( $paramCalendarIdList && (! is_array($paramCalendarIdList)) ){
533 $paramCalendarIdList = array( $paramCalendarIdList );
534 }
535
536 $excludeIdList = array();
537 foreach( $paramCalendarIdList as $id ){
538 if( $id < 0 ){
539 $excludeIdList[ -$id ] = -$id;
540 }
541 }
542
543 if( $excludeIdList ){
544 $return = array_diff_key( $return, $excludeIdList );
545
546 $newParamCalendarIdList = array();
547 foreach( $paramCalendarIdList as $id ){
548 if( $id < 0 ) continue;
549 $newParamCalendarIdList[] = $id;
550 }
551
552 $this->request->setParam( 'calendar', $newParamCalendarIdList );
553 }
554 }
555
556 $allowed = array();
557
558 $currentUser = $this->auth->getCurrentUser();
559 $currentUserId = $currentUser->getId();
560 $employee = $this->appQuery->findEmployeeByUser( $currentUser );
561
562 if( $currentUserId ){
563 $thisReturn = $this->appQuery->filterCalendarsManagedByUser( $return, $currentUser );
564 $allowed = $allowed + $thisReturn;
565
566 $thisReturn = $this->appQuery->filterCalendarsViewedByUser( $return, $currentUser );
567 $allowed = $allowed + $thisReturn;
568 }
569
570 $calendarsForEmployee = array();
571 if( $employee ){
572 $calendarsForEmployee = $this->appQuery->filterCalendarsForEmployee( $return, $employee );
573 $allowed = $allowed + $calendarsForEmployee;
574 }
575
576 foreach( $return as $calendar ){
577 $calendarId = $calendar->getId();
578
579 $permNames = array();
580 $permNames[] = 'visitor_view_others_publish';
581 $permNames[] = 'visitor_view_others_draft';
582 $permNames[] = 'visitor_view_open_publish';
583 $permNames[] = 'visitor_view_open_draft';
584
585 if( $currentUserId ){
586 // if( ! isset($calendarsForEmployee[$currentUserId]) ){
587 // continue;
588 // }
589
590 if( isset($calendarsForEmployee[$currentUserId]) ){
591 $permNames[] = 'employee_view_others_publish';
592 $permNames[] = 'employee_view_others_draft';
593 }
594 else {
595 $permNames[] = 'employee2_view_others_publish';
596 $permNames[] = 'employee2_view_others_draft';
597 }
598 }
599
600 reset( $permNames );
601 foreach( $permNames as $permName ){
602 $perm = $this->calendarsPermissions->get( $calendar, $permName );
603 if( $perm ){
604 $allowed[ $calendarId ] = $calendar;
605 break;
606 }
607 }
608 }
609
610 $ids = array_keys( $return );
611 foreach( $ids as $id ){
612 // if( ! array_key_exists($id, $allowed) ){
613 if( ! isset($allowed[$id]) ){
614 unset( $return[$id] );
615 }
616 }
617
618 return $return;
619 }
620
621 public function getShifts()
622 {
623 static $cache = null;
624 if( null !== $cache ) return $cache;
625
626 // init params
627 $params = $this->request->getParams();
628 if( isset($params['time']) && ('now' == $params['time']) ){
629 $params['time'] = $this->t->setNow()->formatDateTimeDb();
630 }
631
632 $type = $params['type'];
633 $startDate = $params['start'];
634 $endDate = $params['end'];
635
636 if( 'start-week' == $startDate ){
637 $this->t->setNow()->setStartWeek();
638 $startDate = $this->t->formatDateDb();
639 }
640 elseif( 'start-month' == $startDate ){
641 $this->t->setNow()->setStartMonth();
642 $startDate = $this->t->formatDateDb();
643 }
644 elseif( 'start-year' == $startDate ){
645 $this->t->setNow()->setStartYear();
646 $startDate = $this->t->formatDateDb();
647 }
648 elseif( 'first-non-blank' == $startDate ){
649 $now = $this->t->setNow()->setStartDay()->formatDateTimeDb();
650
651 $q = array();
652 $q[] = array( 'sort', 'starts_at', 'asc' );
653 $q[] = array( 'starts_at', '>', $now );
654 $q[] = array( 'limit', 1 );
655
656 $allCalendars = $this->self->findAllCalendars();
657 $listCalendarId = array_keys( $allCalendars );
658 $calendarFilter = $this->getCalendarFilter();
659 if( $calendarFilter ){
660 $listCalendarId = array_intersect( $listCalendarId, $calendarFilter );
661 }
662 $q[] = array( 'calendar_id', 'IN', $listCalendarId );
663
664 $allEmployees = $this->self->findAllEmployees();
665 $listEmployeeId = array_keys( $allEmployees );
666 $employeeFilter = $this->getEmployeeFilter();
667 if( $employeeFilter ){
668 $listEmployeeId = array_intersect( $listEmployeeId, $employeeFilter );
669 }
670 $q[] = array( 'employee_id', 'IN', $listEmployeeId );
671
672 $res = $this->shiftsQuery->read( $q );
673 if( $res ){
674 $shift = current( $res );
675 $startDate = $this->t->setDateTimeDb( $shift->getStart() )->formatDateDb();
676 }
677 else {
678 $startDate = $this->t->setDateTimeDb( $now )->formatDateDb();
679 }
680 }
681 elseif( 'first-blank' == $startDate ){
682 $now = $this->t->setNow()->setStartDay()->formatDateTimeDb();
683
684 $q = array();
685 $q[] = array( 'sort', 'ends_at', 'desc' );
686 $q[] = array( 'limit', 1 );
687
688 $allCalendars = $this->self->findAllCalendars();
689 $listCalendarId = array_keys( $allCalendars );
690 $calendarFilter = $this->getCalendarFilter();
691 if( $calendarFilter ){
692 $listCalendarId = array_intersect( $listCalendarId, $calendarFilter );
693 }
694 $q[] = array( 'calendar_id', 'IN', $listCalendarId );
695
696 $allEmployees = $this->self->findAllEmployees();
697 $listEmployeeId = array_keys( $allEmployees );
698 $employeeFilter = $this->getEmployeeFilter();
699 if( $employeeFilter ){
700 $listEmployeeId = array_intersect( $listEmployeeId, $employeeFilter );
701 }
702 $q[] = array( 'employee_id', 'IN', $listEmployeeId );
703
704 $res = $this->shiftsQuery->read( $q );
705 if( $res ){
706 $shift = current( $res );
707 $startDate = $this->t->setDateTimeDb( $shift->getStart() )->formatDateDb();
708 }
709 else {
710 $startDate = $this->t->setDateTimeDb( $now )->formatDateDb();
711 }
712
713 switch( $type ){
714 case 'day':
715 $startDate = $this->t->setDateDb( $startDate )->modify( '+1 day' )->formatDateDb();
716 break;
717
718 case 'week':
719 $startDate = $this->t->setDateDb( $startDate )->modify( '+1 week' )->setStartWeek()->formatDateDb();
720 break;
721
722 case '4weeks':
723 $nWeeks = $this->settings->get( 'datetime_n_weeks' );
724 if( ! $nWeeks ) $nWeeks = 4;
725 $startDate = $this->t->setDateDb( $startDate )->modify('+' . $nWeeks . ' weeks')->setStartWeek()->formatDateDb();
726 break;
727
728 case '2weeks':
729 $startDate = $this->t->setDateDb( $startDate )->modify('+2 weeks')->setStartWeek()->formatDateDb();
730 break;
731
732 case 'month':
733 $startDate = $this->t->setDateDb( $startDate )->modify( '+1 month' )->setStartMonth()->formatDateDb();
734 break;
735 }
736 }
737 else {
738 if( (FALSE !== strpos($startDate, '+')) OR (FALSE !== strpos($startDate, '-')) ){
739 $this->t->setNow()->setStartDay()->modify($startDate);
740 $startDate = $this->t->formatDateDb();
741 }
742 }
743
744 if( FALSE !== strpos($endDate, '+') ){
745 $this->t->setDateDb($startDate)->modify($endDate)->modify('-1 day')->setEndDay();
746 $endDate = $this->t->formatDateDb();
747 }
748
749 $exactDateTime = isset( $params['time'] ) ? $params['time'] : NULL;
750 if( $exactDateTime ){
751 $startDate = $this->t->setDateTimeDb( $exactDateTime )->formatDateDb();
752 $endDate = $startDate;
753 }
754
755 switch( $type ){
756 case 'day':
757 $this->t->setDateDb($startDate);
758 $start = $this->t->formatDateTimeDb();
759
760 $startDate = $this->t->formatDateDb();
761 $this->request->setParam('start', $startDate);
762 $endDate = $this->t->formatDateDb();
763 $this->request->setParam('end', $endDate);
764
765 $daysToShow = isset( SH4_Schedule_Html_View_Day::$daysToShow ) ? SH4_Schedule_Html_View_Day::$daysToShow : 1;
766 if( $daysToShow > 1 ){
767 $this->t->modify( '+' . ($daysToShow - 1) . ' days' );
768 }
769 $end = $this->t->setEndDay()->formatDateTimeDb();
770 break;
771
772 case 'week':
773 $this->t->setDateDb($startDate)->setStartWeek();
774 $startDate = $this->t->formatDateDb();
775 $this->request->setParam('start', $startDate);
776
777 $start = $this->t->formatDateTimeDb();
778 $this->t->setEndWeek();
779 // $this->t->modify('+1 week')
780 // ->modify('-1 day')
781 // ;
782 $endDate = $this->t->formatDateDb();
783
784 $this->request->setParam('end', $endDate);
785 $end = $this->t->setEndDay()->formatDateTimeDb();
786 break;
787
788 case '4weeks':
789 $nWeeks = $this->settings->get( 'datetime_n_weeks' );
790 if( ! $nWeeks ) $nWeeks = 4;
791
792 $this->t->setDateDb($startDate)->setStartWeek();
793 $startDate = $this->t->formatDateDb();
794 $this->request->setParam('start', $startDate);
795
796 $start = $this->t->formatDateTimeDb();
797 $this->t->modify('+' . $nWeeks . ' weeks')
798 ->modify('-1 day')
799 ;
800 $endDate = $this->t->formatDateDb();
801 $this->request->setParam('end', $endDate);
802
803 $end = $this->t->setEndDay()->formatDateTimeDb();
804 break;
805
806 case '2weeks':
807 $this->t->setDateDb($startDate)->setStartWeek();
808 $startDate = $this->t->formatDateDb();
809 $this->request->setParam('start', $startDate);
810
811 $start = $this->t->formatDateTimeDb();
812 $this->t->modify('+2 weeks')
813 ->modify('-1 day')
814 ;
815 $endDate = $this->t->formatDateDb();
816 $this->request->setParam('end', $endDate);
817
818 $end = $this->t->setEndDay()->formatDateTimeDb();
819 break;
820
821 case 'month':
822 $this->t->setDateDb($startDate)->setStartMonth();
823 $startDate = $this->t->formatDateDb();
824 $this->request->setParam('start', $startDate);
825
826 $start = $this->t->formatDateTimeDb();
827 $this->t->setEndMonth();
828 // $this->t->modify('+1 month')
829 // ->modify('-1 day')
830 // ;
831 $endDate = $this->t->formatDateDb();
832 $this->request->setParam('end', $endDate);
833
834 $end = $this->t->setEndDay()->formatDateTimeDb();
835 break;
836
837 default:
838 if( $exactDateTime ){
839 $start = $exactDateTime;
840 $end = $exactDateTime;
841 }
842 else {
843 $this->t->setDateDb($startDate);
844 $start = $this->t->formatDateTimeDb();
845
846 // $this->t->setDateDb($endDate)->modify('+1 day');
847 $this->t->setDateDb($endDate);
848 $end = $this->t->setEndDay()->formatDateTimeDb();
849 }
850
851 break;
852 }
853
854 $this->shiftsQuery
855 ->setStart( $start )
856 ->setEnd( $end )
857 ;
858
859 $return = $this->shiftsQuery->find();
860
861 // filter
862 $employees = $this->getEmployees();
863 $calendars = $this->getCalendars();
864
865 $employeeFilter = $this->getEmployeeFilter();
866 $calendarFilter = $this->getCalendarFilter();
867
868 foreach( array_keys($return) as $id ){
869 $shift = $return[$id];
870
871 $calendar = $shift->getCalendar();
872 // if( ! array_key_exists($calendar->getId(), $calendars) ){
873 // unset( $return[$id] );
874 // continue;
875 // }
876 if( $calendarFilter && (! in_array($calendar->getId(), $calendarFilter)) ){
877 unset( $return[$id] );
878 continue;
879 }
880
881 $employee = $shift->getEmployee();
882 // if( ! array_key_exists($employee->getId(), $employees) ){
883 // unset( $return[$id] );
884 // continue;
885 // }
886
887 $employeeId = $employee->getId();
888
889 if( $employeeFilter ){
890 // any assigned
891 if( in_array(-1, $employeeFilter) ){
892 if( ! $employeeId ){
893 unset( $return[$id] );
894 continue;
895 }
896 }
897 else {
898 if( ! in_array($employeeId, $employeeFilter) ){
899 unset( $return[$id] );
900 continue;
901 }
902 }
903 }
904
905 $shiftStart = $shift->getStart();
906 $shiftEnd = $shift->getEnd();
907
908 if( $shiftEnd <= $start ){
909 unset( $return[$id] );
910 continue;
911 }
912
913 if( $shiftStart >= $end ){
914 unset( $return[$id] );
915 continue;
916 }
917
918 // if( $shiftStart < $start ){
919 // unset( $return[$id] );
920 // continue;
921 // }
922 }
923
924 $params = $this->request->getParams();
925
926 // skip those shifts that start before period and end during the first day
927 // $when = array( 'report', 'list' );
928 // if( in_array('report', $when) ){
929 if( 1 ){
930 $startRange = $this->t->setDateDb( $params['start'] )->formatDateTimeDb();
931 $endFirstDay = $this->t->setDateDb( $params['start'] )->modify('+1 day')->formatDateTimeDb();
932
933 $ids = array_keys( $return );
934 foreach( $ids as $id ){
935 $shift = $return[ $id ];
936 if( $shift->getStart() >= $startRange ) continue;
937 if( $shift->getEnd() >= $endFirstDay ) continue;
938 unset( $return[$id] );
939 }
940 }
941
942 // filter conflicts?
943 if( isset($params['conflict']) && $params['conflict'] ){
944 $withConflicts = ( $params['conflict'] > 0 ) ? true : false;
945
946 $ids = array_keys( $return );
947 foreach( $ids as $id ){
948 $shift = $return[ $id ];
949 $conflicts = $this->conflicts->get( $shift );
950 if( $withConflicts && (! $conflicts) ){
951 unset( $return[$id] );
952 }
953
954 if( (! $withConflicts) && $conflicts ){
955 unset( $return[$id] );
956 }
957 }
958 }
959
960 // t1: array of start/end time like this: 32400-576000_36000-64800
961 if (isset($params['t'])) {
962 $tParam = $params['t'];
963
964 if (false !== strpos($tParam, '_')) {
965 $tParam = explode('_', $tParam);
966 } else {
967 $tParam = [$tParam];
968 }
969
970 foreach (array_keys($tParam) as $ii) {
971 $tParam[$ii] = explode('-', $tParam[$ii]);
972 $tParam[$ii] = array_map('intval', $tParam[$ii]);
973 }
974
975 foreach (array_keys($return) as $id) {
976 $startTime = (int) $return[$id]->getStartInDay();
977 $endTime = (int) $return[$id]->getEndInDay();
978
979 $ok = false;
980 reset($tParam);
981 foreach ($tParam as $p) {
982 if (($p[0] == $startTime) && ($p[1] == $endTime)) {
983 $ok = true;
984 break;
985 }
986 }
987
988 if (!$ok) {
989 unset($return[$id]);
990 continue;
991 }
992 }
993 }
994
995 // t1 or t2 params: start/end time
996 if (isset($params['t1']) or isset($params['t2'])) {
997 $t1 = $t2 = array();
998
999 if (isset($params['t1'])) {
1000 $t1 = $params['t1'];
1001 if (false !== strpos($t1, '-')) {
1002 $t1 = explode('-', $t1);
1003 } else {
1004 $t1 = [$t1];
1005 }
1006 $t1 = array_map('intval', $t1);
1007 }
1008
1009 if (isset($params['t2'])) {
1010 $t2 = $params['t2'];
1011 if (false !== strpos($t2, '-')) {
1012 $t2 = explode('-', $t2);
1013 } else {
1014 $t2 = [$t2];
1015 }
1016 $t2 = array_map('intval', $t2);
1017 }
1018
1019 foreach (array_keys($return) as $id) {
1020 if ($t1) {
1021 $startTime = (int) $return[$id]->getStartInDay();
1022 if (!in_array($startTime, $t1)) {
1023 unset($return[$id]);
1024 continue;
1025 }
1026 }
1027
1028 if ($t2) {
1029 $endTime = (int) $return[$id]->getEndInDay();
1030 if (!in_array($endTime, $t2)) {
1031 unset($return[$id]);
1032 continue;
1033 }
1034 }
1035 }
1036 }
1037
1038 $return = $this->self->filterShifts( $return );
1039 $cache = $return;
1040 return $return;
1041 }
1042
1043 public function getCalendars()
1044 {
1045 $return = $this->self->findAllCalendars();
1046 $params = $this->request->getParams();
1047
1048 if( $params['calendar'] ){
1049 $filterIds = $params['calendar'];
1050
1051 $ids = array_keys($return);
1052 foreach( $ids as $id ){
1053 if( ! in_array($id, $filterIds) ){
1054 unset( $return[$id] );
1055 }
1056 }
1057 }
1058
1059 if( $params['employee'] ){
1060 $employees = $this->self->findAllEmployees();
1061
1062 $filterEmployeeIds = $params['employee'];
1063 $filter = array();
1064 foreach( $employees as $employee ){
1065 $employeeId = $employee->getId();
1066 if( ! (in_array(-1, $filterEmployeeIds) && ($employeeId > 0)) ){
1067 if( ! in_array($employeeId, $filterEmployeeIds) ){
1068 continue;
1069 }
1070 }
1071 $filter = $filter + $this->appQuery->filterCalendarsForEmployee( $return, $employee );
1072 }
1073 $filterIds = array_keys( $filter );
1074
1075 $ids = array_keys($return);
1076 foreach( $ids as $id ){
1077 if( ! in_array($id, $filterIds) ){
1078 unset( $return[$id] );
1079 }
1080 }
1081 }
1082
1083 return $return;
1084 }
1085
1086 public function getEmployees()
1087 {
1088 $return = $this->self->findAllEmployees();
1089 $params = $this->request->getParams();
1090
1091 if( $params['employee'] ){
1092 $filterIds = $params['employee'];
1093
1094 $ids = array_keys($return);
1095 foreach( $ids as $id ){
1096 if( in_array(-1, $filterIds) && ($id > 0) ){
1097 continue;
1098 }
1099
1100 if( ! in_array($id, $filterIds) ){
1101 unset( $return[$id] );
1102 }
1103 }
1104 }
1105
1106 if( $params['calendar'] ){
1107 $calendars = $this->self->findAllCalendars();
1108
1109 $filter = array();
1110 foreach( $calendars as $calendar ){
1111 if( ! in_array($calendar->getId(), $params['calendar']) ){
1112 continue;
1113 }
1114 $filter = $filter + $this->appQuery->findEmployeesForCalendar( $calendar );
1115 }
1116 $filterIds = array_keys( $filter );
1117
1118 $ids = array_keys($return);
1119 foreach( $ids as $id ){
1120 if( ! in_array($id, $filterIds) ){
1121 unset( $return[$id] );
1122 }
1123 }
1124 }
1125
1126 return $return;
1127 }
1128
1129 public function getEmployeeFilter()
1130 {
1131 $params = $this->request->getParams();
1132 $ret = isset( $params['employee'] ) ? $params['employee'] : array();
1133 return $ret;
1134 }
1135
1136 public function getCalendarFilter()
1137 {
1138 $params = $this->request->getParams();
1139 $ret = isset( $params['calendar'] ) ? $params['calendar'] : array();
1140 return $ret;
1141 }
1142 }