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

1,133 lines 30.7 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( $calendarFilter && (! in_array($calendar->getId(), $calendarFilter)) ){
873 unset( $return[$id] );
874 continue;
875 }
876
877 $employee = $shift->getEmployee();
878 $employeeId = $employee->getId();
879
880 if( $employeeFilter ){
881 // any assigned
882 if( in_array(-1, $employeeFilter) ){
883 if( ! $employeeId ){
884 unset( $return[$id] );
885 continue;
886 }
887 }
888 else {
889 if( ! in_array($employeeId, $employeeFilter) ){
890 unset( $return[$id] );
891 continue;
892 }
893 }
894 }
895
896 $shiftStart = $shift->getStart();
897 $shiftEnd = $shift->getEnd();
898
899 if( $shiftEnd <= $start ){
900 unset( $return[$id] );
901 continue;
902 }
903
904 if( $shiftStart >= $end ){
905 unset( $return[$id] );
906 continue;
907 }
908
909 // if( $shiftStart < $start ){
910 // unset( $return[$id] );
911 // continue;
912 // }
913 }
914
915 $params = $this->request->getParams();
916
917 // skip those shifts that start before period and end during the first day
918 // $when = array( 'report', 'list' );
919 // if( in_array('report', $when) ){
920 if( 1 ){
921 $startRange = $this->t->setDateDb( $params['start'] )->formatDateTimeDb();
922 $endFirstDay = $this->t->setDateDb( $params['start'] )->modify('+1 day')->formatDateTimeDb();
923
924 $ids = array_keys( $return );
925 foreach( $ids as $id ){
926 $shift = $return[ $id ];
927 if( $shift->getStart() >= $startRange ) continue;
928 if( $shift->getEnd() >= $endFirstDay ) continue;
929 unset( $return[$id] );
930 }
931 }
932
933 // filter conflicts?
934 if( isset($params['conflict']) && $params['conflict'] ){
935 $withConflicts = ( $params['conflict'] > 0 ) ? true : false;
936
937 $ids = array_keys( $return );
938 foreach( $ids as $id ){
939 $shift = $return[ $id ];
940 $conflicts = $this->conflicts->get( $shift );
941 if( $withConflicts && (! $conflicts) ){
942 unset( $return[$id] );
943 }
944
945 if( (! $withConflicts) && $conflicts ){
946 unset( $return[$id] );
947 }
948 }
949 }
950
951 // t1: array of start/end time like this: 32400-576000_36000-64800
952 if (isset($params['t'])) {
953 $tParam = $params['t'];
954
955 if (false !== strpos($tParam, '_')) {
956 $tParam = explode('_', $tParam);
957 } else {
958 $tParam = [$tParam];
959 }
960
961 foreach (array_keys($tParam) as $ii) {
962 $tParam[$ii] = explode('-', $tParam[$ii]);
963 $tParam[$ii] = array_map('intval', $tParam[$ii]);
964 }
965
966 foreach (array_keys($return) as $id) {
967 $startTime = (int) $return[$id]->getStartInDay();
968 $endTime = (int) $return[$id]->getEndInDay();
969
970 $ok = false;
971 reset($tParam);
972 foreach ($tParam as $p) {
973 if (($p[0] == $startTime) && ($p[1] == $endTime)) {
974 $ok = true;
975 break;
976 }
977 }
978
979 if (!$ok) {
980 unset($return[$id]);
981 continue;
982 }
983 }
984 }
985
986 // t1 or t2 params: start/end time
987 if (isset($params['t1']) or isset($params['t2'])) {
988 $t1 = $t2 = array();
989
990 if (isset($params['t1'])) {
991 $t1 = $params['t1'];
992 if (false !== strpos($t1, '-')) {
993 $t1 = explode('-', $t1);
994 } else {
995 $t1 = [$t1];
996 }
997 $t1 = array_map('intval', $t1);
998 }
999
1000 if (isset($params['t2'])) {
1001 $t2 = $params['t2'];
1002 if (false !== strpos($t2, '-')) {
1003 $t2 = explode('-', $t2);
1004 } else {
1005 $t2 = [$t2];
1006 }
1007 $t2 = array_map('intval', $t2);
1008 }
1009
1010 foreach (array_keys($return) as $id) {
1011 if ($t1) {
1012 $startTime = (int) $return[$id]->getStartInDay();
1013 if (!in_array($startTime, $t1)) {
1014 unset($return[$id]);
1015 continue;
1016 }
1017 }
1018
1019 if ($t2) {
1020 $endTime = (int) $return[$id]->getEndInDay();
1021 if (!in_array($endTime, $t2)) {
1022 unset($return[$id]);
1023 continue;
1024 }
1025 }
1026 }
1027 }
1028
1029 $return = $this->self->filterShifts( $return );
1030 $cache = $return;
1031 return $return;
1032 }
1033
1034 public function getCalendars()
1035 {
1036 $return = $this->self->findAllCalendars();
1037 $params = $this->request->getParams();
1038
1039 if( $params['calendar'] ){
1040 $filterIds = $params['calendar'];
1041
1042 $ids = array_keys($return);
1043 foreach( $ids as $id ){
1044 if( ! in_array($id, $filterIds) ){
1045 unset( $return[$id] );
1046 }
1047 }
1048 }
1049
1050 if( $params['employee'] ){
1051 $employees = $this->self->findAllEmployees();
1052
1053 $filterEmployeeIds = $params['employee'];
1054 $filter = array();
1055 foreach( $employees as $employee ){
1056 $employeeId = $employee->getId();
1057 if( ! (in_array(-1, $filterEmployeeIds) && ($employeeId > 0)) ){
1058 if( ! in_array($employeeId, $filterEmployeeIds) ){
1059 continue;
1060 }
1061 }
1062 $filter = $filter + $this->appQuery->filterCalendarsForEmployee( $return, $employee );
1063 }
1064 $filterIds = array_keys( $filter );
1065
1066 $ids = array_keys($return);
1067 foreach( $ids as $id ){
1068 if( ! in_array($id, $filterIds) ){
1069 unset( $return[$id] );
1070 }
1071 }
1072 }
1073
1074 return $return;
1075 }
1076
1077 public function getEmployees()
1078 {
1079 $return = $this->self->findAllEmployees();
1080 $params = $this->request->getParams();
1081
1082 if( $params['employee'] ){
1083 $filterIds = $params['employee'];
1084
1085 $ids = array_keys($return);
1086 foreach( $ids as $id ){
1087 if( in_array(-1, $filterIds) && ($id > 0) ){
1088 continue;
1089 }
1090
1091 if( ! in_array($id, $filterIds) ){
1092 unset( $return[$id] );
1093 }
1094 }
1095 }
1096
1097 if( $params['calendar'] ){
1098 $calendars = $this->self->findAllCalendars();
1099
1100 $filter = array();
1101 foreach( $calendars as $calendar ){
1102 if( ! in_array($calendar->getId(), $params['calendar']) ){
1103 continue;
1104 }
1105 $filter = $filter + $this->appQuery->findEmployeesForCalendar( $calendar );
1106 }
1107 $filterIds = array_keys( $filter );
1108
1109 $ids = array_keys($return);
1110 foreach( $ids as $id ){
1111 if( ! in_array($id, $filterIds) ){
1112 unset( $return[$id] );
1113 }
1114 }
1115 }
1116
1117 return $return;
1118 }
1119
1120 public function getEmployeeFilter()
1121 {
1122 $params = $this->request->getParams();
1123 $ret = isset( $params['employee'] ) ? $params['employee'] : array();
1124 return $ret;
1125 }
1126
1127 public function getCalendarFilter()
1128 {
1129 $params = $this->request->getParams();
1130 $ret = isset( $params['calendar'] ) ? $params['calendar'] : array();
1131 return $ret;
1132 }
1133 }