Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
UserGroupForm.inc.php
1 <?php
2 
16 import('lib.pkp.classes.form.Form');
17 
18 class UserGroupForm extends Form {
19 
21  var $_userGroupId;
22 
25 
26 
32  function UserGroupForm($contextId, $userGroupId = null) {
33  parent::Form('controllers/grid/settings/roles/form/userGroupForm.tpl');
34  $this->_contextId = $contextId;
35  $this->_userGroupId = $userGroupId;
36 
37  // Validation checks for this form
38  $this->addCheck(new FormValidatorLocale($this, 'name', 'required', 'settings.roles.nameRequired'));
39  $this->addCheck(new FormValidatorLocale($this, 'abbrev', 'required', 'settings.roles.abbrevRequired'));
40  $this->addCheck(new FormValidatorArray($this, 'assignedStages', 'required', 'settings.roles.stageIdRequired'));
41  if ($this->getUserGroupId() == null) {
42  $this->addCheck(new FormValidator($this, 'roleId', 'required', 'settings.roles.roleIdRequired'));
43  }
44  $this->addCheck(new FormValidatorPost($this));
45  }
46 
47  //
48  // Getters and Setters
49  //
54  function getUserGroupId() {
55  return $this->_userGroupId;
56  }
57 
62  function getContextId() {
63  return $this->_contextId;
64  }
65 
66  //
67  // Implement template methods from Form.
68  //
72  function getLocaleFieldNames() {
73  return array('name', 'abbrev');
74  }
75 
79  function initData() {
80  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
81  $userGroup = $userGroupDao->getById($this->getUserGroupId());
82  $stages = $userGroupDao->getWorkflowStageTranslationKeys();
83  $this->setData('stages', $stages);
84  $this->setData('assignedStages', array()); // sensible default
85 
86  if ($userGroup) {
87  $assignedStages = $userGroupDao->getAssignedStagesByUserGroupId($this->getContextId(), $userGroup->getId());
88 
89  $data = array(
90  'userGroupId' => $userGroup->getId(),
91  'roleId' => $userGroup->getRoleId(),
92  'name' => $userGroup->getName(null), //Localized
93  'abbrev' => $userGroup->getAbbrev(null), //Localized
94  'assignedStages' => array_keys($assignedStages),
95  'showTitle' => $userGroup->getShowTitle(),
96  'permitSelfRegistration' => $userGroup->getPermitSelfRegistration(),
97  );
98  foreach ($data as $field => $value) {
99  $this->setData($field, $value);
100  }
101  }
102  }
103 
107  function readInputData() {
108  $this->readUserVars(array('roleId', 'name', 'abbrev', 'assignedStages', 'showTitle', 'permitSelfRegistration'));
109  }
110 
114  function fetch($request) {
115  $templateMgr = TemplateManager::getManager($request);
116 
117  import('classes.security.RoleDAO');
118  $roleOptions = RoleDAO::getRoleNames(true);
119  $templateMgr->assign('roleOptions', $roleOptions);
121  // Users can't edit the role once user group is created.
122  // userGroupId is 0 for new User Groups because it is cast to int in UserGroupGridHandler.
123  $disableRoleSelect = ($this->getUserGroupId() > 0) ? true : false;
124  $templateMgr->assign('disableRoleSelect', $disableRoleSelect);
125  $templateMgr->assign('selfRegistrationRoleIds', $this->getPermitSelfRegistrationRoles());
126 
127  return parent::fetch($request);
128  }
129 
134  function getPermitSelfRegistrationRoles() {
135  return array(ROLE_ID_REVIEWER, ROLE_ID_AUTHOR);
136  }
137 
141  function execute($request) {
142  $userGroupId = $this->getUserGroupId();
143  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
144 
145  // Check if we are editing an existing user group or creating another one.
146  if ($userGroupId == null) {
147  $userGroup = $userGroupDao->newDataObject();
148  $role = new Role($this->getData('roleId'));
149  $userGroup->setRoleId($role->getId());
150  $userGroup->setContextId($this->getContextId());
151  $userGroup->setPath($role->getPath());
152  $userGroup->setDefault(false);
153  $userGroup->setShowTitle($this->getData('showTitle'));
154  $userGroup->setPermitSelfRegistration($this->getData('permitSelfRegistration') && in_array($role->getId(), $this->getPermitSelfRegistrationRoles()));
155  $userGroup = $this->_setUserGroupLocaleFields($userGroup, $request);
156  $userGroupId = $userGroupDao->insertObject($userGroup);
157  } else {
158  $userGroup = $userGroupDao->getById($userGroupId);
159  $userGroup = $this->_setUserGroupLocaleFields($userGroup, $request);
160  $userGroup->setShowTitle($this->getData('showTitle'));
161  $userGroup->setPermitSelfRegistration($this->getData('permitSelfRegistration') && in_array($userGroup->getRoleId(), $this->getPermitSelfRegistrationRoles()));
162  $userGroupDao->updateObject($userGroup);
163  }
164 
165  // After we have created/edited the user group, we assign/update its stages.
166  if ($this->getData('assignedStages')) {
167  $this->_assignStagesToUserGroup($userGroupId, $this->getData('assignedStages'));
168  }
169  }
170 
171 
172  //
173  // Private helper methods
174  //
180  function _assignStagesToUserGroup($userGroupId, $userAssignedStages) {
181  $contextId = $this->getContextId();
182  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
183 
184  // Current existing workflow stages.
185  $stages = $userGroupDao->getWorkflowStageTranslationKeys();
187  foreach (array_keys($stages) as $stageId) {
188  $userGroupDao->removeGroupFromStage($contextId, $userGroupId, $stageId);
189  }
190 
191  foreach ($userAssignedStages as $stageId) {
192 
193  // Check if is a valid stage.
194  if (in_array($stageId, array_keys($stages))) {
195  $userGroupDao->assignGroupToStage($contextId, $userGroupId, $stageId);
196  } else {
197  fatalError('Invalid stage id');
198  }
199  }
200  }
201 
208  function _setUserGroupLocaleFields($userGroup, $request) {
209  $router = $request->getRouter();
210  $context = $router->getContext($request);
211  $supportedLocales = $context->getSupportedLocaleNames();
212 
213  if (!empty($supportedLocales)) {
214  foreach ($context->getSupportedLocaleNames() as $localeKey => $localeName) {
215  $name = $this->getData('name');
216  $abbrev = $this->getData('abbrev');
217  $userGroup->setName($name[$localeKey], $localeKey);
218  $userGroup->setAbbrev($abbrev[$localeKey], $localeKey);
219  }
220  } else {
221  $localeKey = AppLocale::getLocale();
222  $userGroup->setName($this->getData('name'), $localeKey);
223  $userGroup->setAbbrev($this->getData('abbrev'), $localeKey);
224  }
225 
226  return $userGroup;
227  }
228 }
229 
230 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:396
Form to add/edit user group.
setData($key, $value)
Definition: Form.inc.php:219
Form validation check to make sure the form is POSTed.
readUserVars($vars)
Definition: Form.inc.php:364
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
UserGroupForm($contextId, $userGroupId=null)
Describes user roles within the system and the associated permissions.
Definition: Role.inc.php:26
getData($key)
Definition: Form.inc.php:210
static getLocale()
Class to represent a form validation check for localized fields.
$supportedLocales
Definition: Form.inc.php:83
static getRoleNames($contextOnly=false, $roleIds=null)
Definition: RoleDAO.inc.php:69
_assignStagesToUserGroup($userGroupId, $userAssignedStages)
Form validation check that checks an array of fields.
Class to represent a form validation check.
_setUserGroupLocaleFields($userGroup, $request)