| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Schedule_Html_View_TwoWeeks |
| 4 |
{ |
| 5 |
protected $borderColor = 'gray'; |
| 6 |
protected $allCombos = array(); |
| 7 |
protected $allCombosShift = array(); |
| 8 |
protected $allCombosTimeoff = array(); |
| 9 |
|
| 10 |
public function __construct( |
| 11 |
HC3_Hooks $hooks, |
| 12 |
HC3_Ui $ui, |
| 13 |
HC3_Request $request, |
| 14 |
HC3_Time $t, |
| 15 |
HC3_Settings $settings, |
| 16 |
|
| 17 |
HC3_Uri $uri, |
| 18 |
|
| 19 |
SH4_New_Acl $newAcl, |
| 20 |
|
| 21 |
SH4_Shifts_Duration $shiftsDuration, |
| 22 |
SH4_Schedule_Html_View_Week $viewWeek, |
| 23 |
SH4_Shifts_View_Widget $widget, |
| 24 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 25 |
SH4_Employees_Presenter $employeesPresenter, |
| 26 |
SH4_Schedule_Html_View_Common $common, |
| 27 |
SH4_Shifts_View_Common $shiftsCommon |
| 28 |
) |
| 29 |
{ |
| 30 |
$this->self = $hooks->wrap($this); |
| 31 |
|
| 32 |
$this->ui = $ui; |
| 33 |
|
| 34 |
$this->uri = $uri; |
| 35 |
$this->t = $t; |
| 36 |
$this->request = $request; |
| 37 |
|
| 38 |
$this->settings = $hooks->wrap( $settings ); |
| 39 |
$this->newAcl = $hooks->wrap( $newAcl ); |
| 40 |
|
| 41 |
$this->viewWeek = $hooks->wrap( $viewWeek ); |
| 42 |
$this->shiftsDuration = $hooks->wrap( $shiftsDuration ); |
| 43 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 44 |
$this->employeesPresenter = $hooks->wrap( $employeesPresenter ); |
| 45 |
|
| 46 |
$this->widget = $hooks->wrap( $widget ); |
| 47 |
$this->common = $hooks->wrap( $common ); |
| 48 |
$this->shiftsCommon = $hooks->wrap( $shiftsCommon ); |
| 49 |
|
| 50 |
$this->allCombos = $this->newAcl->findAllCombos(); |
| 51 |
$this->allCombosShift = $this->newAcl->findAllCombosShift(); |
| 52 |
$this->allCombosTimeoff = $this->newAcl->findAllCombosTimeoff(); |
| 53 |
} |
| 54 |
|
| 55 |
public function renderDateLabel( $date ) |
| 56 |
{ |
| 57 |
$slug = $this->request->getSlug(); |
| 58 |
$today = $this->t->setNow()->formatDateDb(); |
| 59 |
|
| 60 |
$this->t->setDateDb( $date ); |
| 61 |
$weekdayView = $this->t->getWeekdayName(); |
| 62 |
$weekdayView = $this->ui->makeBlock( $weekdayView )->tag('muted')->tag('font-size', 1); |
| 63 |
|
| 64 |
$ret = $this->t->getDay() . ' ' . $this->t->getMonthName(); |
| 65 |
|
| 66 |
$ret = $this->ui->makeListInline( array($weekdayView, $ret) ) |
| 67 |
->tag('align', 'center') |
| 68 |
; |
| 69 |
|
| 70 |
$toDateParams = array(); |
| 71 |
$toDateParams['type'] = 'day'; |
| 72 |
$toDateParams['start'] = $date; |
| 73 |
$toDate = array( $slug, $toDateParams ); |
| 74 |
|
| 75 |
$weekLabel = $this->t->getWeekNo(); |
| 76 |
$weekLabel = ' [' . '__Week__' . ' #' . $weekLabel . ']'; |
| 77 |
|
| 78 |
$ret = $this->ui->makeAhref( $toDate, $ret ) |
| 79 |
->tag('print') |
| 80 |
->addAttr( 'title', $weekLabel ) |
| 81 |
; |
| 82 |
|
| 83 |
if( $date == $today ){ |
| 84 |
$ret = $this->ui->makeBlock( $ret ) |
| 85 |
->tag('border') |
| 86 |
->tag('border-color', 'gray') |
| 87 |
; |
| 88 |
} |
| 89 |
|
| 90 |
return $ret; |
| 91 |
} |
| 92 |
|
| 93 |
public function render() |
| 94 |
{ |
| 95 |
$slug = $this->request->getSlug(); |
| 96 |
$today = $this->t->setNow()->formatDateDb(); |
| 97 |
|
| 98 |
$disabledWeekdays = $this->settings->get( 'skip_weekdays', TRUE ); |
| 99 |
|
| 100 |
$shifts = $this->common->getShifts(); |
| 101 |
|
| 102 |
$params = $this->request->getParams(); |
| 103 |
$startDate = $params['start']; |
| 104 |
|
| 105 |
$startDate = $this->t->setDateDb( $startDate )->setStartWeek()->formatDateDb(); |
| 106 |
$endDate = $this->t->modify('+2 weeks')->modify('-1 day')->formatDateDb(); |
| 107 |
|
| 108 |
$this->t->setDateDb( $startDate ); |
| 109 |
$dates = array(); |
| 110 |
$rexDate = $startDate; |
| 111 |
while( $rexDate <= $endDate ){ |
| 112 |
if( $disabledWeekdays ){ |
| 113 |
$wkd = $this->t->getWeekday(); |
| 114 |
if( in_array($wkd, $disabledWeekdays) ){ |
| 115 |
$rexDate = $this->t->modify('+1 day')->formatDateDb(); |
| 116 |
continue; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$dates[] = $rexDate; |
| 121 |
$rexDate = $this->t->modify('+1 day')->formatDateDb(); |
| 122 |
} |
| 123 |
|
| 124 |
$viewShifts = array(); |
| 125 |
$iknow = array('date'); |
| 126 |
|
| 127 |
$allCalendars = $this->common->findAllCalendars(); |
| 128 |
if( count($allCalendars) <= 1 ) $iknow[] = 'calendar'; |
| 129 |
|
| 130 |
$this->shiftsDuration->reset(); |
| 131 |
foreach( $shifts as $shift ){ |
| 132 |
$this->shiftsDuration->add( $shift ); |
| 133 |
$start = $shift->getStart(); |
| 134 |
$end = $shift->getEnd(); |
| 135 |
|
| 136 |
$startDayTime = substr($start, 8, 4); |
| 137 |
$endDayTime = substr($end, 8, 4); |
| 138 |
$shiftAllDay = ( (0 == $startDayTime) && (0 == $endDayTime) ) ? TRUE : FALSE; |
| 139 |
|
| 140 |
$this->t->setDateTimeDb( $end )->modify('-1 second'); |
| 141 |
$shiftEndDate = $this->t->formatDateDb(); |
| 142 |
|
| 143 |
$this->t->setDateTimeDb( $start ); |
| 144 |
$shiftStartDate = $this->t->formatDateDb(); |
| 145 |
|
| 146 |
$rexDate = $shiftStartDate; |
| 147 |
while( $rexDate <= $shiftEndDate ){ |
| 148 |
if( in_array($rexDate, $dates) ){ |
| 149 |
if( ! array_key_exists($rexDate, $viewShifts) ){ |
| 150 |
$viewShifts[$rexDate] = array(); |
| 151 |
} |
| 152 |
$viewShifts[$rexDate][] = $shift; |
| 153 |
} |
| 154 |
|
| 155 |
if( ! $shiftAllDay ){ |
| 156 |
break; |
| 157 |
} |
| 158 |
$this->t->modify( '+1 day' ); |
| 159 |
$rexDate = $this->t->formatDateDb(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$this->t->setDateDb( $startDate ); |
| 164 |
// $monthMatrix = $this->t->getMonthMatrix( $disabledWeekdays ); |
| 165 |
$monthMatrix = $this->t->getWeeksMatrix( 2, $disabledWeekdays ); |
| 166 |
$rows = array(); |
| 167 |
foreach( $monthMatrix as $week => $days ){ |
| 168 |
$row = array(); |
| 169 |
|
| 170 |
foreach( $days as $date ){ |
| 171 |
if( ! $date ){ |
| 172 |
$row[] = NULL; |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
$cell = array(); |
| 177 |
|
| 178 |
$this->t->setDateDb( $date ); |
| 179 |
$dateView = $this->self->renderDateLabel( $date ); |
| 180 |
|
| 181 |
$cell[] = $dateView; |
| 182 |
|
| 183 |
if( isset($viewShifts[$date]) ){ |
| 184 |
$shiftsView = $this->viewWeek->renderDay( $viewShifts[$date], $iknow ); |
| 185 |
$cell[] = $shiftsView; |
| 186 |
} |
| 187 |
|
| 188 |
$to = 'new'; |
| 189 |
$toParams = array( |
| 190 |
'date' => $date |
| 191 |
); |
| 192 |
if( array_key_exists('employee', $params) && (count($params['employee']) == 1) ){ |
| 193 |
if( ! in_array(-1, $params['employee']) ){ |
| 194 |
$toParams['employee'] = $params['employee'][0]; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
$links = array(); |
| 199 |
|
| 200 |
if( $this->allCombosShift ){ |
| 201 |
$label = '+' . ' ' . '__Shift__'; |
| 202 |
$thisTo = 'new/shift'; |
| 203 |
$thisTo = array( $thisTo, $toParams ); |
| 204 |
$link = $this->ui->makeAhref( $thisTo, $label ) |
| 205 |
->tag('tab-link') |
| 206 |
->tag('align', 'center') |
| 207 |
// ->addAttr('title', '__Add New__') |
| 208 |
; |
| 209 |
if( $today > $date ){ |
| 210 |
$link->tag('muted', 3); |
| 211 |
} |
| 212 |
$links[] = $link; |
| 213 |
} |
| 214 |
|
| 215 |
if( $this->allCombosTimeoff ){ |
| 216 |
$label = '+' . ' ' . '__Time Off__'; |
| 217 |
$thisTo = 'new/timeoff'; |
| 218 |
$thisTo = array( $thisTo, $toParams ); |
| 219 |
$link = $this->ui->makeAhref( $thisTo, $label ) |
| 220 |
->tag('tab-link') |
| 221 |
->tag('align', 'center') |
| 222 |
// ->addAttr('title', '__Add New__') |
| 223 |
; |
| 224 |
if( $today > $date ){ |
| 225 |
$link->tag('muted', 3); |
| 226 |
} |
| 227 |
$links[] = $link; |
| 228 |
} |
| 229 |
|
| 230 |
$links = $this->ui->makeList( $links )->gutter(0); |
| 231 |
|
| 232 |
$cell[] = $links; |
| 233 |
|
| 234 |
|
| 235 |
$cell = $this->ui->makeList( $cell ) |
| 236 |
->gutter(1) |
| 237 |
->tag('margin', 1) |
| 238 |
; |
| 239 |
|
| 240 |
$row[] = $cell; |
| 241 |
} |
| 242 |
|
| 243 |
$rows[] = $row; |
| 244 |
} |
| 245 |
|
| 246 |
$out = $this->ui->makeTable( NULL, $rows, FALSE ) |
| 247 |
->gutter(0) |
| 248 |
->setBordered( $this->borderColor ) |
| 249 |
// ->setSegments( $weekSegments ) |
| 250 |
; |
| 251 |
|
| 252 |
return $out; |
| 253 |
} |
| 254 |
|
| 255 |
public function renderByCalendar() |
| 256 |
{ |
| 257 |
$slug = $this->request->getSlug(); |
| 258 |
$today = $this->t->setNow()->formatDateDb(); |
| 259 |
|
| 260 |
$disabledWeekdays = $this->settings->get( 'skip_weekdays', TRUE ); |
| 261 |
|
| 262 |
$calendars = $this->common->getCalendars(); |
| 263 |
$employees = $this->common->getEmployees(); |
| 264 |
$shifts = $this->common->getShifts(); |
| 265 |
|
| 266 |
// finalize calendars to include those that employee was removed from but still have shifts in |
| 267 |
$listCalendarId = array_keys( $calendars ); |
| 268 |
$dictCalendarId = array_combine( $listCalendarId, $listCalendarId ); |
| 269 |
foreach( $shifts as $shift ){ |
| 270 |
$calendar = $shift->getCalendar(); |
| 271 |
if( ! $calendar ) continue; |
| 272 |
$calendarId = $calendar->getId(); |
| 273 |
$dictCalendarId[ $calendarId ] = $calendarId; |
| 274 |
} |
| 275 |
if( count($dictCalendarId) > count($calendars) ){ |
| 276 |
$allCalendars = $this->common->findAllCalendars(); |
| 277 |
$calendars = array_intersect_key( $allCalendars, $dictCalendarId ); |
| 278 |
} |
| 279 |
|
| 280 |
// we can probably have archived calendars so if such calendars have no shifts then skip them |
| 281 |
$removeArchivedCalendars = array(); |
| 282 |
foreach( $calendars as $calendar ){ |
| 283 |
if( $calendar->isArchived() ){ |
| 284 |
$removeArchivedCalendars[ $calendar->getId() ] = $calendar->getId(); |
| 285 |
} |
| 286 |
} |
| 287 |
if( $removeArchivedCalendars ){ |
| 288 |
foreach( $shifts as $shift ){ |
| 289 |
$calendar = $shift->getCalendar(); |
| 290 |
$id = $calendar ? $calendar->getId() : 0; |
| 291 |
if( isset($removeArchivedCalendars[$id]) ){ |
| 292 |
unset( $removeArchivedCalendars[$id] ); |
| 293 |
} |
| 294 |
if( ! $removeArchivedCalendars ){ |
| 295 |
break; |
| 296 |
} |
| 297 |
} |
| 298 |
foreach( $removeArchivedCalendars as $id ){ |
| 299 |
unset( $calendars[$id] ); |
| 300 |
} |
| 301 |
reset( $shifts ); |
| 302 |
} |
| 303 |
|
| 304 |
$params = $this->request->getParams(); |
| 305 |
$startDate = $params['start']; |
| 306 |
|
| 307 |
$startDate = $this->t->setDateDb( $startDate )->setStartWeek()->formatDateDb(); |
| 308 |
$endDate = $this->t->modify('+2 weeks')->modify('-1 day')->formatDateDb(); |
| 309 |
|
| 310 |
$this->t->setDateDb( $startDate ); |
| 311 |
$dates = array(); |
| 312 |
$rexDate = $startDate; |
| 313 |
while( $rexDate <= $endDate ){ |
| 314 |
if( $disabledWeekdays ){ |
| 315 |
$wkd = $this->t->getWeekday(); |
| 316 |
if( in_array($wkd, $disabledWeekdays) ){ |
| 317 |
$rexDate = $this->t->modify('+1 day')->formatDateDb(); |
| 318 |
continue; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
$dates[] = $rexDate; |
| 323 |
$rexDate = $this->t->modify('+1 day')->formatDateDb(); |
| 324 |
} |
| 325 |
|
| 326 |
$viewCalendars = array(); |
| 327 |
foreach( $calendars as $calendar ){ |
| 328 |
$label = $this->calendarsPresenter->presentTitle( $calendar ); |
| 329 |
$title = htmlspecialchars( $calendar->getTitle() ); |
| 330 |
$description = $calendar->getDescription(); |
| 331 |
if( strlen($description) ){ |
| 332 |
$description = htmlspecialchars( $description ); |
| 333 |
$title .= "\n" . $description; |
| 334 |
} |
| 335 |
|
| 336 |
$label = $this->ui->makeSpan( $label ) |
| 337 |
->addAttr( 'title', $title ) |
| 338 |
; |
| 339 |
|
| 340 |
$viewCalendars[ $calendar->getId() ] = $label; |
| 341 |
} |
| 342 |
|
| 343 |
$viewShifts = array(); |
| 344 |
|
| 345 |
$iknow = array('calendar', 'date'); |
| 346 |
foreach( $shifts as $shift ){ |
| 347 |
$calendar = $shift->getCalendar(); |
| 348 |
$id = $calendar ? $calendar->getId() : 0; |
| 349 |
|
| 350 |
if( ! array_key_exists($id, $viewShifts) ){ |
| 351 |
$viewShifts[$id] = array(); |
| 352 |
} |
| 353 |
|
| 354 |
$start = $shift->getStart(); |
| 355 |
$end = $shift->getEnd(); |
| 356 |
|
| 357 |
$startDayTime = substr($start, 8, 4); |
| 358 |
$endDayTime = substr($end, 8, 4); |
| 359 |
$shiftAllDay = ( (0 == $startDayTime) && (0 == $endDayTime) ) ? TRUE : FALSE; |
| 360 |
|
| 361 |
$this->t->setDateTimeDb( $end )->modify('-1 second'); |
| 362 |
$shiftEndDate = $this->t->formatDateDb(); |
| 363 |
|
| 364 |
$this->t->setDateTimeDb( $start ); |
| 365 |
$shiftStartDate = $this->t->formatDateDb(); |
| 366 |
|
| 367 |
$rexDate = $shiftStartDate; |
| 368 |
while( $rexDate <= $shiftEndDate ){ |
| 369 |
if( in_array($rexDate, $dates) ){ |
| 370 |
if( ! array_key_exists($rexDate, $viewShifts[$id]) ){ |
| 371 |
$viewShifts[$id][$rexDate] = array(); |
| 372 |
} |
| 373 |
$viewShifts[$id][$rexDate][] = $shift; |
| 374 |
} |
| 375 |
|
| 376 |
if( ! $shiftAllDay ){ |
| 377 |
break; |
| 378 |
} |
| 379 |
$this->t->modify( '+1 day' ); |
| 380 |
$rexDate = $this->t->formatDateDb(); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
$header = array(); |
| 385 |
$header[] = NULL; |
| 386 |
|
| 387 |
$weekStartsOn = $this->t->getWeekStartsOn(); |
| 388 |
$weekSegments = array(); |
| 389 |
|
| 390 |
$this->t->setDateDb( $startDate ); |
| 391 |
// $monthMatrix = $this->t->getMonthMatrix( $disabledWeekdays ); |
| 392 |
$monthMatrix = $this->t->getWeeksMatrix( 2, $disabledWeekdays ); |
| 393 |
|
| 394 |
foreach( $monthMatrix as $week => $days ){ |
| 395 |
$wdi = 0; |
| 396 |
foreach( $days as $date ){ |
| 397 |
if( ! $date ){ |
| 398 |
continue; |
| 399 |
} |
| 400 |
$this->t->setDateDb( $date ); |
| 401 |
|
| 402 |
$dateView = array(); |
| 403 |
$weekdayView = $this->t->getWeekdayName(); |
| 404 |
$weekdayView = $this->ui->makeBlock( $weekdayView )->tag('muted')->tag('font-size', 1); |
| 405 |
$dateView[] = $weekdayView; |
| 406 |
|
| 407 |
$dayView = $this->t->getDay(); |
| 408 |
$dateView[] = $dayView; |
| 409 |
$dateView[] = $this->t->getMonthName(); |
| 410 |
|
| 411 |
$dateView = $this->ui->makeList($dateView) |
| 412 |
->gutter(0) |
| 413 |
->tag('align', 'center') |
| 414 |
; |
| 415 |
|
| 416 |
$toDateParams = array(); |
| 417 |
$toDateParams['type'] = 'day'; |
| 418 |
$toDateParams['start'] = $date; |
| 419 |
$toDate = array( $slug, $toDateParams ); |
| 420 |
|
| 421 |
$weekLabel = $this->t->getWeekNo(); |
| 422 |
$weekLabel = ' [' . '__Week__' . ' #' . $weekLabel . ']'; |
| 423 |
|
| 424 |
$dateView = $this->ui->makeAhref( $toDate, $dateView ) |
| 425 |
->tag('print') |
| 426 |
->addAttr( 'title', $weekLabel ) |
| 427 |
; |
| 428 |
|
| 429 |
if( $date == $today ){ |
| 430 |
$dateView = $this->ui->makeBlock( $dateView ) |
| 431 |
->tag('border') |
| 432 |
->tag('border-color', 'gray') |
| 433 |
; |
| 434 |
} |
| 435 |
|
| 436 |
$header[] = $dateView; |
| 437 |
|
| 438 |
if( ! $wdi ){ |
| 439 |
$weekSegments[] = count($header); |
| 440 |
} |
| 441 |
|
| 442 |
$wdi++; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
// while( count($header) < 32 ){ |
| 447 |
// $header[] = NULL; |
| 448 |
// } |
| 449 |
|
| 450 |
$rows = array(); |
| 451 |
foreach( $viewCalendars as $id => $calendarView ){ |
| 452 |
$row = array(); |
| 453 |
|
| 454 |
$this->shiftsDuration->reset(); |
| 455 |
if( isset($viewShifts[$id]) ){ |
| 456 |
foreach( $viewShifts[$id] as $date => $dateShifts ){ |
| 457 |
foreach( $dateShifts as $shift ){ |
| 458 |
$this->shiftsDuration->add( $shift ); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
$outReport = $this->common->renderReport( $this->shiftsDuration, FALSE ); |
| 463 |
$outReport = $this->ui->makeSpan( $outReport ) |
| 464 |
->tag('font-size', 2) |
| 465 |
; |
| 466 |
$calendarView = $this->ui->makeList( array($calendarView, $outReport) ) |
| 467 |
->gutter(1) |
| 468 |
; |
| 469 |
} |
| 470 |
|
| 471 |
$calendarView = $this->ui->makeBlock( $calendarView ) |
| 472 |
->tag('padding', array('x1', 'y2')) |
| 473 |
->tag('nowrap') |
| 474 |
; |
| 475 |
$row[] = $calendarView; |
| 476 |
|
| 477 |
$countDates = count( $dates ); |
| 478 |
foreach( $dates as $date ){ |
| 479 |
$cell = array(); |
| 480 |
|
| 481 |
if( isset($viewShifts[$id][$date]) ){ |
| 482 |
// if( $countDates <= 15 ){ |
| 483 |
$shiftsView = $this->viewWeek->renderDay( $viewShifts[$id][$date], $iknow ); |
| 484 |
// } |
| 485 |
// else { |
| 486 |
// $shiftsView = $this->viewMonth->renderDay( $viewShifts[$id][$date], $iknow ); |
| 487 |
// } |
| 488 |
$cell[] = $shiftsView; |
| 489 |
} |
| 490 |
|
| 491 |
$label = '+'; |
| 492 |
|
| 493 |
$to = 'new'; |
| 494 |
$toParams = array( |
| 495 |
'date' => $date, |
| 496 |
'calendar' => $id, |
| 497 |
); |
| 498 |
if( array_key_exists('employee', $params) && (count($params['employee']) == 1) ){ |
| 499 |
if( ! in_array(-1, $params['employee']) ){ |
| 500 |
$toParams['employee'] = $params['employee'][0]; |
| 501 |
} |
| 502 |
} |
| 503 |
$to = array( $to, $toParams ); |
| 504 |
|
| 505 |
$addOk = FALSE; |
| 506 |
if( isset($toParams['employee']) ){ |
| 507 |
$testComboId = $id . '-' . $toParams['employee']; |
| 508 |
if( isset($this->allCombos[$testComboId]) ){ |
| 509 |
$addOk = TRUE; |
| 510 |
} |
| 511 |
} |
| 512 |
else { |
| 513 |
$testComboId = $id . '-'; |
| 514 |
reset( $this->allCombos ); |
| 515 |
foreach( $this->allCombos as $comboId ){ |
| 516 |
if( $testComboId == substr($comboId, 0, strlen($testComboId)) ){ |
| 517 |
$addOk = TRUE; |
| 518 |
break; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
if( $addOk ){ |
| 524 |
$link = $this->ui->makeAhref( $to, $label ) |
| 525 |
->tag('tab-link') |
| 526 |
->tag('align', 'center') |
| 527 |
->addAttr('title', '__Add New__') |
| 528 |
; |
| 529 |
|
| 530 |
if( $today > $date ){ |
| 531 |
$link->tag('muted', 3); |
| 532 |
} |
| 533 |
|
| 534 |
$cell[] = $link; |
| 535 |
} |
| 536 |
|
| 537 |
$cell = $this->ui->makeList( $cell ) |
| 538 |
->gutter(1) |
| 539 |
->tag('margin', '05') |
| 540 |
; |
| 541 |
|
| 542 |
$row[] = $cell; |
| 543 |
} |
| 544 |
|
| 545 |
// while( count($row) < 32 ){ |
| 546 |
// $row[] = NULL; |
| 547 |
// } |
| 548 |
$rows[] = $row; |
| 549 |
} |
| 550 |
|
| 551 |
$out = $this->ui->makeTable( $header, $rows, FALSE ) |
| 552 |
->gutter(0) |
| 553 |
->setBordered( $this->borderColor ) |
| 554 |
->setSegments( $weekSegments ) |
| 555 |
->setLabelled( TRUE ) |
| 556 |
; |
| 557 |
|
| 558 |
return $out; |
| 559 |
} |
| 560 |
|
| 561 |
public function renderByEmployee() |
| 562 |
{ |
| 563 |
$slug = $this->request->getSlug(); |
| 564 |
$today = $this->t->setNow()->formatDateDb(); |
| 565 |
|
| 566 |
$disabledWeekdays = $this->settings->get( 'skip_weekdays', TRUE ); |
| 567 |
|
| 568 |
$calendars = $this->common->getCalendars(); |
| 569 |
$employees = $this->common->getEmployees(); |
| 570 |
$shifts = $this->common->getShifts(); |
| 571 |
|
| 572 |
// finalize employees to include those that employee was removed from but still have shifts in |
| 573 |
$listEmployeeId = array_keys( $employees ); |
| 574 |
$dictEmployeeId = array_combine( $listEmployeeId, $listEmployeeId ); |
| 575 |
foreach( $shifts as $shift ){ |
| 576 |
$employee = $shift->getEmployee(); |
| 577 |
if( ! $employee ) continue; |
| 578 |
$employeeId = $employee->getId(); |
| 579 |
$dictEmployeeId[ $employeeId ] = $employeeId; |
| 580 |
} |
| 581 |
if( count($dictEmployeeId) > count($employees) ){ |
| 582 |
$allEmployees = $this->common->findAllEmployees(); |
| 583 |
$employees = array_intersect_key( $allEmployees, $dictEmployeeId ); |
| 584 |
} |
| 585 |
|
| 586 |
// we can probably have archived employees so if such employees have no shifts then skip them |
| 587 |
$removeArchivedEmployees = array(); |
| 588 |
foreach( $employees as $employee ){ |
| 589 |
if( $employee->isArchived() ){ |
| 590 |
$removeArchivedEmployees[ $employee->getId() ] = $employee->getId(); |
| 591 |
} |
| 592 |
} |
| 593 |
if( $removeArchivedEmployees ){ |
| 594 |
foreach( $shifts as $shift ){ |
| 595 |
$employee = $shift->getEmployee(); |
| 596 |
$id = $employee ? $employee->getId() : 0; |
| 597 |
if( isset($removeArchivedEmployees[$id]) ){ |
| 598 |
unset( $removeArchivedEmployees[$id] ); |
| 599 |
} |
| 600 |
if( ! $removeArchivedEmployees ){ |
| 601 |
break; |
| 602 |
} |
| 603 |
} |
| 604 |
foreach( $removeArchivedEmployees as $id ){ |
| 605 |
unset( $employees[$id] ); |
| 606 |
} |
| 607 |
reset( $shifts ); |
| 608 |
} |
| 609 |
|
| 610 |
$params = $this->request->getParams(); |
| 611 |
$startDate = $params['start']; |
| 612 |
|
| 613 |
$startDate = $this->t->setDateDb( $startDate )->setStartWeek()->formatDateDb(); |
| 614 |
$endDate = $this->t->modify('+2 weeks')->modify('-1 day')->formatDateDb(); |
| 615 |
|
| 616 |
$this->t->setDateDb( $startDate ); |
| 617 |
$dates = array(); |
| 618 |
$rexDate = $startDate; |
| 619 |
while( $rexDate <= $endDate ){ |
| 620 |
if( $disabledWeekdays ){ |
| 621 |
$wkd = $this->t->getWeekday(); |
| 622 |
if( in_array($wkd, $disabledWeekdays) ){ |
| 623 |
$rexDate = $this->t->modify('+1 day')->formatDateDb(); |
| 624 |
continue; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
$dates[] = $rexDate; |
| 629 |
$rexDate = $this->t->modify('+1 day')->formatDateDb(); |
| 630 |
} |
| 631 |
|
| 632 |
$viewEmployees = array(); |
| 633 |
foreach( $employees as $employee ){ |
| 634 |
$label = $this->employeesPresenter->presentTitle( $employee ); |
| 635 |
|
| 636 |
$title = htmlspecialchars( $label ); |
| 637 |
$description = $employee->getDescription(); |
| 638 |
if( strlen($description) ){ |
| 639 |
$description = htmlspecialchars( $description ); |
| 640 |
$title .= "\n" . $description; |
| 641 |
} |
| 642 |
|
| 643 |
$label = $this->ui->makeSpan( $label ) |
| 644 |
->tag( 'font-size', 4 ) |
| 645 |
->addAttr( 'title', $title ) |
| 646 |
; |
| 647 |
|
| 648 |
$viewEmployees[ $employee->getId() ] = $label; |
| 649 |
} |
| 650 |
|
| 651 |
$viewShifts = array(); |
| 652 |
|
| 653 |
$iknow = array('employee', 'date'); |
| 654 |
|
| 655 |
$allCalendars = $this->common->findAllCalendars(); |
| 656 |
if( count($allCalendars) <= 1 ) $iknow[] = 'calendar'; |
| 657 |
|
| 658 |
foreach( $shifts as $shift ){ |
| 659 |
$employee = $shift->getEmployee(); |
| 660 |
$id = $employee ? $employee->getId() : 0; |
| 661 |
|
| 662 |
if( ! array_key_exists($id, $viewShifts) ){ |
| 663 |
$viewShifts[$id] = array(); |
| 664 |
} |
| 665 |
|
| 666 |
$start = $shift->getStart(); |
| 667 |
$end = $shift->getEnd(); |
| 668 |
|
| 669 |
$startDayTime = substr($start, 8, 4); |
| 670 |
$endDayTime = substr($end, 8, 4); |
| 671 |
$shiftAllDay = ( (0 == $startDayTime) && (0 == $endDayTime) ) ? TRUE : FALSE; |
| 672 |
|
| 673 |
$this->t->setDateTimeDb( $end )->modify('-1 second'); |
| 674 |
$shiftEndDate = $this->t->formatDateDb(); |
| 675 |
|
| 676 |
$this->t->setDateTimeDb( $start ); |
| 677 |
$shiftStartDate = $this->t->formatDateDb(); |
| 678 |
|
| 679 |
$rexDate = $shiftStartDate; |
| 680 |
while( $rexDate <= $shiftEndDate ){ |
| 681 |
if( in_array($rexDate, $dates) ){ |
| 682 |
if( ! array_key_exists($rexDate, $viewShifts[$id]) ){ |
| 683 |
$viewShifts[$id][$rexDate] = array(); |
| 684 |
} |
| 685 |
$viewShifts[$id][$rexDate][] = $shift; |
| 686 |
} |
| 687 |
|
| 688 |
if( ! $shiftAllDay ){ |
| 689 |
break; |
| 690 |
} |
| 691 |
$this->t->modify( '+1 day' ); |
| 692 |
$rexDate = $this->t->formatDateDb(); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
$header = array(); |
| 697 |
$header[] = NULL; |
| 698 |
|
| 699 |
$weekStartsOn = $this->t->getWeekStartsOn(); |
| 700 |
$weekSegments = array(); |
| 701 |
|
| 702 |
$this->t->setDateDb( $startDate ); |
| 703 |
// $monthMatrix = $this->t->getMonthMatrix( $disabledWeekdays ); |
| 704 |
$monthMatrix = $this->t->getWeeksMatrix( 2, $disabledWeekdays ); |
| 705 |
|
| 706 |
foreach( $monthMatrix as $week => $days ){ |
| 707 |
$wdi = 0; |
| 708 |
foreach( $days as $date ){ |
| 709 |
if( ! $date ){ |
| 710 |
continue; |
| 711 |
} |
| 712 |
$this->t->setDateDb( $date ); |
| 713 |
|
| 714 |
$dateView = array(); |
| 715 |
$weekdayView = $this->t->getWeekdayName(); |
| 716 |
$weekdayView = $this->ui->makeBlock( $weekdayView )->tag('muted')->tag('font-size', 1); |
| 717 |
$dateView[] = $weekdayView; |
| 718 |
|
| 719 |
$dayView = $this->t->getDay(); |
| 720 |
$dateView[] = $dayView; |
| 721 |
$dateView[] = $this->t->getMonthName(); |
| 722 |
|
| 723 |
$dateView = $this->ui->makeList($dateView) |
| 724 |
->gutter(0) |
| 725 |
->tag('align', 'center') |
| 726 |
; |
| 727 |
|
| 728 |
$toDateParams = array(); |
| 729 |
$toDateParams['type'] = 'day'; |
| 730 |
$toDateParams['start'] = $date; |
| 731 |
$toDate = array( $slug, $toDateParams ); |
| 732 |
|
| 733 |
$weekLabel = $this->t->getWeekNo(); |
| 734 |
$weekLabel = ' [' . '__Week__' . ' #' . $weekLabel . ']'; |
| 735 |
|
| 736 |
$dateView = $this->ui->makeAhref( $toDate, $dateView ) |
| 737 |
->tag('print') |
| 738 |
->addAttr( 'title', $weekLabel ) |
| 739 |
; |
| 740 |
|
| 741 |
if( $date == $today ){ |
| 742 |
$dateView = $this->ui->makeBlock( $dateView ) |
| 743 |
->tag('border') |
| 744 |
->tag('border-color', 'gray') |
| 745 |
; |
| 746 |
} |
| 747 |
|
| 748 |
$header[] = $dateView; |
| 749 |
|
| 750 |
if( ! $wdi ){ |
| 751 |
$weekSegments[] = count($header); |
| 752 |
} |
| 753 |
|
| 754 |
$wdi++; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
// while( count($header) < 32 ){ |
| 759 |
// $header[] = NULL; |
| 760 |
// } |
| 761 |
|
| 762 |
$rows = array(); |
| 763 |
|
| 764 |
foreach( $viewEmployees as $id => $employeeView ){ |
| 765 |
$row = array(); |
| 766 |
|
| 767 |
$this->shiftsDuration->reset(); |
| 768 |
if( isset($viewShifts[$id]) ){ |
| 769 |
$counted = 0; |
| 770 |
foreach( $viewShifts[$id] as $date => $dateShifts ){ |
| 771 |
foreach( $dateShifts as $shift ){ |
| 772 |
$shiftCalendar = $shift->getCalendar(); |
| 773 |
// if( $shiftCalendar->isTimeoff() ){ |
| 774 |
// continue; |
| 775 |
// } |
| 776 |
$this->shiftsDuration->add( $shift ); |
| 777 |
$counted++; |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
if( $counted ){ |
| 782 |
$outReport = $this->common->renderReport( $this->shiftsDuration, FALSE ); |
| 783 |
$outReport = $this->ui->makeSpan( $outReport ) |
| 784 |
->tag('font-size', 2) |
| 785 |
; |
| 786 |
$employeeView = $this->ui->makeList( array($employeeView, $outReport) ) |
| 787 |
->gutter(1) |
| 788 |
; |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
$thisShifts = isset($viewShifts[$id]) ? $viewShifts[$id] : array(); |
| 793 |
$employeeView = $this->common->employeeRowLabel( $employeeView, $id, '4weeks', $dates, $thisShifts ); |
| 794 |
|
| 795 |
$employeeView = $this->ui->makeBlock( $employeeView ) |
| 796 |
->tag('padding', array('x1', 'y2')) |
| 797 |
->tag('nowrap') |
| 798 |
; |
| 799 |
$row[] = $employeeView; |
| 800 |
|
| 801 |
$countDates = count( $dates ); |
| 802 |
foreach( $dates as $date ){ |
| 803 |
$cell = array(); |
| 804 |
|
| 805 |
if( isset($viewShifts[$id][$date]) ){ |
| 806 |
// if( $countDates <= 10 ){ |
| 807 |
$shiftsView = $this->viewWeek->renderDay( $viewShifts[$id][$date], $iknow ); |
| 808 |
// } |
| 809 |
// else { |
| 810 |
// $shiftsView = $this->viewMonth->renderDay( $viewShifts[$id][$date], $iknow ); |
| 811 |
// } |
| 812 |
$cell[] = $shiftsView; |
| 813 |
} |
| 814 |
|
| 815 |
$comboId = 0 . '-' . $id; |
| 816 |
if( isset($this->allCombos[$comboId]) ){ |
| 817 |
$label = '+'; |
| 818 |
|
| 819 |
$to = 'new'; |
| 820 |
$toParams = array( |
| 821 |
'date' => $date, |
| 822 |
'employee' => $id, |
| 823 |
); |
| 824 |
$to = array( $to, $toParams ); |
| 825 |
|
| 826 |
$link = $this->ui->makeAhref( $to, $label ) |
| 827 |
->tag('tab-link') |
| 828 |
->tag('align', 'center') |
| 829 |
->addAttr('title', '__Add New__') |
| 830 |
; |
| 831 |
|
| 832 |
if( $today > $date ){ |
| 833 |
$link->tag('muted', 3); |
| 834 |
} |
| 835 |
|
| 836 |
$cell[] = $link; |
| 837 |
} |
| 838 |
|
| 839 |
$cell = $this->ui->makeList( $cell ) |
| 840 |
->gutter(1) |
| 841 |
->tag('margin', '05') |
| 842 |
; |
| 843 |
|
| 844 |
$row[] = $cell; |
| 845 |
} |
| 846 |
|
| 847 |
// while( count($row) < 32 ){ |
| 848 |
// $row[] = NULL; |
| 849 |
// } |
| 850 |
$rows[] = $row; |
| 851 |
} |
| 852 |
|
| 853 |
$out = $this->ui->makeTable( $header, $rows, FALSE ) |
| 854 |
->gutter(0) |
| 855 |
->setBordered( $this->borderColor ) |
| 856 |
->setSegments( $weekSegments ) |
| 857 |
->setLabelled( TRUE ) |
| 858 |
; |
| 859 |
|
| 860 |
return $out; |
| 861 |
} |
| 862 |
} |