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

1,058 lines 28.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 elseif( 'first-non-blank' == $startDate ){
643 $now = $this->t->setNow()->setStartDay()->formatDateTimeDb();
644
645 $q = array();
646 $q[] = array( 'sort', 'starts_at', 'asc' );
647 $q[] = array( 'starts_at', '>', $now );
648 $q[] = array( 'limit', 1 );
649
650 $allCalendars = $this->self->findAllCalendars();
651 $listCalendarId = array_keys( $allCalendars );
652 $calendarFilter = $this->getCalendarFilter();
653 if( $calendarFilter ){
654 $listCalendarId = array_intersect( $listCalendarId, $calendarFilter );
655 }
656 $q[] = array( 'calendar_id', 'IN', $listCalendarId );
657
658 $allEmployees = $this->self->findAllEmployees();
659 $listEmployeeId = array_keys( $allEmployees );
660 $employeeFilter = $this->getEmployeeFilter();
661 if( $employeeFilter ){
662 $listEmployeeId = array_intersect( $listEmployeeId, $employeeFilter );
663 }
664 $q[] = array( 'employee_id', 'IN', $listEmployeeId );
665
666 $res = $this->shiftsQuery->read( $q );
667 if( $res ){
668 $shift = current( $res );
669 $startDate = $this->t->setDateTimeDb( $shift->getStart() )->formatDateDb();
670 }
671 else {
672 $startDate = $this->t->setDateTimeDb( $now )->formatDateDb();
673 }
674 }
675 elseif( 'first-blank' == $startDate ){
676 $now = $this->t->setNow()->setStartDay()->formatDateTimeDb();
677
678 $q = array();
679 $q[] = array( 'sort', 'ends_at', 'desc' );
680 $q[] = array( 'limit', 1 );
681
682 $allCalendars = $this->self->findAllCalendars();
683 $listCalendarId = array_keys( $allCalendars );
684 $calendarFilter = $this->getCalendarFilter();
685 if( $calendarFilter ){
686 $listCalendarId = array_intersect( $listCalendarId, $calendarFilter );
687 }
688 $q[] = array( 'calendar_id', 'IN', $listCalendarId );
689
690 $allEmployees = $this->self->findAllEmployees();
691 $listEmployeeId = array_keys( $allEmployees );
692 $employeeFilter = $this->getEmployeeFilter();
693 if( $employeeFilter ){
694 $listEmployeeId = array_intersect( $listEmployeeId, $employeeFilter );
695 }
696 $q[] = array( 'employee_id', 'IN', $listEmployeeId );
697
698 $res = $this->shiftsQuery->read( $q );
699 if( $res ){
700 $shift = current( $res );
701 $startDate = $this->t->setDateTimeDb( $shift->getStart() )->formatDateDb();
702 }
703 else {
704 $startDate = $this->t->setDateTimeDb( $now )->formatDateDb();
705 }
706
707 switch( $type ){
708 case 'day':
709 $startDate = $this->t->setDateDb( $startDate )->modify( '+1 day' )->formatDateDb();
710 break;
711
712 case 'week':
713 $startDate = $this->t->setDateDb( $startDate )->modify( '+1 week' )->setStartWeek()->formatDateDb();
714 break;
715
716 case '4weeks':
717 $nWeeks = $this->settings->get( 'datetime_n_weeks' );
718 if( ! $nWeeks ) $nWeeks = 4;
719 $startDate = $this->t->setDateDb( $startDate )->modify('+' . $nWeeks . ' weeks')->setStartWeek()->formatDateDb();
720 break;
721
722 case '2weeks':
723 $startDate = $this->t->setDateDb( $startDate )->modify('+2 weeks')->setStartWeek()->formatDateDb();
724 break;
725
726 case 'month':
727 $startDate = $this->t->setDateDb( $startDate )->modify( '+1 month' )->setStartMonth()->formatDateDb();
728 break;
729 }
730 }
731 else {
732 if( (FALSE !== strpos($startDate, '+')) OR (FALSE !== strpos($startDate, '-')) ){
733 $this->t->setNow()->setStartDay()->modify($startDate);
734 $startDate = $this->t->formatDateDb();
735 }
736 }
737
738 if( FALSE !== strpos($endDate, '+') ){
739 $this->t->setDateDb($startDate)->modify($endDate)->modify('-1 day')->setEndDay();
740 $endDate = $this->t->formatDateDb();
741 }
742
743 $exactDateTime = isset( $params['time'] ) ? $params['time'] : NULL;
744 if( $exactDateTime ){
745 $startDate = $this->t->setDateTimeDb( $exactDateTime )->formatDateDb();
746 $endDate = $startDate;
747 }
748
749 switch( $type ){
750 case 'day':
751 $this->t->setDateDb($startDate);
752 $start = $this->t->formatDateTimeDb();
753
754 $startDate = $this->t->formatDateDb();
755 $this->request->setParam('start', $startDate);
756 $endDate = $this->t->formatDateDb();
757 $this->request->setParam('end', $endDate);
758
759 $daysToShow = isset( SH4_Schedule_Html_View_Day::$daysToShow ) ? SH4_Schedule_Html_View_Day::$daysToShow : 7;
760 if( $daysToShow ){
761 $this->t->modify( '+' . $daysToShow . ' days' );
762 }
763 $end = $this->t->setEndDay()->formatDateTimeDb();
764 break;
765
766 case 'week':
767 $this->t->setDateDb($startDate)->setStartWeek();
768 $startDate = $this->t->formatDateDb();
769 $this->request->setParam('start', $startDate);
770
771 $start = $this->t->formatDateTimeDb();
772 $this->t->setEndWeek();
773 // $this->t->modify('+1 week')
774 // ->modify('-1 day')
775 // ;
776 $endDate = $this->t->formatDateDb();
777
778 $this->request->setParam('end', $endDate);
779 $end = $this->t->setEndDay()->formatDateTimeDb();
780 break;
781
782 case '4weeks':
783 $nWeeks = $this->settings->get( 'datetime_n_weeks' );
784 if( ! $nWeeks ) $nWeeks = 4;
785
786 $this->t->setDateDb($startDate)->setStartWeek();
787 $startDate = $this->t->formatDateDb();
788 $this->request->setParam('start', $startDate);
789
790 $start = $this->t->formatDateTimeDb();
791 $this->t->modify('+' . $nWeeks . ' weeks')
792 ->modify('-1 day')
793 ;
794 $endDate = $this->t->formatDateDb();
795 $this->request->setParam('end', $endDate);
796
797 $end = $this->t->setEndDay()->formatDateTimeDb();
798 break;
799
800 case '2weeks':
801 $this->t->setDateDb($startDate)->setStartWeek();
802 $startDate = $this->t->formatDateDb();
803 $this->request->setParam('start', $startDate);
804
805 $start = $this->t->formatDateTimeDb();
806 $this->t->modify('+2 weeks')
807 ->modify('-1 day')
808 ;
809 $endDate = $this->t->formatDateDb();
810 $this->request->setParam('end', $endDate);
811
812 $end = $this->t->setEndDay()->formatDateTimeDb();
813 break;
814
815 case 'month':
816 $this->t->setDateDb($startDate)->setStartMonth();
817 $startDate = $this->t->formatDateDb();
818 $this->request->setParam('start', $startDate);
819
820 $start = $this->t->formatDateTimeDb();
821 $this->t->setEndMonth();
822 // $this->t->modify('+1 month')
823 // ->modify('-1 day')
824 // ;
825 $endDate = $this->t->formatDateDb();
826 $this->request->setParam('end', $endDate);
827
828 $end = $this->t->setEndDay()->formatDateTimeDb();
829 break;
830
831 default:
832 if( $exactDateTime ){
833 $start = $exactDateTime;
834 $end = $exactDateTime;
835 }
836 else {
837 $this->t->setDateDb($startDate);
838 $start = $this->t->formatDateTimeDb();
839
840 // $this->t->setDateDb($endDate)->modify('+1 day');
841 $this->t->setDateDb($endDate);
842 $end = $this->t->setEndDay()->formatDateTimeDb();
843 }
844
845 break;
846 }
847
848 $this->shiftsQuery
849 ->setStart( $start )
850 ->setEnd( $end )
851 ;
852
853 $return = $this->shiftsQuery->find();
854
855 // filter
856 $employees = $this->getEmployees();
857 $calendars = $this->getCalendars();
858
859 $employeeFilter = $this->getEmployeeFilter();
860 $calendarFilter = $this->getCalendarFilter();
861
862 foreach( array_keys($return) as $id ){
863 $shift = $return[$id];
864
865 $calendar = $shift->getCalendar();
866 // if( ! array_key_exists($calendar->getId(), $calendars) ){
867 // unset( $return[$id] );
868 // continue;
869 // }
870 if( $calendarFilter && (! in_array($calendar->getId(), $calendarFilter)) ){
871 unset( $return[$id] );
872 continue;
873 }
874
875 $employee = $shift->getEmployee();
876 // if( ! array_key_exists($employee->getId(), $employees) ){
877 // unset( $return[$id] );
878 // continue;
879 // }
880
881 $employeeId = $employee->getId();
882
883 if( $employeeFilter ){
884 // any assigned
885 if( in_array(-1, $employeeFilter) ){
886 if( ! $employeeId ){
887 unset( $return[$id] );
888 continue;
889 }
890 }
891 else {
892 if( ! in_array($employeeId, $employeeFilter) ){
893 unset( $return[$id] );
894 continue;
895 }
896 }
897 }
898
899 $shiftStart = $shift->getStart();
900 $shiftEnd = $shift->getEnd();
901
902 if( $shiftEnd <= $start ){
903 unset( $return[$id] );
904 continue;
905 }
906
907 if( $shiftStart >= $end ){
908 unset( $return[$id] );
909 continue;
910 }
911
912 // if( $shiftStart < $start ){
913 // unset( $return[$id] );
914 // continue;
915 // }
916 }
917
918 $params = $this->request->getParams();
919
920 // skip those shifts that start before period and end during the first day
921 // $when = array( 'report', 'list' );
922 // if( in_array('report', $when) ){
923 if( 1 ){
924 $startRange = $this->t->setDateDb( $params['start'] )->formatDateTimeDb();
925 $endFirstDay = $this->t->setDateDb( $params['start'] )->modify('+1 day')->formatDateTimeDb();
926
927 $ids = array_keys( $return );
928 foreach( $ids as $id ){
929 $shift = $return[ $id ];
930 if( $shift->getStart() >= $startRange ) continue;
931 if( $shift->getEnd() >= $endFirstDay ) continue;
932 unset( $return[$id] );
933 }
934 }
935
936 // filter conflicts?
937 if( isset($params['conflict']) && $params['conflict'] ){
938 $withConflicts = ( $params['conflict'] > 0 ) ? true : false;
939
940 $ids = array_keys( $return );
941 foreach( $ids as $id ){
942 $shift = $return[ $id ];
943 $conflicts = $this->conflicts->get( $shift );
944 if( $withConflicts && (! $conflicts) ){
945 unset( $return[$id] );
946 }
947
948 if( (! $withConflicts) && $conflicts ){
949 unset( $return[$id] );
950 }
951 }
952 }
953
954 $return = $this->self->filterShifts( $return );
955 $cache = $return;
956 return $return;
957 }
958
959 public function getCalendars()
960 {
961 $return = $this->self->findAllCalendars();
962 $params = $this->request->getParams();
963
964 if( $params['calendar'] ){
965 $filterIds = $params['calendar'];
966
967 $ids = array_keys($return);
968 foreach( $ids as $id ){
969 if( ! in_array($id, $filterIds) ){
970 unset( $return[$id] );
971 }
972 }
973 }
974
975 if( $params['employee'] ){
976 $employees = $this->self->findAllEmployees();
977
978 $filterEmployeeIds = $params['employee'];
979 $filter = array();
980 foreach( $employees as $employee ){
981 $employeeId = $employee->getId();
982 if( ! (in_array(-1, $filterEmployeeIds) && ($employeeId > 0)) ){
983 if( ! in_array($employeeId, $filterEmployeeIds) ){
984 continue;
985 }
986 }
987 $filter = $filter + $this->appQuery->filterCalendarsForEmployee( $return, $employee );
988 }
989 $filterIds = array_keys( $filter );
990
991 $ids = array_keys($return);
992 foreach( $ids as $id ){
993 if( ! in_array($id, $filterIds) ){
994 unset( $return[$id] );
995 }
996 }
997 }
998
999 return $return;
1000 }
1001
1002 public function getEmployees()
1003 {
1004 $return = $this->self->findAllEmployees();
1005 $params = $this->request->getParams();
1006
1007 if( $params['employee'] ){
1008 $filterIds = $params['employee'];
1009
1010 $ids = array_keys($return);
1011 foreach( $ids as $id ){
1012 if( in_array(-1, $filterIds) && ($id > 0) ){
1013 continue;
1014 }
1015
1016 if( ! in_array($id, $filterIds) ){
1017 unset( $return[$id] );
1018 }
1019 }
1020 }
1021
1022 if( $params['calendar'] ){
1023 $calendars = $this->self->findAllCalendars();
1024
1025 $filter = array();
1026 foreach( $calendars as $calendar ){
1027 if( ! in_array($calendar->getId(), $params['calendar']) ){
1028 continue;
1029 }
1030 $filter = $filter + $this->appQuery->findEmployeesForCalendar( $calendar );
1031 }
1032 $filterIds = array_keys( $filter );
1033
1034 $ids = array_keys($return);
1035 foreach( $ids as $id ){
1036 if( ! in_array($id, $filterIds) ){
1037 unset( $return[$id] );
1038 }
1039 }
1040 }
1041
1042 return $return;
1043 }
1044
1045 public function getEmployeeFilter()
1046 {
1047 $params = $this->request->getParams();
1048 $ret = isset( $params['employee'] ) ? $params['employee'] : array();
1049 return $ret;
1050 }
1051
1052 public function getCalendarFilter()
1053 {
1054 $params = $this->request->getParams();
1055 $ret = isset( $params['calendar'] ) ? $params['calendar'] : array();
1056 return $ret;
1057 }
1058 }