Get all groups of one user
public static getGroupsByUser ( integer $id ) : array | ||
$id | integer | |
Результат | array |
/** * Update the widgets * * @param \SpoonFormElement[] $widgetPresets The widgets presets. * @return array */ private function updateWidgets($widgetPresets) { // empty dashboard sequence $this->dashboardSequence = array(); // get users $users = BackendGroupsModel::getUsers($this->id); // loop through users and create objects foreach ($users as $user) { $userObjects[] = new BackendUser($user['id']); } // any users present? if (!empty($userObjects)) { // loop through user objects and get all sequences foreach ($userObjects as $user) { $userSequences[$user->getUserId()] = $user->getSetting('dashboard_sequence'); } } // loop through all widgets foreach ($this->widgetInstances as $widget) { if (!BackendModel::isModuleInstalled($widget['module'])) { continue; } // create instance $instance = new $widget['className']($this->getKernel()); // execute instance $instance->execute(); // create module array if no existence if (!isset($this->dashboardSequence[$widget['module']])) { $this->dashboardSequence[$widget['module']] = array(); } // create dashboard sequence $this->dashboardSequence[$widget['module']] += array($widget['widget'] => array('column' => $instance->getColumn(), 'position' => (int) $instance->getPosition(), 'hidden' => false, 'present' => false)); // loop through selected widgets foreach ($widgetPresets as $preset) { // if selected if ($preset->getChecked()) { // get the preset module name $presetModule = str_replace('widgets_', '', str_replace($widget['widget'], '', $preset->getName())); // if the preset module name matches the widget module name if ($presetModule == $widget['module']) { // remove widgets_[modulename] prefix $selected = str_replace('widgets_' . $widget['module'], '', $preset->getName()); // if right widget set visible if ($selected == $widget['widget']) { $this->dashboardSequence[$widget['module']][$widget['widget']]['present'] = true; } } } } } // build group $userGroup['name'] = $this->frm->getField('name')->getValue(); $userGroup['id'] = $this->id; // build setting $setting['group_id'] = $this->id; $setting['name'] = 'dashboard_sequence'; $setting['value'] = serialize($this->dashboardSequence); // update group BackendGroupsModel::update($userGroup, $setting); // loop through all widgets foreach ($this->widgetInstances as $widget) { // loop through users foreach ($users as $user) { // unset visible if already present if (isset($userSequences[$user['id']][$widget['module']][$widget['widget']])) { $userSequences[$user['id']][$widget['module']][$widget['widget']]['present'] = false; } // get groups for user $groups = BackendGroupsModel::getGroupsByUser($user['id']); // loop through groups foreach ($groups as $group) { // get group sequence $groupSequence = BackendGroupsModel::getSetting($group['id'], 'dashboard_sequence'); // loop through selected widgets foreach ($widgetPresets as $preset) { // if selected if ($preset->getChecked()) { // convert camelcasing to underscore notation $selected = str_replace('widgets_', '', $preset->getName()); // if selected is the right widget if ($selected == $widget['widget']) { // set widgets visible $this->dashboardSequence[$widget['module']][$widget['widget']]['present'] = true; // usersequence has widget? if (isset($userSequences[$user['id']][$widget['module']][$widget['widget']])) { // set visible $userSequences[$user['id']][$widget['module']][$widget['widget']]['present'] = true; } else { // assign module if not yet present if (!isset($userSequences[$user['id']][$widget['module']])) { $userSequences[$user['id']][$widget['module']] = array(); } // add widget $userSequences[$user['id']][$widget['module']] += array($widget['widget'] => array('column' => $instance->getColumn(), 'position' => (int) $instance->getPosition(), 'hidden' => false, 'present' => true)); } } } // widget in visible in other group? if ($groupSequence[$widget['module']][$widget['widget']]['present']) { // set visible $userSequences[$user['id']][$widget['module']][$widget['widget']]['present'] = true; } } } } } // any users present? if (!empty($userObjects)) { // loop through users and update sequence foreach ($userObjects as $user) { $user->setSetting('dashboard_sequence', $userSequences[$user->getUserId()]); } } return $userGroup; }
/** * Load the form */ private function loadForm() { // create user objects $this->user = new BackendUser($this->id); $this->allowUserRights = BackendAuthentication::isAllowedAction('Add') || $this->authenticatedUser->getUserId() != $this->id || $this->authenticatedUser->isGod(); // redirect to error page when not allowed to edit other profiles if (!$this->authenticatedUser->isGod() && ($this->authenticatedUser->getUserId() != $this->id && !BackendAuthentication::isAllowedAction('Add'))) { $this->redirect(BackendModel::createURLForAction('Error') . '&type=not-allowed'); } // create form $this->frm = new BackendForm('edit'); // get active groups $groups = BackendGroupsModel::getGroupsByUser($this->id); // loop through groups and set checked foreach ($groups as $group) { $checkedGroups[] = $group['id']; } // create elements // profile $this->frm->addText('email', $this->record['email'], 255); if ($this->user->isGod()) { $this->frm->getField('email')->setAttributes(array('disabled' => 'disabled')); } $this->frm->addText('name', $this->record['settings']['name'], 255); $this->frm->addText('surname', $this->record['settings']['surname'], 255); $this->frm->addText('nickname', $this->record['settings']['nickname'], 24); $this->frm->addImage('avatar'); // password // check if we're god or same user if ($this->authenticatedUser->getUserId() == $this->id || $this->authenticatedUser->isGod()) { // allow to set new password $this->frm->addPassword('new_password', null, 75); $this->frm->addPassword('confirm_password', null, 75); // disable autocomplete $this->frm->getField('new_password')->setAttributes(array('autocomplete' => 'off')); $this->frm->getField('confirm_password')->setAttributes(array('autocomplete' => 'off')); } // settings $this->frm->addDropdown('interface_language', BL::getInterfaceLanguages(), $this->record['settings']['interface_language']); $this->frm->addDropdown('date_format', BackendUsersModel::getDateFormats(), $this->user->getSetting('date_format')); $this->frm->addDropdown('time_format', BackendUsersModel::getTimeFormats(), $this->user->getSetting('time_format')); $this->frm->addDropdown('number_format', BackendUsersModel::getNumberFormats(), $this->user->getSetting('number_format', 'dot_nothing')); $this->frm->addDropDown('csv_split_character', BackendUsersModel::getCSVSplitCharacters(), $this->user->getSetting('csv_split_character')); $this->frm->addDropDown('csv_line_ending', BackendUsersModel::getCSVLineEndings(), $this->user->getSetting('csv_line_ending')); // permissions $this->frm->addCheckbox('active', $this->record['active'] == 'Y'); // only when GOD or when you can edit other users if ($this->allowUserRights) { // disable active field for current users if ($this->authenticatedUser->getUserId() == $this->record['id']) { $this->frm->getField('active')->setAttribute('disabled', 'disabled'); } $this->frm->addCheckbox('api_access', isset($this->record['settings']['api_access']) && $this->record['settings']['api_access'] == 'Y'); $this->frm->addMultiCheckbox('groups', BackendGroupsModel::getAll(), $checkedGroups); } }