Open Journal Systems  3.0.0
 All Data Structures 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 import('lib.pkp.classes.workflow.WorkflowStageDAO');
20 
21 // Link action & modal classes
22 import('lib.pkp.classes.linkAction.request.AjaxModal');
23 
24 class UserGroupGridHandler extends GridHandler {
25 
27  private $_contextId;
28 
30  private $_userGroup;
31 
32 
36  function UserGroupGridHandler() {
37  parent::GridHandler();
38 
39  $this->addRoleAssignment(
40  array(ROLE_ID_MANAGER),
41  array(
42  'fetchGrid',
43  'fetchCategory',
44  'fetchRow',
45  'addUserGroup',
46  'editUserGroup',
47  'updateUserGroup',
48  'removeUserGroup',
49  'assignStage',
50  'unassignStage'
51  )
52  );
53  }
54 
55  //
56  // Overridden methods from PKPHandler.
57  //
61  function authorize($request, &$args, $roleAssignments) {
62  import('lib.pkp.classes.security.authorization.ContextAccessPolicy');
63  $this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
64 
65  $operation = $request->getRequestedOp();
66  $workflowStageRequiredOps = array('assignStage', 'unassignStage');
67  if (in_array($operation, $workflowStageRequiredOps)) {
68  import('lib.pkp.classes.security.authorization.internal.WorkflowStageRequiredPolicy');
69  $this->addPolicy(new WorkflowStageRequiredPolicy($request->getUserVar('stageId')));
70  }
71 
72  $userGroupRequiredOps = array_merge($workflowStageRequiredOps, array('editUserGroup', 'updateUserGroup', 'removeUserGroup'));
73  if (in_array($operation, $userGroupRequiredOps)) {
74  // Validate the user group object.
75  $userGroupId = $request->getUserVar('userGroupId');
76  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
77  $userGroup = $userGroupDao->getById($userGroupId);
78 
79  if (!$userGroup) {
80  fatalError('Invalid user group id!');
81  } else {
82  $this->_userGroup = $userGroup;
83  }
84  }
85 
86  return parent::authorize($request, $args, $roleAssignments);
87  }
88 
92  function initialize($request) {
93  parent::initialize($request);
94 
95  $context = $request->getContext();
96  $this->_contextId = $context->getId();
97 
98  // Load user-related translations.
100  LOCALE_COMPONENT_PKP_USER,
101  LOCALE_COMPONENT_PKP_MANAGER,
102  LOCALE_COMPONENT_APP_MANAGER,
103  LOCALE_COMPONENT_PKP_SUBMISSION
104  );
105 
106  // Basic grid configuration.
107  $this->setTitle('grid.roles.currentRoles');
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  $workflowStagesLocales = WorkflowStageDAO::getWorkflowStageTranslationKeys();
128 
129  // Set array containing the columns info with the same cell provider.
130  $columnsInfo = array(
131  1 => array('id' => 'name', 'title' => 'settings.roles.roleName', 'template' => null),
132  2 => array('id' => 'abbrev', 'title' => 'settings.roles.roleAbbrev', 'template' => null)
133  );
134 
135  foreach ($workflowStagesLocales as $stageId => $stageTitleKey) {
136  $columnsInfo[] = array('id' => $stageId, 'title' => $stageTitleKey, 'template' => 'controllers/grid/common/cell/selectStatusCell.tpl');
137  }
138 
139  // Add array columns to the grid.
140  foreach($columnsInfo as $columnInfo) {
141  $this->addColumn(
142  new GridColumn(
143  $columnInfo['id'], $columnInfo['title'], null,
144  $columnInfo['template'], $cellProvider
145  )
146  );
147  }
148  }
149 
153  protected function loadData($request, $filter) {
154  $contextId = $this->_getContextId();
155  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
156 
157  $roleIdFilter = null;
158  $stageIdFilter = null;
159 
160  if (!is_array($filter)) {
161  $filter = array();
162  }
163 
164  if (isset($filter['selectedRoleId'])) {
165  $roleIdFilter = $filter['selectedRoleId'];
166  }
167 
168  if (isset($filter['selectedStageId'])) {
169  $stageIdFilter = $filter['selectedStageId'];
170  }
171 
172  $rangeInfo = $this->getGridRangeInfo($request, $this->getId());
173 
174  if ($stageIdFilter && $stageIdFilter != 0) {
175  return $userGroupDao->getUserGroupsByStage($contextId, $stageIdFilter, false, false, $roleIdFilter, $rangeInfo);
176  } else if ($roleIdFilter && $roleIdFilter != 0) {
177  return $userGroupDao->getByRoleId($contextId, $roleIdFilter, false, $rangeInfo);
178  } else {
179  return $userGroupDao->getByContextId($contextId, $rangeInfo);
180  }
181  }
182 
187  protected function getRowInstance() {
188  import('lib.pkp.controllers.grid.settings.roles.UserGroupGridRow');
189  return new UserGroupGridRow();
190  }
191 
195  function renderFilter($request) {
196  // Get filter data.
197  import('classes.security.RoleDAO');
198  $roleOptions = array(0 => 'grid.user.allPermissionLevels') + RoleDAO::getRoleNames(true);
199 
200  // Reader roles are not important for stage assignments.
201  if (array_key_exists(ROLE_ID_READER, $roleOptions)) {
202  unset($roleOptions[ROLE_ID_READER]);
203  }
204 
205  $filterData = array('roleOptions' => $roleOptions);
206 
207  $workflowStages = array(0 => 'grid.userGroup.allStages') + WorkflowStageDAO::getWorkflowStageTranslationKeys();
208  $filterData['stageOptions'] = $workflowStages;
209 
210  return parent::renderFilter($request, $filterData);
211  }
212 
217  function getFilterSelectionData($request) {
218  $selectedRoleId = $request->getUserVar('selectedRoleId');
219  $selectedStageId = $request->getUserVar('selectedStageId');
220 
221  // Cast or set to grid filter default value (all roles).
222  $selectedRoleId = (is_null($selectedRoleId) ? 0 : (int)$selectedRoleId);
223  $selectedStageId = (is_null($selectedStageId) ? 0 : (int)$selectedStageId);
224 
225  return array ('selectedRoleId' => $selectedRoleId, 'selectedStageId' => $selectedStageId);
226  }
227 
232  protected function getFilterForm() {
233  return 'controllers/grid/settings/roles/userGroupsGridFilter.tpl';
234  }
235 
239  function initFeatures($request, $args) {
240  import('lib.pkp.classes.controllers.grid.feature.PagingFeature');
241  return array(new PagingFeature());
242  }
243 
244 
245  //
246  // Handler operations.
247  //
253  function addUserGroup($args, $request) {
254  return $this->editUserGroup($args, $request);
255  }
256 
262  function editUserGroup($args, $request) {
263  $userGroupForm = $this->_getUserGroupForm($request);
264 
265  $userGroupForm->initData();
266 
267  return new JSONMessage(true, $userGroupForm->fetch($request));
268  }
269 
276  function updateUserGroup($args, $request) {
277  $userGroupForm = $this->_getUserGroupForm($request);
278 
279  $userGroupForm->readInputData();
280  if($userGroupForm->validate()) {
281  $userGroupForm->execute($request);
282  return DAO::getDataChangedEvent();
283  } else {
284  return new JSONMessage(true, $userGroupForm->fetch($request));
285  }
286  }
287 
294  function removeUserGroup($args, $request) {
295  $user = $request->getUser();
296  $userGroup = $this->_userGroup;
297  $contextId = $this->_getContextId();
298  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
299  $notificationMgr = new NotificationManager();
300 
301  $usersAssignedToUserGroupCount = $userGroupDao->getContextUsersCount($contextId, $userGroup->getId());
302  if ($usersAssignedToUserGroupCount == 0) {
303  if ($userGroupDao->isDefault($userGroup->getId())) {
304  // Can't delete default user groups.
305  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_WARNING,
306  array('contents' => __('grid.userGroup.cantRemoveDefaultUserGroup',
307  array('userGroupName' => $userGroup->getLocalizedName() )
308  )));
309  } else {
310  // We can delete, no user assigned yet.
311  $userGroupDao->deleteObject($userGroup);
312  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS,
313  array('contents' => __('grid.userGroup.removed',
314  array('userGroupName' => $userGroup->getLocalizedName() )
315  )));
316  }
317  } else {
318  // Can't delete while an user
319  // is still assigned to that user group.
320  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_WARNING,
321  array('contents' => __('grid.userGroup.cantRemoveUserGroup',
322  array('userGroupName' => $userGroup->getLocalizedName() , 'usersCount' => $usersAssignedToUserGroupCount)
323  )));
324 
325  }
326 
327  return DAO::getDataChangedEvent($userGroup->getId());
328  }
329 
335  function assignStage($args, $request) {
336  return $this->_toggleAssignment($args, $request);
337  }
338 
344  function unassignStage($args, $request) {
345  return $this->_toggleAssignment($args, $request);
346  }
347 
348  //
349  // Private helper methods.
350  //
351 
358  private function _toggleAssignment($args, $request) {
359  $userGroup = $this->_userGroup;
360  $stageId = $this->getAuthorizedContextObject(ASSOC_TYPE_WORKFLOW_STAGE);
361  $contextId = $this->_getContextId();
362  $operation = $request->getRequestedOp();
363 
364  $userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /* @var $userGroupDao UserGroupDAO */
365 
366  switch($operation) {
367  case 'assignStage':
368  $userGroupDao->assignGroupToStage($contextId, $userGroup->getId(), $stageId);
369  $messageKey = 'grid.userGroup.assignedStage';
370  break;
371  case 'unassignStage':
372  $userGroupDao->removeGroupFromStage($contextId, $userGroup->getId(), $stageId);
373  $messageKey = 'grid.userGroup.unassignedStage';
374  break;
375  }
376 
377  $notificationMgr = new NotificationManager();
378  $user = $request->getUser();
379 
381 
382  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS,
383  array('contents' => __($messageKey,
384  array('userGroupName' => $userGroup->getLocalizedName(), 'stageName' => __($stageLocaleKeys[$stageId]))
385  )));
386 
387  return DAO::getDataChangedEvent($userGroup->getId());
388  }
389 
395  private function _getUserGroupForm($request) {
396  // Get the user group Id.
397  $userGroupId = (int) $request->getUserVar('userGroupId');
398 
399  // Instantiate the files form.
400  import('lib.pkp.controllers.grid.settings.roles.form.UserGroupForm');
401  $contextId = $this->_getContextId();
402  return new UserGroupForm($contextId, $userGroupId);
403  }
404 
409  private function _getContextId() {
410  return $this->_contextId;
411  }
412 }
413 
414 ?>
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)
This class defines basic operations for handling HTML grids. Grids are used to implement a standardiz...
authorize($request, &$args, $roleAssignments, $enforceRestrictedSite=true)
loadData($request, $filter)
addRoleAssignment($roleIds, $operations)
A modal that retrieves its content from via AJAX.
Class to represent a JSON (Javascript Object Notation) message.
User group grid row definition.
Class to control access to PKP applications&#39; setup components.
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:42
getGridRangeInfo($request, $rangeName, $contextData=null)
static getWorkflowStageTranslationKeys()
The GridColumn class represents a column within a grid. It is used to format the data presented in a ...
static getDataChangedEvent($elementId=null, $parentElementId=null, $content= '')
Definition: DAO.inc.php:633
& getAuthorizedContextObject($assocType)
Base class defining an action that can be performed by the user in the user interface.