• Main Page
  • Modules
  • Classes
  • Files
  • File List

controllers/grid/settings/roles/form/UserGroupForm.inc.php

00001 <?php
00002 
00015 import('lib.pkp.classes.form.Form');
00016 
00017 class UserGroupForm extends Form {
00018 
00020    var $_userGroupId;
00021 
00023    var $_pressId;
00024 
00025 
00031    function UserGroupForm($pressId, $userGroupId = null) {
00032       parent::Form('controllers/grid/settings/roles/form/userGroupForm.tpl');
00033       $this->_pressId = $pressId;
00034       $this->_userGroupId = $userGroupId;
00035 
00036       // Validation checks for this form
00037       $this->addCheck(new FormValidatorLocale($this, 'name', 'required', 'settings.roles.nameRequired'));
00038       $this->addCheck(new FormValidatorLocale($this, 'abbrev', 'required', 'settings.roles.abbrevRequired'));
00039       $this->addCheck(new FormValidatorArray($this, 'assignedStages', 'required', 'settings.roles.stageIdRequired'));
00040       if ($this->getUserGroupId() == null) {
00041          $this->addCheck(new FormValidator($this, 'roleId', 'required', 'settings.roles.roleIdRequired'));
00042       }
00043       $this->addCheck(new FormValidatorPost($this));
00044    }
00045 
00046    //
00047    // Getters and Setters
00048    //
00053    function getUserGroupId() {
00054       return $this->_userGroupId;
00055    }
00056 
00061    function getPressId() {
00062       return $this->_pressId;
00063    }
00064 
00065    //
00066    // Implement template methods from Form.
00067    //
00071    function getLocaleFieldNames() {
00072       return array('name', 'abbrev');
00073    }
00074 
00078    function initData() {
00079       $userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
00080       $userGroup = $userGroupDao->getById($this->getUserGroupId());
00081       $stages = $userGroupDao->getWorkflowStageTranslationKeys();
00082       $this->setData('stages', $stages);
00083       $this->setData('assignedStages', array()); // sensible default
00084 
00085       if ($userGroup) {
00086          $assignedStages =& $userGroupDao->getAssignedStagesByUserGroupId($this->getPressId(), $userGroup->getId());
00087 
00088          $data = array(
00089             'userGroupId' => $userGroup->getId(),
00090             'roleId' => $userGroup->getRoleId(),
00091             'name' => $userGroup->getName(null), //Localized
00092             'abbrev' => $userGroup->getAbbrev(null), //Localized
00093             'assignedStages' => array_keys($assignedStages),
00094          );
00095          foreach ($data as $field => $value) {
00096             $this->setData($field, $value);
00097          }
00098       }
00099    }
00100 
00104    function readInputData() {
00105       $this->readUserVars(array('roleId', 'name', 'abbrev', 'assignedStages'));
00106    }
00107 
00111    function fetch(&$request) {
00112       $templateMgr =& TemplateManager::getManager();
00113 
00114       import('classes.security.RoleDAO');
00115       $roleOptions = RoleDAO::getRoleNames(true);
00116       $templateMgr->assign('roleOptions', $roleOptions);
00117 
00118       // Users can't edit the role once user group is created.
00119       // userGroupId is 0 for new User Groups because it is cast to int in UserGroupGridHandler.
00120       $disableRoleSelect = ($this->getUserGroupId() > 0) ? true : false;
00121       $templateMgr->assign('disableRoleSelect', $disableRoleSelect);
00122 
00123       return parent::fetch($request);
00124    }
00125 
00129    function execute(&$request) {
00130       $userGroupId = $this->getUserGroupId();
00131       $userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
00132 
00133       // Check if we are editing an existing user group or creating another one.
00134       if ($userGroupId == null) {
00135          $userGroup = $userGroupDao->newDataObject();
00136          $role = new Role($this->getData('roleId'));
00137          $userGroup->setRoleId($role->getId());
00138          $userGroup->setContextId($this->getPressId());
00139          $userGroup->setPath($role->getPath());
00140          $userGroup->setDefault(false);
00141          $userGroup = $this->_setUserGroupLocaleFields($userGroup, $request);
00142          $userGroupId = $userGroupDao->insertUserGroup($userGroup);
00143       } else {
00144          $userGroup = $userGroupDao->getById($userGroupId);
00145          $userGroup = $this->_setUserGroupLocaleFields($userGroup, $request);
00146          $userGroupDao->updateLocaleFields($userGroup);
00147       }
00148 
00149       // After we have created/edited the user group, we assign/update its stages.
00150       if ($this->getData('assignedStages')) {
00151          $this->_assignStagesToUserGroup($userGroupId, $this->getData('assignedStages'));
00152       }
00153    }
00154 
00155 
00156    //
00157    // Private helper methods
00158    //
00164    function _assignStagesToUserGroup($userGroupId, $userAssignedStages) {
00165       $pressId = $this->getPressId();
00166       $userGroupDao =& DAORegistry::getDAO('UserGroupDAO');
00167 
00168       // Current existing workflow stages.
00169       $stages = $userGroupDao->getWorkflowStageTranslationKeys();
00170 
00171       foreach (array_keys($stages) as $stageId) {
00172          $userGroupDao->removeGroupFromStage($pressId, $userGroupId, $stageId);
00173       }
00174 
00175       foreach ($userAssignedStages as $stageId) {
00176 
00177          // Check if is a valid stage.
00178          if (in_array($stageId, array_keys($stages))) {
00179             $userGroupDao->assignGroupToStage($pressId, $userGroupId, $stageId);
00180          } else {
00181             fatalError('Invalid stage id');
00182          }
00183       }
00184    }
00185 
00192    function _setUserGroupLocaleFields($userGroup, &$request) {
00193       $router = $request->getRouter();
00194       $press = $router->getContext($request);
00195       $supportedLocales = $press->getSupportedLocaleNames();
00196 
00197       if (!empty($supportedLocales)) {
00198          foreach ($press->getSupportedLocaleNames() as $localeKey => $localeName) {
00199             $name = $this->getData('name');
00200             $abbrev = $this->getData('abbrev');
00201             $userGroup->setName($name[$localeKey], $localeKey);
00202             $userGroup->setAbbrev($abbrev[$localeKey], $localeKey);
00203          }
00204       } else {
00205          $localeKey = AppLocale::getLocale();
00206          $userGroup->setName($this->getData('name'), $localeKey);
00207          $userGroup->setAbbrev($this->getData('abbrev'), $localeKey);
00208       }
00209 
00210       return $userGroup;
00211    }
00212 }
00213 
00214 ?>

Generated on Mon Sep 17 2012 13:58:56 for Open Monograph Press by  doxygen 1.7.1