| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_New_View_Employee |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
|
| 8 |
HC3_Request $request, |
| 9 |
HC3_Ui $ui, |
| 10 |
HC3_Ui_Layout1 $layout, |
| 11 |
HC3_Settings $settings, |
| 12 |
|
| 13 |
SH4_New_View_Common $common, |
| 14 |
|
| 15 |
SH4_Shifts_Conflicts $conflicts, |
| 16 |
SH4_Shifts_Availability $availability, |
| 17 |
|
| 18 |
SH4_Calendars_Query $calendars, |
| 19 |
SH4_Employees_Query $employees, |
| 20 |
|
| 21 |
SH4_ShiftTypes_Query $shiftTypesQuery, |
| 22 |
SH4_Employees_Presenter $employeesPresenter, |
| 23 |
SH4_App_Query $appQuery, |
| 24 |
SH4_New_Query $newQuery, |
| 25 |
|
| 26 |
HC3_Time $t |
| 27 |
) |
| 28 |
{ |
| 29 |
$this->self = $hooks->wrap( $this ); |
| 30 |
|
| 31 |
$this->request = $request; |
| 32 |
$this->ui = $ui; |
| 33 |
$this->layout = $layout; |
| 34 |
$this->common = $hooks->wrap($common); |
| 35 |
|
| 36 |
$this->t = $t; |
| 37 |
|
| 38 |
$this->settings = $hooks->wrap( $settings ); |
| 39 |
|
| 40 |
$this->conflicts = $hooks->wrap($conflicts); |
| 41 |
$this->availability = $hooks->wrap( $availability ); |
| 42 |
|
| 43 |
$this->calendars = $hooks->wrap($calendars); |
| 44 |
$this->employees = $hooks->wrap($employees); |
| 45 |
|
| 46 |
$this->shiftTypesQuery = $hooks->wrap( $shiftTypesQuery ); |
| 47 |
$this->employeesPresenter = $hooks->wrap( $employeesPresenter ); |
| 48 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 49 |
$this->newQuery = $hooks->wrap( $newQuery ); |
| 50 |
} |
| 51 |
|
| 52 |
public function render() |
| 53 |
{ |
| 54 |
$params = $this->request->getParams(); |
| 55 |
|
| 56 |
$dateString = $params['date']; |
| 57 |
$shiftTypeId = $params['shifttype']; |
| 58 |
$calendarId = $params['calendar']; |
| 59 |
|
| 60 |
$dates = HC3_Functions::unglueArray( $dateString ); |
| 61 |
$datesView = array(); |
| 62 |
foreach( $dates as $date ){ |
| 63 |
$this->t->setDateDb( $date ); |
| 64 |
$dateView = $this->t->getWeekdayName() . ', ' . $this->t->formatDate(); |
| 65 |
$datesView[] = $dateView; |
| 66 |
} |
| 67 |
|
| 68 |
$shiftType = $this->shiftTypesQuery->findById( $shiftTypeId ); |
| 69 |
$range = $shiftType->getRange(); |
| 70 |
|
| 71 |
switch( $range ){ |
| 72 |
case SH4_ShiftTypes_Model::RANGE_DAYS: |
| 73 |
$datesView = $this->ui->makeListInline( $datesView ) |
| 74 |
->separated( '→' ) |
| 75 |
; |
| 76 |
$datesView = array( $datesView ); |
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
// employees |
| 81 |
$calendars = $this->newQuery->findAllCalendars(); |
| 82 |
if( array_key_exists($calendarId, $calendars) ){ |
| 83 |
$calendar = $calendars[$calendarId]; |
| 84 |
} |
| 85 |
else { |
| 86 |
$calendar = NULL; |
| 87 |
} |
| 88 |
|
| 89 |
if( ! $calendar ){ |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$isAvailability = $calendar->isAvailability(); |
| 94 |
|
| 95 |
$employees = $this->newQuery->findAllEmployees(); |
| 96 |
if( $calendar ){ |
| 97 |
$filter = $this->appQuery->findEmployeesForCalendar( $calendar ); |
| 98 |
|
| 99 |
$filterIds = array_keys( $filter ); |
| 100 |
$ids = array_keys( $employees ); |
| 101 |
foreach( $ids as $id ){ |
| 102 |
if( ! in_array($id, $filterIds) ){ |
| 103 |
unset( $employees[$id] ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if( ! $employees ){ |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if( count($employees) == 1 ){ |
| 113 |
$params = $this->request->getParams(); |
| 114 |
$employee = current( $employees ); |
| 115 |
$employeeId = $employee->getId(); |
| 116 |
|
| 117 |
$to = 'new'; |
| 118 |
$toParams = array( |
| 119 |
'calendar' => $calendarId, |
| 120 |
'shifttype' => $shiftTypeId, |
| 121 |
'date' => $dateString, |
| 122 |
'employee' => $employeeId |
| 123 |
); |
| 124 |
$to = array( $to, $toParams ); |
| 125 |
|
| 126 |
$return = array( $to, NULL ); |
| 127 |
return $return; |
| 128 |
} |
| 129 |
|
| 130 |
$employeesView = array(); |
| 131 |
$employeesWithConflictsView = array(); |
| 132 |
|
| 133 |
foreach( $employees as $employee ){ |
| 134 |
$withConflicts = 0; |
| 135 |
$withAvailability = 0; |
| 136 |
$withNothing = 0; |
| 137 |
|
| 138 |
$employeeId = $employee->getId(); |
| 139 |
$thisView = array(); |
| 140 |
|
| 141 |
if( $employeeId ){ |
| 142 |
$label = $this->employeesPresenter->presentTitle( $employee ); |
| 143 |
$input = $this->ui->makeInputCheckbox( 'employee[]', $label, $employeeId ); |
| 144 |
$thisView[] = $input; |
| 145 |
} |
| 146 |
// open shift |
| 147 |
else { |
| 148 |
$label = $this->employeesPresenter->presentTitle( $employee ); |
| 149 |
$options = array(); |
| 150 |
$options[0] = $label; |
| 151 |
for( $ii = 1; $ii <= 50; $ii++ ){ |
| 152 |
$options[ '0' . SH4_NEW_OPEN_SEPARATOR . $ii ] = $ii; |
| 153 |
} |
| 154 |
$input = $this->ui->makeInputSelect( 'employee[]', NULL, $options ); |
| 155 |
$thisView[] = $input; |
| 156 |
} |
| 157 |
|
| 158 |
// check conflicts |
| 159 |
if( $employeeId && (! $isAvailability) ){ |
| 160 |
$testModels = array(); |
| 161 |
|
| 162 |
$range = $shiftType->getRange(); |
| 163 |
switch( $range ){ |
| 164 |
case SH4_ShiftTypes_Model::RANGE_DAYS: |
| 165 |
$start = $this->t->setDateDb( $dates[0] ) |
| 166 |
->formatDateTimeDb() |
| 167 |
; |
| 168 |
$end = $this->t->setDateDb( $dates[1] ) |
| 169 |
->modify('+1 day') |
| 170 |
->formatDateTimeDb() |
| 171 |
; |
| 172 |
|
| 173 |
$testModel = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $employee ); |
| 174 |
$testModels[] = $testModel; |
| 175 |
break; |
| 176 |
|
| 177 |
case SH4_ShiftTypes_Model::RANGE_HOURS: |
| 178 |
$timeStart = $shiftType->getStart(); |
| 179 |
$timeEnd = $shiftType->getEnd(); |
| 180 |
reset( $dates ); |
| 181 |
foreach( $dates as $date ){ |
| 182 |
$start = $this->t->setDateDb( $date ) |
| 183 |
->modify( '+' . $timeStart . ' seconds' ) |
| 184 |
->formatDateTimeDb() |
| 185 |
; |
| 186 |
$end = $this->t->setDateDb( $date ) |
| 187 |
->modify( '+' . $timeEnd . ' seconds' ) |
| 188 |
->formatDateTimeDb() |
| 189 |
; |
| 190 |
$testModel = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $employee ); |
| 191 |
$testModels[] = $testModel; |
| 192 |
} |
| 193 |
break; |
| 194 |
} |
| 195 |
|
| 196 |
reset( $testModels ); |
| 197 |
foreach( $testModels as $testModel ){ |
| 198 |
$conflicts = $this->conflicts->get( $testModel ); |
| 199 |
if( $conflicts ){ |
| 200 |
$withConflicts++; |
| 201 |
} |
| 202 |
else { |
| 203 |
if( (! $this->availability->hasAvailability()) OR $this->availability->get($testModel) ){ |
| 204 |
$withAvailability++; |
| 205 |
} |
| 206 |
else { |
| 207 |
$withNothing++; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
$conflictsView = array(); |
| 213 |
if( $withAvailability ){ |
| 214 |
$sign = ( count($testModels) < 2 ) ? '✓' : $withAvailability; |
| 215 |
$sign = $this->ui->makeBlock( $sign ) |
| 216 |
->tag('padding', 'x1') |
| 217 |
->tag('color', 'white') |
| 218 |
->tag('bgcolor', 'olive') |
| 219 |
->addAttr( 'title', '__Available__' ) |
| 220 |
; |
| 221 |
$conflictsView[] = $sign; |
| 222 |
} |
| 223 |
|
| 224 |
if( $withNothing ){ |
| 225 |
$sign = ( count($testModels) < 2 ) ? '✓' : $withNothing; |
| 226 |
$sign = $this->ui->makeBlock( $sign ) |
| 227 |
->tag('padding', 'x1') |
| 228 |
->tag('color', 'white') |
| 229 |
->tag('bgcolor', 'gray') |
| 230 |
->addAttr( 'title', '__No Availability__' ) |
| 231 |
; |
| 232 |
$conflictsView[] = $sign; |
| 233 |
} |
| 234 |
|
| 235 |
if( $withConflicts ){ |
| 236 |
$sign = (count($testModels) < 2) ? ' ! ' : $withConflicts; |
| 237 |
$sign = $this->ui->makeBlock( $sign ) |
| 238 |
->tag('padding', 'x1') |
| 239 |
->tag('color', 'white') |
| 240 |
->tag('bgcolor', 'maroon') |
| 241 |
->addAttr( 'title', '__Conflicts__' ) |
| 242 |
; |
| 243 |
$conflictsView[] = $sign; |
| 244 |
} |
| 245 |
|
| 246 |
$conflictsView = $this->ui->makeListInline( $conflictsView ) |
| 247 |
->gutter(1) |
| 248 |
; |
| 249 |
$thisView[] = $conflictsView; |
| 250 |
} |
| 251 |
|
| 252 |
$thisView = $this->ui->makeListInline( $thisView ); |
| 253 |
|
| 254 |
if( $employeeId ){ |
| 255 |
$descriptionView = $employee->getDescription(); |
| 256 |
if( strlen($descriptionView) ){ |
| 257 |
$thisView = $this->ui->makeList( array($thisView, $descriptionView) ); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 262 |
->addAttr('style', 'position: relative;') |
| 263 |
; |
| 264 |
|
| 265 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 266 |
->tag('border') |
| 267 |
->tag('padding', 2) |
| 268 |
; |
| 269 |
|
| 270 |
if( $withAvailability == count($dates) ){ |
| 271 |
$thisView |
| 272 |
->tag('border-color', 'green') |
| 273 |
; |
| 274 |
} |
| 275 |
elseif( $withConflicts ){ |
| 276 |
$thisView |
| 277 |
->tag('border-color', 'red') |
| 278 |
; |
| 279 |
} |
| 280 |
else { |
| 281 |
$thisView |
| 282 |
->tag('border-color', 'gray') |
| 283 |
; |
| 284 |
} |
| 285 |
|
| 286 |
if( $withConflicts ){ |
| 287 |
$employeesWithConflictsView[] = $thisView; |
| 288 |
} |
| 289 |
else { |
| 290 |
$employeesView[] = $thisView; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// $employeesView = $this->ui->makeListInline( $employeesView ); |
| 295 |
// $employeesView = $this->ui->makeList( $employeesView ); |
| 296 |
$employeesView = $this->ui->makeGrid( $employeesView, 2 ); |
| 297 |
|
| 298 |
$toggleAllLabel = '__Select All__'; |
| 299 |
$toggleAll = $this->ui->makeInputCheckbox( 'toggle', $toggleAllLabel, 1 ) |
| 300 |
->addAttr( 'class', 'sh4-new-employee-toggler' ) |
| 301 |
; |
| 302 |
|
| 303 |
$employeesView = $this->ui->makeList( array($toggleAll, $employeesView) ); |
| 304 |
|
| 305 |
$js = <<<EOT |
| 306 |
|
| 307 |
<script language="JavaScript"> |
| 308 |
jQuery('.sh4-new-employee-container').on( 'change', '.sh4-new-employee-toggler', function(event){ |
| 309 |
var \$checkers = jQuery(this).closest('.sh4-new-employee-container').find('input[type="checkbox"]'); |
| 310 |
var meOn = jQuery(this).prop('checked'); |
| 311 |
\$checkers.each( function(){ |
| 312 |
var \$checker = jQuery(this); |
| 313 |
if ( ! \$checker.hasClass('sh4-new-employee-toggler') ){ |
| 314 |
// \$checker.prop( 'checked', ! \$checker.prop('checked') ); |
| 315 |
\$checker.prop( 'checked', meOn ); |
| 316 |
} |
| 317 |
}); |
| 318 |
return false; |
| 319 |
}); |
| 320 |
</script> |
| 321 |
|
| 322 |
EOT; |
| 323 |
|
| 324 |
$employeesView = $this->ui->makeCollection( array($js, $employeesView) ); |
| 325 |
$employeesView = $this->ui->makeBlock( $employeesView ) |
| 326 |
->addAttr( 'class', 'sh4-new-employee-container' ) |
| 327 |
; |
| 328 |
|
| 329 |
$out = array(); |
| 330 |
$out[] = $employeesView; |
| 331 |
|
| 332 |
if( $employeesWithConflictsView ){ |
| 333 |
// $employeesWithConflictsView = $this->ui->makeListInline( $employeesWithConflictsView ); |
| 334 |
$employeesWithConflictsView = $this->ui->makeGrid( $employeesWithConflictsView, 2 ); |
| 335 |
$employeesWithConflictsView = $this->ui->makeCollapse( '__Unavailable Employees__', $employeesWithConflictsView ); |
| 336 |
$out[] = $employeesWithConflictsView; |
| 337 |
} |
| 338 |
|
| 339 |
$btn = $this->self->buttons( $calendarId, $shiftTypeId ); |
| 340 |
$btn = $this->ui->makeListInline( $btn ); |
| 341 |
|
| 342 |
// open shift |
| 343 |
// $inputs = $this->common->inputs( $calendarId, $shiftTypeId, $employeeId ); |
| 344 |
// $out = array_merge( $out, $inputs ); |
| 345 |
|
| 346 |
$out[] = $btn; |
| 347 |
|
| 348 |
$out = $this->ui->makeList( $out )->gutter(3); |
| 349 |
|
| 350 |
$to = 'new/employee'; |
| 351 |
$toParams = $params; |
| 352 |
$to = array( $to, $toParams ); |
| 353 |
|
| 354 |
$out = $this->ui->makeForm( $to, $out ); |
| 355 |
|
| 356 |
$breadcrumb = array(); |
| 357 |
$breadcrumb[] = $this->common->breadcrumb($calendarId, $shiftTypeId); |
| 358 |
$breadcrumb[] = $datesView; |
| 359 |
|
| 360 |
$this->layout |
| 361 |
->setContent( $out ) |
| 362 |
->setBreadcrumb( $breadcrumb, TRUE ) |
| 363 |
->setHeader( $this->self->header() ) |
| 364 |
; |
| 365 |
$out = $this->layout->render(); |
| 366 |
|
| 367 |
return $out; |
| 368 |
} |
| 369 |
|
| 370 |
public function buttons( $calendarId, $shiftTypeId ) |
| 371 |
{ |
| 372 |
$btn = $this->ui->makeInputSubmit( '__Continue__') |
| 373 |
->tag('primary') |
| 374 |
; |
| 375 |
|
| 376 |
$return = array( 'continue' => $btn ); |
| 377 |
return $return; |
| 378 |
} |
| 379 |
|
| 380 |
public function header() |
| 381 |
{ |
| 382 |
$out = '__Employee__'; |
| 383 |
return $out; |
| 384 |
} |
| 385 |
} |