| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Schedule_Html_View_List |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
|
| 8 |
HC3_Ui $ui, |
| 9 |
HC3_Request $request, |
| 10 |
HC3_Time $t, |
| 11 |
|
| 12 |
SH4_New_Acl $newAcl, |
| 13 |
|
| 14 |
SH4_Shifts_Duration $shiftsDuration, |
| 15 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 16 |
SH4_Employees_Presenter $employeesPresenter, |
| 17 |
SH4_Shifts_View_Widget $widget, |
| 18 |
SH4_Schedule_Html_View_Common $common, |
| 19 |
SH4_Shifts_View_Common $shiftsCommon |
| 20 |
) |
| 21 |
{ |
| 22 |
$this->self = $hooks->wrap( $this ); |
| 23 |
|
| 24 |
$this->ui = $ui; |
| 25 |
$this->t = $t; |
| 26 |
$this->request = $request; |
| 27 |
$this->newAcl = $hooks->wrap( $newAcl ); |
| 28 |
|
| 29 |
$this->shiftsDuration = $hooks->wrap( $shiftsDuration ); |
| 30 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 31 |
$this->employeesPresenter = $hooks->wrap( $employeesPresenter ); |
| 32 |
|
| 33 |
$this->widget = $hooks->wrap( $widget ); |
| 34 |
$this->common = $hooks->wrap( $common ); |
| 35 |
$this->shiftsCommon = $hooks->wrap( $shiftsCommon ); |
| 36 |
} |
| 37 |
|
| 38 |
public function render() |
| 39 |
{ |
| 40 |
$params = $this->request->getParams(); |
| 41 |
$startDate = $params['start']; |
| 42 |
|
| 43 |
$hideui = $params['hideui']; |
| 44 |
$noZoom = in_array('shiftdetails', $hideui); |
| 45 |
|
| 46 |
$allEmployees = $this->common->findAllEmployees(); |
| 47 |
$allCalendars = $this->common->findAllCalendars(); |
| 48 |
|
| 49 |
$shifts = $this->common->getShifts(); |
| 50 |
|
| 51 |
$iknow = array(); |
| 52 |
if( count($allCalendars) <= 1 ) $iknow[] = 'calendar'; |
| 53 |
|
| 54 |
$hori = TRUE; |
| 55 |
|
| 56 |
$this->shiftsDuration->reset(); |
| 57 |
$thisOut = array(); |
| 58 |
foreach( $shifts as $shift ){ |
| 59 |
$id = $shift->getId(); |
| 60 |
$thisView = $this->renderShift( $shift, $iknow, $hori, $noZoom ); |
| 61 |
|
| 62 |
$menu = array(); |
| 63 |
$fullMenu = $this->shiftsCommon->menu( $shift ); |
| 64 |
foreach( $fullMenu as $k0 => $thisMenu ){ |
| 65 |
foreach( $thisMenu as $k1 => $actionArray ){ |
| 66 |
$k = $k0 . '_' . $k1; |
| 67 |
$menu[$k] = $actionArray; |
| 68 |
} |
| 69 |
} |
| 70 |
if( $menu ){ |
| 71 |
$menu = $this->ui->helperActionsFromArray( $menu, TRUE ); |
| 72 |
$menu = $this->ui->makeCollection( $menu ); |
| 73 |
|
| 74 |
$checkbox = $this->ui->makeInputCheckbox( 'id[]', NULL, $id, TRUE ); |
| 75 |
$checkbox = $this->ui->makeBlock( $checkbox ) |
| 76 |
->addAttr('class', 'sh4-shift-checker') |
| 77 |
->addAttr('style', 'display: none;') |
| 78 |
; |
| 79 |
$thisView = $this->ui->makeCollection( array($thisView, $checkbox, $menu) ); |
| 80 |
} |
| 81 |
|
| 82 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 83 |
->tag('block') |
| 84 |
->addAttr('class', 'sh4-shift-widget') |
| 85 |
; |
| 86 |
|
| 87 |
$thisOut[] = $thisView; |
| 88 |
|
| 89 |
$this->shiftsDuration->add( $shift ); |
| 90 |
} |
| 91 |
|
| 92 |
$out = $this->ui->makeList( $thisOut ); |
| 93 |
|
| 94 |
return $out; |
| 95 |
} |
| 96 |
|
| 97 |
public function renderByCalendar() |
| 98 |
{ |
| 99 |
$isPrintView = $this->request->isPrintView(); |
| 100 |
|
| 101 |
$params = $this->request->getParams(); |
| 102 |
$startDate = $params['start']; |
| 103 |
|
| 104 |
$calendars = $this->common->getCalendars(); |
| 105 |
$employees = $this->common->getEmployees(); |
| 106 |
$shifts = $this->common->getShifts(); |
| 107 |
|
| 108 |
// finalize calendars to include those that employee was removed from but still have shifts in |
| 109 |
$listCalendarId = array_keys( $calendars ); |
| 110 |
$dictCalendarId = array_combine( $listCalendarId, $listCalendarId ); |
| 111 |
foreach( $shifts as $shift ){ |
| 112 |
$calendar = $shift->getCalendar(); |
| 113 |
if( ! $calendar ) continue; |
| 114 |
$calendarId = $calendar->getId(); |
| 115 |
$dictCalendarId[ $calendarId ] = $calendarId; |
| 116 |
} |
| 117 |
if( count($dictCalendarId) > count($calendars) ){ |
| 118 |
$allCalendars = $this->common->findAllCalendars(); |
| 119 |
$calendars = array_intersect_key( $allCalendars, $dictCalendarId ); |
| 120 |
} |
| 121 |
|
| 122 |
// we can probably have archived calendars so if such calendars have no shifts then skip them |
| 123 |
$removeArchivedCalendars = array(); |
| 124 |
foreach( $calendars as $calendar ){ |
| 125 |
if( $calendar->isArchived() ){ |
| 126 |
$removeArchivedCalendars[ $calendar->getId() ] = $calendar->getId(); |
| 127 |
} |
| 128 |
} |
| 129 |
if( $removeArchivedCalendars ){ |
| 130 |
foreach( $shifts as $shift ){ |
| 131 |
$calendar = $shift->getCalendar(); |
| 132 |
$id = $calendar ? $calendar->getId() : 0; |
| 133 |
if( isset($removeArchivedCalendars[$id]) ){ |
| 134 |
unset( $removeArchivedCalendars[$id] ); |
| 135 |
} |
| 136 |
if( ! $removeArchivedCalendars ){ |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
foreach( $removeArchivedCalendars as $id ){ |
| 141 |
unset( $calendars[$id] ); |
| 142 |
} |
| 143 |
reset( $shifts ); |
| 144 |
} |
| 145 |
|
| 146 |
$hideui = $params['hideui']; |
| 147 |
$noZoom = in_array('shiftdetails', $hideui); |
| 148 |
|
| 149 |
$viewCalendars = array(); |
| 150 |
foreach( $calendars as $calendar ){ |
| 151 |
$calendarId = $calendar->getId(); |
| 152 |
// $label = $this->calendarsPresenter->presentTitle( $calendar ); |
| 153 |
// $label = $calendar->getTitle(); |
| 154 |
$label = $this->calendarsPresenter->presentTitle( $calendar ); |
| 155 |
$viewCalendars[ $calendarId ] = $label; |
| 156 |
} |
| 157 |
|
| 158 |
$shiftsBy = array(); |
| 159 |
|
| 160 |
$iknow = array( 'calendar' ); |
| 161 |
$hori = TRUE; |
| 162 |
|
| 163 |
$allEmployees = $this->common->findAllEmployees(); |
| 164 |
if( count($allEmployees) < 2 ){ |
| 165 |
$iknow[] = 'employee'; |
| 166 |
} |
| 167 |
|
| 168 |
foreach( $shifts as $shift ){ |
| 169 |
$calendar = $shift->getCalendar(); |
| 170 |
$calendarId = $calendar ? $calendar->getId() : 0; |
| 171 |
if( ! array_key_exists($calendarId, $shiftsBy) ){ |
| 172 |
$shiftsBy[$calendarId] = array(); |
| 173 |
} |
| 174 |
$shiftsBy[$calendarId][] = $shift; |
| 175 |
} |
| 176 |
|
| 177 |
$out = array(); |
| 178 |
foreach( $viewCalendars as $calendarId => $calendarView ){ |
| 179 |
if( ! isset($shiftsBy[$calendarId]) ){ |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
if( ! isset($calendars[$calendarId]) ){ |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
$thisCalendar = $calendars[$calendarId]; |
| 188 |
$thisOut = array(); |
| 189 |
|
| 190 |
$this->shiftsDuration->reset(); |
| 191 |
$outShifts = array(); |
| 192 |
foreach( $shiftsBy[$calendarId] as $shift ){ |
| 193 |
$id = $shift->getId(); |
| 194 |
$thisView = $this->renderShift( $shift, $iknow, $hori, $noZoom ); |
| 195 |
|
| 196 |
$menu = array(); |
| 197 |
$fullMenu = $this->shiftsCommon->menu( $shift ); |
| 198 |
foreach( $fullMenu as $k0 => $thisMenu ){ |
| 199 |
foreach( $thisMenu as $k1 => $actionArray ){ |
| 200 |
$k = $k0 . '_' . $k1; |
| 201 |
$menu[$k] = $actionArray; |
| 202 |
} |
| 203 |
} |
| 204 |
if( $menu ){ |
| 205 |
$menu = $this->ui->helperActionsFromArray( $menu, TRUE ); |
| 206 |
$menu = $this->ui->makeCollection( $menu ); |
| 207 |
|
| 208 |
$checkbox = $this->ui->makeInputCheckbox( 'id[]', NULL, $id, TRUE ); |
| 209 |
$checkbox = $this->ui->makeBlock( $checkbox ) |
| 210 |
->addAttr('class', 'sh4-shift-checker') |
| 211 |
->addAttr('style', 'display: none;') |
| 212 |
; |
| 213 |
$thisView = $this->ui->makeCollection( array($thisView, $checkbox, $menu) ); |
| 214 |
} |
| 215 |
|
| 216 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 217 |
->tag('block') |
| 218 |
->addAttr('class', 'sh4-shift-widget') |
| 219 |
; |
| 220 |
$outShifts[] = $thisView; |
| 221 |
|
| 222 |
$this->shiftsDuration->add( $shift ); |
| 223 |
} |
| 224 |
$outShifts = $this->ui->makeList( $outShifts )->gutter(1); |
| 225 |
|
| 226 |
$thisOut[] = $outShifts; |
| 227 |
|
| 228 |
// report |
| 229 |
$outReport = $this->common->renderReport( $this->shiftsDuration ); |
| 230 |
|
| 231 |
// new links |
| 232 |
$links = array(); |
| 233 |
|
| 234 |
if( $thisCalendar->isTimeOff() ){ |
| 235 |
$label = '+' . ' ' . '__Time Off__'; |
| 236 |
} |
| 237 |
elseif( $thisCalendar->isAvailability() ){ |
| 238 |
$label = '+' . ' ' . '__Availability__'; |
| 239 |
} |
| 240 |
else { |
| 241 |
$label = '+' . ' ' . '__Shift__'; |
| 242 |
} |
| 243 |
|
| 244 |
$to = 'new'; |
| 245 |
$toParams = array( |
| 246 |
'calendar' => $calendarId |
| 247 |
); |
| 248 |
if( array_key_exists('employee', $params) && (count($params['employee']) == 1) ){ |
| 249 |
$toParams['employee'] = $params['employee'][0]; |
| 250 |
} |
| 251 |
|
| 252 |
$allowed = $this->newAcl->checkNewShift( $toParams ); |
| 253 |
if( $allowed ){ |
| 254 |
$to = array( $to, $toParams ); |
| 255 |
$newLink = $this->ui->makeAhref( $to, $label ) |
| 256 |
->tag('secondary') |
| 257 |
; |
| 258 |
$links[] = $newLink; |
| 259 |
} |
| 260 |
|
| 261 |
if( $links ){ |
| 262 |
$links = $this->ui->makeListInline( $links ) |
| 263 |
->gutter(1) |
| 264 |
; |
| 265 |
$calendarView = $this->ui->makeListInline( array($calendarView, $links) ); |
| 266 |
} |
| 267 |
|
| 268 |
$onTop = $this->ui->makeGrid() |
| 269 |
->add( $calendarView, 4, 12 ) |
| 270 |
->add( $outReport, 8, 12 ) |
| 271 |
; |
| 272 |
|
| 273 |
$thisOut = $this->ui->makeList( $thisOut ); |
| 274 |
$thisOut = $this->ui->makeList( array($onTop, $thisOut) ); |
| 275 |
|
| 276 |
$pageBreak = '<div style="page-break-before: always;"></div>'; |
| 277 |
if( ! $out ){ |
| 278 |
if( $isPrintView ){ |
| 279 |
$out[] = $pageBreak; |
| 280 |
} |
| 281 |
} |
| 282 |
$out[] = $thisOut; |
| 283 |
if( $isPrintView ){ |
| 284 |
$out[] = $pageBreak; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
$out = $this->ui->makeList( $out ) |
| 289 |
->gutter(4) |
| 290 |
; |
| 291 |
|
| 292 |
return $out; |
| 293 |
} |
| 294 |
|
| 295 |
public function renderByEmployee() |
| 296 |
{ |
| 297 |
$isPrintView = $this->request->isPrintView(); |
| 298 |
|
| 299 |
$params = $this->request->getParams(); |
| 300 |
$startDate = $params['start']; |
| 301 |
|
| 302 |
$calendars = $this->common->getCalendars(); |
| 303 |
$employees = $this->common->getEmployees(); |
| 304 |
|
| 305 |
$shifts = $this->common->getShifts(); |
| 306 |
|
| 307 |
// finalize employees to include those that employee was removed from but still have shifts in |
| 308 |
$listEmployeeId = array_keys( $employees ); |
| 309 |
$dictEmployeeId = array_combine( $listEmployeeId, $listEmployeeId ); |
| 310 |
foreach( $shifts as $shift ){ |
| 311 |
$employee = $shift->getEmployee(); |
| 312 |
if( ! $employee ) continue; |
| 313 |
$employeeId = $employee->getId(); |
| 314 |
$dictEmployeeId[ $employeeId ] = $employeeId; |
| 315 |
} |
| 316 |
if( count($dictEmployeeId) > count($employees) ){ |
| 317 |
$allEmployees = $this->common->findAllEmployees(); |
| 318 |
$employees = array_intersect_key( $allEmployees, $dictEmployeeId ); |
| 319 |
} |
| 320 |
|
| 321 |
// we can probably have archived employees so if such employees have no shifts then skip them |
| 322 |
$removeArchivedEmployees = array(); |
| 323 |
foreach( $employees as $employee ){ |
| 324 |
if( $employee->isArchived() ){ |
| 325 |
$removeArchivedEmployees[ $employee->getId() ] = $employee->getId(); |
| 326 |
} |
| 327 |
} |
| 328 |
if( $removeArchivedEmployees ){ |
| 329 |
foreach( $shifts as $shift ){ |
| 330 |
$employee = $shift->getEmployee(); |
| 331 |
$id = $employee ? $employee->getId() : 0; |
| 332 |
if( isset($removeArchivedEmployees[$id]) ){ |
| 333 |
unset( $removeArchivedEmployees[$id] ); |
| 334 |
} |
| 335 |
if( ! $removeArchivedEmployees ){ |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
foreach( $removeArchivedEmployees as $id ){ |
| 340 |
unset( $employees[$id] ); |
| 341 |
} |
| 342 |
reset( $shifts ); |
| 343 |
} |
| 344 |
|
| 345 |
$hideui = $params['hideui']; |
| 346 |
$noZoom = in_array('shiftdetails', $hideui); |
| 347 |
|
| 348 |
$viewEmployees = array(); |
| 349 |
foreach( $employees as $employee ){ |
| 350 |
$label = $this->employeesPresenter->presentTitle( $employee ); |
| 351 |
$label = $this->ui->makeSpan( $label ) |
| 352 |
->tag('font-size', 5) |
| 353 |
; |
| 354 |
$viewEmployees[ $employee->getId() ] = $label; |
| 355 |
} |
| 356 |
|
| 357 |
$shiftsBy = array(); |
| 358 |
|
| 359 |
$iknow = array( 'employee' ); |
| 360 |
|
| 361 |
$allCalendars = $this->common->findAllCalendars(); |
| 362 |
if( count($allCalendars) <= 1 ) $iknow[] = 'calendar'; |
| 363 |
|
| 364 |
$hori = TRUE; |
| 365 |
|
| 366 |
foreach( $shifts as $shift ){ |
| 367 |
$employee = $shift->getEmployee(); |
| 368 |
$employeeId = $employee ? $employee->getId() : 0; |
| 369 |
if( ! array_key_exists($employeeId, $shiftsBy) ){ |
| 370 |
$shiftsBy[$employeeId] = array(); |
| 371 |
} |
| 372 |
$shiftsBy[$employeeId][] = $shift; |
| 373 |
} |
| 374 |
|
| 375 |
$out = array(); |
| 376 |
foreach( $viewEmployees as $employeeId => $employeeView ){ |
| 377 |
if( ! isset($shiftsBy[$employeeId]) ){ |
| 378 |
continue; |
| 379 |
} |
| 380 |
|
| 381 |
$thisOut = array(); |
| 382 |
|
| 383 |
$this->shiftsDuration->reset(); |
| 384 |
$counted = 0; |
| 385 |
$outShifts = array(); |
| 386 |
|
| 387 |
foreach( $shiftsBy[$employeeId] as $shift ){ |
| 388 |
$id = $shift->getId(); |
| 389 |
$thisView = $this->renderShift( $shift, $iknow, $hori, $noZoom ); |
| 390 |
|
| 391 |
$menu = array(); |
| 392 |
$fullMenu = $this->shiftsCommon->menu( $shift ); |
| 393 |
foreach( $fullMenu as $k0 => $thisMenu ){ |
| 394 |
foreach( $thisMenu as $k1 => $actionArray ){ |
| 395 |
$k = $k0 . '_' . $k1; |
| 396 |
$menu[$k] = $actionArray; |
| 397 |
} |
| 398 |
} |
| 399 |
if( $menu ){ |
| 400 |
$menu = $this->ui->helperActionsFromArray( $menu, TRUE ); |
| 401 |
$menu = $this->ui->makeCollection( $menu ); |
| 402 |
|
| 403 |
$checkbox = $this->ui->makeInputCheckbox( 'id[]', NULL, $id, TRUE ); |
| 404 |
$checkbox = $this->ui->makeBlock( $checkbox ) |
| 405 |
->addAttr('class', 'sh4-shift-checker') |
| 406 |
->addAttr('style', 'display: none;') |
| 407 |
; |
| 408 |
$thisView = $this->ui->makeCollection( array($thisView, $checkbox, $menu) ); |
| 409 |
} |
| 410 |
|
| 411 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 412 |
->tag('block') |
| 413 |
->addAttr('class', 'sh4-shift-widget') |
| 414 |
; |
| 415 |
|
| 416 |
$outShifts[] = $thisView; |
| 417 |
|
| 418 |
$shiftCalendar = $shift->getCalendar(); |
| 419 |
// if( ! $shiftCalendar->isShift() ){ |
| 420 |
// continue; |
| 421 |
// } |
| 422 |
$this->shiftsDuration->add( $shift ); |
| 423 |
$counted++; |
| 424 |
} |
| 425 |
|
| 426 |
$outShifts = $this->ui->makeList( $outShifts )->gutter(1); |
| 427 |
$thisOut[] = $outShifts; |
| 428 |
|
| 429 |
// report |
| 430 |
$outReport = NULL; |
| 431 |
if( $counted ){ |
| 432 |
$outReport = $this->common->renderReport( $this->shiftsDuration ); |
| 433 |
} |
| 434 |
|
| 435 |
// new links |
| 436 |
$newLinks = array(); |
| 437 |
|
| 438 |
$label = '+' . ' ' . '__Shift__'; |
| 439 |
$to = 'new/shift'; |
| 440 |
$toParams = array( |
| 441 |
'employee' => $employeeId |
| 442 |
); |
| 443 |
|
| 444 |
$allowed = $this->newAcl->checkNewShift( $toParams ); |
| 445 |
if( $allowed ){ |
| 446 |
$to = array( $to, $toParams ); |
| 447 |
$link = $this->ui->makeAhref( $to, $label ) |
| 448 |
->tag('secondary') |
| 449 |
; |
| 450 |
$newLinks[] = $link; |
| 451 |
} |
| 452 |
|
| 453 |
$label = '+' . ' ' . '__Time Off__'; |
| 454 |
$to = 'new/timeoff'; |
| 455 |
$toParams = array( |
| 456 |
'employee' => $employeeId |
| 457 |
); |
| 458 |
|
| 459 |
$allowed = $this->newAcl->checkNewTimeoff( $toParams ); |
| 460 |
if( $allowed ){ |
| 461 |
$to = array( $to, $toParams ); |
| 462 |
$link = $this->ui->makeAhref( $to, $label ) |
| 463 |
->tag('secondary') |
| 464 |
; |
| 465 |
$newLinks[] = $link; |
| 466 |
} |
| 467 |
|
| 468 |
if( $newLinks ){ |
| 469 |
$newLinks = $this->ui->makeListInline( $newLinks ) |
| 470 |
->gutter(1) |
| 471 |
; |
| 472 |
$employeeView = $this->ui->makeListInline( array($employeeView, $newLinks) ); |
| 473 |
} |
| 474 |
|
| 475 |
$onTop = $this->ui->makeGrid() |
| 476 |
->add( $employeeView, 4, 12 ) |
| 477 |
->add( $outReport, 8, 12 ) |
| 478 |
; |
| 479 |
|
| 480 |
$thisOut = $this->ui->makeList( $thisOut ); |
| 481 |
$thisOut = $this->ui->makeList( array($onTop, $thisOut) ); |
| 482 |
|
| 483 |
$pageBreak = '<div style="page-break-before: always;"></div>'; |
| 484 |
if( ! $out ){ |
| 485 |
if( $isPrintView ){ |
| 486 |
$out[] = $pageBreak; |
| 487 |
} |
| 488 |
} |
| 489 |
$out[] = $thisOut; |
| 490 |
if( $isPrintView ){ |
| 491 |
$out[] = $pageBreak; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
$out = $this->ui->makeList( $out ) |
| 496 |
->gutter(4) |
| 497 |
; |
| 498 |
|
| 499 |
return $out; |
| 500 |
} |
| 501 |
|
| 502 |
public function renderShift( $shift, $iknow, $hori ) |
| 503 |
{ |
| 504 |
$params = $this->request->getParams(); |
| 505 |
|
| 506 |
if( array_key_exists('employee', $params) && (count($params['employee']) == 1) ){ |
| 507 |
if( ! in_array(-1, $params['employee']) ){ |
| 508 |
$iknow[] = 'employee'; |
| 509 |
} |
| 510 |
} |
| 511 |
if( array_key_exists('calendar', $params) && (count($params['calendar']) == 1) ){ |
| 512 |
$iknow[] = 'calendar'; |
| 513 |
} |
| 514 |
|
| 515 |
$out = $this->widget->render( $shift, $iknow, $hori ); |
| 516 |
return $out; |
| 517 |
} |
| 518 |
} |