Open Journal Systems  3.0.0
 All Data Structures Namespaces Functions Variables Groups Pages
UserGridHandler.inc.php
1 <?php
2 
16 import('lib.pkp.classes.controllers.grid.GridHandler');
17 import('lib.pkp.classes.controllers.grid.DataObjectGridCellProvider');
18 
19 import('lib.pkp.controllers.grid.settings.user.UserGridRow');
20 import('lib.pkp.controllers.grid.settings.user.form.UserDetailsForm');
21 
25 
29  function UserGridHandler() {
30  parent::GridHandler();
31  $this->addRoleAssignment(array(
32  ROLE_ID_MANAGER),
33  array('fetchGrid', 'fetchRow', 'editUser', 'updateUser', 'updateUserRoles',
34  'editDisableUser', 'disableUser', 'removeUser', 'addUser',
35  'editEmail', 'sendEmail', 'mergeUsers')
36  );
37  }
38 
39 
40  //
41  // Implement template methods from PKPHandler.
42  //
46  function authorize($request, &$args, $roleAssignments) {
47  import('lib.pkp.classes.security.authorization.ContextAccessPolicy');
48  $this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
49  return parent::authorize($request, $args, $roleAssignments);
50  }
51 
55  function initialize($request) {
56  parent::initialize($request);
57 
58  // Load user-related translations.
60  LOCALE_COMPONENT_PKP_USER,
61  LOCALE_COMPONENT_PKP_MANAGER,
62  LOCALE_COMPONENT_APP_MANAGER
63  );
64 
65  $this->_oldUserId = (int) $request->getUserVar('oldUserId');
66  // Basic grid configuration.
67  $this->setTitle('grid.user.currentUsers');
68 
69  // Grid actions.
70  $router = $request->getRouter();
71 
72  import('lib.pkp.classes.linkAction.request.AjaxModal');
73  $this->addAction(
74  new LinkAction(
75  'addUser',
76  new AjaxModal(
77  $router->url($request, null, null, 'addUser', null, null),
78  __('grid.user.add'),
79  'modal_add_user',
80  true
81  ),
82  __('grid.user.add'),
83  'add_user')
84  );
85 
86  //
87  // Grid columns.
88  //
89 
90  // First Name.
91  $cellProvider = new DataObjectGridCellProvider();
92  $this->addColumn(
93  new GridColumn(
94  'firstName',
95  'user.firstName',
96  null,
97  null,
98  $cellProvider
99  )
100  );
101 
102  // Last Name.
103  $cellProvider = new DataObjectGridCellProvider();
104  $this->addColumn(
105  new GridColumn(
106  'lastName',
107  'user.lastName',
108  null,
109  null,
110  $cellProvider
111  )
112  );
113 
114  // User name.
115  $cellProvider = new DataObjectGridCellProvider();
116  $this->addColumn(
117  new GridColumn(
118  'username',
119  'user.username',
120  null,
121  null,
122  $cellProvider
123  )
124  );
125 
126  // Email.
127  $cellProvider = new DataObjectGridCellProvider();
128  $this->addColumn(
129  new GridColumn(
130  'email',
131  'user.email',
132  null,
133  null,
134  $cellProvider
135  )
136  );
137  }
138 
139 
140  //
141  // Implement methods from GridHandler.
142  //
147  protected function getRowInstance() {
148  return new UserGridRow($this->_oldUserId);
149  }
150 
154  function initFeatures($request, $args) {
155  import('lib.pkp.classes.controllers.grid.feature.PagingFeature');
156  return array(new PagingFeature());
157  }
158 
164  protected function loadData($request, $filter) {
165  // Get the context.
166  $context = $request->getContext();
167 
168  // Get all users for this context that match search criteria.
169  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
170  $rangeInfo = $this->getGridRangeInfo($request, $this->getId());
171 
172  return $userGroupDao->getUsersById(
173  $filter['userGroup'],
174  $filter['includeNoRole']?null:$context->getId(),
175  $filter['searchField'],
176  $filter['search']?$filter['search']:null,
177  $filter['searchMatch'],
178  $rangeInfo
179  );
180  }
181 
185  function renderFilter($request) {
186  $context = $request->getContext();
187  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
188  $userGroups = $userGroupDao->getByContextId($context->getId());
189  $userGroupOptions = array('' => __('grid.user.allRoles'));
190  while ($userGroup = $userGroups->next()) {
191  $userGroupOptions[$userGroup->getId()] = $userGroup->getLocalizedName();
192  }
193 
194  // Import PKPUserDAO to define the USER_FIELD_* constants.
195  import('lib.pkp.classes.user.PKPUserDAO');
196  $fieldOptions = array(
197  USER_FIELD_FIRSTNAME => 'user.firstName',
198  USER_FIELD_LASTNAME => 'user.lastName',
199  USER_FIELD_USERNAME => 'user.username',
200  USER_FIELD_EMAIL => 'user.email'
201  );
202 
203  $matchOptions = array(
204  'contains' => 'form.contains',
205  'is' => 'form.is'
206  );
207 
208  $filterData = array(
209  'userGroupOptions' => $userGroupOptions,
210  'fieldOptions' => $fieldOptions,
211  'matchOptions' => $matchOptions
212  );
213 
214  return parent::renderFilter($request, $filterData);
215  }
216 
221  function getFilterSelectionData($request) {
222  // Get the search terms.
223  $includeNoRole = $request->getUserVar('includeNoRole') ? (int) $request->getUserVar('includeNoRole') : null;
224  $userGroup = $request->getUserVar('userGroup') ? (int)$request->getUserVar('userGroup') : null;
225  $searchField = $request->getUserVar('searchField');
226  $searchMatch = $request->getUserVar('searchMatch');
227  $search = $request->getUserVar('search');
228 
229  return $filterSelectionData = array(
230  'includeNoRole' => $includeNoRole,
231  'userGroup' => $userGroup,
232  'searchField' => $searchField,
233  'searchMatch' => $searchMatch,
234  'search' => $search ? $search : ''
235  );
236  }
237 
242  protected function getFilterForm() {
243  return 'controllers/grid/settings/user/userGridFilter.tpl';
244  }
245 
246 
247  //
248  // Public grid actions.
249  //
255  function addUser($args, $request) {
256  // Calling editUser with an empty row id will add a new user.
257  return $this->editUser($args, $request);
258  }
259 
266  function editUser($args, $request) {
267  // Identify the user Id.
268  $userId = $request->getUserVar('rowId');
269  if (!$userId) $userId = $request->getUserVar('userId');
270 
271  $user = $request->getUser();
272  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
273  // We don't have administrative rights over this user.
274  return new JSONMessage(false, __('grid.user.cannotAdminister'));
275  } else {
276  // Form handling.
277  $userForm = new UserDetailsForm($request, $userId);
278  $userForm->initData($args, $request);
279 
280  return new JSONMessage(true, $userForm->display($args, $request));
281  }
282  }
283 
290  function updateUser($args, $request) {
291  $user = $request->getUser();
292 
293  // Identify the user Id.
294  $userId = $request->getUserVar('userId');
295 
296  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
297  // We don't have administrative rights over this user.
298  return new JSONMessage(false, __('grid.user.cannotAdminister'));
299  }
300 
301  // Form handling.
302  $userForm = new UserDetailsForm($request, $userId);
303  $userForm->readInputData();
304 
305  if ($userForm->validate()) {
306  $user = $userForm->execute($args, $request);
307 
308  // If this is a newly created user, show role management form.
309  if (!$userId) {
310  import('lib.pkp.controllers.grid.settings.user.form.UserRoleForm');
311  $userRoleForm = new UserRoleForm($user->getId(), $user->getFullName());
312  $userRoleForm->initData($args, $request);
313  return new JSONMessage(true, $userRoleForm->display($args, $request));
314  } else {
315 
316  // Successful edit of an existing user.
317  $notificationManager = new NotificationManager();
318  $user = $request->getUser();
319  $notificationManager->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('notification.editedUser')));
320 
321  // Prepare the grid row data.
322  return DAO::getDataChangedEvent($userId);
323  }
324  } else {
325  return new JSONMessage(false);
326  }
327  }
328 
335  function updateUserRoles($args, $request) {
336  $user = $request->getUser();
337 
338  // Identify the user Id.
339  $userId = $request->getUserVar('userId');
340 
341  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
342  // We don't have administrative rights over this user.
343  return new JSONMessage(false, __('grid.user.cannotAdminister'));
344  }
345 
346  // Form handling.
347  import('lib.pkp.controllers.grid.settings.user.form.UserRoleForm');
348  $userRoleForm = new UserRoleForm($userId, $user->getFullName());
349  $userRoleForm->readInputData();
350 
351  if ($userRoleForm->validate()) {
352  $userRoleForm->execute($args, $request);
353 
354  // Successfully managed newly created user's roles.
355  return DAO::getDataChangedEvent($userId);
356  } else {
357  return new JSONMessage(false);
358  }
359  }
360 
367  function editDisableUser($args, $request) {
368  $user = $request->getUser();
369 
370  // Identify the user Id.
371  $userId = $request->getUserVar('rowId');
372  if (!$userId) $userId = $request->getUserVar('userId');
373 
374  // Are we enabling or disabling this user.
375  $enable = isset($args['enable']) ? (bool) $args['enable'] : false;
376 
377  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
378  // We don't have administrative rights over this user.
379  return new JSONMessage(false, __('grid.user.cannotAdminister'));
380  } else {
381  // Form handling
382  import('lib.pkp.controllers.grid.settings.user.form.UserDisableForm');
383  $userForm = new UserDisableForm($userId, $enable);
384 
385  $userForm->initData($args, $request);
386 
387  return new JSONMessage(true, $userForm->display($args, $request));
388  }
389  }
390 
397  function disableUser($args, $request) {
398  $user = $request->getUser();
399 
400  // Identify the user Id.
401  $userId = $request->getUserVar('userId');
402 
403  // Are we enabling or disabling this user.
404  $enable = (bool) $request->getUserVar('enable');
405 
406  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
407  // We don't have administrative rights over this user.
408  return new JSONMessage(false, __('grid.user.cannotAdminister'));
409  }
410 
411  // Form handling.
412  import('lib.pkp.controllers.grid.settings.user.form.UserDisableForm');
413  $userForm = new UserDisableForm($userId, $enable);
414 
415  $userForm->readInputData();
416 
417  if ($userForm->validate()) {
418  $user = $userForm->execute($args, $request);
419 
420  // Successful enable/disable of an existing user.
421  // Update grid data.
422  return DAO::getDataChangedEvent($userId);
423 
424  } else {
425  return new JSONMessage(false, $userForm->display($args, $request));
426  }
427  }
428 
435  function removeUser($args, $request) {
436  $context = $request->getContext();
437  $user = $request->getUser();
438 
439  // Identify the user Id.
440  $userId = $request->getUserVar('rowId');
441 
442  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
443  // We don't have administrative rights over this user.
444  return new JSONMessage(false, __('grid.user.cannotAdminister'));
445  }
446 
447  // Remove user from all user group assignments for this context.
448  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
449 
450  // Check if this user has any user group assignments for this context.
451  if (!$userGroupDao->userInAnyGroup($userId, $context->getId())) {
452  return new JSONMessage(false, __('grid.user.userNoRoles'));
453  } else {
454  $userGroupDao->deleteAssignmentsByContextId($context->getId(), $userId);
455  return DAO::getDataChangedEvent($userId);
456  }
457  }
458 
465  function editEmail($args, $request) {
466  $user = $request->getUser();
467 
468  // Identify the user Id.
469  $userId = $request->getUserVar('rowId');
470 
471  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
472  // We don't have administrative rights over this user.
473  return new JSONMessage(false, __('grid.user.cannotAdminister'));
474  } else {
475  // Form handling.
476  import('lib.pkp.controllers.grid.settings.user.form.UserEmailForm');
477  $userEmailForm = new UserEmailForm($userId);
478  $userEmailForm->initData();
479 
480  return new JSONMessage(true, $userEmailForm->fetch($args, $request));
481  }
482  }
483 
490  function sendEmail($args, $request) {
491  $user = $request->getUser();
492 
493  // Identify the user Id.
494  $userId = $request->getUserVar('userId');
495 
496  if ($userId !== null && !Validation::canAdminister($userId, $user->getId())) {
497  // We don't have administrative rights over this user.
498  return new JSONMessage(false, __('grid.user.cannotAdminister'));
499  }
500  // Form handling.
501  import('lib.pkp.controllers.grid.settings.user.form.UserEmailForm');
502  $userEmailForm = new UserEmailForm($userId);
503  $userEmailForm->readInputData();
504 
505  if ($userEmailForm->validate()) {
506  $userEmailForm->execute($args, $request);
507  return new JSONMessage(true);
508  } else {
509  return new JSONMessage(false, $userEmailForm->fetch($args, $request));
510  }
511  }
512 
519  function mergeUsers($args, $request) {
520 
521  // if there is a $newUserId, this is the second time through, so merge the users.
522  $newUserId = (int) $request->getUserVar('newUserId');
523  $oldUserId = (int) $request->getUserVar('oldUserId');
524  $user = $request->getUser();
525  if ($newUserId > 0 && $oldUserId > 0 && Validation::canAdminister($oldUserId, $user->getId())) {
526  import('classes.user.UserAction');
527  $userAction = new UserAction();
528  $userAction->mergeUsers($oldUserId, $newUserId);
529  return DAO::getDataChangedEvent();
530  } else {
531  // The grid shouldn't have presented an action in this
532  // case.
533  return new JSONMessage(false, __('grid.user.cannotAdminister'));
534  }
535  }
536 
540  function getRequestArgs() {
541  $requestArgs = (array) parent::getRequestArgs();
542  $requestArgs['oldUserId'] = $this->_oldUserId;
543  return $requestArgs;
544  }
545 }
546 
547 ?>
static & getDAO($name, $dbconn=null)
addPolicy($authorizationPolicy, $addToTop=false)
Form for enabling/disabling a user.
initFeatures($request, $args)
loadData($request, $filter)
static requireComponents()
authorize($request, &$args, $roleAssignments)
Form for managing roles for a newly created user.
updateUser($args, $request)
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
addColumn($column)
editDisableUser($args, $request)
This class defines basic operations for handling HTML grids. Grids are used to implement a standardiz...
initData()
Definition: Form.inc.php:229
User grid row definition.
addRoleAssignment($roleIds, $operations)
A modal that retrieves its content from via AJAX.
mergeUsers($args, $request)
Class to represent a JSON (Javascript Object Notation) message.
sendEmail($args, $request)
static canAdminister($administeredUserId, $administratorUserId)
updateUserRoles($args, $request)
Handle user grid requests.
Class to control access to PKP applications&#39; setup components.
UserAction class.
Form for editing user profiles.
Add paging functionality to grids.
Base class for a cell provider that can retrieve simple labels from DataObjects. If you need more com...
removeUser($args, $request)
Form for sending an email to a user.
getGridRangeInfo($request, $rangeName, $contextData=null)
getFilterSelectionData($request)
editEmail($args, $request)
editUser($args, $request)
The GridColumn class represents a column within a grid. It is used to format the data presented in a ...
disableUser($args, $request)
addUser($args, $request)
static getDataChangedEvent($elementId=null, $parentElementId=null, $content= '')
Definition: DAO.inc.php:633
Base class defining an action that can be performed by the user in the user interface.