| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Shifts_View_Employee |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
HC3_Post $post, |
| 8 |
HC3_Request $request, |
| 9 |
|
| 10 |
HC3_Ui $ui, |
| 11 |
HC3_Ui_Layout1 $layout, |
| 12 |
|
| 13 |
SH4_Shifts_View_Common $common, |
| 14 |
SH4_Shifts_Query $shiftsQuery, |
| 15 |
|
| 16 |
SH4_Employees_Query $employees, |
| 17 |
SH4_App_Query $appQuery, |
| 18 |
SH4_Shifts_Command $command, |
| 19 |
|
| 20 |
SH4_Shifts_Availability $availability, |
| 21 |
SH4_Shifts_Conflicts $conflicts, |
| 22 |
SH4_Shifts_View_Zoom $viewZoom |
| 23 |
) |
| 24 |
{ |
| 25 |
$this->post = $post; |
| 26 |
$this->request = $request; |
| 27 |
|
| 28 |
$this->self = $hooks->wrap( $this ); |
| 29 |
$this->ui = $ui; |
| 30 |
$this->layout = $layout; |
| 31 |
|
| 32 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 33 |
|
| 34 |
$this->employees = $hooks->wrap($employees); |
| 35 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 36 |
|
| 37 |
$this->viewZoom = $hooks->wrap( $viewZoom ); |
| 38 |
$this->common = $hooks->wrap( $common ); |
| 39 |
|
| 40 |
$this->availability = $hooks->wrap( $availability ); |
| 41 |
$this->conflicts = $hooks->wrap( $conflicts ); |
| 42 |
$this->command = $hooks->wrap($command); |
| 43 |
} |
| 44 |
|
| 45 |
public function post( $ids, $employeeId, $copy = false ) |
| 46 |
{ |
| 47 |
$employee = $this->employees->findById( $employeeId ); |
| 48 |
|
| 49 |
$ids = HC3_Functions::unglueArray( $ids ); |
| 50 |
$shifts = $this->shiftsQuery->findManyById( $ids ); |
| 51 |
|
| 52 |
foreach( $shifts as $shift ){ |
| 53 |
if( $copy ){ |
| 54 |
$this->command->copy( |
| 55 |
$shift, |
| 56 |
$shift->getCalendar(), |
| 57 |
$shift->getStart(), |
| 58 |
$shift->getEnd(), |
| 59 |
$employee, |
| 60 |
$shift->getBreakStart(), |
| 61 |
$shift->getBreakEnd(), |
| 62 |
$shift->getStatus() |
| 63 |
); |
| 64 |
} |
| 65 |
else { |
| 66 |
$this->command->changeEmployee( $shift, $employee ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$to = $this->post->get( 'back' ); |
| 71 |
if( $to ){ |
| 72 |
$to = json_decode( $to, TRUE ); |
| 73 |
} |
| 74 |
else { |
| 75 |
$to = array( 'schedule', array() ); |
| 76 |
} |
| 77 |
|
| 78 |
if( $copy ){ |
| 79 |
$msg = '__Copy Created__'; |
| 80 |
} |
| 81 |
else { |
| 82 |
$msg = $employeeId ? '__Employee Changed__' : '__Employee Unassigned__'; |
| 83 |
} |
| 84 |
|
| 85 |
$ret = array( $to, $msg ); |
| 86 |
return $ret; |
| 87 |
} |
| 88 |
|
| 89 |
public function renderConfirm( $id, $employeeId ) |
| 90 |
{ |
| 91 |
$change = array(); |
| 92 |
|
| 93 |
$employee = $this->employees->findById( $employeeId ); |
| 94 |
$changeView = $employee->getTitle(); |
| 95 |
|
| 96 |
$change['employee'] = $changeView; |
| 97 |
$to = 'shifts/' . $id . '/employee/' . $employeeId; |
| 98 |
|
| 99 |
$out = $this->viewZoom->render( $id, $change, $to ); |
| 100 |
return $out; |
| 101 |
} |
| 102 |
|
| 103 |
public function render( $ids ) |
| 104 |
{ |
| 105 |
$ids = HC3_Functions::unglueArray( $ids ); |
| 106 |
$strIds = (count($ids) < 2) ? $ids[0] : HC3_Functions::glueArray( $ids ); |
| 107 |
|
| 108 |
$models = $this->shiftsQuery->findManyById( $ids ); |
| 109 |
|
| 110 |
$currentEmployeesIds = array(); |
| 111 |
$employeesIds = array(); |
| 112 |
foreach( $models as $model ){ |
| 113 |
$calendar = $model->getCalendar(); |
| 114 |
$thisEmployees = $this->appQuery->findEmployeesForCalendar( $calendar ); |
| 115 |
foreach( $thisEmployees as $eid => $emp ){ |
| 116 |
if( ! in_array($eid, $employeesIds) ){ |
| 117 |
$employeesIds[] = $eid; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$thisEmployee = $model->getEmployee(); |
| 122 |
$thisEmployeeId = $thisEmployee->getId(); |
| 123 |
if( ! in_array($thisEmployeeId, $currentEmployeesIds) ){ |
| 124 |
$currentEmployeesIds[] = $thisEmployeeId; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
$employees = array(); |
| 129 |
if( $employeesIds ){ |
| 130 |
$employees = $this->employees->findManyActiveById( $employeesIds ); |
| 131 |
} |
| 132 |
|
| 133 |
unset( $employees[0] ); |
| 134 |
if( count($currentEmployeesIds) < 2 ){ |
| 135 |
foreach( $currentEmployeesIds as $currentEmployeeId ){ |
| 136 |
unset( $employees[$currentEmployeeId] ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
$employeesView = array(); |
| 141 |
$employeesWithConflictsView = array(); |
| 142 |
|
| 143 |
foreach( $employees as $employee ){ |
| 144 |
$eid = $employee->getId(); |
| 145 |
|
| 146 |
$withConflicts = 0; |
| 147 |
$withAvailability = 0; |
| 148 |
$withNothing = 0; |
| 149 |
|
| 150 |
reset( $models ); |
| 151 |
foreach( $models as $model ){ |
| 152 |
$id = $model->getId(); |
| 153 |
$calendar = $model->getCalendar(); |
| 154 |
$calendarId = $calendar->getId(); |
| 155 |
$start = $model->getStart(); |
| 156 |
$end = $model->getEnd(); |
| 157 |
|
| 158 |
// $testModel = new SH4_Shifts_Model( $id, $calendar, $start, $end, $employee ); |
| 159 |
$testModel = new SH4_Shifts_Model( null, $calendar, $start, $end, $employee ); |
| 160 |
|
| 161 |
$conflicts = $this->conflicts->get($testModel); |
| 162 |
if( $conflicts ){ |
| 163 |
$withConflicts++; |
| 164 |
} |
| 165 |
else { |
| 166 |
if( (! $this->availability->hasAvailability()) OR $this->availability->get($testModel) ){ |
| 167 |
$withAvailability++; |
| 168 |
} |
| 169 |
else { |
| 170 |
$withNothing++; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
$thisView = array(); |
| 176 |
|
| 177 |
$employeeView = $employee->getTitle(); |
| 178 |
$employeeView = $this->ui->makeSpan( $employeeView ) |
| 179 |
->tag('font-size', 4) |
| 180 |
->tag('font-style', 'bold') |
| 181 |
; |
| 182 |
|
| 183 |
$descriptionView = $employee->getDescription(); |
| 184 |
if( strlen($descriptionView) ){ |
| 185 |
$descriptionView = $this->ui->makeLongText( $descriptionView ); |
| 186 |
$employeeView = $this->ui->makeList( array($employeeView, $descriptionView) ) |
| 187 |
->gutter(1) |
| 188 |
; |
| 189 |
} |
| 190 |
|
| 191 |
$thisView[] = $employeeView; |
| 192 |
|
| 193 |
$conflictsView = array(); |
| 194 |
|
| 195 |
if( $withAvailability ){ |
| 196 |
$sign = ( count($ids) < 2 ) ? '✓' : $withAvailability; |
| 197 |
$sign = $this->ui->makeBlock( $sign ) |
| 198 |
->tag('padding', 'x1') |
| 199 |
->tag('color', 'white') |
| 200 |
->tag('bgcolor', 'olive') |
| 201 |
->addAttr( 'title', '__Available__' ) |
| 202 |
; |
| 203 |
$conflictsView[] = $sign; |
| 204 |
} |
| 205 |
|
| 206 |
if( $withNothing ){ |
| 207 |
$sign = ( count($ids) < 2 ) ? '✓' : $withNothing; |
| 208 |
$sign = $this->ui->makeBlock( $sign ) |
| 209 |
->tag('padding', 'x1') |
| 210 |
->tag('color', 'white') |
| 211 |
->tag('bgcolor', 'gray') |
| 212 |
->addAttr( 'title', '__No Availability__' ) |
| 213 |
; |
| 214 |
$conflictsView[] = $sign; |
| 215 |
} |
| 216 |
|
| 217 |
if( $withConflicts ){ |
| 218 |
$sign = (count($ids) < 2) ? ' ! ' : $withConflicts; |
| 219 |
$sign = $this->ui->makeBlock( $sign ) |
| 220 |
->tag('padding', 'x1') |
| 221 |
->tag('color', 'white') |
| 222 |
->tag('bgcolor', 'maroon') |
| 223 |
->addAttr( 'title', '__Conflicts__' ) |
| 224 |
; |
| 225 |
|
| 226 |
if( count($ids) < 2 ){ |
| 227 |
$shiftId = $ids[0]; |
| 228 |
$conflictsLink = array('conflicts', $shiftId, $calendarId, $start, $end, $eid); |
| 229 |
$conflictsLink = join('/', $conflictsLink); |
| 230 |
|
| 231 |
$sign = $this->ui->makeAhref( $conflictsLink, $sign ) |
| 232 |
->newWindow() |
| 233 |
; |
| 234 |
} |
| 235 |
|
| 236 |
$conflictsView[] = $sign; |
| 237 |
} |
| 238 |
|
| 239 |
$conflictsView = $this->ui->makeListInline( $conflictsView ) |
| 240 |
->gutter(1) |
| 241 |
; |
| 242 |
$thisView[] = $conflictsView; |
| 243 |
|
| 244 |
$thisView = $this->ui->makeBlock( $this->ui->makeListInline($thisView) ) |
| 245 |
->addAttr('style', 'position: relative;') |
| 246 |
; |
| 247 |
// $thisView = $this->ui->makeList( $thisView )->gutter(0); |
| 248 |
|
| 249 |
$btn = $this->ui->makeInputSubmit('__Change__') |
| 250 |
->tag('secondary') |
| 251 |
; |
| 252 |
if( $withConflicts ){ |
| 253 |
$btn |
| 254 |
->tag('confirm') |
| 255 |
; |
| 256 |
} |
| 257 |
|
| 258 |
$out = $btn; |
| 259 |
|
| 260 |
// schedule link |
| 261 |
$scheduleLink = HC3_Session::instance()->getUserdata( 'scheduleLink' ); |
| 262 |
if( ! $scheduleLink ){ |
| 263 |
$scheduleLink = array( 'schedule', array() ); |
| 264 |
} |
| 265 |
$scheduleLinkValue = json_encode( $scheduleLink ); |
| 266 |
$inputBackHidden = $this->ui->makeInputHidden( 'back', $scheduleLinkValue ); |
| 267 |
|
| 268 |
// change |
| 269 |
$label = $model->isOpen() ? '__Assign Employee__' : '__Change Employee__'; |
| 270 |
$btn1 = $this->ui->makeInputSubmit( $label ) |
| 271 |
->tag('secondary') |
| 272 |
->tag('block') |
| 273 |
->tag( 'nowrap' ) |
| 274 |
; |
| 275 |
if( $withConflicts ){ |
| 276 |
$btn1->tag('confirm'); |
| 277 |
} |
| 278 |
|
| 279 |
$slug = $this->request->getSlug(); |
| 280 |
if( 'employeecopy' == substr($slug, -strlen('employeecopy')) ){ |
| 281 |
$btn2 = $this->ui->makeInputSubmit( '__Copy To Employee__' ) |
| 282 |
->tag('secondary') |
| 283 |
->tag( 'block' ) |
| 284 |
->tag( 'nowrap' ) |
| 285 |
; |
| 286 |
if( $withConflicts ){ |
| 287 |
$btn2->tag('confirm'); |
| 288 |
} |
| 289 |
|
| 290 |
$out2 = $this->ui->makeCollection( array($btn2, $inputBackHidden) ); |
| 291 |
$to = array( 'shifts', $strIds, 'employee', $eid, 1 ); |
| 292 |
$to = join('/', $to); |
| 293 |
|
| 294 |
$form = $this->ui->makeForm( $to, $out2 ); |
| 295 |
} |
| 296 |
else { |
| 297 |
$out1 = $this->ui->makeCollection( array($btn1, $inputBackHidden) ); |
| 298 |
$to = array( 'shifts', $strIds, 'employee', $eid ); |
| 299 |
$to = join('/', $to); |
| 300 |
$form = $this->ui->makeForm( $to, $out1 ); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
// $form = $this->ui->makeList( [$form1, $form2] )->gutter(1); |
| 305 |
|
| 306 |
$thisView = $this->ui->makeList( array($thisView, $form) )->gutter(3); |
| 307 |
|
| 308 |
$thisView = $this->ui->makeBlock( $thisView ) |
| 309 |
->tag('border') |
| 310 |
->tag('padding', 2) |
| 311 |
; |
| 312 |
|
| 313 |
if( $withAvailability == count($ids) ){ |
| 314 |
$thisView |
| 315 |
->tag('border-color', 'green') |
| 316 |
; |
| 317 |
} |
| 318 |
|
| 319 |
if( $withConflicts ){ |
| 320 |
$employeesWithConflictsView[] = $thisView; |
| 321 |
} |
| 322 |
else { |
| 323 |
$employeesView[] = $thisView; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if( $employeesView OR $employeesWithConflictsView ){ |
| 328 |
$out = $this->ui->makeListInline( $employeesView ); |
| 329 |
|
| 330 |
if( $employeesWithConflictsView ){ |
| 331 |
$out = array( $out ); |
| 332 |
|
| 333 |
$employeesWithConflictsView = $this->ui->makeListInline( $employeesWithConflictsView ); |
| 334 |
$employeesWithConflictsView = $this->ui->makeCollapse( '__Unavailable Employees__', $employeesWithConflictsView ); |
| 335 |
$out[] = $employeesWithConflictsView; |
| 336 |
|
| 337 |
$out = $this->ui->makeList( $out )->gutter(2); |
| 338 |
} |
| 339 |
} |
| 340 |
else { |
| 341 |
$out = array(); |
| 342 |
$out[] = '__No Available Employees__'; |
| 343 |
$out = $this->ui->makeList( $out ); |
| 344 |
} |
| 345 |
|
| 346 |
$this->layout |
| 347 |
->setContent( $out ) |
| 348 |
->setHeader( $this->self->header($model) ) |
| 349 |
; |
| 350 |
|
| 351 |
if( count($ids) > 1 ){ |
| 352 |
$breadcrumb = array(); |
| 353 |
reset( $models ); |
| 354 |
foreach( $models as $model ){ |
| 355 |
$breadcrumb[] = $this->common->breadcrumb($model); |
| 356 |
} |
| 357 |
$breadcrumbMultiline = TRUE; |
| 358 |
} |
| 359 |
else { |
| 360 |
$breadcrumb = $this->common->breadcrumb($model); |
| 361 |
$breadcrumbMultiline = FALSE; |
| 362 |
} |
| 363 |
|
| 364 |
$this->layout |
| 365 |
->setBreadcrumb( $breadcrumb, $breadcrumbMultiline ) |
| 366 |
; |
| 367 |
|
| 368 |
$out = $this->layout->render(); |
| 369 |
return $out; |
| 370 |
} |
| 371 |
|
| 372 |
public function header( SH4_Shifts_Model $model ) |
| 373 |
{ |
| 374 |
if( $model->isOpen() ){ |
| 375 |
$ret = '__Assign Employee__'; |
| 376 |
} |
| 377 |
else { |
| 378 |
$ret = '__Change Employee__'; |
| 379 |
$slug = $this->request->getSlug(); |
| 380 |
if( 'employeecopy' == substr($slug, -strlen('employeecopy')) ){ |
| 381 |
$ret = '__Copy Shift To Another Employee__'; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
return $ret; |
| 386 |
} |
| 387 |
} |