| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Calendars_Html_Admin_View_ManagersNew |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
HC3_Ui $ui, |
| 8 |
HC3_Ui_Layout1 $layout, |
| 9 |
HC3_Request $request, |
| 10 |
HC3_UriAction $uriAction, |
| 11 |
|
| 12 |
SH4_App_Query $appQuery, |
| 13 |
HC3_IPermission $permission, |
| 14 |
|
| 15 |
SH4_Calendars_Query $calendarsQuery, |
| 16 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 17 |
|
| 18 |
HC3_Users_Presenter $usersPresenter, |
| 19 |
HC3_Users_Query $users |
| 20 |
) |
| 21 |
{ |
| 22 |
$this->self = $hooks->wrap($this); |
| 23 |
$this->ui = $ui; |
| 24 |
$this->layout = $layout; |
| 25 |
$this->request = $request; |
| 26 |
$this->uriAction = $uriAction; |
| 27 |
|
| 28 |
$this->permission = $hooks->wrap($permission); |
| 29 |
$this->appQuery = $hooks->wrap($appQuery); |
| 30 |
$this->calendarsQuery = $hooks->wrap($calendarsQuery); |
| 31 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 32 |
|
| 33 |
$this->usersPresenter = $hooks->wrap($usersPresenter); |
| 34 |
$this->users = $hooks->wrap($users); |
| 35 |
} |
| 36 |
|
| 37 |
public function renderWpRoles( $id ) |
| 38 |
{ |
| 39 |
$model = $this->calendarsQuery->findById($id); |
| 40 |
|
| 41 |
$currentManagers = $this->appQuery->findManagersForCalendar( $model ); |
| 42 |
$currentManagersIds = array_keys( $currentManagers ); |
| 43 |
|
| 44 |
$currentViewers = $this->appQuery->findViewersForCalendar( $model ); |
| 45 |
$currentViewersIds = array_keys( $currentViewers ); |
| 46 |
|
| 47 |
$alreadyIds = array_merge( $currentManagersIds, $currentViewersIds ); |
| 48 |
|
| 49 |
$out = array(); |
| 50 |
|
| 51 |
$wp_roles = new WP_Roles(); |
| 52 |
$wordpressRoles = $wp_roles->get_names(); |
| 53 |
$wordpressCountUsers = count_users(); |
| 54 |
|
| 55 |
$to = 'admin/calendars/' . $id . '/managers-new'; |
| 56 |
$wpRoles = array(); |
| 57 |
foreach( $wordpressRoles as $wpRoleId => $wpRoleLabel ){ |
| 58 |
$label = $wpRoleLabel; |
| 59 |
|
| 60 |
$thisWpRoleCount = ( isset($wordpressCountUsers['avail_roles'][$wpRoleId]) ) ? $wordpressCountUsers['avail_roles'][$wpRoleId] : 0; |
| 61 |
if( ! $thisWpRoleCount ) continue; |
| 62 |
|
| 63 |
$q = array(); |
| 64 |
$q[] = array( 'role', '=', $wpRoleId ); |
| 65 |
$q[] = array( 'id', '<>', $alreadyIds ); |
| 66 |
|
| 67 |
$users = $this->users->read( $q ); |
| 68 |
$thisWpRoleCount = count( $users ); |
| 69 |
if( ! $thisWpRoleCount ) continue; |
| 70 |
|
| 71 |
$label .= ' ('; |
| 72 |
$label .= $thisWpRoleCount; |
| 73 |
$label .= ')'; |
| 74 |
|
| 75 |
$wpRoles[ $wpRoleId ] = $label; |
| 76 |
} |
| 77 |
|
| 78 |
if( $wpRoles ){ |
| 79 |
$out[] = '__Select a user from WordPress role__'; |
| 80 |
foreach( $wpRoles as $wpRoleId => $wpRoleLabel ){ |
| 81 |
$thisParams = array( 'wprole' => $wpRoleId ); |
| 82 |
$thisTo = array( $to, $thisParams ); |
| 83 |
$out[] = $this->ui->makeAhref( $thisTo, $wpRoleLabel ) |
| 84 |
// ->tag('tab-link') |
| 85 |
; |
| 86 |
} |
| 87 |
} |
| 88 |
else { |
| 89 |
$out[] = '__No Available Users__'; |
| 90 |
} |
| 91 |
|
| 92 |
$out = $this->ui->makeList( $out ); |
| 93 |
|
| 94 |
$this->layout |
| 95 |
->setContent( $out ) |
| 96 |
->setBreadcrumb( $this->self->breadcrumb($model) ) |
| 97 |
->setHeader( $this->self->header($model) ) |
| 98 |
; |
| 99 |
|
| 100 |
$out = $this->layout->render(); |
| 101 |
return $out; |
| 102 |
} |
| 103 |
|
| 104 |
public function render( $id ) |
| 105 |
{ |
| 106 |
$wpRole = NULL; |
| 107 |
if( defined('WPINC') ){ |
| 108 |
$params = $this->request->getParams(); |
| 109 |
$wpRole = isset( $params['wprole'] ) ? $params['wprole'] : NULL; |
| 110 |
if( ! $wpRole ){ |
| 111 |
return $this->self->renderWpRoles( $id ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
$model = $this->calendarsQuery->findById($id); |
| 116 |
|
| 117 |
$currentManagers = $this->appQuery->findManagersForCalendar( $model ); |
| 118 |
$currentManagersIds = array_keys( $currentManagers ); |
| 119 |
|
| 120 |
$currentViewers = $this->appQuery->findViewersForCalendar( $model ); |
| 121 |
$currentViewersIds = array_keys( $currentViewers ); |
| 122 |
|
| 123 |
$alreadyIds = array_merge( $currentManagersIds, $currentViewersIds ); |
| 124 |
|
| 125 |
$q = array(); |
| 126 |
if( $wpRole ){ |
| 127 |
$q[] = array( 'role', '=', $wpRole ); |
| 128 |
} |
| 129 |
|
| 130 |
$q[] = array( 'id', 'NOTIN', $alreadyIds ); |
| 131 |
$users = $this->users->read( $q ); |
| 132 |
|
| 133 |
// remove already managers/viewers |
| 134 |
foreach( $currentManagersIds as $uid ){ |
| 135 |
unset( $users[$uid] ); |
| 136 |
} |
| 137 |
foreach( $currentViewersIds as $uid ){ |
| 138 |
unset( $users[$uid] ); |
| 139 |
} |
| 140 |
|
| 141 |
// $users = $currentViewers + $users; |
| 142 |
// $users = $currentManagers + $users; |
| 143 |
|
| 144 |
$header = array( |
| 145 |
'title' => '__User__', |
| 146 |
'actions' => ' ', |
| 147 |
); |
| 148 |
|
| 149 |
$keys = array_keys( $header ); |
| 150 |
$firstKey = array_shift( $keys ); |
| 151 |
|
| 152 |
$rows = array(); |
| 153 |
foreach( $users as $e ){ |
| 154 |
$userId = $e->getId(); |
| 155 |
$row = array(); |
| 156 |
|
| 157 |
$userView = $this->usersPresenter->presentTitle( $e ); |
| 158 |
$row['title'] = $userView; |
| 159 |
|
| 160 |
$actions = array(); |
| 161 |
if( in_array($userId, $currentManagersIds) ){ |
| 162 |
if( ! $this->permission->isAdmin($e) ){ |
| 163 |
$actions[] = array( 'admin/calendars/' . $id . '/managers/' . $userId . '/remove', NULL, '__Remove From Managers__' ); |
| 164 |
} |
| 165 |
} |
| 166 |
else { |
| 167 |
$actions[] = array( 'admin/calendars/' . $id . '/managers/' . $userId . '/add', NULL, '__Add To Managers__' ); |
| 168 |
|
| 169 |
if( in_array($userId, $currentViewersIds) ){ |
| 170 |
if( ! $this->permission->isAdmin($e) ){ |
| 171 |
$actions[] = array( 'admin/calendars/' . $id . '/viewers/' . $userId . '/remove', NULL, '__Remove From Viewers__' ); |
| 172 |
} |
| 173 |
} |
| 174 |
else { |
| 175 |
$actions[] = array( 'admin/calendars/' . $id . '/viewers/' . $userId . '/add', NULL, '__Add To Viewers__' ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if( $actions ){ |
| 180 |
$actions = $this->ui->helperActionsFromArray( $actions ); |
| 181 |
if( $actions ){ |
| 182 |
$actions = $this->ui->makeListInline( $actions )->separated()->gutter(1); |
| 183 |
$row['actions'] = $actions; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
$rows[] = $row; |
| 188 |
} |
| 189 |
|
| 190 |
$out = $this->ui->makeTable( $header, $rows ); |
| 191 |
|
| 192 |
$this->layout |
| 193 |
->setContent( $out ) |
| 194 |
->setBreadcrumb( $this->self->breadcrumb($model) ) |
| 195 |
->setHeader( $this->self->header($model) ) |
| 196 |
; |
| 197 |
|
| 198 |
$out = $this->layout->render(); |
| 199 |
return $out; |
| 200 |
} |
| 201 |
|
| 202 |
public function header( $model ) |
| 203 |
{ |
| 204 |
$out = '__New Manager__'; |
| 205 |
return $out; |
| 206 |
} |
| 207 |
|
| 208 |
public function breadcrumb( $model ) |
| 209 |
{ |
| 210 |
$calendarId = $model->getId(); |
| 211 |
$calendarTitle = $this->calendarsPresenter->presentTitle( $model ); |
| 212 |
|
| 213 |
$ret = array(); |
| 214 |
$ret['admin'] = array( 'admin', '__Administration__' ); |
| 215 |
$ret['calendars'] = array( 'admin/calendars', '__Calendars__' ); |
| 216 |
$ret['calendars/edit'] = $calendarTitle; |
| 217 |
$ret['calendars/edit/managers'] = array( 'admin/calendars/' . $model->getId() . '/managers', '__Managers__' ); |
| 218 |
|
| 219 |
return $ret; |
| 220 |
} |
| 221 |
} |