Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
UserGroupGridHandler.inc.php
1 <?php
2 
16 // Import the base GridHandler.
17 import('lib.pkp.classes.controllers.grid.GridHandler');
18 import('lib.pkp.classes.controllers.grid.DataObjectGridCellProvider');
19 
20 // Link action & modal classes
21 import('lib.pkp.classes.linkAction.request.AjaxModal');
22 
23 class UserGroupGridHandler extends GridHandler {
24 
26  private $_contextId;
27 
29  private $_userGroup;
30 
31 
35  function UserGroupGridHandler() {
36  parent::GridHandler();
37 
38  $this->addRoleAssignment(
39  array(ROLE_ID_MANAGER),
40  array(
41  'fetchGrid',
42  'fetchCategory',
43  'fetchRow',
44  'addUserGroup',
45  'editUserGroup',
46  'updateUserGroup',
47  'removeUserGroup',
48  'assignStage',
49  'unassignStage'
50  )
51  );
52  }
53 
54  //
55  // Overridden methods from PKPHandler.
56  //
60  function authorize($request, &$args, $roleAssignments) {
61  import('lib.pkp.classes.security.authorization.PkpContextAccessPolicy');
62  $this->addPolicy(new PkpContextAccessPolicy($request, $roleAssignments));
63 
64  $operation = $request->getRequestedOp();
65  $workflowStageRequiredOps = array('assignStage', 'unassignStage');
66  if (in_array($operation, $workflowStageRequiredOps)) {
67  import('lib.pkp.classes.security.authorization.internal.WorkflowStageRequiredPolicy');
68  $this->addPolicy(new WorkflowStageRequiredPolicy($request->getUserVar('stageId')));
69  }
70 
71  $userGroupRequiredOps = array_merge($workflowStageRequiredOps, array('editUserGroup', 'updateUserGroup', 'removeUserGroup'));
72  if (in_array($operation, $userGroupRequiredOps)) {
73  // Validate the user group object.
74  $userGroupId = $request->getUserVar('userGroupId');
75  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
76  $userGroup = $userGroupDao->getById($userGroupId);
77 
78  if (!$userGroup) {
79  fatalError('Invalid user group id!');
80  } else {
81  $this->_userGroup = $userGroup;
82  }
83  }
84 
85  return parent::authorize($request, $args, $roleAssignments);
86  }
87 
91  function initialize($request) {
92  parent::initialize($request);
93 
94  $context = $request->getContext();
95  $this->_contextId = $context->getId();
96 
97  // Load user-related translations.
99  LOCALE_COMPONENT_PKP_USER,
100  LOCALE_COMPONENT_PKP_MANAGER,
101  LOCALE_COMPONENT_APP_MANAGER,
102  LOCALE_COMPONENT_PKP_SUBMISSION
103  );
104 
105  // Basic grid configuration.
106  $this->setTitle('grid.roles.currentRoles');
107  $this->setInstructions('settings.roles.gridDescription');
108 
109  // Add grid-level actions.
110  $router = $request->getRouter();
111  $this->addAction(
112  new LinkAction(
113  'addUserGroup',
114  new AjaxModal(
115  $router->url($request, null, null, 'addUserGroup'),
116  __('grid.roles.add'),
117  'modal_add_role'
118  ),
119  __('grid.roles.add'),
120  'add_role'
121  )
122  );
123 
124  import('lib.pkp.controllers.grid.settings.roles.UserGroupGridCellProvider');
125  $cellProvider = new UserGroupGridCellProvider();
126 
127  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
128  $workflowStagesLocales = $userGroupDao->getWorkflowStageTranslationKeys();
129 
130  // Set array containing the columns info with the same cell provider.
131  $columnsInfo = array(
132  1 => array('id' => 'name', 'title' => 'settings.roles.roleName', 'template' => 'controllers/grid/gridCell.tpl'),
133  2 => array('id' => 'abbrev', 'title' => 'settings.roles.roleAbbrev', 'template' => 'controllers/grid/gridCell.tpl')
134  );
135 
136  foreach ($workflowStagesLocales as $stageId => $stageTitleKey) {
137  $columnsInfo[] = array('id' => $stageId, 'title' => $stageTitleKey, 'template' => 'controllers/grid/common/cell/selectStatusCell.tpl');
138  }
139 
140  // Add array columns to the grid.
141  foreach($columnsInfo as $columnInfo) {
142  $this->addColumn(
143  new GridColumn(
144  $columnInfo['id'], $columnInfo['title'], null,
145  $columnInfo['template'], $cellProvider
146  )
147  );
148  }
149  }
150 
154  function loadData($request, $filter) {
155  $contextId = $this->_getContextId();
156  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
157 
158  $roleIdFilter = null;
159  $stageIdFilter = null;
160 
161  if (!is_array($filter)) {
162  $filter = array();
163  }
164 
165  if (isset($filter['selectedRoleId'])) {
166  $roleIdFilter = $filter['selectedRoleId'];
167  }
168 
169  if (isset($filter['selectedStageId'])) {
170  $stageIdFilter = $filter['selectedStageId'];
171  }
172 
173  $rangeInfo = $this->getGridRangeInfo($request, $this->getId());
174 
175  if ($stageIdFilter && $stageIdFilter != 0) {
176  $userGroups = $userGroupDao->getUserGroupsByStage($contextId, $stageIdFilter, false, false, $roleIdFilter, $rangeInfo);
177  } else if ($roleIdFilter && $roleIdFilter != 0) {
178  $userGroups = $userGroupDao->getByRoleId($contextId, $roleIdFilter, false, $rangeInfo);
179  } else {
180  $userGroups = $userGroupDao->getByContextId($contextId, $rangeInfo);
181  }
182 
183  return $userGroups;
184  }
185 
190  function getRowInstance() {
191  import('lib.pkp.controllers.grid.settings.roles.UserGroupGridRow');
192  return new UserGroupGridRow();
193  }
194 
198  function renderFilter($request) {
199  // Get filter data.
200  import('classes.security.RoleDAO');
201  $roleOptions = array(0 => 'grid.user.allPermissionLevels') + RoleDAO::getRoleNames(true);
202 
203  // Reader roles are not important for stage assignments.
204  if (array_key_exists(ROLE_ID_READER, $roleOptions)) {
205  unset($roleOptions[ROLE_ID_READER]);
206  }
207 
208  $filterData = array('roleOptions' => $roleOptions);
209 
210  $workflowStages = array(0 => 'grid.userGroup.allStages') + UserGroupDao::getWorkflowStageTranslationKeys();
211  $filterData['stageOptions'] = $workflowStages;
212 
213  return parent::renderFilter($request, $filterData);
214  }
215 
220  function getFilterSelectionData($request) {
221  $selectedRoleId = $request->getUserVar('selectedRoleId');
222  $selectedStageId = $request->getUserVar('selectedStageId');
223 
224  // Cast or set to grid filter default value (all roles).
225  $selectedRoleId = (is_null($selectedRoleId) ? 0 : (int)$selectedRoleId);
226  $selectedStageId = (is_null($selectedStageId) ? 0 : (int)$selectedStageId);
227 
228  return array ('selectedRoleId' => $selectedRoleId, 'selectedStageId' => $selectedStageId);
229  }
230 
235  function getFilterForm() {
236  return 'controllers/grid/settings/roles/userGroupsGridFilter.tpl';
237  }
238 
242  function initFeatures($request, $args) {
243  import('lib.pkp.classes.controllers.grid.feature.PagingFeature');
244  return array(new PagingFeature());
245  }
246 
247 
248  //
249  // Handler operations.
250  //
256  function addUserGroup($args, $request) {
257  return $this->editUserGroup($args, $request);
258  }
259 
265  function editUserGroup($args, $request) {
266  $userGroupForm = $this->_getUserGroupForm($request);
267 
268  $userGroupForm->initData();
269 
270  $json = new JSONMessage(true, $userGroupForm->fetch($request));
271  return $json->getString();
272  }
273 
279  function updateUserGroup($args, $request) {
280  $userGroupForm = $this->_getUserGroupForm($request);
281 
282  $userGroupForm->readInputData();
283  if($userGroupForm->validate()) {
284  $userGroupForm->execute($request);
285  return DAO::getDataChangedEvent();
286  } else {
287  $json = new JSONMessage(true, $userGroupForm->fetch($request));
288  return $json->getString();
289  }
290  }
291 
297  function removeUserGroup($args, $request) {
298  $user = $request->getUser();
299  $userGroup = $this->_userGroup;
300  $contextId = $this->_getContextId();
301  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
302  $notificationMgr = new NotificationManager();
303 
304  $usersAssignedToUserGroupCount = $userGroupDao->getContextUsersCount($contextId, $userGroup->getId());
305  if ($usersAssignedToUserGroupCount == 0) {
306  if ($userGroupDao->isDefault($userGroup->getId())) {
307  // Can't delete default user groups.
308  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_WARNING,
309  array('contents' => __('grid.userGroup.cantRemoveDefaultUserGroup',
310  array('userGroupName' => $userGroup->getLocalizedName() )
311  )));
312  } else {
313  // We can delete, no user assigned yet.
314  $userGroupDao->deleteObject($userGroup);
315  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS,
316  array('contents' => __('grid.userGroup.removed',
317  array('userGroupName' => $userGroup->getLocalizedName() )
318  )));
319  }
320  } else {
321  // Can't delete while an user
322  // is still assigned to that user group.
323  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_WARNING,
324  array('contents' => __('grid.userGroup.cantRemoveUserGroup',
325  array('userGroupName' => $userGroup->getLocalizedName() , 'usersCount' => $usersAssignedToUserGroupCount)
326  )));
327 
328  }
329 
330  return DAO::getDataChangedEvent($userGroup->getId());
331  }
332 
338  function assignStage($args, $request) {
339  return $this->_toggleAssignment($args, $request);
340  }
341 
347  function unassignStage($args, $request) {
348  return $this->_toggleAssignment($args, $request);
349  }
350 
351  //
352  // Private helper methods.
353  //
354 
360  private function _toggleAssignment($args, $request) {
361  $userGroup = $this->_userGroup;
362  $stageId = $this->getAuthorizedContextObject(ASSOC_TYPE_WORKFLOW_STAGE);
363  $contextId = $this->_getContextId();
364  $operation = $request->getRequestedOp();
365 
366  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
367 
368  switch($operation) {
369  case 'assignStage':
370  $userGroupDao->assignGroupToStage($contextId, $userGroup->getId(), $stageId);
371  $messageKey = 'grid.userGroup.assignedStage';
372  break;
373  case 'unassignStage':
374  $userGroupDao->removeGroupFromStage($contextId, $userGroup->getId(), $stageId);
375  $messageKey = 'grid.userGroup.unassignedStage';
376  break;
377  }
378 
379  $notificationMgr = new NotificationManager();
380  $user = $request->getUser();
381 
382  $stageLocaleKeys = UserGroupDao::getWorkflowStageTranslationKeys();
383 
384  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS,
385  array('contents' => __($messageKey,
386  array('userGroupName' => $userGroup->getLocalizedName(), 'stageName' => __($stageLocaleKeys[$stageId]))
387  )));
388 
389  return DAO::getDataChangedEvent($userGroup->getId());
390  }
391 
397  private function _getUserGroupForm($request) {
398  // Get the user group Id.
399  $userGroupId = (int) $request->getUserVar('userGroupId');
400 
401  // Instantiate the files form.
402  import('lib.pkp.controllers.grid.settings.roles.form.UserGroupForm');
403  $contextId = $this->_getContextId();
404  return new UserGroupForm($contextId, $userGroupId);
405  }
406 
411  private function _getContextId() {
412  return $this->_contextId;
413  }
414 }
415 
416 ?>
static & getDAO($name, $dbconn=null)
addPolicy($authorizationPolicy, $addToTop=false)
Policy that ensures that the given workflow stage is valid.
Form to add/edit user group.
static requireComponents()
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
addColumn($column)
Class defining basic operations for handling HTML grids.
authorize($request, &$args, $roleAssignments, $enforceRestrictedSite=true)
loadData($request, $filter)
Class to control access to PKP applications&#39; setup components.
addRoleAssignment($roleIds, $operations)
A modal that retrieves its content from via AJAX.
Class to represent a JSON (Javascript Object Notation) message.
static getDataChangedEvent($elementId=null, $parentElementId=null)
Definition: DAO.inc.php:606
User group grid row definition.
renderFilter($request, $filterData=array())
initialize($request, $args=null)
initFeatures($request, &$args)
getFilterSelectionData($request)
Add paging functionality to grids.
Cell provider for columns in a user group grid.
static getRoleNames($contextOnly=false, $roleIds=null)
Definition: RoleDAO.inc.php:45
setInstructions($instructions)
getGridRangeInfo($request, $rangeName, $contextData=null)
Represents a column within a grid. It is used to configure the way cells within a column are displaye...
& getAuthorizedContextObject($assocType)
Base class defining an action that can be performed by the user in the user interface.