| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class SH4_Shifts_Conflict_Overlap implements SH4_Shifts_Conflict_IConflict |
| 3 |
{ |
| 4 |
private $_conflictingShifts = array(); |
| 5 |
public $ui, $shiftsQuery, $widget, $settings; |
| 6 |
|
| 7 |
public function __construct( |
| 8 |
HC3_Hooks $hooks, |
| 9 |
HC3_Ui $ui, |
| 10 |
HC3_Settings $settings, |
| 11 |
SH4_Shifts_Query $shiftsQuery, |
| 12 |
SH4_Shifts_View_Widget $widget |
| 13 |
) |
| 14 |
{ |
| 15 |
$this->ui = $hooks->wrap( $ui ); |
| 16 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 17 |
$this->widget = $hooks->wrap( $widget ); |
| 18 |
$this->settings = $hooks->wrap($settings); |
| 19 |
} |
| 20 |
|
| 21 |
// find overlapping shifts for the same employee |
| 22 |
public function check( SH4_Shifts_Model $shift, array $skipIdList = array() ) |
| 23 |
{ |
| 24 |
$return = TRUE; |
| 25 |
|
| 26 |
$conflictSameCalendarOnly = $this->settings->get( 'conflicts_calendar_only' ); |
| 27 |
|
| 28 |
$this->_conflictingShifts = array(); |
| 29 |
|
| 30 |
$isOpen = $shift->isOpen(); |
| 31 |
if( $isOpen ){ |
| 32 |
return $return; |
| 33 |
} |
| 34 |
|
| 35 |
$calendar = $shift->getCalendar(); |
| 36 |
if( $calendar->isAvailability() ){ |
| 37 |
return $return; |
| 38 |
} |
| 39 |
|
| 40 |
$employee = $shift->getEmployee(); |
| 41 |
if( ! $employee ){ |
| 42 |
return $return; |
| 43 |
} |
| 44 |
$employeeId = $employee->getId(); |
| 45 |
$id = $shift->getId(); |
| 46 |
|
| 47 |
$myStart = $shift->getStart(); |
| 48 |
$myEnd = $shift->getEnd(); |
| 49 |
|
| 50 |
$this->shiftsQuery |
| 51 |
->setStart( $shift->getStart() ) |
| 52 |
->setEnd( $shift->getEnd() ) |
| 53 |
; |
| 54 |
|
| 55 |
$testShifts = $this->shiftsQuery->find(); |
| 56 |
|
| 57 |
foreach( $testShifts as $testShift ){ |
| 58 |
$testId = $testShift->getId(); |
| 59 |
|
| 60 |
if( $testId == $id ){ |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
if( in_array($testId, $skipIdList) ){ |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
$testStart = $testShift->getStart(); |
| 69 |
$testEnd = $testShift->getEnd(); |
| 70 |
|
| 71 |
/* assuming shifts are sorted by start time */ |
| 72 |
if( $testStart >= $myEnd ){ |
| 73 |
break; |
| 74 |
} |
| 75 |
if( $testEnd <= $myStart ){ |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
$testCalendar = $testShift->getCalendar(); |
| 80 |
if( $testCalendar->isAvailability() ){ |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
$testEmployee = $testShift->getEmployee(); |
| 85 |
if( ! $testEmployee ){ |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
$testEmployeeId = $testEmployee->getId(); |
| 90 |
if( $testEmployeeId != $employeeId ){ |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
if( $conflictSameCalendarOnly && (! $testCalendar->isTimeoff()) && ( ! $calendar->isTimeoff() ) ){ |
| 95 |
if( $testCalendar->getId() != $calendar->getId() ){ |
| 96 |
continue; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
$this->_conflictingShifts[] = $testShift; |
| 101 |
} |
| 102 |
|
| 103 |
if( $this->_conflictingShifts ){ |
| 104 |
$return = FALSE; |
| 105 |
} |
| 106 |
|
| 107 |
return $return; |
| 108 |
} |
| 109 |
|
| 110 |
public function render() |
| 111 |
{ |
| 112 |
$label = '__Overlap__'; |
| 113 |
|
| 114 |
$iknow = array('conflicts'); |
| 115 |
$hori = TRUE; |
| 116 |
|
| 117 |
$viewShifts = array(); |
| 118 |
foreach( $this->_conflictingShifts as $shift ){ |
| 119 |
$viewShifts[] = $this->widget->render( $shift, $iknow, $hori ); |
| 120 |
} |
| 121 |
$out = $this->ui->makeList( $viewShifts ); |
| 122 |
|
| 123 |
$out = $this->ui->makeLabelled( $label, $out ); |
| 124 |
return $out; |
| 125 |
} |
| 126 |
} |