* @package WB * @subpackage content */ /** * Load required class */ WBClass::load('WBContent' , 'WBContent_TableEditor' , 'WBDatasource'); /** * Content component: User_Manager2 * * @todo migrate import feature from User_Manager Version 1 * * @version 0.6.1 * @package WB * @subpackage content */ class WBContent_User_Manager2 extends WBContent_TableEditor { protected $config = array( 'hideuser' => array(1), 'feature' => array( // 'useradd', 'useraddmandator', // 'userimport' // not implemented yet 'userimportmandator' // not implemented yet ), 'action' => 'list', 'requiredgroup' => 'admin', 'requiredgroupmanager' => 'admin', 'showsomeoneelses' => 1, 'table' => WBDatasource::TABLE_USER, 'tableenv' => WBDatasource::TABLE_USER, 'limit' => 25, 'goto' => 0, 'id' => '__new', 'managesortcolumn' => '', 'levels' => 1, 'order' => '', 'addcurrentuser' => 'never', 'searchfields' => array(), 'usefilter' => 1, 'translator' => 'auto', 'database' => '__default', 'tmplDir' => 'User/Manager2', ); /** * user object * @var WBUser */ protected $patron; /** * 2nd constructor * * Reset template folder */ protected function init() { parent::init(); $this->config['addcurrentuser'] = 'never'; if ($this->mandator->isEnabled()) { if ($this->mandator->isMaster()) { // enable features for mandators if (!in_array('useradd', $this->config['feature']) && in_array('useraddmandator', $this->config['feature'])) { $this->config[] = 'useradd'; } if (!in_array('userimport', $this->config['feature']) && in_array('userimportmandator', $this->config['feature'])) { $this->config[] = 'userimport'; } } else { // only mandator master may edit groups and gender definitions $this->config['tableenv'] = WBDatasource::TABLE_USER; } } // mandator is not enabled, therefore, copy mandator features else { if (!in_array('useradd', $this->config['feature']) && in_array('useraddmandator', $this->config['feature'])) { $this->config[] = 'useradd'; } if (!in_array('userimport', $this->config['feature']) && in_array('userimportmandator', $this->config['feature'])) { $this->config[] = 'userimport'; } } switch ($this->config['tableenv']) { case WBDatasource::TABLE_PERSONGENDER: $this->config['table'] = WBDatasource::TABLE_PERSONGENDER; $this->config['searchfields'] = array('gender', 'salutation'); break; case WBDatasource::TABLE_GROUP: $this->config['table'] = WBDatasource::TABLE_GROUP; $this->config['searchfields'] = array('groupname', 'grouptitle'); break; default: case WBDatasource::TABLE_USER: $this->config['table'] = WBDatasource::TABLE_USER; $this->config['searchfields'] = array('nickname', 'emaillocal', 'emaildomain', 'forename', 'surname'); break; } $this->patron = WBClass::create('WBUser'); $this->patron->setMandatorId($this->mandator->getId()); $this->tmpl->addGlobalVars($this->mandator->get(), 'mandator_'); $feature = array(); foreach ($this->config['feature'] as $f) { $feature[$f] = 1; } $this->tmpl->addGlobalVars($feature, 'FEATURE_'); } /** * Get List of Variables 4 Filter * * @return */ protected function getFilterVars() { $mKey = 'filter_' . $this->mandator->getIdentifier(); $mId = $this->req->get($mKey, 0); if (is_array($mId)) { $mId = array_shift($mId); } $data = array(); $data[$mKey] = $mId; return $data; } /** * Setup Id * * Load record if a proper id is given * * @param string $foreign * @return array */ protected function setupId($foreign) { if (empty($this->config['id'])) { return parent::setupId($foreign); } $p = array(); switch ($this->config['tableenv']) { case WBDatasource::TABLE_USER: if ($this->patron->load($this->config['id'])) { $p = $this->patron->getData(); $this->id = $this->patron->getId(); } break; default: $p = parent::setupId($foreign); break; } return $p; } /** * Actually Run Action * * Special behaviour for action "passwort", otherwise use parent's method * * @param array * @param string * @param string */ protected function runAction($p, $foreign, $tmplDir) { switch ($this->config['action']) { case 'password': if (WBDatasource::TABLE_USER == $this->config['tableenv']) { $this->runPassword($p, $foreign, $tmplDir); $this->displayList($foreign); return; } break; case 'add': if (!in_array('useradd', $this->config['feature'])) { $this->config['action'] = 'list'; } break; case 'import': if (!in_array('userimport', $this->config['feature'])) { $this->config['action'] = 'list'; } break; default: break; } return parent::runAction($p, $foreign, $tmplDir); } /** * Actually Run Action: Password * * @param array * @param string * @param string */ protected function runPassword($p, $foreign, $tmplDir) { $tmp = array(); foreach ($p as $k => $v) { if (is_scalar($v)) { $tmp[$k] = $v; } } $this->tmpl->addGlobalVars($tmp); $this->processForm('password', $p); } /** * Save User's Password * * Use storage module to save password. Also add new passwort to template placeholder. * This allows to show password once, after it was saved. * * @param patForms $form * @param array $values */ public function onPasswordValid($form, $values) { if (empty($values['password'])) { $this->tmpl->addGlobalVar('password_saved', 'not saved'); return true; } $save = array( 'password' => $values['password'] ); $this->patron->set($save); $download = <<patron->getNickname(); if (empty($loginName)) { $loginField = patI18n::dgettext('wombat', 'E-mail'); $loginName = $this->patron->getEMail(); } $data = array( 'password' => $save['password'], 'password_field' => patI18n::dgettext('wombat', 'password'), 'login' => $loginName, 'login_field' => $loginField ); $download = WBString::populate($download, $data); // make windows line breaks :-/ str_replace("\n", "\r\n", $download); $download = base64_encode($download); $this->tmpl->addGlobalVar('PASSWORD_DOWNLOADBASE64', $download); $this->tmpl->addGlobalVar('PASSWORD_SAVED', 'saved'); $this->tmpl->addGlobalVar('PASSWORD_PLAIN', $values['password']); return true; } /** * Helper function to add User * * Only add user when feature is enabled * * @param array */ private function addUser($values) { if (!in_array('useradd', $this->config['feature'])) { return; } // add user with well known data $storage = $this->patron->getStorageModule(); $key = array( 'email', 'enabled', 'nickname', 'forename', 'surname', 'country', 'language' ); $major = array(); $minior = array(); foreach ($values as $k => $v) { if (in_array($k, $key)) { $major[$k] = $values[$k]; continue; } $minor[$k] = $values[$k]; } // add user $this->config['id'] = $storage->set($major, true); // load patron with new user $this->patron->load($this->config['id']); // store additional data $this->patron->set($minor); if ($this->relTable) { $this->relTable->add($this->config['id']); } } /** * Save User's Primary Data * * Mangle records before actually saving it * * @param bool true if database record is new * @param array validated form values to be saved * @return string id of saved record */ protected function saveRecord($new, $values) { $memberSave = false; $member = array(); switch ($this->config['tableenv']) { // save user data and group membership case WBDatasource::TABLE_USER: if ($new) { $this->addUser($values); } else { if ($this->relTable) { $this->relTable->delete($this->config['id']); } $this->patron->set($values); } if (isset($values['groups'])) { $this->patron->setGroups($values['groups']); } $id = $this->config['id']; if (!$new && $this->relTable) { $this->relTable->add($id); } break; case WBDatasource::TABLE_GROUP: // don't save groupname by default if ('edit' == $this->config['action']) { if (!isset($values['changegroupname']) || empty($values['changegroupname']) || empty($values['groupname'])) { unset($values['groupname']); } } // remove switch if (isset($values['changegroupname'])) { unset($values['changegroupname']); } // members if (isset($values['groupmember'])) { $member = $values['groupmember']; unset($values['groupmember']); } $id = parent::saveRecord($new, $values); $memberSave = 1; break; default: $id = parent::saveRecord($new, $values); break; } if (!$memberSave) { return $id; } $pGroup = $this->table->getIdentifier(WBDatasource::TABLE_GROUP, true); $pUser = $this->table->getIdentifier(WBDatasource::TABLE_USER, true); $pMand = $this->mandator->getIdentifier(); $clause = array(); $clause[] = array( 'field' => $pGroup, 'value' => $id ); $this->mandator->inject2Clause($clause); $this->table->delete(WBDatasource::TABLE_USERGROUP, null, null, $clause); if (empty($member)) { return $id; } $save = array(); foreach ($member as $m) { $save[] = array( $pGroup => $id, $pUser => $m, $pMand => $this->mandator->getId() ); } $this->table->save(WBDatasource::TABLE_USERGROUP, '__new', $save); return $id; } /** * location of form config * * Return sub directory where form element definitions are located * * @return string folder */ protected function getFormConfigDir() { return 'user/manager2/' . $this->config['tableenv']; } /** * Mangle Form Element List * * Dispatch to different methods depending on current tableenv * * @param string name of the xml- and template-filename * @return array $elements */ protected function getFormElementList($name) { switch ($this->config['tableenv']) { case WBDatasource::TABLE_USER: return $this->getFormElementList4User($name); break; case WBDatasource::TABLE_GROUP: return $this->getFormElementList4Group($name); break; default: break; } return parent::getFormElementList($name); } /** * Start relation table * * If relations are configured, start relation table object * * @todo improve persformace if called twice * @return bool */ protected function initRelationTable() { if ($this->relTable) { return true; } if (WBDatasource::TABLE_USER != $this->config['table']) { return parent::initRelationTable(); } if (!$this->mandator->isEnabled() || !$this->mandator->isMaster()) { return parent::initRelationTable(); } $this->relTable = WBClass::create('WBDatasource_RelationTable'); $this->relTable->setTable($this->table); /** mandatoruser
mandator
*/ $rel = $this->relTable->getConfig4Key(WBDatasource::TABLE_USER); $rel['usermandator'] = array( 'table' => WBDatasource::TABLE_MANDATORUSER, 'foreigntable' => WBDatasource::TABLE_MANDATOR ); $this->relTable->setConfig(WBDatasource::TABLE_USER, $rel); return true; } /** * Mangle Form Element List 4 User * * Fill groups default using current groups from user object (patron) * * @see getFormElementList() * @param string name of the xml- and template-filename * @return array $elements */ protected function getFormElementList4User($name) { $elements = parent::getFormElementList($name); if (is_array($elements['groups'])) { $elements['groups']['attributes']['default'] = array_keys($this->patron->getGroups()); } if (!$this->mandator->isEnabled() || !$this->mandator->isMaster()) { return $elements; } $conf = WBClass::create('WBConfig'); $conf->load($this->getFormConfigDir() . '/edit-mandator'); $elements = array_merge($elements, $conf->get()); return $elements; } /** * Mangle Form Element List 4 User * * Fill groups default using current groups from user object (patron) * * @see getFormElementList() * @param string name of the xml- and template-filename * @return array $elements */ protected function getFormElementList4Group($name) { $elements = parent::getFormElementList($name); if (!isset($elements['groupmember'])) { return $elements; } $clause = array(); $clause[] = array( 'field' => $this->table->getIdentifier(WBDatasource::TABLE_GROUP, true), 'value' => $this->id ); $this->mandator->inject2Clause($clause); $list = $this->table->get(WBDatasource::TABLE_USERGROUP, null, null, $clause); $pUser = $this->table->getIdentifier(WBDatasource::TABLE_USER, true); $elements['groupmember']['attributes']['default'] = array(); foreach ($list as $l) { $elements['groupmember']['attributes']['default'][] = $l[$pUser]; } return $elements; } /** * Tweak Table Filter On Demand * * Implement this method to mange with filter clause and options * * @param array * @param option */ protected function tweakFilter(&$clause, &$option) { $pMan = $this->mandator->getIdentifier(); $mId = $this->mandator->getId(); switch ($this->config['tableenv']) { case WBDatasource::TABLE_USER: if (0 != $mId) { $clause[] = array( 'field' => $pMan, 'table' => WBDatasource::TABLE_MANDATORUSER, 'value' => $mId ); if (empty($option['join'])) { $option['join'] = array(); } $option['join'][] = array( 'table' => WBDatasource::TABLE_MANDATORUSER, 'using' => $this->table->getIdentifier(WBDatasource::TABLE_USER) ); } if (!empty($this->config['hideuser']) && is_array($this->config['hideuser'])) { $clause[] = array( 'field' => $this->table->getIdentifier('user'), 'relation' => 'not_in', 'value' => $this->config['hideuser'] ); } // hide mandatory system users: mandator-* $clause[] = array( 'field' => 'nickname', 'relation' => 'not_begin', 'value' => 'mandator-' ); break; } } }