| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Calendars_Html_Admin_View_Index |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
|
| 8 |
HC3_Ui $ui, |
| 9 |
HC3_Ui_Layout1 $layout, |
| 10 |
HC3_Request $request, |
| 11 |
|
| 12 |
SH4_App_Query $appQuery, |
| 13 |
|
| 14 |
HC3_Users_Presenter $usersPresenter, |
| 15 |
SH4_Calendars_Query $calendarsQuery, |
| 16 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 17 |
SH4_Calendars_Permissions $calendarsPermissions, |
| 18 |
|
| 19 |
SH4_Notifications_Service $notificationsService, |
| 20 |
|
| 21 |
SH4_Employees_Query $employees |
| 22 |
) |
| 23 |
{ |
| 24 |
$this->request = $request; |
| 25 |
$this->ui = $ui; |
| 26 |
$this->layout = $layout; |
| 27 |
|
| 28 |
$this->appQuery = $hooks->wrap($appQuery); |
| 29 |
|
| 30 |
$this->usersPresenter = $hooks->wrap($usersPresenter); |
| 31 |
|
| 32 |
$this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 33 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 34 |
$this->calendarsPermissions = $hooks->wrap( $calendarsPermissions ); |
| 35 |
|
| 36 |
$this->employees = $hooks->wrap($employees); |
| 37 |
$this->notificationsService = $hooks->wrap( $notificationsService ); |
| 38 |
|
| 39 |
$this->self = $hooks->wrap($this); |
| 40 |
} |
| 41 |
|
| 42 |
public function render() |
| 43 |
{ |
| 44 |
$this->request |
| 45 |
->initParam('status', 'active') |
| 46 |
; |
| 47 |
|
| 48 |
$params = $this->request->getParams(); |
| 49 |
$currentStatus = $params['status']; |
| 50 |
|
| 51 |
switch( $currentStatus ){ |
| 52 |
case 'all': |
| 53 |
$entries = $this->calendarsQuery->findAll(); |
| 54 |
break; |
| 55 |
case 'active': |
| 56 |
$entries = $this->calendarsQuery->findActive(); |
| 57 |
break; |
| 58 |
case 'archive': |
| 59 |
$entries = $this->calendarsQuery->findArchived(); |
| 60 |
break; |
| 61 |
} |
| 62 |
|
| 63 |
$tableColumns = $this->self->listingColumns(); |
| 64 |
|
| 65 |
$keys = array_keys( $tableColumns ); |
| 66 |
$firstKey = array_shift( $keys ); |
| 67 |
|
| 68 |
$tableRows = array(); |
| 69 |
foreach( $entries as $e ){ |
| 70 |
$row = $this->self->listingCell($e); |
| 71 |
|
| 72 |
// actions for first cell |
| 73 |
$actions = array(); |
| 74 |
$itemMenu = $this->self->listingCellMenu( $e ); |
| 75 |
$actions = $this->ui->helperActionsFromArray( $itemMenu ); |
| 76 |
if( $actions ){ |
| 77 |
$actions = $this->ui->makeListInline($actions) |
| 78 |
->gutter(1) |
| 79 |
->separated() |
| 80 |
; |
| 81 |
$row[$firstKey] = $this->ui->makeList( array($row[$firstKey], $actions) )->gutter(1); |
| 82 |
} |
| 83 |
|
| 84 |
$tableRows[] = $row; |
| 85 |
} |
| 86 |
|
| 87 |
$content = $this->ui->makeTable( $tableColumns, $tableRows ); |
| 88 |
|
| 89 |
// form |
| 90 |
$option = array(); |
| 91 |
// $option[] = '- ' . '__With Selected__' . ' -'; |
| 92 |
$option['archive'] = '__Archive__'; |
| 93 |
$option['restore'] = '__Restore__'; |
| 94 |
$option['delete'] = '__Delete__'; |
| 95 |
|
| 96 |
$allowed = array(); |
| 97 |
foreach( $entries as $model ){ |
| 98 |
if( $model->isArchived() ){ |
| 99 |
$allowed['restore'] = 1; |
| 100 |
$allowed['delete'] = 1; |
| 101 |
} |
| 102 |
else { |
| 103 |
$allowed['archive'] = 1; |
| 104 |
} |
| 105 |
} |
| 106 |
$option = array_intersect_key( $option, $allowed ); |
| 107 |
|
| 108 |
if( $option ){ |
| 109 |
array_unshift( $option, '- ' . '__Action__' . ' -' ); |
| 110 |
|
| 111 |
$selectAction = $this->ui->makeInputSelect( 'action', '__With Selected__', $option ); |
| 112 |
$buttons = $this->ui->makeInputSubmit( '__Apply Action__' )->tag('primary'); |
| 113 |
|
| 114 |
$hidden = $this->ui->makeInputHidden( 'id', null ); |
| 115 |
$js = <<<EOT |
| 116 |
<script> |
| 117 |
function sh4CalendarForm(){ |
| 118 |
var idValue = []; |
| 119 |
var idInputs = document.getElementsByName("hc-id[]"); |
| 120 |
for( var ii = 0; ii < idInputs.length; ii++ ){ |
| 121 |
if( idInputs[ii].checked ){ |
| 122 |
idValue.push( idInputs[ii].value ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if( idValue.length ){ |
| 127 |
var idInput = document.getElementsByName("hc-id")[0]; |
| 128 |
idInput.value = idValue.join( ':' ); |
| 129 |
return true; |
| 130 |
} |
| 131 |
else { |
| 132 |
return false; |
| 133 |
} |
| 134 |
} |
| 135 |
</script> |
| 136 |
EOT; |
| 137 |
|
| 138 |
$form = $this->ui->makeList( array($js, $hidden, $selectAction, $buttons) ); |
| 139 |
$form = $this->ui->makeForm( |
| 140 |
'admin/calendars', |
| 141 |
$form |
| 142 |
); |
| 143 |
$form->setId( 'sh4-calendar-form' ); |
| 144 |
$form->setAttr( 'onsubmit', 'return sh4CalendarForm();' ); |
| 145 |
|
| 146 |
$content = $this->ui->makeList( array($content, $form) ); |
| 147 |
} |
| 148 |
|
| 149 |
$byStatus = $this->self->byStatus( $entries ); |
| 150 |
if( $byStatus ){ |
| 151 |
$byStatusView = array(); |
| 152 |
foreach( $byStatus as $bys ){ |
| 153 |
list( $href, $hrefLabel, $countAddon ) = $bys; |
| 154 |
|
| 155 |
$thisSelected = FALSE; |
| 156 |
if( isset($href[1]['status']) && $href[1]['status'] == $currentStatus ){ |
| 157 |
$thisSelected = TRUE; |
| 158 |
} |
| 159 |
|
| 160 |
$thisOne = $thisSelected ? $this->ui->makeSpan( $hrefLabel ) : $this->ui->makeAhref( $href, $hrefLabel ); |
| 161 |
if( strlen($countAddon) ){ |
| 162 |
$thisOne = $this->ui->makeListInline( array($thisOne, '(' . $countAddon . ')') )->gutter(1); |
| 163 |
} |
| 164 |
|
| 165 |
if( $thisSelected ){ |
| 166 |
$thisOne |
| 167 |
->tag('font-style', 'bold') |
| 168 |
; |
| 169 |
} |
| 170 |
|
| 171 |
$byStatusView[] = $thisOne; |
| 172 |
} |
| 173 |
$byStatusView = $this->ui->makeListInline( $byStatusView )->separated(); |
| 174 |
|
| 175 |
$content = $this->ui->makeList( array($byStatusView, $content) )->gutter(1); |
| 176 |
} |
| 177 |
|
| 178 |
$this->layout |
| 179 |
->setContent( $content ) |
| 180 |
->setBreadcrumb( $this->self->breadcrumb() ) |
| 181 |
->setHeader( $this->self->header() ) |
| 182 |
->setMenu( $this->self->menu() ) |
| 183 |
; |
| 184 |
|
| 185 |
$out = $this->layout->render(); |
| 186 |
return $out; |
| 187 |
} |
| 188 |
|
| 189 |
public function breadcrumb() |
| 190 |
{ |
| 191 |
$return = array(); |
| 192 |
$return['admin'] = array( 'admin', '__Administration__' ); |
| 193 |
return $return; |
| 194 |
} |
| 195 |
|
| 196 |
public function menu() |
| 197 |
{ |
| 198 |
$return = array( |
| 199 |
'new' => array( 'admin/calendars/new', '__Add New__' ) |
| 200 |
); |
| 201 |
$return['sort'] = array( 'admin/calendars/sort', '__Sort Order__' ); |
| 202 |
|
| 203 |
return $return; |
| 204 |
} |
| 205 |
|
| 206 |
public function header() |
| 207 |
{ |
| 208 |
$out = '__Calendars__'; |
| 209 |
return $out; |
| 210 |
} |
| 211 |
|
| 212 |
public function byStatus( $entries ) |
| 213 |
{ |
| 214 |
$return = array(); |
| 215 |
|
| 216 |
$count1 = $this->calendarsQuery->countActive(); |
| 217 |
$count2 = $this->calendarsQuery->countArchived(); |
| 218 |
|
| 219 |
$return['all'] = array( array('admin/calendars', array('status' => 'all')), '__All__', ($count1 + $count2) ); |
| 220 |
|
| 221 |
if( $count1 = $this->calendarsQuery->countActive() ){ |
| 222 |
$return['active'] = array( array('admin/calendars', array('status' => 'active')), '__Active__', $count1 ); |
| 223 |
} |
| 224 |
if( $count2 = $this->calendarsQuery->countArchived() ){ |
| 225 |
$return['archived'] = array( array('admin/calendars', array('status' => 'archive')), '__Archived__', $count2 ); |
| 226 |
} |
| 227 |
|
| 228 |
return $return; |
| 229 |
} |
| 230 |
|
| 231 |
public function listingColumns() |
| 232 |
{ |
| 233 |
$return = array( |
| 234 |
// 'title' => '__Name__', |
| 235 |
'title' => NULL, |
| 236 |
'details' => NULL, |
| 237 |
); |
| 238 |
return $return; |
| 239 |
} |
| 240 |
|
| 241 |
public function listingCell( SH4_Calendars_Model $model ) |
| 242 |
{ |
| 243 |
$return = array(); |
| 244 |
|
| 245 |
$id = $model->getId(); |
| 246 |
|
| 247 |
$idView = $model->getId(); |
| 248 |
$idLabel = $this->ui->makeBlock('id') |
| 249 |
->tag('mute') |
| 250 |
->tag('font-size', 2) |
| 251 |
; |
| 252 |
$idView = $this->ui->makeListInline( array($idLabel, $idView) ) |
| 253 |
->gutter(1) |
| 254 |
; |
| 255 |
|
| 256 |
$titleView = $this->calendarsPresenter->presentTitle( $model ); |
| 257 |
// $titleView = $this->ui->makeList( array($titleView, $idView) )->gutter(0); |
| 258 |
|
| 259 |
$checkbox = $this->ui->makeInputCheckbox( 'id[]', null, $model->getId() ); |
| 260 |
$titleView = $this->ui->makeListInline( array($checkbox,$titleView, $idView) )->gutter(1); |
| 261 |
$titleView = $this->ui->makeElement( 'label', $titleView ); |
| 262 |
|
| 263 |
$return['title'] = $titleView; |
| 264 |
|
| 265 |
$detailsView = array(); |
| 266 |
$details = $this->self->listingCellDetails( $model ); |
| 267 |
foreach( $details as $d ){ |
| 268 |
$dView = array(); |
| 269 |
$dView[] = $this->ui->makeAhref( $d[0], $d[1] ) |
| 270 |
->tag('nice-link') |
| 271 |
; |
| 272 |
|
| 273 |
if( isset($d[2]) ){ |
| 274 |
$countView = $this->ui->makeSpan( $d[2] ) |
| 275 |
->tag('color', 'white') |
| 276 |
->tag('padding', 'x1') |
| 277 |
; |
| 278 |
|
| 279 |
$ok = isset($d[3]) ? $d[3] : ( ($d[2] > 0) ? TRUE : FALSE); |
| 280 |
|
| 281 |
if( $ok ){ |
| 282 |
$countView->tag('bgcolor', 'olive'); |
| 283 |
} |
| 284 |
else { |
| 285 |
$countView->tag('bgcolor', 'maroon'); |
| 286 |
} |
| 287 |
$dView[] = $countView; |
| 288 |
} |
| 289 |
|
| 290 |
$dView = $this->ui->makeListInline( $dView )->gutter(0); |
| 291 |
$detailsView[] = $dView; |
| 292 |
} |
| 293 |
if( $detailsView ){ |
| 294 |
$detailsView = $this->ui->makeListInline( $detailsView ) |
| 295 |
->gutter(2) |
| 296 |
// ->separated() |
| 297 |
; |
| 298 |
$return['details'] = $detailsView; |
| 299 |
} |
| 300 |
|
| 301 |
return $return; |
| 302 |
} |
| 303 |
|
| 304 |
public function listingCellMenu( SH4_Calendars_Model $model ) |
| 305 |
{ |
| 306 |
$id = $model->getId(); |
| 307 |
|
| 308 |
$return = array( |
| 309 |
'edit' => array( 'admin/calendars/' . $id, '__Edit__' ), |
| 310 |
); |
| 311 |
|
| 312 |
$allNotifications = $this->notificationsService->findAll(); |
| 313 |
$allNotifications = array_keys( $allNotifications ); |
| 314 |
$notificationsCount = 0; |
| 315 |
foreach( $allNotifications as $notificationId ){ |
| 316 |
if( $this->notificationsService->isOn($model, $notificationId) ){ |
| 317 |
$notificationsCount++; |
| 318 |
} |
| 319 |
} |
| 320 |
$label = '__Notifications__' . ' [' . $notificationsCount . ']'; |
| 321 |
|
| 322 |
$return['notifications'] = array( 'admin/notifications/' . $id, $label ); |
| 323 |
|
| 324 |
$allPermissionsOptions = $this->calendarsPermissions->categorizedOptions(); |
| 325 |
$thisPermissionsOptions = $this->calendarsPermissions->getAll( $model ); |
| 326 |
|
| 327 |
$permissionCounts = array(); |
| 328 |
foreach( $allPermissionsOptions as $group => $perms ){ |
| 329 |
// $permissionCounts[ $group ] = array( 0, count($perms) ); |
| 330 |
$permissionCounts[ $group ] = 0; |
| 331 |
} |
| 332 |
|
| 333 |
foreach( $thisPermissionsOptions as $k => $v ){ |
| 334 |
if( ! $v ){ |
| 335 |
continue; |
| 336 |
} |
| 337 |
|
| 338 |
$ka = explode( '_', $k ); |
| 339 |
$group = $ka[0]; |
| 340 |
|
| 341 |
if( ! isset($allPermissionsOptions[$group]) ){ |
| 342 |
continue; |
| 343 |
} |
| 344 |
|
| 345 |
if( ! in_array($k, $allPermissionsOptions[$group]) ){ |
| 346 |
continue; |
| 347 |
} |
| 348 |
|
| 349 |
$permissionCounts[ $group ]++; |
| 350 |
} |
| 351 |
$permissionCountsView = join( '/', $permissionCounts ); |
| 352 |
|
| 353 |
$label = '__Permissions__' . ' [' . $permissionCountsView . ']'; |
| 354 |
$return['permissions'] = array( 'admin/calendars/' . $id . '/prm', $label); |
| 355 |
|
| 356 |
if( $model->isArchived() ){ |
| 357 |
$return['restore'] = array( 'admin/calendars/' . $id . '/restore', NULL, '__Restore__'); |
| 358 |
$return['delete'] = array( 'admin/calendars/' . $id . '/delete', '__Delete__' ); |
| 359 |
} |
| 360 |
else { |
| 361 |
$return['archive'] = array( 'admin/calendars/' . $id . '/archive', NULL, '__Archive__' ); |
| 362 |
} |
| 363 |
|
| 364 |
return $return; |
| 365 |
} |
| 366 |
|
| 367 |
public function listingCellDetails( SH4_Calendars_Model $model ) |
| 368 |
{ |
| 369 |
$id = $model->getId(); |
| 370 |
$return = array(); |
| 371 |
|
| 372 |
$employees = $this->appQuery->findEmployeesForCalendar( $model ); |
| 373 |
// don't count open shifts |
| 374 |
if( isset($employees[0]) ){ |
| 375 |
$openOn = TRUE; |
| 376 |
unset( $employees[0] ); |
| 377 |
} |
| 378 |
else { |
| 379 |
$openOn = FALSE; |
| 380 |
} |
| 381 |
$return['employees'] = array( 'admin/calendars/' . $id . '/employees', '__Employees__', count($employees) ) ; |
| 382 |
|
| 383 |
$shiftTypes = $this->appQuery->findShiftTypesForCalendar( $model ); |
| 384 |
$return['shifttypes'] = array( 'admin/calendars/' . $id . '/shifttypes', '__Shift Time__', count($shiftTypes) ) ; |
| 385 |
|
| 386 |
$managers = $this->appQuery->findManagersForCalendar( $model ); |
| 387 |
$return['managers'] = array( 'admin/calendars/' . $id . '/managers', '__Managers__', count($managers) ) ; |
| 388 |
|
| 389 |
$viewers = $this->appQuery->findViewersForCalendar( $model ); |
| 390 |
if( count($viewers) ){ |
| 391 |
$return['viewers'] = array( 'admin/calendars/' . $id . '/managers', '__Viewers__', count($viewers) ) ; |
| 392 |
} |
| 393 |
|
| 394 |
$openLabel = $openOn ? '✓' : '×'; |
| 395 |
$return['open'] = array( 'admin/calendars/' . $id . '/employees', '__Open Shifts__', $openLabel, $openOn ) ; |
| 396 |
|
| 397 |
return $return; |
| 398 |
} |
| 399 |
} |