| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_Shifts_IAvailability |
| 3 |
{ |
| 4 |
public function get( SH4_Shifts_Model $shift ); |
| 5 |
public function hasAvailability(); |
| 6 |
} |
| 7 |
|
| 8 |
class SH4_Shifts_Availability implements SH4_Shifts_IAvailability |
| 9 |
{ |
| 10 |
protected $checks = array(); |
| 11 |
protected $availabilityCalendars = array(); |
| 12 |
public $self, $shiftsQuery, $calendarsQuery; |
| 13 |
|
| 14 |
public function __construct( |
| 15 |
HC3_Hooks $hooks, |
| 16 |
SH4_Calendars_Query $calendarsQuery, |
| 17 |
SH4_Shifts_Query $shiftsQuery |
| 18 |
) |
| 19 |
{ |
| 20 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 21 |
$this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 22 |
|
| 23 |
$this->availabilityCalendars = array(); |
| 24 |
$activeCalendars = $this->calendarsQuery->findActive(); |
| 25 |
foreach( $activeCalendars as $c ){ |
| 26 |
if( $c->isAvailability() ){ |
| 27 |
$this->availabilityCalendars[] = $c; |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
public function hasAvailability() |
| 33 |
{ |
| 34 |
$return = count( $this->availabilityCalendars ) ? TRUE : FALSE; |
| 35 |
return $return; |
| 36 |
} |
| 37 |
|
| 38 |
public function get( SH4_Shifts_Model $shift ) |
| 39 |
{ |
| 40 |
$return = array(); |
| 41 |
|
| 42 |
if( ! $this->availabilityCalendars ){ |
| 43 |
return $return; |
| 44 |
} |
| 45 |
|
| 46 |
$start = $shift->getStart(); |
| 47 |
$end = $shift->getEnd(); |
| 48 |
$employee = $shift->getEmployee(); |
| 49 |
|
| 50 |
$this->shiftsQuery |
| 51 |
->setStart( $start ) |
| 52 |
->setEnd( $end ) |
| 53 |
; |
| 54 |
|
| 55 |
$shifts = $this->shiftsQuery->find(); |
| 56 |
|
| 57 |
foreach( $shifts as $checkShift ){ |
| 58 |
$checkCalendar = $checkShift->getCalendar(); |
| 59 |
if( ! $checkCalendar->isAvailability() ){ |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
$checkEmployee = $checkShift->getEmployee(); |
| 64 |
|
| 65 |
if( $employee->getId() != $checkEmployee->getId() ){ |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
$checkStart = $checkShift->getStart(); |
| 70 |
$checkEnd = $checkShift->getEnd(); |
| 71 |
|
| 72 |
if( $checkStart > $start ){ |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
if( $checkEnd < $end ){ |
| 77 |
continue; |
| 78 |
} |
| 79 |
|
| 80 |
$return[] = $checkShift; |
| 81 |
} |
| 82 |
|
| 83 |
return $return; |
| 84 |
} |
| 85 |
} |