| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_New_Controller_Confirm |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
HC3_Post $post, |
| 8 |
HC3_Notificator $notificator, |
| 9 |
|
| 10 |
SH4_New_Controller_Common $common, |
| 11 |
SH4_Shifts_Command $shiftsCommand, |
| 12 |
|
| 13 |
SH4_Calendars_Query $calendars, |
| 14 |
SH4_Employees_Query $employees, |
| 15 |
SH4_ShiftTypes_Query $shiftTypes, |
| 16 |
|
| 17 |
HC3_Time $t |
| 18 |
) |
| 19 |
{ |
| 20 |
$this->t = $t; |
| 21 |
$this->post = $hooks->wrap( $post ); |
| 22 |
$this->notificator = $notificator; |
| 23 |
|
| 24 |
$this->common = $hooks->wrap( $common ); |
| 25 |
$this->shiftsCommand = $hooks->wrap($shiftsCommand); |
| 26 |
|
| 27 |
$this->calendars = $hooks->wrap($calendars); |
| 28 |
$this->employees = $hooks->wrap($employees); |
| 29 |
$this->shiftTypes = $hooks->wrap($shiftTypes); |
| 30 |
|
| 31 |
$this->self = $hooks->wrap($this); |
| 32 |
} |
| 33 |
|
| 34 |
public function executePublish( $calendarId, $shiftTypeId, $dateString, $employeeString ) |
| 35 |
{ |
| 36 |
return $this->_execute( $calendarId, $shiftTypeId, $dateString, $employeeString, 'publish' ); |
| 37 |
} |
| 38 |
|
| 39 |
public function executeDraft( $calendarId, $shiftTypeId, $dateString, $employeeString ) |
| 40 |
{ |
| 41 |
return $this->_execute( $calendarId, $shiftTypeId, $dateString, $employeeString, 'draft' ); |
| 42 |
} |
| 43 |
|
| 44 |
public function _execute( $calendarId, $shiftTypeId, $dateString, $employeeString, $status ) |
| 45 |
{ |
| 46 |
set_time_limit( 600 ); |
| 47 |
$this->common->beforeExecute(); |
| 48 |
|
| 49 |
$shiftType = $this->shiftTypes->findById( $shiftTypeId ); |
| 50 |
$calendar = $this->calendars->findById( $calendarId ); |
| 51 |
|
| 52 |
$employeesIds = HC3_Functions::unglueArray( $employeeString ); |
| 53 |
|
| 54 |
// if we have multiple open shifts |
| 55 |
$multiOpenShifts = 0; |
| 56 |
for( $ii = 0; $ii < count($employeesIds); $ii++ ){ |
| 57 |
if( substr($employeesIds[$ii], 0, strlen('0' . SH4_NEW_OPEN_SEPARATOR)) == '0'.SH4_NEW_OPEN_SEPARATOR ){ |
| 58 |
$multiOpenShifts = substr( $employeesIds[$ii], strlen('0'.SH4_NEW_OPEN_SEPARATOR) ); |
| 59 |
$employeesIds[$ii] = substr( $employeesIds[$ii], 0, -strlen(SH4_NEW_OPEN_SEPARATOR . $multiOpenShifts) ); |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$employees = $this->employees->findManyActiveById( $employeesIds ); |
| 65 |
|
| 66 |
$dates = HC3_Functions::unglueArray( $dateString ); |
| 67 |
|
| 68 |
$shifts = array(); |
| 69 |
$ids = array(); |
| 70 |
|
| 71 |
$range = $shiftType->getRange(); |
| 72 |
switch( $range ){ |
| 73 |
case SH4_ShiftTypes_Model::RANGE_DAYS: |
| 74 |
$start = $this->t->setDateDb( $dates[0] ) |
| 75 |
->formatDateTimeDb() |
| 76 |
; |
| 77 |
$end = $this->t->setDateDb( $dates[1] ) |
| 78 |
->modify('+1 day') |
| 79 |
->formatDateTimeDb() |
| 80 |
; |
| 81 |
foreach( $employees as $employee ){ |
| 82 |
$toCreate = 1; |
| 83 |
$employeeId = $employee->getId(); |
| 84 |
if( (! $employeeId) && $multiOpenShifts ){ |
| 85 |
$toCreate = $multiOpenShifts; |
| 86 |
} |
| 87 |
|
| 88 |
for( $ii = 1; $ii <= $toCreate; $ii++ ){ |
| 89 |
$shift = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $employee ); |
| 90 |
$shifts[] = $shift; |
| 91 |
} |
| 92 |
} |
| 93 |
break; |
| 94 |
|
| 95 |
case SH4_ShiftTypes_Model::RANGE_HOURS: |
| 96 |
$timeStart = $shiftType->getStart(); |
| 97 |
$timeEnd = $shiftType->getEnd(); |
| 98 |
|
| 99 |
$breakTimeStart = $shiftType->getBreakStart(); |
| 100 |
$breakTimeEnd = $shiftType->getBreakEnd(); |
| 101 |
|
| 102 |
reset( $dates ); |
| 103 |
foreach( $dates as $date ){ |
| 104 |
$start = $this->t->setDateDb( $date ) |
| 105 |
->modify( '+' . $timeStart . ' seconds' ) |
| 106 |
->formatDateTimeDb() |
| 107 |
; |
| 108 |
$end = $this->t->setDateDb( $date ) |
| 109 |
->modify( '+' . $timeEnd . ' seconds' ) |
| 110 |
->formatDateTimeDb() |
| 111 |
; |
| 112 |
|
| 113 |
$breakStart = $breakEnd = NULL; |
| 114 |
if( ! ((NULL === $breakTimeStart) && (NULL === $breakTimeEnd)) ){ |
| 115 |
if( $breakTimeStart OR $breakTimeEnd ){ |
| 116 |
$breakStart = $this->t->setDateDb( $date ); |
| 117 |
if( $breakTimeStart ){ |
| 118 |
$this->t->modify( '+' . $breakTimeStart . ' seconds' ); |
| 119 |
} |
| 120 |
$breakStart = $this->t->formatDateTimeDb(); |
| 121 |
|
| 122 |
$breakEnd = $this->t->setDateDb( $date ); |
| 123 |
if( $breakTimeEnd ){ |
| 124 |
$this->t->modify( '+' . $breakTimeEnd . ' seconds' ); |
| 125 |
} |
| 126 |
$breakEnd = $this->t->formatDateTimeDb(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
foreach( $employees as $employee ){ |
| 131 |
$toCreate = 1; |
| 132 |
$employeeId = $employee->getId(); |
| 133 |
if( (! $employeeId) && $multiOpenShifts ){ |
| 134 |
$toCreate = $multiOpenShifts; |
| 135 |
} |
| 136 |
|
| 137 |
for( $ii = 1; $ii <= $toCreate; $ii++ ){ |
| 138 |
$shift = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $employee, $breakStart, $breakEnd ); |
| 139 |
$shifts[] = $shift; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
break; |
| 144 |
} |
| 145 |
|
| 146 |
$count = count( $shifts ); |
| 147 |
for( $ii = 0; $ii < $count; $ii++ ){ |
| 148 |
$id = $this->shiftsCommand->createNew( $shifts[$ii] ); |
| 149 |
$ids[] = $id; |
| 150 |
} |
| 151 |
|
| 152 |
$this->common->afterExecute( $ids ); |
| 153 |
|
| 154 |
for( $ii = 0; $ii < $count; $ii++ ){ |
| 155 |
switch( $status ){ |
| 156 |
case 'publish': |
| 157 |
$id = $this->shiftsCommand->publish( $shifts[$ii] ); |
| 158 |
break; |
| 159 |
case 'draft': |
| 160 |
$id = $this->shiftsCommand->draft( $shifts[$ii] ); |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
$scheduleLink = $this->post->get( 'back' ); |
| 166 |
if( $scheduleLink ){ |
| 167 |
$scheduleLink = json_decode( $scheduleLink, TRUE ); |
| 168 |
} |
| 169 |
else { |
| 170 |
$scheduleLink = HC3_Session::instance()->getUserdata( 'scheduleLink' ); |
| 171 |
if( ! $scheduleLink ){ |
| 172 |
$scheduleLink = array( 'schedule', array() ); |
| 173 |
} |
| 174 |
$scheduleLink = array( 'schedule', array() ); |
| 175 |
} |
| 176 |
|
| 177 |
$returnToDate = $dates[0]; |
| 178 |
$scheduleLink[1]['start'] = $returnToDate; |
| 179 |
|
| 180 |
$return = array( $scheduleLink, '__New Created__' . ' [' . count($ids) . ']' ); |
| 181 |
return $return; |
| 182 |
} |
| 183 |
} |