PluginProbe
ShiftController Employee Shift Scheduling / 4.9.59
ShiftController Employee Shift Scheduling v4.9.59
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.59, at sh4/schedule/html/view/common.php

969 lines 25.1 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 )
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 $return[] = $this->ui->makeSpan( $shiftsDuration->formatDuration($duration) )
205 ->addAttr( 'title', '__Hours__' )
206 ;
207 $return[] = $this->ui->makeBlockInline( '(' . $qty . ')' )
208 ->addAttr( 'title', '__Shifts__' )
209 ->tag('muted')
210 ->tag('font-size', 2)
211 ;
212 $return = $this->ui->makeListInline( $return )
213 ->gutter(1)
214 ;
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 = $this->ui->makeListInline( array($return, $timeoffReturn) )
231 ->gutter(1)
232 ;
233 }
234
235 $return = $this->ui->makeBlockInline( $return )
236 ->tag('border')
237 ->tag('padding', 1 )
238 ;
239 if( $alignRight ){
240 $return = $this->ui->makeBlock( $return )
241 ->tag('align', 'right')
242 ;
243 }
244
245 return $return;
246 }
247
248 public function filterShifts( $return )
249 {
250 $currentUser = $this->auth->getCurrentUser();
251 $currentUserId = $currentUser->getId();
252
253 $allCalendars = $this->self->findAllCalendars();
254
255 $meEmployeeId = NULL;
256 $meEmployeeCalendars = array();
257
258 $meEmployee = $this->appQuery->findEmployeeByUser( $currentUser );
259 if( $meEmployee ){
260 $meEmployeeId = $meEmployee->getId();
261 $meEmployeeCalendars = $this->appQuery->filterCalendarsForEmployee( $allCalendars, $meEmployee );
262 }
263
264 $myManagedCalendars = array();
265 $myViewedCalendars = array();
266 if( $currentUserId ){
267 $myManagedCalendars = $this->appQuery->filterCalendarsManagedByUser( $allCalendars, $currentUser );
268 $myViewedCalendars = $this->appQuery->filterCalendarsViewedByUser( $allCalendars, $currentUser );
269 }
270
271 $ids = array_keys( $return );
272 foreach( $ids as $id ){
273 $shift = $return[$id];
274
275 $shiftCalendar = $shift->getCalendar();
276 $shiftCalendarId = $shiftCalendar->getId();
277 $shiftEmployee = $shift->getEmployee();
278 $shiftEmployeeId = $shiftEmployee->getId();
279
280 // is manager?
281 if( isset($myManagedCalendars[$shiftCalendarId]) ){
282 continue;
283 }
284
285 // is viewer?
286 if( isset($myViewedCalendars[$shiftCalendarId]) ){
287 continue;
288 }
289
290 // is visitor
291 if( ! $meEmployeeId ){
292 if( $shift->isOpen() ){
293 $permName = $shift->isPublished() ? 'visitor_view_open_publish' : 'visitor_view_open_draft';
294 }
295 else {
296 $permName = $shift->isPublished() ? 'visitor_view_others_publish' : 'visitor_view_others_draft';
297 }
298 $perm = $this->calendarsPermissions->get( $shiftCalendar, $permName );
299 if( ! $perm ){
300 unset( $return[$id] );
301 }
302 continue;
303 }
304
305 // is shift employee
306 if( $meEmployeeId == $shiftEmployeeId ){
307 $permName = $shift->isPublished() ? 'employee_view_own_publish' : 'employee_view_own_draft';
308 $perm = $this->calendarsPermissions->get( $shiftCalendar, $permName );
309 if( ! $perm ){
310 unset( $return[$id] );
311 }
312 continue;
313 }
314
315 // is other employee
316 if( $meEmployeeId != $shiftEmployeeId ){
317 // calendar employees
318 if( isset($meEmployeeCalendars[$shiftCalendarId]) ){
319 if( $shift->isOpen() ){
320 $permName = $shift->isPublished() ? 'employee_view_open_publish' : 'employee_view_open_draft';
321 }
322 else {
323 $permName = $shift->isPublished() ? 'employee_view_others_publish' : 'employee_view_others_draft';
324 }
325 }
326 // other employees
327 else {
328 if( $shift->isOpen() ){
329 $permName = $shift->isPublished() ? 'employee2_view_open_publish' : 'employee2_view_open_draft';
330 }
331 else {
332 $permName = $shift->isPublished() ? 'employee2_view_others_publish' : 'employee2_view_others_draft';
333 }
334 }
335
336 $perm = $this->calendarsPermissions->get( $shiftCalendar, $permName );
337 if( ! $perm ){
338 unset( $return[$id] );
339 }
340
341 continue;
342 }
343 }
344
345 // sort
346 uasort( $return, array($this, '_sortShifts') );
347
348 return $return;
349 }
350
351 public function _sortShifts( $a, $b )
352 {
353 $compare1 = $a->getStart();
354 $compare2 = $b->getStart();
355 if( $compare1 != $compare2 ){
356 return ( $compare1 > $compare2 ) ? 1 : -1;
357 }
358
359 $compare1 = $a->getEnd();
360 $compare2 = $b->getEnd();
361 if( $compare1 != $compare2 ){
362 return ( $compare1 > $compare2 ) ? 1 : -1;
363 }
364
365 $cal1 = $a->getCalendar();
366 $cal2 = $b->getCalendar();
367 if( ! ($cal1 && $cal2) ){
368 return 0;
369 }
370
371 $compare1 = $cal1->getSortOrder();
372 $compare2 = $cal2->getSortOrder();
373 if( $compare1 != $compare2 ){
374 return ( $compare1 > $compare2 ) ? 1 : -1;
375 }
376
377 $compare1 = $cal1->getTitle();
378 $compare2 = $cal2->getTitle();
379 if( $compare1 != $compare2 ){
380 return strcmp( $compare1, $compare2 );
381 }
382
383 $emp1 = $a->getEmployee();
384 $emp2 = $b->getEmployee();
385 if( ! ($emp1 && $emp2) ){
386 return 0;
387 }
388
389 $compare1 = $emp1->getSortOrder();
390 $compare2 = $emp2->getSortOrder();
391 if( $compare1 != $compare2 ){
392 return ( $compare1 > $compare2 ) ? 1 : -1;
393 }
394
395 $compare1 = $emp1->getTitle();
396 $compare2 = $emp2->getTitle();
397 if( $compare1 != $compare2 ){
398 return strcmp( $compare1, $compare2 );
399 }
400 }
401
402 public function filterViewTypes( $return )
403 {
404 $currentUser = $this->auth->getCurrentUser();
405 $currentUserId = $currentUser->getId();
406 if( ! $currentUserId ){
407 unset( $return['report'] );
408 }
409 return $return;
410 }
411
412 public function findAllEmployees()
413 {
414 // $return = $this->employeesQuery->findActive();
415 $return = $this->employeesQuery->findAll();
416
417 $currentUser = $this->auth->getCurrentUser();
418 $currentUserId = $currentUser->getId();
419
420 $employee = $this->appQuery->findEmployeeByUser( $currentUser );
421 $managedCalendars = $this->appQuery->findCalendarsManagedByUser( $currentUser );
422 $viewedCalendars = $this->appQuery->findCalendarsViewedByUser( $currentUser );
423
424 $allowed = array();
425 if( $employee ){
426 $allowed[ $employee->getId() ] = $employee;
427 }
428
429 $calendars = $this->self->findAllCalendars();
430 foreach( $calendars as $calendar ){
431 $thisCalendarId = $calendar->getId();
432
433 // $thisAllowed = $this->appQuery->findEmployeesForCalendar( $calendar );
434 $thisAllowed = $this->appQuery->filterEmployeesForCalendar( $return, $calendar );
435
436 if( isset($managedCalendars[$thisCalendarId]) ){
437 $allowed = $allowed + $thisAllowed;
438 continue;
439 }
440
441 if( isset($viewedCalendars[$thisCalendarId]) ){
442 $allowed = $allowed + $thisAllowed;
443 continue;
444 }
445
446 // OPEN?
447 if( isset($thisAllowed[0]) ){
448 $permNames = array();
449 $permNames[] = 'visitor_view_open_publish';
450 $permNames[] = 'visitor_view_open_draft';
451 if( $employee ){
452 $permNames[] = 'employee_view_open_publish';
453 $permNames[] = 'employee_view_open_draft';
454 }
455
456 reset( $permNames );
457 foreach( $permNames as $permName ){
458 $perm = $this->calendarsPermissions->get( $calendar, $permName );
459 if( $perm ){
460 $allowed[0] = $thisAllowed[0];
461 break;
462 }
463 }
464
465 unset( $thisAllowed[0] );
466 }
467
468 $permNames = array();
469 $permNames[] = 'visitor_view_others_publish';
470 $permNames[] = 'visitor_view_others_draft';
471 if( $employee ){
472 $permNames[] = 'employee_view_others_publish';
473 $permNames[] = 'employee_view_others_draft';
474 }
475
476 reset( $permNames );
477 foreach( $permNames as $permName ){
478 $perm = $this->calendarsPermissions->get( $calendar, $permName );
479 if( $perm ){
480 $allowed = $allowed + $thisAllowed;
481 break;
482 }
483 }
484
485 // PICKUP?
486 if( $employee ){
487 $permNames = array();
488 $permNames[] = 'employee_pickup_others';
489
490 reset( $permNames );
491 foreach( $permNames as $permName ){
492 $perm = $this->calendarsPermissions->get( $calendar, $permName );
493 if( $perm ){
494 $allowed = $allowed + $thisAllowed;
495 break;
496 }
497 }
498 }
499
500 // $allowed = $allowed + $thisReturn;
501 }
502
503 $ids = array_keys( $return );
504 foreach( $ids as $id ){
505 if( ! array_key_exists($id, $allowed) ){
506 unset( $return[$id] );
507 }
508 }
509
510 return $return;
511 }
512
513 public function findAllCalendars()
514 {
515 static $return = null;
516 if( null !== $return ){
517 return $return;
518 }
519
520 // $return = $this->calendarsQuery->findActive();
521 $return = $this->calendarsQuery->findAll();
522
523 $params = $this->request->getParams();
524 if( isset($params['calendar']) && $params['calendar'] ){
525 $paramCalendarIdList = $params['calendar'];
526 if( $paramCalendarIdList && (! is_array($paramCalendarIdList)) ){
527 $paramCalendarIdList = array( $paramCalendarIdList );
528 }
529
530 $excludeIdList = array();
531 foreach( $paramCalendarIdList as $id ){
532 if( $id < 0 ){
533 $excludeIdList[ -$id ] = -$id;
534 }
535 }
536
537 if( $excludeIdList ){
538 $return = array_diff_key( $return, $excludeIdList );
539
540 $newParamCalendarIdList = array();
541 foreach( $paramCalendarIdList as $id ){
542 if( $id < 0 ) continue;
543 $newParamCalendarIdList[] = $id;
544 }
545
546 $this->request->setParam( 'calendar', $newParamCalendarIdList );
547 }
548 }
549
550 $allowed = array();
551
552 $currentUser = $this->auth->getCurrentUser();
553 $currentUserId = $currentUser->getId();
554 $employee = $this->appQuery->findEmployeeByUser( $currentUser );
555
556 if( $currentUserId ){
557 $thisReturn = $this->appQuery->filterCalendarsManagedByUser( $return, $currentUser );
558 $allowed = $allowed + $thisReturn;
559
560 $thisReturn = $this->appQuery->filterCalendarsViewedByUser( $return, $currentUser );
561 $allowed = $allowed + $thisReturn;
562 }
563
564 $calendarsForEmployee = array();
565 if( $employee ){
566 $calendarsForEmployee = $this->appQuery->filterCalendarsForEmployee( $return, $employee );
567 $allowed = $allowed + $calendarsForEmployee;
568 }
569
570 foreach( $return as $calendar ){
571 $calendarId = $calendar->getId();
572
573 $permNames = array();
574 $permNames[] = 'visitor_view_others_publish';
575 $permNames[] = 'visitor_view_others_draft';
576 $permNames[] = 'visitor_view_open_publish';
577 $permNames[] = 'visitor_view_open_draft';
578
579 if( $currentUserId ){
580 // if( ! isset($calendarsForEmployee[$currentUserId]) ){
581 // continue;
582 // }
583
584 if( isset($calendarsForEmployee[$currentUserId]) ){
585 $permNames[] = 'employee_view_others_publish';
586 $permNames[] = 'employee_view_others_draft';
587 }
588 else {
589 $permNames[] = 'employee2_view_others_publish';
590 $permNames[] = 'employee2_view_others_draft';
591 }
592 }
593
594 reset( $permNames );
595 foreach( $permNames as $permName ){
596 $perm = $this->calendarsPermissions->get( $calendar, $permName );
597 if( $perm ){
598 $allowed[ $calendarId ] = $calendar;
599 break;
600 }
601 }
602 }
603
604 $ids = array_keys( $return );
605 foreach( $ids as $id ){
606 // if( ! array_key_exists($id, $allowed) ){
607 if( ! isset($allowed[$id]) ){
608 unset( $return[$id] );
609 }
610 }
611
612 return $return;
613 }
614
615 public function getShifts()
616 {
617 static $cache = null;
618 if( null !== $cache ) return $cache;
619
620 // init params
621 $params = $this->request->getParams();
622 if( isset($params['time']) && ('now' == $params['time']) ){
623 $params['time'] = $this->t->setNow()->formatDateTimeDb();
624 }
625
626 $type = $params['type'];
627 $startDate = $params['start'];
628 $endDate = $params['end'];
629
630 if( 'start-week' == $startDate ){
631 $this->t->setNow()->setStartWeek();
632 $startDate = $this->t->formatDateDb();
633 }
634 elseif( 'start-month' == $startDate ){
635 $this->t->setNow()->setStartMonth();
636 $startDate = $this->t->formatDateDb();
637 }
638 elseif( 'start-year' == $startDate ){
639 $this->t->setNow()->setStartYear();
640 $startDate = $this->t->formatDateDb();
641 }
642 else {
643 if( (FALSE !== strpos($startDate, '+')) OR (FALSE !== strpos($startDate, '-')) ){
644 $this->t->setNow()->setStartDay()->modify($startDate);
645 $startDate = $this->t->formatDateDb();
646 }
647 }
648
649 if( FALSE !== strpos($endDate, '+') ){
650 $this->t->setDateDb($startDate)->modify($endDate)->modify('-1 day')->setEndDay();
651 $endDate = $this->t->formatDateDb();
652 }
653
654 $exactDateTime = isset( $params['time'] ) ? $params['time'] : NULL;
655 if( $exactDateTime ){
656 $startDate = $this->t->setDateTimeDb( $exactDateTime )->formatDateDb();
657 $endDate = $startDate;
658 }
659
660 switch( $type ){
661 case 'day':
662 $this->t->setDateDb($startDate);
663 $start = $this->t->formatDateTimeDb();
664
665 $startDate = $this->t->formatDateDb();
666 $this->request->setParam('start', $startDate);
667 $endDate = $this->t->formatDateDb();
668 $this->request->setParam('end', $endDate);
669
670 $daysToShow = isset( SH4_Schedule_Html_View_Day::$daysToShow ) ? SH4_Schedule_Html_View_Day::$daysToShow : 7;
671 if( $daysToShow ){
672 $this->t->modify( '+' . $daysToShow . ' days' );
673 }
674 $end = $this->t->setEndDay()->formatDateTimeDb();
675 break;
676
677 case 'week':
678 $this->t->setDateDb($startDate)->setStartWeek();
679 $startDate = $this->t->formatDateDb();
680 $this->request->setParam('start', $startDate);
681
682 $start = $this->t->formatDateTimeDb();
683 $this->t->setEndWeek();
684 // $this->t->modify('+1 week')
685 // ->modify('-1 day')
686 // ;
687 $endDate = $this->t->formatDateDb();
688
689 $this->request->setParam('end', $endDate);
690 $end = $this->t->setEndDay()->formatDateTimeDb();
691 break;
692
693 case '4weeks':
694 $nWeeks = $this->settings->get( 'datetime_n_weeks' );
695 if( ! $nWeeks ) $nWeeks = 4;
696
697 $this->t->setDateDb($startDate)->setStartWeek();
698 $startDate = $this->t->formatDateDb();
699 $this->request->setParam('start', $startDate);
700
701 $start = $this->t->formatDateTimeDb();
702 $this->t->modify('+' . $nWeeks . ' weeks')
703 ->modify('-1 day')
704 ;
705 $endDate = $this->t->formatDateDb();
706 $this->request->setParam('end', $endDate);
707
708 $end = $this->t->setEndDay()->formatDateTimeDb();
709 break;
710
711 case '2weeks':
712 $this->t->setDateDb($startDate)->setStartWeek();
713 $startDate = $this->t->formatDateDb();
714 $this->request->setParam('start', $startDate);
715
716 $start = $this->t->formatDateTimeDb();
717 $this->t->modify('+2 weeks')
718 ->modify('-1 day')
719 ;
720 $endDate = $this->t->formatDateDb();
721 $this->request->setParam('end', $endDate);
722
723 $end = $this->t->setEndDay()->formatDateTimeDb();
724 break;
725
726 case 'month':
727 $this->t->setDateDb($startDate)->setStartMonth();
728 $startDate = $this->t->formatDateDb();
729 $this->request->setParam('start', $startDate);
730
731 $start = $this->t->formatDateTimeDb();
732 $this->t->setEndMonth();
733 // $this->t->modify('+1 month')
734 // ->modify('-1 day')
735 // ;
736 $endDate = $this->t->formatDateDb();
737 $this->request->setParam('end', $endDate);
738
739 $end = $this->t->setEndDay()->formatDateTimeDb();
740 break;
741
742 default:
743 if( $exactDateTime ){
744 $start = $exactDateTime;
745 $end = $exactDateTime;
746 }
747 else {
748 $this->t->setDateDb($startDate);
749 $start = $this->t->formatDateTimeDb();
750
751 // $this->t->setDateDb($endDate)->modify('+1 day');
752 $this->t->setDateDb($endDate);
753 $end = $this->t->setEndDay()->formatDateTimeDb();
754 }
755
756 break;
757 }
758
759 $this->shiftsQuery
760 ->setStart( $start )
761 ->setEnd( $end )
762 ;
763
764 $return = $this->shiftsQuery->find();
765
766 // filter
767 $employees = $this->getEmployees();
768 $calendars = $this->getCalendars();
769
770 $employeeFilter = $this->getEmployeeFilter();
771 $calendarFilter = $this->getCalendarFilter();
772
773 foreach( array_keys($return) as $id ){
774 $shift = $return[$id];
775
776 $calendar = $shift->getCalendar();
777 // if( ! array_key_exists($calendar->getId(), $calendars) ){
778 // unset( $return[$id] );
779 // continue;
780 // }
781 if( $calendarFilter && (! in_array($calendar->getId(), $calendarFilter)) ){
782 unset( $return[$id] );
783 continue;
784 }
785
786 $employee = $shift->getEmployee();
787 // if( ! array_key_exists($employee->getId(), $employees) ){
788 // unset( $return[$id] );
789 // continue;
790 // }
791
792 $employeeId = $employee->getId();
793
794 if( $employeeFilter ){
795 // any assigned
796 if( in_array(-1, $employeeFilter) ){
797 if( ! $employeeId ){
798 unset( $return[$id] );
799 continue;
800 }
801 }
802 else {
803 if( ! in_array($employeeId, $employeeFilter) ){
804 unset( $return[$id] );
805 continue;
806 }
807 }
808 }
809
810 $shiftStart = $shift->getStart();
811 $shiftEnd = $shift->getEnd();
812
813 if( $shiftEnd <= $start ){
814 unset( $return[$id] );
815 continue;
816 }
817
818 if( $shiftStart >= $end ){
819 unset( $return[$id] );
820 continue;
821 }
822
823 // if( $shiftStart < $start ){
824 // unset( $return[$id] );
825 // continue;
826 // }
827 }
828
829 $params = $this->request->getParams();
830
831 // skip those shifts that start before period and end during the first day
832 // $when = array( 'report', 'list' );
833 // if( in_array('report', $when) ){
834 if( 1 ){
835 $startRange = $this->t->setDateDb( $params['start'] )->formatDateTimeDb();
836 $endFirstDay = $this->t->setDateDb( $params['start'] )->modify('+1 day')->formatDateTimeDb();
837
838 $ids = array_keys( $return );
839 foreach( $ids as $id ){
840 $shift = $return[ $id ];
841 if( $shift->getStart() >= $startRange ) continue;
842 if( $shift->getEnd() >= $endFirstDay ) continue;
843 unset( $return[$id] );
844 }
845 }
846
847 // filter conflicts?
848 if( isset($params['conflict']) && $params['conflict'] ){
849 $withConflicts = ( $params['conflict'] > 0 ) ? true : false;
850
851 $ids = array_keys( $return );
852 foreach( $ids as $id ){
853 $shift = $return[ $id ];
854 $conflicts = $this->conflicts->get( $shift );
855 if( $withConflicts && (! $conflicts) ){
856 unset( $return[$id] );
857 }
858
859 if( (! $withConflicts) && $conflicts ){
860 unset( $return[$id] );
861 }
862 }
863 }
864
865 $return = $this->self->filterShifts( $return );
866 $cache = $return;
867 return $return;
868 }
869
870 public function getCalendars()
871 {
872 $return = $this->self->findAllCalendars();
873 $params = $this->request->getParams();
874
875 if( $params['calendar'] ){
876 $filterIds = $params['calendar'];
877
878 $ids = array_keys($return);
879 foreach( $ids as $id ){
880 if( ! in_array($id, $filterIds) ){
881 unset( $return[$id] );
882 }
883 }
884 }
885
886 if( $params['employee'] ){
887 $employees = $this->self->findAllEmployees();
888
889 $filterEmployeeIds = $params['employee'];
890 $filter = array();
891 foreach( $employees as $employee ){
892 $employeeId = $employee->getId();
893 if( ! (in_array(-1, $filterEmployeeIds) && ($employeeId > 0)) ){
894 if( ! in_array($employeeId, $filterEmployeeIds) ){
895 continue;
896 }
897 }
898 $filter = $filter + $this->appQuery->filterCalendarsForEmployee( $return, $employee );
899 }
900 $filterIds = array_keys( $filter );
901
902 $ids = array_keys($return);
903 foreach( $ids as $id ){
904 if( ! in_array($id, $filterIds) ){
905 unset( $return[$id] );
906 }
907 }
908 }
909
910 return $return;
911 }
912
913 public function getEmployees()
914 {
915 $return = $this->self->findAllEmployees();
916 $params = $this->request->getParams();
917
918 if( $params['employee'] ){
919 $filterIds = $params['employee'];
920
921 $ids = array_keys($return);
922 foreach( $ids as $id ){
923 if( in_array(-1, $filterIds) && ($id > 0) ){
924 continue;
925 }
926
927 if( ! in_array($id, $filterIds) ){
928 unset( $return[$id] );
929 }
930 }
931 }
932
933 if( $params['calendar'] ){
934 $calendars = $this->self->findAllCalendars();
935
936 $filter = array();
937 foreach( $calendars as $calendar ){
938 if( ! in_array($calendar->getId(), $params['calendar']) ){
939 continue;
940 }
941 $filter = $filter + $this->appQuery->findEmployeesForCalendar( $calendar );
942 }
943 $filterIds = array_keys( $filter );
944
945 $ids = array_keys($return);
946 foreach( $ids as $id ){
947 if( ! in_array($id, $filterIds) ){
948 unset( $return[$id] );
949 }
950 }
951 }
952
953 return $return;
954 }
955
956 public function getEmployeeFilter()
957 {
958 $params = $this->request->getParams();
959 $ret = isset( $params['employee'] ) ? $params['employee'] : array();
960 return $ret;
961 }
962
963 public function getCalendarFilter()
964 {
965 $params = $this->request->getParams();
966 $ret = isset( $params['calendar'] ) ? $params['calendar'] : array();
967 return $ret;
968 }
969 }