| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_Shifts_Command_ |
| 3 |
{ |
| 4 |
public function draft( SH4_Shifts_Model $model ); |
| 5 |
public function publish( SH4_Shifts_Model $model ); |
| 6 |
public function unpublish( SH4_Shifts_Model $model ); |
| 7 |
public function delete( SH4_Shifts_Model $model ); |
| 8 |
public function deleteAll(); |
| 9 |
public function create( |
| 10 |
SH4_Calendars_Model $calendar, |
| 11 |
$start, |
| 12 |
$end, |
| 13 |
SH4_Employees_Model $employee, |
| 14 |
$breakStart = null, |
| 15 |
$breakEnd = null, |
| 16 |
$status = SH4_Shifts_Model::STATUS_DRAFT |
| 17 |
); |
| 18 |
public function copy( |
| 19 |
SH4_Shifts_Model $model, |
| 20 |
SH4_Calendars_Model $calendar, |
| 21 |
$start, |
| 22 |
$end, |
| 23 |
SH4_Employees_Model $employee, |
| 24 |
$breakStart = null, |
| 25 |
$breakEnd = null, |
| 26 |
$status = SH4_Shifts_Model::STATUS_DRAFT |
| 27 |
); |
| 28 |
|
| 29 |
public function changeEmployee( SH4_Shifts_Model $model, SH4_Employees_Model $employee ); |
| 30 |
public function changeCalendar( SH4_Shifts_Model $model, SH4_Calendars_Model $calendar ); |
| 31 |
public function reschedule( SH4_Shifts_Model $model, $newStart, $newEnd, $newStartBreak = NULL, $newEndBreak = NULL ); |
| 32 |
} |
| 33 |
|
| 34 |
class SH4_Shifts_Command implements SH4_Shifts_Command_ |
| 35 |
{ |
| 36 |
public $self, $shiftsQuery, $crud; |
| 37 |
|
| 38 |
public function __construct( |
| 39 |
SH4_Shifts_Query $shiftsQuery, |
| 40 |
HC3_Hooks $hooks, |
| 41 |
HC3_CrudFactory $crudFactory |
| 42 |
) |
| 43 |
{ |
| 44 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 45 |
$this->crud = $hooks->wrap( $crudFactory->make('shift') ); |
| 46 |
$this->self = $hooks->wrap( $this ); |
| 47 |
} |
| 48 |
|
| 49 |
public function create( |
| 50 |
SH4_Calendars_Model $calendar, |
| 51 |
$start, |
| 52 |
$end, |
| 53 |
SH4_Employees_Model $employee, |
| 54 |
$breakStart = null, |
| 55 |
$breakEnd = null, |
| 56 |
$status = SH4_Shifts_Model::STATUS_DRAFT |
| 57 |
) |
| 58 |
{ |
| 59 |
$calendarId = $calendar->getId(); |
| 60 |
$employeeId = $employee ? $employee->getId() : null; |
| 61 |
|
| 62 |
$array = array( |
| 63 |
'calendar_id' => $calendarId, |
| 64 |
'employee_id' => $employeeId, |
| 65 |
'starts_at' => $start, |
| 66 |
'ends_at' => $end, |
| 67 |
'break_starts_at' => $breakStart, |
| 68 |
'break_ends_at' => $breakEnd, |
| 69 |
'status' => $status, |
| 70 |
); |
| 71 |
|
| 72 |
$return = $this->crud->create( $array ); |
| 73 |
$return = $return['id']; |
| 74 |
return $return; |
| 75 |
} |
| 76 |
|
| 77 |
public function copy( |
| 78 |
SH4_Shifts_Model $model, |
| 79 |
SH4_Calendars_Model $calendar, |
| 80 |
$start, |
| 81 |
$end, |
| 82 |
SH4_Employees_Model $employee, |
| 83 |
$breakStart = null, |
| 84 |
$breakEnd = null, |
| 85 |
$status = SH4_Shifts_Model::STATUS_DRAFT |
| 86 |
) |
| 87 |
{ |
| 88 |
// $newId = $this->self->create( $calendar, $start, $end, $employee, $breakStart, $breakEnd, $status ); |
| 89 |
$newId = $this->self->create( $calendar, $start, $end, $employee, $breakStart, $breakEnd ); |
| 90 |
|
| 91 |
$wasPublished = $model->isPublished(); |
| 92 |
|
| 93 |
$m2 = $this->shiftsQuery->findById( $newId ); |
| 94 |
if( $wasPublished ){ |
| 95 |
$this->self->publish( $m2 ); |
| 96 |
} |
| 97 |
else { |
| 98 |
$this->self->draft( $m2 ); |
| 99 |
} |
| 100 |
|
| 101 |
return $newId; |
| 102 |
} |
| 103 |
|
| 104 |
public function createNew( SH4_Shifts_Model $model ) |
| 105 |
{ |
| 106 |
$id = $model->getId(); |
| 107 |
if( ! $id ){ |
| 108 |
$id = $this->self->create( |
| 109 |
$model->getCalendar(), |
| 110 |
$model->getStart(), |
| 111 |
$model->getEnd(), |
| 112 |
$model->getEmployee(), |
| 113 |
$model->getBreakStart(), |
| 114 |
$model->getBreakEnd() |
| 115 |
); |
| 116 |
$model->setId( $id ); |
| 117 |
} |
| 118 |
|
| 119 |
$this->shiftsQuery->keep( $model ); |
| 120 |
return $id; |
| 121 |
} |
| 122 |
|
| 123 |
public function publish( SH4_Shifts_Model $model ) |
| 124 |
{ |
| 125 |
$id = $model->getId(); |
| 126 |
if( ! $id ){ |
| 127 |
$id = $this->self->create( |
| 128 |
$model->getCalendar(), |
| 129 |
$model->getStart(), |
| 130 |
$model->getEnd(), |
| 131 |
$model->getEmployee(), |
| 132 |
$model->getBreakStart(), |
| 133 |
$model->getBreakEnd() |
| 134 |
); |
| 135 |
$model->setId( $id ); |
| 136 |
} |
| 137 |
|
| 138 |
$array = array( |
| 139 |
'status' => SH4_Shifts_Model::STATUS_PUBLISH |
| 140 |
); |
| 141 |
|
| 142 |
$this->crud->update( $id, $array ); |
| 143 |
return $id; |
| 144 |
} |
| 145 |
|
| 146 |
public function draft( SH4_Shifts_Model $model ) |
| 147 |
{ |
| 148 |
$id = $model->getId(); |
| 149 |
|
| 150 |
if( ! $id ){ |
| 151 |
$id = $this->self->create( |
| 152 |
$model->getCalendar(), |
| 153 |
$model->getStart(), |
| 154 |
$model->getEnd(), |
| 155 |
$model->getEmployee(), |
| 156 |
$model->getBreakStart(), |
| 157 |
$model->getBreakEnd() |
| 158 |
); |
| 159 |
$model->setId( $id ); |
| 160 |
} |
| 161 |
|
| 162 |
$array = array( |
| 163 |
'status' => SH4_Shifts_Model::STATUS_DRAFT |
| 164 |
); |
| 165 |
|
| 166 |
$this->crud->update( $id, $array ); |
| 167 |
return $id; |
| 168 |
} |
| 169 |
|
| 170 |
public function unpublish( SH4_Shifts_Model $model ) |
| 171 |
{ |
| 172 |
$id = $model->getId(); |
| 173 |
if( ! $id ){ |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$array = array( |
| 178 |
'status' => SH4_Shifts_Model::STATUS_DRAFT |
| 179 |
); |
| 180 |
|
| 181 |
$this->crud->update( $id, $array ); |
| 182 |
return $id; |
| 183 |
} |
| 184 |
|
| 185 |
public function delete( SH4_Shifts_Model $model ) |
| 186 |
{ |
| 187 |
$id = $model->getId(); |
| 188 |
if( ! $id ){ |
| 189 |
return; |
| 190 |
} |
| 191 |
return $this->crud->delete( $id ); |
| 192 |
} |
| 193 |
|
| 194 |
public function deleteAll() |
| 195 |
{ |
| 196 |
return $this->crud->deleteAll(); |
| 197 |
} |
| 198 |
|
| 199 |
public function changeEmployee( SH4_Shifts_Model $model, SH4_Employees_Model $employee ) |
| 200 |
{ |
| 201 |
$id = $model->getId(); |
| 202 |
if( ! $id ){ |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
// unpublish to notify the current employee |
| 207 |
$wasPublished = $model->isPublished(); |
| 208 |
|
| 209 |
$oldEmployee = $model->getEmployee(); |
| 210 |
if( $oldEmployee && $oldEmployee->getId() ){ |
| 211 |
$this->self->unpublish( $model ); |
| 212 |
} |
| 213 |
|
| 214 |
$employeeId = $employee->getId(); |
| 215 |
$array = array( |
| 216 |
'employee_id' => $employeeId, |
| 217 |
); |
| 218 |
|
| 219 |
/* quick tweak, later change it to something better */ |
| 220 |
$refObject = new ReflectionObject( $model ); |
| 221 |
|
| 222 |
$refProperty = $refObject->getProperty( 'employee' ); |
| 223 |
$refProperty->setAccessible( TRUE ); |
| 224 |
$refProperty->setValue( $model, $employee ); |
| 225 |
|
| 226 |
$ret = $this->crud->update( $id, $array ); |
| 227 |
|
| 228 |
// if( $employeeId ){ |
| 229 |
if( $wasPublished ){ |
| 230 |
$this->self->publish( $model ); |
| 231 |
} |
| 232 |
else { |
| 233 |
$this->self->draft( $model ); |
| 234 |
} |
| 235 |
// } |
| 236 |
|
| 237 |
return $ret; |
| 238 |
} |
| 239 |
|
| 240 |
public function changeCalendar( SH4_Shifts_Model $model, SH4_Calendars_Model $calendar ) |
| 241 |
{ |
| 242 |
$id = $model->getId(); |
| 243 |
if( ! $id ){ |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
$calendarId = $calendar->getId(); |
| 248 |
$array = array( |
| 249 |
'calendar_id' => $calendarId, |
| 250 |
); |
| 251 |
|
| 252 |
/* quick tweak, later change it to something better */ |
| 253 |
$refObject = new ReflectionObject( $model ); |
| 254 |
|
| 255 |
$refProperty = $refObject->getProperty( 'calendar' ); |
| 256 |
$refProperty->setAccessible( TRUE ); |
| 257 |
$refProperty->setValue( $model, $calendar ); |
| 258 |
|
| 259 |
return $this->crud->update( $id, $array ); |
| 260 |
} |
| 261 |
|
| 262 |
public function reschedule( SH4_Shifts_Model $model, $newStart, $newEnd, $newStartBreak = NULL, $newEndBreak = NULL ) |
| 263 |
{ |
| 264 |
$id = $model->getId(); |
| 265 |
if( ! $id ){ |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
$array = array( |
| 270 |
'starts_at' => $newStart, |
| 271 |
'ends_at' => $newEnd, |
| 272 |
'break_starts_at' => NULL, |
| 273 |
'break_ends_at' => NULL |
| 274 |
); |
| 275 |
|
| 276 |
if( NULL !== $newStartBreak ){ |
| 277 |
$array['break_starts_at'] = $newStartBreak; |
| 278 |
} |
| 279 |
if( NULL !== $newEndBreak ){ |
| 280 |
$array['break_ends_at'] = $newEndBreak; |
| 281 |
} |
| 282 |
|
| 283 |
/* quick tweak, later change it to something better */ |
| 284 |
$refObject = new ReflectionObject( $model ); |
| 285 |
|
| 286 |
$refProperty = $refObject->getProperty( 'start' ); |
| 287 |
$refProperty->setAccessible( TRUE ); |
| 288 |
$refProperty->setValue( $model, $array['starts_at'] ); |
| 289 |
|
| 290 |
$refProperty = $refObject->getProperty( 'end' ); |
| 291 |
$refProperty->setAccessible( TRUE ); |
| 292 |
$refProperty->setValue( $model, $array['ends_at'] ); |
| 293 |
|
| 294 |
$refProperty = $refObject->getProperty( 'breakStart' ); |
| 295 |
$refProperty->setAccessible( TRUE ); |
| 296 |
$refProperty->setValue( $model, $array['break_starts_at'] ); |
| 297 |
|
| 298 |
$refProperty = $refObject->getProperty( 'breakEnd' ); |
| 299 |
$refProperty->setAccessible( TRUE ); |
| 300 |
$refProperty->setValue( $model, $array['break_ends_at'] ); |
| 301 |
|
| 302 |
return $this->crud->update( $id, $array ); |
| 303 |
} |
| 304 |
} |