| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Users_Html_Admin_View_Index |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
|
| 8 |
HC3_Ui $ui, |
| 9 |
HC3_Ui_Layout1 $layout, |
| 10 |
|
| 11 |
HC3_IPermission $permission, |
| 12 |
|
| 13 |
SH4_App_Query $appQuery, |
| 14 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 15 |
SH4_Calendars_Query $calendarsQuery, |
| 16 |
|
| 17 |
SH4_Employees_Presenter $employeesPresenter, |
| 18 |
HC3_Users_Presenter $usersPresenter, |
| 19 |
HC3_Users_Query $usersQuery |
| 20 |
) |
| 21 |
{ |
| 22 |
$this->self = $hooks->wrap($this); |
| 23 |
$this->ui = $ui; |
| 24 |
$this->layout = $layout; |
| 25 |
|
| 26 |
$this->permission = $hooks->wrap($permission); |
| 27 |
|
| 28 |
$this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 29 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 30 |
|
| 31 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 32 |
$this->employeesPresenter = $hooks->wrap( $employeesPresenter ); |
| 33 |
$this->usersPresenter = $hooks->wrap( $usersPresenter ); |
| 34 |
$this->usersQuery = $hooks->wrap( $usersQuery ); |
| 35 |
} |
| 36 |
|
| 37 |
public function render() |
| 38 |
{ |
| 39 |
$entries = $this->appQuery->findAllUsersWithEmployee(); |
| 40 |
|
| 41 |
$managers = array(); |
| 42 |
$calendars = $this->calendarsQuery->findAll(); |
| 43 |
foreach( $calendars as $calendar ){ |
| 44 |
$thisManagers = $this->appQuery->findManagersForCalendar( $calendar ); |
| 45 |
$managers = $managers + $thisManagers; |
| 46 |
} |
| 47 |
|
| 48 |
$admins = $this->permission->findAdmins(); |
| 49 |
|
| 50 |
$entries = $admins + $managers + $entries; |
| 51 |
|
| 52 |
$tableColumns = $this->self->listingColumns(); |
| 53 |
|
| 54 |
$keys = array_keys( $tableColumns ); |
| 55 |
$firstKey = array_shift( $keys ); |
| 56 |
|
| 57 |
$tableRows = array(); |
| 58 |
foreach( $entries as $e ){ |
| 59 |
$row = $this->self->listingCell($e); |
| 60 |
|
| 61 |
// actions for first cell |
| 62 |
$actions = array(); |
| 63 |
$itemMenu = $this->self->listingCellMenu( $e ); |
| 64 |
foreach( $itemMenu as $item ){ |
| 65 |
// form |
| 66 |
if( count($item) == 3 ){ |
| 67 |
list( $href, $formContent, $btnLabel ) = $item; |
| 68 |
$btn = $this->ui->makeInputSubmit($btnLabel) |
| 69 |
->tag('nice-link') |
| 70 |
; |
| 71 |
$formContent = $formContent ? $this->ui->makeListInline( array($formContent, $btn) ) : $btn; |
| 72 |
$item = $this->ui->makeForm( $href, $formContent ); |
| 73 |
if( ! $item ){ |
| 74 |
continue; |
| 75 |
} |
| 76 |
} |
| 77 |
// link |
| 78 |
else { |
| 79 |
list( $href, $label ) = $item; |
| 80 |
$item = $this->ui->makeAhref( $href, $label ); |
| 81 |
if( ! $item ){ |
| 82 |
continue; |
| 83 |
} |
| 84 |
$item->tag('nice-link'); |
| 85 |
} |
| 86 |
$actions[] = $item; |
| 87 |
} |
| 88 |
|
| 89 |
if( $actions ){ |
| 90 |
$actions = $this->ui->makeListInline($actions) |
| 91 |
->gutter(1) |
| 92 |
->separated() |
| 93 |
; |
| 94 |
$row[$firstKey] = $this->ui->makeList( array($row[$firstKey], $actions) )->gutter(1); |
| 95 |
} |
| 96 |
|
| 97 |
$tableRows[] = $row; |
| 98 |
} |
| 99 |
|
| 100 |
$content = $this->ui->makeTable( $tableColumns, $tableRows ); |
| 101 |
|
| 102 |
$this->layout |
| 103 |
->setContent( $content ) |
| 104 |
->setBreadcrumb( $this->self->breadcrumb() ) |
| 105 |
->setHeader( $this->self->header() ) |
| 106 |
->setMenu( $this->self->menu() ) |
| 107 |
; |
| 108 |
|
| 109 |
$out = $this->layout->render(); |
| 110 |
return $out; |
| 111 |
} |
| 112 |
|
| 113 |
public function breadcrumb() |
| 114 |
{ |
| 115 |
$return = array(); |
| 116 |
$return['admin'] = array( 'admin', '__Administration__' ); |
| 117 |
return $return; |
| 118 |
} |
| 119 |
|
| 120 |
public function menu() |
| 121 |
{ |
| 122 |
$return = array( |
| 123 |
'new' => array( admin_url('user-new.php'), '__Add New__' ) |
| 124 |
); |
| 125 |
return $return; |
| 126 |
} |
| 127 |
|
| 128 |
public function header() |
| 129 |
{ |
| 130 |
$out = '__Users__'; |
| 131 |
return $out; |
| 132 |
} |
| 133 |
|
| 134 |
public function listingColumns() |
| 135 |
{ |
| 136 |
$return = array( |
| 137 |
'title' => '__WordPress User__' . ' / ' . '__Role__', |
| 138 |
'role' => '__ShiftController Role__', |
| 139 |
'employee' => '__Linked Employee__', |
| 140 |
'calendar' => '__Managed Calendars__', |
| 141 |
'notes' => '__Notes__', |
| 142 |
); |
| 143 |
return $return; |
| 144 |
} |
| 145 |
|
| 146 |
public function listingCell( HC3_Users_Model $model ) |
| 147 |
{ |
| 148 |
$return = array(); |
| 149 |
$id = $model->getId(); |
| 150 |
|
| 151 |
$titleView = $this->usersPresenter->presentTitle( $model ); |
| 152 |
|
| 153 |
$wpUser = get_userdata( $id ); |
| 154 |
$wpRole = NULL; |
| 155 |
if( isset($wpUser->roles) ){ |
| 156 |
global $wp_roles; |
| 157 |
$wpRole = array(); |
| 158 |
foreach( $wpUser->roles as $wpr ){ |
| 159 |
$wpRole[] = $wp_roles->roles[ $wpr ]['name']; |
| 160 |
} |
| 161 |
$wpRole = join(', ', $wpRole); |
| 162 |
$titleView = $this->ui->makeList( array($titleView, $wpRole) )->gutter(0); |
| 163 |
} |
| 164 |
$return['title'] = $titleView; |
| 165 |
|
| 166 |
$employee = $this->appQuery->findEmployeeByUser( $model ); |
| 167 |
$calendars = $this->appQuery->findCalendarsManagedByUser( $model ); |
| 168 |
|
| 169 |
$employeeActions = array(); |
| 170 |
if( $employee ){ |
| 171 |
$employeeView = $this->employeesPresenter->presentTitle( $employee ); |
| 172 |
$employeeActions[] = array( 'admin/users/' . $id . '/employee/0', NULL, '__Unlink__' ); |
| 173 |
} |
| 174 |
else { |
| 175 |
$employeeView = '__N/A__'; |
| 176 |
$employeeActions[] = array( 'admin/users/' . $id . '/employee', '__Link To Employee__' ); |
| 177 |
} |
| 178 |
|
| 179 |
$employeeActions = $this->ui->helperActionsFromArray( $employeeActions ); |
| 180 |
if( $employeeActions ){ |
| 181 |
$employeeActions = $this->ui->makeListInline( $employeeActions )->gutter(1)->separated(); |
| 182 |
$employeeView = $this->ui->makeList( array($employeeView, $employeeActions) )->gutter(0); |
| 183 |
} |
| 184 |
|
| 185 |
$return['employee'] = $employeeView; |
| 186 |
|
| 187 |
$isAdmin = $this->permission->isAdmin( $model ); |
| 188 |
|
| 189 |
$calendarsView = array(); |
| 190 |
$roleView = array(); |
| 191 |
|
| 192 |
if( $employee ){ |
| 193 |
$roleView[] = '__Employee__'; |
| 194 |
} |
| 195 |
|
| 196 |
if( $isAdmin ){ |
| 197 |
$roleView[] = '__Administrator__'; |
| 198 |
$calendarsView[] = '__All__'; |
| 199 |
} |
| 200 |
elseif( $calendars ){ |
| 201 |
$roleView[] = '__Manager__'; |
| 202 |
foreach( $calendars as $calendar ){ |
| 203 |
$calendarsView[] = $this->calendarsPresenter->presentTitle($calendar); |
| 204 |
} |
| 205 |
} |
| 206 |
else { |
| 207 |
$calendarsView[] = '__N/A__'; |
| 208 |
} |
| 209 |
|
| 210 |
$roleView = join( ', ', $roleView ); |
| 211 |
$return['role'] = $roleView; |
| 212 |
|
| 213 |
$calendarsView = $this->ui->makeListInline( $calendarsView ); |
| 214 |
$return['calendar'] = $calendarsView; |
| 215 |
|
| 216 |
return $return; |
| 217 |
} |
| 218 |
|
| 219 |
public function listingCellMenu( $model ) |
| 220 |
{ |
| 221 |
$id = $model->getId(); |
| 222 |
$return = array(); |
| 223 |
|
| 224 |
$return['edit'] = array( get_edit_user_link($id), '__Edit WordPress User__' ); |
| 225 |
|
| 226 |
return $return; |
| 227 |
} |
| 228 |
} |