| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_New_View_Confirm |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
|
| 8 |
HC3_Auth $auth, |
| 9 |
HC3_IPermission $permission, |
| 10 |
SH4_Shifts_Conflicts $conflicts, |
| 11 |
|
| 12 |
HC3_Request $request, |
| 13 |
HC3_Ui $ui, |
| 14 |
HC3_Ui_Layout1 $layout, |
| 15 |
HC3_Settings $settings, |
| 16 |
|
| 17 |
SH4_New_View_Common $common, |
| 18 |
|
| 19 |
SH4_Shifts_Availability $availability, |
| 20 |
|
| 21 |
SH4_Calendars_Query $calendars, |
| 22 |
SH4_Employees_Query $employees, |
| 23 |
SH4_ShiftTypes_Query $shiftTypesQuery, |
| 24 |
|
| 25 |
SH4_Employees_Presenter $employeesPresenter, |
| 26 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 27 |
SH4_ShiftTypes_Presenter $shiftTypesPresenter, |
| 28 |
|
| 29 |
SH4_Calendars_Permissions $calendarsPermissions, |
| 30 |
|
| 31 |
SH4_App_Query $appQuery, |
| 32 |
SH4_New_Query $newQuery, |
| 33 |
|
| 34 |
HC3_Time $t |
| 35 |
) |
| 36 |
{ |
| 37 |
$this->self = $hooks->wrap( $this ); |
| 38 |
|
| 39 |
$this->auth = $hooks->wrap( $auth ); |
| 40 |
$this->permission = $hooks->wrap( $permission ); |
| 41 |
$this->request = $request; |
| 42 |
$this->ui = $ui; |
| 43 |
$this->layout = $layout; |
| 44 |
$this->common = $hooks->wrap($common); |
| 45 |
|
| 46 |
$this->calendarsPermissions = $hooks->wrap( $calendarsPermissions ); |
| 47 |
$this->t = $t; |
| 48 |
|
| 49 |
$this->settings = $hooks->wrap( $settings ); |
| 50 |
|
| 51 |
$this->conflicts = $hooks->wrap($conflicts); |
| 52 |
$this->availability = $hooks->wrap( $availability ); |
| 53 |
|
| 54 |
$this->calendars = $hooks->wrap($calendars); |
| 55 |
$this->employees = $hooks->wrap($employees); |
| 56 |
$this->shiftTypesQuery = $hooks->wrap( $shiftTypesQuery ); |
| 57 |
|
| 58 |
$this->employeesPresenter = $hooks->wrap( $employeesPresenter ); |
| 59 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 60 |
$this->shiftTypesPresenter = $hooks->wrap( $shiftTypesPresenter ); |
| 61 |
|
| 62 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 63 |
$this->newQuery = $hooks->wrap( $newQuery ); |
| 64 |
} |
| 65 |
|
| 66 |
public function render() |
| 67 |
{ |
| 68 |
$params = $this->request->getParams(); |
| 69 |
|
| 70 |
$calendarId = $params['calendar']; |
| 71 |
$shiftTypeId = $params['shifttype']; |
| 72 |
$dateString = $params['date']; |
| 73 |
$employeeString = $params['employee']; |
| 74 |
|
| 75 |
$dates = HC3_Functions::unglueArray( $dateString ); |
| 76 |
|
| 77 |
$datesView = array(); |
| 78 |
foreach( $dates as $date ){ |
| 79 |
$this->t->setDateDb( $date ); |
| 80 |
$dateView = $this->t->getWeekdayName() . ', ' . $this->t->formatDate(); |
| 81 |
$dateView = $this->ui->makeBlock( $dateView ) |
| 82 |
->tag('border') |
| 83 |
->tag('border-color', 'gray') |
| 84 |
->tag('padding', 2) |
| 85 |
; |
| 86 |
$datesView[] = $dateView; |
| 87 |
} |
| 88 |
|
| 89 |
$shiftType = $this->shiftTypesQuery->findById( $shiftTypeId ); |
| 90 |
$range = $shiftType->getRange(); |
| 91 |
switch( $range ){ |
| 92 |
case SH4_ShiftTypes_Model::RANGE_DAYS: |
| 93 |
$datesView = $this->ui->makeListInline( $datesView ) |
| 94 |
->separated( '→' ) |
| 95 |
; |
| 96 |
break; |
| 97 |
|
| 98 |
case SH4_ShiftTypes_Model::RANGE_HOURS: |
| 99 |
$datesView = $this->ui->makeListInline( $datesView ) |
| 100 |
->gutter(1) |
| 101 |
; |
| 102 |
break; |
| 103 |
} |
| 104 |
|
| 105 |
$countDate = count( $dates ); |
| 106 |
if( $countDate > 1 ){ |
| 107 |
if( SH4_ShiftTypes_Model::RANGE_HOURS == $range ){ |
| 108 |
$label = '__Dates__' . ' (' . $countDate . ')'; |
| 109 |
} |
| 110 |
else { |
| 111 |
$label = '__Dates__'; |
| 112 |
} |
| 113 |
} |
| 114 |
else { |
| 115 |
$label = '__Date__'; |
| 116 |
} |
| 117 |
|
| 118 |
$datesView = $this->ui->makeLabelled( $label, $datesView ); |
| 119 |
|
| 120 |
$shiftType = $this->shiftTypesQuery->findById( $shiftTypeId ); |
| 121 |
|
| 122 |
// employees |
| 123 |
$calendars = $this->newQuery->findAllCalendars(); |
| 124 |
if( array_key_exists($calendarId, $calendars) ){ |
| 125 |
$calendar = $calendars[$calendarId]; |
| 126 |
} |
| 127 |
else { |
| 128 |
$calendar = NULL; |
| 129 |
} |
| 130 |
|
| 131 |
if( ! $calendar ){ |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$isAvailability = $calendar->isAvailability(); |
| 136 |
|
| 137 |
$employees = $this->newQuery->findAllEmployees(); |
| 138 |
|
| 139 |
if( $calendar ){ |
| 140 |
$filter = $this->appQuery->findEmployeesForCalendar( $calendar ); |
| 141 |
|
| 142 |
$filterIds = array_keys( $filter ); |
| 143 |
$ids = array_keys( $employees ); |
| 144 |
foreach( $ids as $id ){ |
| 145 |
if( ! in_array($id, $filterIds) ){ |
| 146 |
unset( $employees[$id] ); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
$openQty = 0; |
| 152 |
$suppliedEmployeesIds = HC3_Functions::unglueArray( $employeeString ); |
| 153 |
if( in_array(0, $suppliedEmployeesIds) ){ |
| 154 |
$openQty = 1; |
| 155 |
} |
| 156 |
|
| 157 |
$ids = array_keys( $employees ); |
| 158 |
|
| 159 |
foreach( $suppliedEmployeesIds as $id ){ |
| 160 |
if( strpos($id, SH4_NEW_OPEN_SEPARATOR) !== FALSE ){ |
| 161 |
list( $id, $openQty ) = explode( SH4_NEW_OPEN_SEPARATOR, $id ); |
| 162 |
$suppliedEmployeesIds[] = $id; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
foreach( $ids as $id ){ |
| 168 |
if( ! in_array($id, $suppliedEmployeesIds) ){ |
| 169 |
unset( $employees[$id] ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if( ! $openQty ){ |
| 174 |
unset( $employees[0] ); |
| 175 |
} |
| 176 |
|
| 177 |
if( ! $employees ){ |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
$out = array(); |
| 182 |
$calendarView = $this->calendarsPresenter->presentTitle( $calendar ); |
| 183 |
|
| 184 |
// $description = $this->calendarsPresenter->presentDescription( $calendar ); |
| 185 |
// if( strlen($description) ){ |
| 186 |
// $len = 100; |
| 187 |
// if( strlen($description) > $len ){ |
| 188 |
// $descriptionSummary = substr( $description, 0, $len ) . '...'; |
| 189 |
// $descriptionSummary = $this->ui->makeElement( 'summary', $descriptionSummary ); |
| 190 |
// $descriptionView = $this->ui->makeCollection( array($descriptionSummary, $description) ); |
| 191 |
// $descriptionView = $this->ui->makeElement( 'details', $descriptionView ); |
| 192 |
// } |
| 193 |
// else { |
| 194 |
// $descriptionView = $description; |
| 195 |
// } |
| 196 |
|
| 197 |
// $calendarView = $this->ui->makeList( array($calendarView, $descriptionView) ) |
| 198 |
// ->gutter(0) |
| 199 |
// ; |
| 200 |
// } |
| 201 |
|
| 202 |
$shiftTypeView = $this->shiftTypesPresenter->presentTitleTime( $shiftType ); |
| 203 |
|
| 204 |
$calendarView = $this->ui->makeList( array($calendarView, $shiftTypeView) ) |
| 205 |
->gutter(0) |
| 206 |
; |
| 207 |
$calendarView = $this->ui->makeBlock( $calendarView ) |
| 208 |
->tag('border') |
| 209 |
->tag('border-color', 'gray') |
| 210 |
->tag('padding', 2) |
| 211 |
; |
| 212 |
|
| 213 |
$out[] = $calendarView; |
| 214 |
|
| 215 |
$employeesView = array(); |
| 216 |
$timeStart = $shiftType->getStart(); |
| 217 |
$timeEnd = $shiftType->getEnd(); |
| 218 |
|
| 219 |
foreach( $employees as $employee ){ |
| 220 |
$withConflicts = 0; |
| 221 |
$withAvailability = 0; |
| 222 |
$withNothing = 0; |
| 223 |
|
| 224 |
$employeeId = $employee->getId(); |
| 225 |
$thisView = array(); |
| 226 |
|
| 227 |
$label = $this->employeesPresenter->presentTitle( $employee ); |
| 228 |
|
| 229 |
if( ! $employeeId ){ |
| 230 |
if( $openQty > 1 ){ |
| 231 |
$label = $openQty . ' x ' . $label; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
$thisView[] = $label; |
| 236 |
|
| 237 |
// check conflicts |
| 238 |
if( $employeeId && (! $isAvailability) ){ |
| 239 |
$testModels = array(); |
| 240 |
|
| 241 |
$range = $shiftType->getRange(); |
| 242 |
switch( $range ){ |
| 243 |
case SH4_ShiftTypes_Model::RANGE_DAYS: |
| 244 |
$start = $this->t->setDateDb( $dates[0] ) |
| 245 |
->formatDateTimeDb() |
| 246 |
; |
| 247 |
$end = $this->t->setDateDb( $dates[1] ) |
| 248 |
->modify('+1 day') |
| 249 |
->formatDateTimeDb() |
| 250 |
; |
| 251 |
|
| 252 |
$testModel = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $employee ); |
| 253 |
$testModels[] = $testModel; |
| 254 |
break; |
| 255 |
|
| 256 |
case SH4_ShiftTypes_Model::RANGE_HOURS: |
| 257 |
$timeStart = $shiftType->getStart(); |
| 258 |
$timeEnd = $shiftType->getEnd(); |
| 259 |
reset( $dates ); |
| 260 |
foreach( $dates as $date ){ |
| 261 |
$start = $this->t->setDateDb( $date ) |
| 262 |
->modify( '+' . $timeStart . ' seconds' ) |
| 263 |
->formatDateTimeDb() |
| 264 |
; |
| 265 |
$end = $this->t->setDateDb( $date ) |
| 266 |
->modify( '+' . $timeEnd . ' seconds' ) |
| 267 |
->formatDateTimeDb() |
| 268 |
; |
| 269 |
$testModel = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $employee ); |
| 270 |
$testModels[] = $testModel; |
| 271 |
} |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
reset( $testModels ); |
| 276 |
foreach( $testModels as $testModel ){ |
| 277 |
$conflicts = $this->conflicts->get( $testModel ); |
| 278 |
|
| 279 |
if( $conflicts ){ |
| 280 |
$withConflicts++; |
| 281 |
} |
| 282 |
else { |
| 283 |
if( (! $this->availability->hasAvailability()) OR $this->availability->get($testModel) ){ |
| 284 |
$withAvailability++; |
| 285 |
} |
| 286 |
else { |
| 287 |
$withNothing++; |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
$conflictsView = array(); |
| 293 |
|
| 294 |
if( $withAvailability ){ |
| 295 |
$sign = ( count($testModels) < 2 ) ? '✓' : $withAvailability; |
| 296 |
$sign = $this->ui->makeBlock( $sign ) |
| 297 |
->tag('padding', 'x1') |
| 298 |
->tag('color', 'white') |
| 299 |
->tag('bgcolor', 'olive') |
| 300 |
->addAttr( 'title', '__Available__' ) |
| 301 |
; |
| 302 |
$conflictsView[] = $sign; |
| 303 |
} |
| 304 |
|
| 305 |
if( $withNothing ){ |
| 306 |
$sign = ( count($testModels) < 2 ) ? '✓' : $withNothing; |
| 307 |
$sign = $this->ui->makeBlock( $sign ) |
| 308 |
->tag('padding', 'x1') |
| 309 |
->tag('color', 'white') |
| 310 |
->tag('bgcolor', 'gray') |
| 311 |
->addAttr( 'title', '__No Availability__' ) |
| 312 |
; |
| 313 |
$conflictsView[] = $sign; |
| 314 |
} |
| 315 |
|
| 316 |
if( $withConflicts ){ |
| 317 |
$sign = (count($testModels) < 2) ? ' ! ' : $withConflicts; |
| 318 |
$sign = $this->ui->makeBlock( $sign ) |
| 319 |
->tag('padding', 'x1') |
| 320 |
->tag('color', 'white') |
| 321 |
->tag('bgcolor', 'maroon') |
| 322 |
->addAttr( 'title', '__Conflicts__' ) |
| 323 |
; |
| 324 |
$conflictsView[] = $sign; |
| 325 |
} |
| 326 |
|
| 327 |
$conflictsView = $this->ui->makeListInline( $conflictsView ) |
| 328 |
->gutter(1) |
| 329 |
; |
| 330 |
array_unshift( $thisView, $conflictsView ); |
| 331 |
} |
| 332 |
|
| 333 |
$thisView = $this->ui->makeListInline( $thisView ); |
| 334 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 335 |
->addAttr('style', 'position: relative;') |
| 336 |
; |
| 337 |
|
| 338 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 339 |
->tag('border') |
| 340 |
->tag('padding', 2) |
| 341 |
; |
| 342 |
|
| 343 |
if( $withAvailability == count($dates) ){ |
| 344 |
$thisView |
| 345 |
->tag('border-color', 'green') |
| 346 |
; |
| 347 |
} |
| 348 |
elseif( $withConflicts ){ |
| 349 |
$thisView |
| 350 |
->tag('border-color', 'red') |
| 351 |
; |
| 352 |
} |
| 353 |
else { |
| 354 |
$thisView |
| 355 |
->tag('border-color', 'gray') |
| 356 |
; |
| 357 |
} |
| 358 |
|
| 359 |
$employeesView[] = $thisView; |
| 360 |
} |
| 361 |
|
| 362 |
$employeesView = $this->ui->makeListInline( $employeesView ); |
| 363 |
|
| 364 |
$countEmployee = count( $employees ); |
| 365 |
if( $countEmployee > 1 ){ |
| 366 |
$label = '__Employees__' . ' (' . $countEmployee . ')'; |
| 367 |
} |
| 368 |
else { |
| 369 |
$label = '__Employee__'; |
| 370 |
} |
| 371 |
|
| 372 |
$employeesView = $this->ui->makeLabelled( $label, $employeesView ); |
| 373 |
|
| 374 |
$out[] = $datesView; |
| 375 |
$out[] = $employeesView; |
| 376 |
|
| 377 |
$out = $this->ui->makeList( $out ); |
| 378 |
|
| 379 |
$out = array( $out ); |
| 380 |
|
| 381 |
// employee_create_own_conflicts |
| 382 |
$showButtons = TRUE; |
| 383 |
|
| 384 |
if( $withConflicts ){ |
| 385 |
$isManager = FALSE; |
| 386 |
|
| 387 |
$currentUser = $this->auth->getCurrentUser(); |
| 388 |
$currentUserId = $currentUser->getId(); |
| 389 |
if( $currentUserId ){ |
| 390 |
if( $this->permission->isAdmin($currentUser) ){ |
| 391 |
$isManager = TRUE; |
| 392 |
} |
| 393 |
else { |
| 394 |
$calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser ); |
| 395 |
if( isset($calendarsAsManager[$calendarId]) ){ |
| 396 |
$isManager = TRUE; |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
if( ! $isManager ){ |
| 402 |
if( ! $this->calendarsPermissions->get($calendar, 'employee_create_own_conflicts') ){ |
| 403 |
$showButtons = FALSE; |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
if( $showButtons ){ |
| 409 |
$btn = $this->self->buttons( $calendarId, $shiftTypeId, $dateString, $employeeString ); |
| 410 |
$btn = $this->ui->makeListInline( $btn ); |
| 411 |
} |
| 412 |
else { |
| 413 |
$btn = '__You cannot create new shifts with conflicts.__'; |
| 414 |
} |
| 415 |
|
| 416 |
// open shift |
| 417 |
$inputs = $this->common->inputs( $calendarId, $shiftTypeId, $employeeId ); |
| 418 |
$out = array_merge( $out, $inputs ); |
| 419 |
|
| 420 |
$out[] = $btn; |
| 421 |
|
| 422 |
$out = $this->ui->makeList( $out )->gutter(3); |
| 423 |
|
| 424 |
// schedule link |
| 425 |
$scheduleLink = HC3_Session::instance()->getUserdata( 'scheduleLink' ); |
| 426 |
if( ! $scheduleLink ){ |
| 427 |
$scheduleLink = array( 'schedule', array() ); |
| 428 |
} |
| 429 |
$scheduleLinkValue = json_encode( $scheduleLink ); |
| 430 |
$inputBackHidden = $this->ui->makeInputHidden( 'back', $scheduleLinkValue ); |
| 431 |
$out = $this->ui->makeCollection( array($out, $inputBackHidden) ); |
| 432 |
|
| 433 |
$to = array( 'new', $calendarId, $shiftTypeId, $dateString, $employeeString ); |
| 434 |
$to = join( '/', $to ); |
| 435 |
$out = $this->ui->makeForm( $to, $out ); |
| 436 |
$out->setAttr( 'enctype', 'multipart/form-data' ); |
| 437 |
|
| 438 |
$this->layout |
| 439 |
->setContent( $out ) |
| 440 |
// ->setBreadcrumb( $this->common->breadcrumb($calendarId, $shiftTypeId) ) |
| 441 |
->setHeader( $this->self->header() ) |
| 442 |
; |
| 443 |
$out = $this->layout->render(); |
| 444 |
|
| 445 |
return $out; |
| 446 |
} |
| 447 |
|
| 448 |
public function buttons( $calendarId, $shiftTypeId, $dateString, $employeeString ) |
| 449 |
{ |
| 450 |
$ret = array(); |
| 451 |
|
| 452 |
$countDate = $countEmployee = 1; |
| 453 |
if( false !== strpos($employeeString, '_') ){ |
| 454 |
$countEmployee = count( HC3_Functions::unglueArray($employeeString) ); |
| 455 |
} |
| 456 |
|
| 457 |
if( false !== strpos($dateString, '_') ){ |
| 458 |
$shiftType = $this->shiftTypesQuery->findById( $shiftTypeId ); |
| 459 |
$range = $shiftType->getRange(); |
| 460 |
if( SH4_ShiftTypes_Model::RANGE_HOURS == $range ){ |
| 461 |
$countDate = count( HC3_Functions::unglueArray($dateString) ); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
$countShift = $countDate * $countEmployee; |
| 466 |
|
| 467 |
$noDraft = $this->settings->get('shifts_no_draft') ? true : false; |
| 468 |
|
| 469 |
// draft |
| 470 |
if( ! $noDraft ){ |
| 471 |
$to = array( 'new', $calendarId, $shiftTypeId, $dateString, $employeeString, 'draft' ); |
| 472 |
$to = join( '/', $to ); |
| 473 |
$label = '__Create Draft__'; |
| 474 |
if( $countShift > 1 ){ |
| 475 |
$label .= ' (' . $countShift . ')'; |
| 476 |
} |
| 477 |
|
| 478 |
$btnDraft = $this->ui->makeInputSubmit( $label ) |
| 479 |
->tag('secondary') |
| 480 |
->setFormAction( $to ) |
| 481 |
; |
| 482 |
$ret['draft'] = $btnDraft; |
| 483 |
} |
| 484 |
|
| 485 |
// publish |
| 486 |
$to = array( 'new', $calendarId, $shiftTypeId, $dateString, $employeeString, 'publish' ); |
| 487 |
$to = join( '/', $to ); |
| 488 |
$label = $noDraft ? '__Post Shift__' : '__Create Published__'; |
| 489 |
if( $countShift > 1 ){ |
| 490 |
$label .= ' (' . $countShift . ')'; |
| 491 |
} |
| 492 |
|
| 493 |
$btnPublish = $this->ui->makeInputSubmit( $label ) |
| 494 |
->tag('primary') |
| 495 |
->setFormAction( $to ) |
| 496 |
; |
| 497 |
|
| 498 |
$confirm = $this->settings->get('shifts_confirm_publish') ? true : false; |
| 499 |
if( $confirm ){ |
| 500 |
$btnPublish->addAttr( 'onclick', ' return confirm(\'' . $label . '?\')' ); |
| 501 |
} |
| 502 |
|
| 503 |
$ret['publish'] = $btnPublish; |
| 504 |
|
| 505 |
return $ret; |
| 506 |
} |
| 507 |
|
| 508 |
public function header() |
| 509 |
{ |
| 510 |
$out = '__Confirm__'; |
| 511 |
return $out; |
| 512 |
} |
| 513 |
} |