| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class SH4_Reminders_Query |
| 3 |
{ |
| 4 |
public function __construct( |
| 5 |
SH4_Reminders_QueryLog $remindersQueryLog, |
| 6 |
|
| 7 |
SH4_Employees_Query $employeesQuery, |
| 8 |
SH4_App_Query $appQuery, |
| 9 |
SH4_Shifts_Query $shiftsQuery, |
| 10 |
|
| 11 |
HC3_Time $t, |
| 12 |
HC3_Hooks $hooks |
| 13 |
) |
| 14 |
{ |
| 15 |
$this->t = $t; |
| 16 |
|
| 17 |
$this->remindersQueryLog = $hooks->wrap( $remindersQueryLog ); |
| 18 |
$this->employeesQuery = $hooks->wrap( $employeesQuery ); |
| 19 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 20 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 21 |
} |
| 22 |
|
| 23 |
public function find( $range, $date ) |
| 24 |
{ |
| 25 |
// echo "RANGE = '$range'<br>"; |
| 26 |
switch( $range ){ |
| 27 |
case SH4_Reminders_Model::RANGE_DAILY: |
| 28 |
case SH4_Reminders_Model::RANGE_DAILY_SMS: |
| 29 |
$start = $this->t->setDateDb( $date )->formatDateTimeDb(); |
| 30 |
$end = $this->t->setDateDb( $date )->modify('+1 day')->formatDateTimeDb(); |
| 31 |
break; |
| 32 |
|
| 33 |
case SH4_Reminders_Model::RANGE_WEEKLY: |
| 34 |
$start = $this->t->setDateDb( $date )->setStartWeek()->formatDateTimeDb(); |
| 35 |
$end = $this->t->setDateDb( $date )->setStartWeek()->modify('+1 week')->formatDateTimeDb(); |
| 36 |
break; |
| 37 |
|
| 38 |
case SH4_Reminders_Model::RANGE_MONTHLY: |
| 39 |
$start = $this->t->setDateDb( $date )->setStartMonth()->formatDateTimeDb(); |
| 40 |
$end = $this->t->setDateDb( $date )->setStartMonth()->modify('+1 month')->formatDateTimeDb(); |
| 41 |
break; |
| 42 |
} |
| 43 |
|
| 44 |
// move the date to the start of the range |
| 45 |
$date = $this->t->setDateTimeDb( $start )->formatDateDb(); |
| 46 |
|
| 47 |
$this->shiftsQuery |
| 48 |
->setStart( $start ) |
| 49 |
->setEnd( $end ) |
| 50 |
; |
| 51 |
$allShifts = $this->shiftsQuery->find(); |
| 52 |
|
| 53 |
$shifts = array(); |
| 54 |
|
| 55 |
// filter |
| 56 |
foreach( $allShifts as $shift ){ |
| 57 |
if( ! $shift->isPublished() ) continue; |
| 58 |
if( $shift->getStart() >= $end ) continue; |
| 59 |
if( $shift->getStart() < $start ) continue; |
| 60 |
$shifts[] = $shift; |
| 61 |
} |
| 62 |
|
| 63 |
// check logs if already sent |
| 64 |
$args = array(); |
| 65 |
$args[] = array( 'range', '=', $range ); |
| 66 |
$args[] = array( 'date', '=', $date ); |
| 67 |
$reminderLogs = $this->remindersQueryLog->read( $args ); |
| 68 |
|
| 69 |
$sentForEmployees = array(); |
| 70 |
foreach( $reminderLogs as $log ){ |
| 71 |
$sentForEmployees[ $log->employee_id ] = $log; |
| 72 |
} |
| 73 |
|
| 74 |
// find employees |
| 75 |
$employees = array(); |
| 76 |
$shiftsByEmployee = array(); |
| 77 |
|
| 78 |
foreach( $shifts as $shift ){ |
| 79 |
$employee = $shift->getEmployee(); |
| 80 |
if( (! $employee) OR (! $employee->getId() ) ) continue; |
| 81 |
|
| 82 |
$employeeId = $employee->getId(); |
| 83 |
// if( isset($sentForEmployees[$employeeId]) ) continue; |
| 84 |
|
| 85 |
$employees[ $employeeId ] = $employee; |
| 86 |
|
| 87 |
if( ! isset($shiftsByEmployee[$employeeId]) ) $shiftsByEmployee[$employeeId] = array(); |
| 88 |
$shiftsByEmployee[$employeeId][] = $shift; |
| 89 |
} |
| 90 |
|
| 91 |
$employeeIds = array_keys( $employees ); |
| 92 |
$employees = $this->employeesQuery->findManyById( $employeeIds ); |
| 93 |
|
| 94 |
$ret = array(); |
| 95 |
foreach( $employees as $employee ){ |
| 96 |
$user = $this->appQuery->findUserByEmployee( $employee ); |
| 97 |
if( ! $user ) continue; |
| 98 |
$employeeId = $employee->getId(); |
| 99 |
|
| 100 |
$model = new SH4_Reminders_Model; |
| 101 |
|
| 102 |
$model->date = $date; |
| 103 |
$model->range = $range; |
| 104 |
$model->user = $user; |
| 105 |
$model->employee = $employee; |
| 106 |
$model->shifts = $shiftsByEmployee[ $employeeId ]; |
| 107 |
$model->log = isset( $sentForEmployees[$employeeId] ) ? $sentForEmployees[$employeeId] : NULL; |
| 108 |
|
| 109 |
$ret[] = $model; |
| 110 |
} |
| 111 |
|
| 112 |
return $ret; |
| 113 |
} |
| 114 |
} |