| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Shifts_View_Time |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
|
| 8 |
HC3_Settings $settings, |
| 9 |
HC3_Request $request, |
| 10 |
HC3_Time $t, |
| 11 |
HC3_Ui $ui, |
| 12 |
HC3_Ui_Layout1 $layout, |
| 13 |
|
| 14 |
SH4_Shifts_View_Common $common, |
| 15 |
SH4_Shifts_Query $shiftsQuery, |
| 16 |
|
| 17 |
SH4_Employees_Query $employees, |
| 18 |
SH4_App_Query $appQuery, |
| 19 |
|
| 20 |
SH4_ShiftTypes_Presenter $shiftTypesPresenter, |
| 21 |
SH4_Shifts_Conflicts $conflicts |
| 22 |
) |
| 23 |
{ |
| 24 |
$this->request = $request; |
| 25 |
$this->ui = $ui; |
| 26 |
$this->t = $t; |
| 27 |
$this->layout = $layout; |
| 28 |
$this->settings = $hooks->wrap($settings); |
| 29 |
|
| 30 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 31 |
|
| 32 |
$this->employees = $hooks->wrap($employees); |
| 33 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 34 |
|
| 35 |
$this->common = $hooks->wrap($common); |
| 36 |
$this->self = $hooks->wrap($this); |
| 37 |
$this->conflicts = $hooks->wrap($conflicts); |
| 38 |
$this->shiftTypesPresenter = $hooks->wrap( $shiftTypesPresenter ); |
| 39 |
} |
| 40 |
|
| 41 |
public function render( $id ) |
| 42 |
{ |
| 43 |
$model = $this->shiftsQuery->findById( $id ); |
| 44 |
|
| 45 |
$calendar = $model->getCalendar(); |
| 46 |
$out = $this->self->renderShiftTypes( $model ); |
| 47 |
if( $out ){ |
| 48 |
$out = $this->ui->makeBlock( $out ) |
| 49 |
->tag('padding', 2) |
| 50 |
->tag('border') |
| 51 |
->tag('border-color', 'gray') |
| 52 |
; |
| 53 |
} |
| 54 |
|
| 55 |
$this->layout |
| 56 |
->setContent( $out ) |
| 57 |
->setBreadcrumb( $this->common->breadcrumb($model) ) |
| 58 |
->setHeader( '__Change Time__' ) |
| 59 |
; |
| 60 |
$out = $this->layout->render(); |
| 61 |
|
| 62 |
return $out; |
| 63 |
} |
| 64 |
|
| 65 |
public function renderStart( $id ) |
| 66 |
{ |
| 67 |
$model = $this->shiftsQuery->findById( $id ); |
| 68 |
$shiftId = $id; |
| 69 |
|
| 70 |
$calendar = $model->getCalendar(); |
| 71 |
$calendarId = $calendar->getId(); |
| 72 |
$employee = $model->getEmployee(); |
| 73 |
$employeeId = $employee->getId(); |
| 74 |
|
| 75 |
$currentStart = $model->getStart(); |
| 76 |
$currentEnd = $model->getEnd(); |
| 77 |
|
| 78 |
$options = array(); |
| 79 |
$this->t->setDateTimeDb( $currentStart )->setStartDay(); |
| 80 |
$option = $this->t->formatDateTimeDb(); |
| 81 |
while( $option < $currentEnd ){ |
| 82 |
$options[] = $option; |
| 83 |
$this->t->modify( '+' . (5 * 60) . ' seconds'); |
| 84 |
$option = $this->t->formatDateTimeDb(); |
| 85 |
} |
| 86 |
|
| 87 |
$allsView = array(); |
| 88 |
|
| 89 |
foreach( $options as $option ){ |
| 90 |
$testModel = new SH4_Shifts_Model( $id, $calendar, $option, $currentEnd, $employee ); |
| 91 |
$conflicts = $this->conflicts->get($testModel); |
| 92 |
|
| 93 |
$this->t->setDateTimeDb( $option ); |
| 94 |
|
| 95 |
$thisView = $this->t->formatTime(); |
| 96 |
$thisView = $this->ui->makeSpan( $thisView ) |
| 97 |
->tag('font-size', 4) |
| 98 |
->tag('font-style', 'bold') |
| 99 |
; |
| 100 |
|
| 101 |
if( $conflicts ){ |
| 102 |
$conflictsView = '__Conflicts__'; |
| 103 |
|
| 104 |
$conflictsView = $this->ui->makeBlock('!') |
| 105 |
->tag('align', 'center') |
| 106 |
->addAttr('style', 'width: 1em;') |
| 107 |
->tag('border') |
| 108 |
->tag('border-color', 'red' ) |
| 109 |
->tag('bgcolor', 'lightred') |
| 110 |
->tag('muted', 1) |
| 111 |
->addAttr('title', '__Conflicts__') |
| 112 |
; |
| 113 |
|
| 114 |
$conflictsLink = array('conflicts', $shiftId, $calendarId, $option, $currentEnd, $employeeId); |
| 115 |
$conflictsLink = join('/', $conflictsLink); |
| 116 |
|
| 117 |
$conflictsView = $this->ui->makeAhref( $conflictsLink, $conflictsView ) |
| 118 |
->newWindow() |
| 119 |
->tag('color', 'red') |
| 120 |
; |
| 121 |
|
| 122 |
$thisView = $this->ui->makeBlock( $this->ui->makeListInline( array($thisView, $conflictsView) ) ) |
| 123 |
->addAttr('style', 'position: relative;') |
| 124 |
; |
| 125 |
} |
| 126 |
|
| 127 |
if( $option === $currentStart ){ |
| 128 |
$btn = $this->ui->makeInputSubmit('__Current Time__')->tag('secondary'); |
| 129 |
} |
| 130 |
else { |
| 131 |
$btn = $this->ui->makeInputSubmit('__Select__')->tag('secondary'); |
| 132 |
if( $conflicts ){ |
| 133 |
$btn->tag('confirm'); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
$form = $this->ui->makeForm( |
| 138 |
'shifts/' . $id . '/time/' . $option . '/' . $currentEnd, |
| 139 |
$btn |
| 140 |
); |
| 141 |
|
| 142 |
$thisView = $this->ui->makeList( array($thisView, $form) )->gutter(2); |
| 143 |
|
| 144 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 145 |
->tag('border') |
| 146 |
->tag('padding', 2) |
| 147 |
; |
| 148 |
|
| 149 |
if( $conflicts ){ |
| 150 |
} |
| 151 |
else { |
| 152 |
$thisView |
| 153 |
->tag('border-color', 'green') |
| 154 |
; |
| 155 |
} |
| 156 |
|
| 157 |
if( $option === $currentStart ){ |
| 158 |
$thisView |
| 159 |
->tag('bgcolor', 'lightgreen') |
| 160 |
; |
| 161 |
} |
| 162 |
|
| 163 |
$allsView[] = $thisView; |
| 164 |
} |
| 165 |
|
| 166 |
if( $allsView ){ |
| 167 |
$out = $this->ui->makeGrid(); |
| 168 |
foreach( $allsView as $ev ){ |
| 169 |
$out->add( $ev, 2, 6 ); |
| 170 |
} |
| 171 |
} |
| 172 |
else { |
| 173 |
$out = array(); |
| 174 |
$out[] = '__No Available Time__'; |
| 175 |
$out = $this->ui->makeList( $out ); |
| 176 |
} |
| 177 |
|
| 178 |
$this->layout |
| 179 |
->setContent( $out ) |
| 180 |
->setBreadcrumb( $this->common->breadcrumb($model) ) |
| 181 |
->setHeader( '__Start Time__' ) |
| 182 |
; |
| 183 |
$out = $this->layout->render(); |
| 184 |
|
| 185 |
return $out; |
| 186 |
} |
| 187 |
|
| 188 |
public function renderEnd( $id ) |
| 189 |
{ |
| 190 |
$model = $this->shiftsQuery->findById( $id ); |
| 191 |
$shiftId = $id; |
| 192 |
|
| 193 |
$calendar = $model->getCalendar(); |
| 194 |
$calendarId = $calendar->getId(); |
| 195 |
$employee = $model->getEmployee(); |
| 196 |
$employeeId = $employee->getId(); |
| 197 |
|
| 198 |
$currentStart = $model->getStart(); |
| 199 |
$currentEnd = $model->getEnd(); |
| 200 |
|
| 201 |
$this->t->setDateTimeDb( $currentStart ); |
| 202 |
$maxEnd = $this->t->modify('+24 hours')->formatDateTimeDb(); |
| 203 |
|
| 204 |
$options = array(); |
| 205 |
$this->t->setDateTimeDb( $currentStart ); |
| 206 |
$option = $this->t->formatDateTimeDb(); |
| 207 |
while( $option <= $maxEnd ){ |
| 208 |
if( $option > $currentStart ){ |
| 209 |
$options[] = $option; |
| 210 |
} |
| 211 |
$this->t->modify( '+' . (5 * 60) . ' seconds'); |
| 212 |
$option = $this->t->formatDateTimeDb(); |
| 213 |
} |
| 214 |
|
| 215 |
$allsView = array(); |
| 216 |
|
| 217 |
foreach( $options as $option ){ |
| 218 |
$testModel = new SH4_Shifts_Model( $id, $calendar, $currentStart, $option, $employee ); |
| 219 |
$conflicts = $this->conflicts->get($testModel); |
| 220 |
|
| 221 |
$this->t->setDateTimeDb( $option ); |
| 222 |
|
| 223 |
$thisView = $this->t->formatTime(); |
| 224 |
$thisView = $this->ui->makeSpan( $thisView ) |
| 225 |
->tag('font-size', 4) |
| 226 |
->tag('font-style', 'bold') |
| 227 |
; |
| 228 |
|
| 229 |
if( $conflicts ){ |
| 230 |
$conflictsView = '__Conflicts__'; |
| 231 |
|
| 232 |
$conflictsView = $this->ui->makeBlock('!') |
| 233 |
->tag('align', 'center') |
| 234 |
->addAttr('style', 'width: 1em;') |
| 235 |
->tag('border') |
| 236 |
->tag('border-color', 'red' ) |
| 237 |
->tag('bgcolor', 'lightred') |
| 238 |
->tag('muted', 1) |
| 239 |
->addAttr('title', '__Conflicts__') |
| 240 |
; |
| 241 |
|
| 242 |
$conflictsLink = array('conflicts', $shiftId, $calendarId, $currentStart, $option, $employeeId); |
| 243 |
$conflictsLink = join('/', $conflictsLink); |
| 244 |
|
| 245 |
$conflictsView = $this->ui->makeAhref( $conflictsLink, $conflictsView ) |
| 246 |
->newWindow() |
| 247 |
->tag('color', 'red') |
| 248 |
; |
| 249 |
|
| 250 |
$thisView = $this->ui->makeBlock( $this->ui->makeListInline( array($thisView, $conflictsView) ) ) |
| 251 |
->addAttr('style', 'position: relative;') |
| 252 |
; |
| 253 |
} |
| 254 |
|
| 255 |
if( $option === $currentEnd ){ |
| 256 |
$btn = $this->ui->makeInputSubmit('__Current Time__')->tag('secondary'); |
| 257 |
} |
| 258 |
else { |
| 259 |
$btn = $this->ui->makeInputSubmit('__Select__')->tag('secondary'); |
| 260 |
if( $conflicts ){ |
| 261 |
$btn->tag('confirm'); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
$form = $this->ui->makeForm( |
| 266 |
'shifts/' . $id . '/time/' . $currentStart . '/' . $option, |
| 267 |
$btn |
| 268 |
); |
| 269 |
|
| 270 |
$thisView = $this->ui->makeList( array($thisView, $form) )->gutter(2); |
| 271 |
|
| 272 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 273 |
->tag('border') |
| 274 |
->tag('padding', 2) |
| 275 |
; |
| 276 |
|
| 277 |
if( $conflicts ){ |
| 278 |
} |
| 279 |
else { |
| 280 |
$thisView |
| 281 |
->tag('border-color', 'green') |
| 282 |
; |
| 283 |
} |
| 284 |
|
| 285 |
if( $option === $currentEnd ){ |
| 286 |
$thisView |
| 287 |
->tag('bgcolor', 'lightgreen') |
| 288 |
; |
| 289 |
} |
| 290 |
|
| 291 |
$allsView[] = $thisView; |
| 292 |
} |
| 293 |
|
| 294 |
if( $allsView ){ |
| 295 |
$out = $this->ui->makeGrid(); |
| 296 |
foreach( $allsView as $ev ){ |
| 297 |
$out->add( $ev, 2, 6 ); |
| 298 |
} |
| 299 |
} |
| 300 |
else { |
| 301 |
$out = array(); |
| 302 |
$out[] = '__No Available Time__'; |
| 303 |
$out = $this->ui->makeList( $out ); |
| 304 |
} |
| 305 |
|
| 306 |
$this->layout |
| 307 |
->setContent( $out ) |
| 308 |
->setBreadcrumb( $this->common->breadcrumb($model) ) |
| 309 |
->setHeader( '__End Time__' ) |
| 310 |
; |
| 311 |
$out = $this->layout->render(); |
| 312 |
|
| 313 |
return $out; |
| 314 |
} |
| 315 |
|
| 316 |
public function renderShiftTypes( $model ) |
| 317 |
{ |
| 318 |
$id = $model->getId(); |
| 319 |
$calendar = $model->getCalendar(); |
| 320 |
$calendarId = $calendar->getId(); |
| 321 |
$params = $this->request->getParams(); |
| 322 |
|
| 323 |
$currentKey = $model->getStartInDay() . '-' . $model->getEndInDay(); |
| 324 |
$breakStartInDay = $model->getBreakStartInDay(); |
| 325 |
if( NULL !== $breakStartInDay ){ |
| 326 |
$currentKey .= '-' . $model->getBreakStartInDay() . '-' . $model->getBreakEndInDay(); |
| 327 |
} |
| 328 |
|
| 329 |
$entries = $this->appQuery->findShiftTypesForCalendar( $calendar ); |
| 330 |
$customTime = NULL; |
| 331 |
if( array_key_exists(0, $entries) ){ |
| 332 |
$customTime = $entries[0]; |
| 333 |
unset( $entries[0] ); |
| 334 |
} |
| 335 |
|
| 336 |
$out = array(); |
| 337 |
$shiftTypeOptions = array(); |
| 338 |
|
| 339 |
$currentShiftTypeId = NULL; |
| 340 |
$templatesOut = array(); |
| 341 |
foreach( $entries as $e ){ |
| 342 |
$thisId = $e->getId(); |
| 343 |
|
| 344 |
$thisKey = $e->getStart() . '-' . $e->getEnd(); |
| 345 |
$breakStart = $e->getBreakStart(); |
| 346 |
if( NULL !== $breakStart ){ |
| 347 |
$thisKey .= '-' . $e->getBreakStart() . '-' . $e->getBreakEnd(); |
| 348 |
} |
| 349 |
|
| 350 |
if( $currentKey == $thisKey ){ |
| 351 |
$currentShiftTypeId = $thisId; |
| 352 |
} |
| 353 |
|
| 354 |
$thisView = $this->shiftTypesPresenter->presentTitle( $e ); |
| 355 |
$timeView = $this->shiftTypesPresenter->presentTime( $e ); |
| 356 |
$timeView = $this->ui->makeSpan($timeView) |
| 357 |
->tag('font-size', 2) |
| 358 |
; |
| 359 |
$thisView = $this->ui->makeList( array($thisView, $timeView) )->gutter(1); |
| 360 |
|
| 361 |
$shiftTypeOptions[ $thisId ] = $thisView; |
| 362 |
|
| 363 |
$to = 'shifts/' . $id . '/time/' . $thisId; |
| 364 |
|
| 365 |
$thisView = $this->ui->makeAhref( $to, $thisView ) |
| 366 |
->tag('tab-link') |
| 367 |
// ->tag('border') |
| 368 |
; |
| 369 |
|
| 370 |
if( $thisView ){ |
| 371 |
$out[] = $thisView; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
if( $out ){ |
| 376 |
$out = $this->ui->makeGrid( $out, 4 ); |
| 377 |
|
| 378 |
$shiftTypeInput = $this->ui->makeInputRadioSet( 'shifttype', NULL, $shiftTypeOptions, $currentShiftTypeId ); |
| 379 |
|
| 380 |
$buttons = $this->ui->makeInputSubmit( '__Save__') |
| 381 |
->tag('primary') |
| 382 |
; |
| 383 |
|
| 384 |
$form = $this->ui->makeList() |
| 385 |
->add( $shiftTypeInput ) |
| 386 |
->add( $buttons ) |
| 387 |
; |
| 388 |
|
| 389 |
// schedule link |
| 390 |
$scheduleLink = HC3_Session::instance()->getUserdata( 'scheduleLink' ); |
| 391 |
if( ! $scheduleLink ){ |
| 392 |
$scheduleLink = array( 'schedule', array() ); |
| 393 |
} |
| 394 |
$scheduleLinkValue = json_encode( $scheduleLink ); |
| 395 |
$inputBackHidden = $this->ui->makeInputHidden( 'back', $scheduleLinkValue ); |
| 396 |
$form = $this->ui->makeCollection( array($form, $inputBackHidden) ); |
| 397 |
|
| 398 |
$out = $this->ui->makeForm( |
| 399 |
'shifts/' . $id . '/time', |
| 400 |
$form |
| 401 |
); |
| 402 |
} |
| 403 |
|
| 404 |
// if we have custom time |
| 405 |
$customTime = TRUE; |
| 406 |
|
| 407 |
if( $customTime ){ |
| 408 |
$time = array( $model->getStartInDay(), $model->getEndInDay() ); |
| 409 |
|
| 410 |
$breakOn = FALSE; |
| 411 |
$breakStart = (string) $model->getBreakStartInDay(); |
| 412 |
$breakEnd = (string) $model->getBreakEndInDay(); |
| 413 |
|
| 414 |
if( $breakStart OR $breakEnd ){ |
| 415 |
$breakOn = true; |
| 416 |
$breakInput = $this->ui->makeInputTimeRange( 'break', NULL, array($breakStart, $breakEnd) ); |
| 417 |
} |
| 418 |
else { |
| 419 |
$breakInput = $this->ui->makeInputTimeRange( 'break', NULL ); |
| 420 |
} |
| 421 |
|
| 422 |
$breakInput = $this->ui->makeCollapseCheckbox( |
| 423 |
'break_on', |
| 424 |
'__Lunch Break__' . '?', |
| 425 |
$breakInput |
| 426 |
); |
| 427 |
if( $breakOn ){ |
| 428 |
$breakInput->expand(); |
| 429 |
} |
| 430 |
|
| 431 |
$inputs = $this->ui->makeList() |
| 432 |
->add( $this->ui->makeInputTimeRange( 'time', '__Time__', $time ) ) |
| 433 |
->add( $breakInput ) |
| 434 |
; |
| 435 |
|
| 436 |
$buttons = $this->ui->makeInputSubmit( '__Save__') |
| 437 |
->tag('primary') |
| 438 |
; |
| 439 |
|
| 440 |
$form = $this->ui->makeList() |
| 441 |
->add( $inputs ) |
| 442 |
->add( $buttons ) |
| 443 |
; |
| 444 |
|
| 445 |
// schedule link |
| 446 |
$scheduleLink = HC3_Session::instance()->getUserdata( 'scheduleLink' ); |
| 447 |
if( ! $scheduleLink ){ |
| 448 |
$scheduleLink = array( 'schedule', array() ); |
| 449 |
} |
| 450 |
$scheduleLinkValue = json_encode( $scheduleLink ); |
| 451 |
$inputBackHidden = $this->ui->makeInputHidden( 'back', $scheduleLinkValue ); |
| 452 |
$form = $this->ui->makeCollection( array($form, $inputBackHidden) ); |
| 453 |
|
| 454 |
$thisForm = $this->ui->makeForm( |
| 455 |
'shifts/' . $id . '/time', |
| 456 |
$form |
| 457 |
); |
| 458 |
|
| 459 |
if( $out ){ |
| 460 |
$label1 = '<h2 style="margin: 0 0 0 0; padding: 0 0 0 0;">__Shift Time__</h2>'; |
| 461 |
$label2 = '<h2 style="margin: 0 0 0 0; padding: 0 0 0 0;">__Custom Time__</h2>'; |
| 462 |
$out = array( $label1, $out, $label2, $thisForm ); |
| 463 |
$out = $this->ui->makeList( $out ); |
| 464 |
} |
| 465 |
else { |
| 466 |
$out = $thisForm; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
return $out; |
| 471 |
} |
| 472 |
} |