From cf251c63a05b5b93cdd8ce7f76d98f8ee6bd5784 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Fri, 14 Dec 2012 23:51:47 +0100 Subject: [PATCH] User option handler overhaul --- .../acp/form/AbstractOptionListForm.class.php | 8 ++ .../files/lib/acp/form/UserAddForm.class.php | 11 +- .../files/lib/acp/form/UserEditForm.class.php | 27 +--- .../files/lib/data/option/Option.class.php | 14 +- .../lib/data/user/option/UserOption.class.php | 133 +++++++++++++----- .../lib/system/option/OptionHandler.class.php | 46 ++---- .../option/user/UserOptionHandler.class.php | 75 ++++++---- 7 files changed, 181 insertions(+), 133 deletions(-) diff --git a/wcfsetup/install/files/lib/acp/form/AbstractOptionListForm.class.php b/wcfsetup/install/files/lib/acp/form/AbstractOptionListForm.class.php index 1c33c9f6e3..b003ae0302 100755 --- a/wcfsetup/install/files/lib/acp/form/AbstractOptionListForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/AbstractOptionListForm.class.php @@ -79,6 +79,14 @@ abstract class AbstractOptionListForm extends AbstractForm { parent::readParameters(); $this->optionHandler = new $this->optionHandlerClassName($this->cacheName, $this->cacheClass, $this->supportI18n, $this->languageItemPattern, $this->categoryName, $this->loadActiveOptions); + $this->initOptionHandler(); + } + + /** + * Initializes the option handler. + */ + protected function initOptionHandler() { + $this->optionHandler->init(); } /** diff --git a/wcfsetup/install/files/lib/acp/form/UserAddForm.class.php b/wcfsetup/install/files/lib/acp/form/UserAddForm.class.php index 0ab204f3c0..968811f8fb 100644 --- a/wcfsetup/install/files/lib/acp/form/UserAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserAddForm.class.php @@ -88,15 +88,6 @@ class UserAddForm extends UserOptionListForm { */ public $additionalFields = array(); - /** - * @see wcf\page\IPage::readParameters() - */ - public function readParameters() { - parent::readParameters(); - - $this->optionHandler->overrideVisibility(); - } - /** * @see wcf\form\IForm::readFormParameters() */ @@ -212,7 +203,7 @@ class UserAddForm extends UserOptionListForm { $this->username = $this->email = $this->confirmEmail = $this->password = $this->confirmPassword = ''; $this->groupIDs = array(); $this->languageID = $this->getDefaultFormLanguageID(); - $this->optionValues = array(); + $this->optionHandler->resetOptionValues(); } /** diff --git a/wcfsetup/install/files/lib/acp/form/UserEditForm.class.php b/wcfsetup/install/files/lib/acp/form/UserEditForm.class.php index dc6a1ce702..d2e7c39ff2 100755 --- a/wcfsetup/install/files/lib/acp/form/UserEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/UserEditForm.class.php @@ -43,19 +43,11 @@ class UserEditForm extends UserAddForm { */ public $user = null; - /** - * @see wcf\acp\form\AbstractOptionListForm::$loadActiveOptions - */ - public $loadActiveOptions = false; - /** * @see wcf\page\IPage::readParameters() */ public function readParameters() { - parent::readParameters(); - if (isset($_REQUEST['id'])) $this->userID = intval($_REQUEST['id']); - $user = new User($this->userID); if (!$user->userID) { throw new IllegalLinkException(); @@ -66,8 +58,14 @@ class UserEditForm extends UserAddForm { throw new PermissionDeniedException(); } + parent::readParameters(); + } + + /** + * wcf\acp\form\AbstractOptionListForm::initOptionHandler() + */ + protected function initOptionHandler() { $this->optionHandler->setUser($this->user->getDecoratedObject()); - $this->optionHandler->showEmptyOptions(); } /** @@ -95,17 +93,6 @@ class UserEditForm extends UserAddForm { parent::readData(); } - /** - * @see wcf\acp\form\UserAddForm::readOptionTree() - */ - protected function readOptionTree() { - if (empty($_POST)) { - $this->optionHandler->setUser($this->user->getDecoratedObject()); - } - - parent::readOptionTree(); - } - /** * Gets the selected languages. */ diff --git a/wcfsetup/install/files/lib/data/option/Option.class.php b/wcfsetup/install/files/lib/data/option/Option.class.php index 90bb5799d9..09ab90dfe2 100644 --- a/wcfsetup/install/files/lib/data/option/Option.class.php +++ b/wcfsetup/install/files/lib/data/option/Option.class.php @@ -26,17 +26,6 @@ class Option extends DatabaseObject { */ protected static $databaseTableIndexName = 'optionID'; - // equals to an empty bitmask, NOT a valid bit! - const VISIBILITY_NONE = 0; - - const VISIBILITY_OWNER = 1; - const VISIBILITY_ADMINISTRATOR = 2; - const VISIBILITY_REGISTERED = 4; - const VISIBILITY_GUEST = 8; - - // equals to VISIBILITY_GUEST, NOT a valid bit! - const VISIBILITY_ALL = 15; - /** * @see wcf\data\DatabaseObject::handleData() */ @@ -150,10 +139,9 @@ class Option extends DatabaseObject { /** * Returns true, if option is visible * - * @param boolean $overrideVisibility * @return boolean */ - public function isVisible($overrideVisibility = false) { + public function isVisible() { return !$this->hidden; } diff --git a/wcfsetup/install/files/lib/data/user/option/UserOption.class.php b/wcfsetup/install/files/lib/data/user/option/UserOption.class.php index abbbfee110..34fb450d87 100644 --- a/wcfsetup/install/files/lib/data/user/option/UserOption.class.php +++ b/wcfsetup/install/files/lib/data/user/option/UserOption.class.php @@ -15,6 +15,66 @@ use wcf\system\WCF; * @category Community Framework */ class UserOption extends Option { + /** + * visible for no one (no valid bit) + * @var integer + */ + const VISIBILITY_NONE = 0; + + /** + * visible for the owner + * @var integer + */ + const VISIBILITY_OWNER = 1; + + /** + * visible for admins + * @var integer + */ + const VISIBILITY_ADMINISTRATOR = 2; + + /** + * visible for users + * @var integer + */ + const VISIBILITY_REGISTERED = 4; + + /** + * visible for guests + * @var integer + */ + const VISIBILITY_GUEST = 8; + + /** + * visible for all (no valid bit) + * @var integer + */ + const VISIBILITY_ALL = 15; + + /** + * editable for no one (no valid bit) + * @var integer + */ + const EDITABILITY_NONE = 0; + + /** + * editable for the owner + * @var integer + */ + const EDITABILITY_OWNER = 1; + + /** + * editable for admins + * @var integer + */ + const EDITABILITY_ADMINISTRATOR = 2; + + /** + * editable for all (no valid bit) + * @var integer + */ + const EDITABILITY_ALL = 3; + /** * @see wcf\data\DatabaseObject::$databaseTableName */ @@ -49,51 +109,54 @@ class UserOption extends Option { /** * @see wcf\data\option\Option::isVisible() */ - public function isVisible($overrideVisibility = false) { - // check if option is hidden - if (!$this->visible || $this->disabled) { - return false; - } - - // ACP visibility override - if ($overrideVisibility) { + public function isVisible() { + // proceed if option is visible for all + if ($this->visible & UserOption::VISIBILITY_GUEST) { return true; } - // proceed if option is visible for all - if ($this->visible & Option::VISIBILITY_GUEST) { - $visible = true; - } // proceed if option is visible for registered users and current user is logged in - else if (($this->visible & Option::VISIBILITY_REGISTERED) && WCF::getUser()->userID) { - $visible = true; + if (($this->visible & UserOption::VISIBILITY_REGISTERED) && WCF::getUser()->userID) { + return true; } - else { - $isAdmin = $isOwner = $visible = false; - // check admin permissions - if ($this->visible & Option::VISIBILITY_ADMINISTRATOR) { - if (WCF::getSession()->getPermission('admin.general.canViewPrivateUserOptions')) { - $isAdmin = true; - } + + // check admin permissions + if ($this->visible & UserOption::VISIBILITY_ADMINISTRATOR) { + if (WCF::getSession()->getPermission('admin.general.canViewPrivateUserOptions')) { + return true; } - - // check owner state - if ($this->visible & Option::VISIBILITY_OWNER) { - if ($this->user !== null && $this->user->userID == WCF::getUser()->userID) { - $isOwner = true; - } + } + + // check owner state + if ($this->visible & UserOption::VISIBILITY_OWNER) { + if ($this->user !== null && $this->user->userID == WCF::getUser()->userID) { + return true; } - - if ($isAdmin) { - $visible = true; + } + + return false; + } + + /** + * Returns true, if this option is editable. + * + * @return boolean + */ + public function isEditable() { + // check admin permissions + if ($this->editable & UserOption::EDITABILITY_ADMINISTRATOR) { + if (WCF::getSession()->getPermission('admin.general.canViewPrivateUserOptions')) { + return true; } - else if ($isOwner) { - $visible = true; + } + + // check owner state + if ($this->editable & UserOption::VISIBILITY_OWNER) { + if ($this->user === null || $this->user->userID == WCF::getUser()->userID) { + return true; } } - if (!$visible || $this->disabled) return false; - - return true; + return false; } } diff --git a/wcfsetup/install/files/lib/system/option/OptionHandler.class.php b/wcfsetup/install/files/lib/system/option/OptionHandler.class.php index 9891251780..005adbaad5 100644 --- a/wcfsetup/install/files/lib/system/option/OptionHandler.class.php +++ b/wcfsetup/install/files/lib/system/option/OptionHandler.class.php @@ -86,12 +86,6 @@ class OptionHandler implements IOptionHandler { */ public $optionValues = array(); - /** - * visibility override - * @var boolean - */ - public $overrideVisibility = false; - /** * raw option values * @var array @@ -113,7 +107,7 @@ class OptionHandler implements IOptionHandler { /** * @see wcf\system\option\IOptionHandler::__construct() */ - public function __construct($cacheName, $cacheClass, $supportI18n, $languageItemPattern = '', $categoryName = '', $loadActiveOptions = true) { + public function __construct($cacheName, $cacheClass, $supportI18n, $languageItemPattern = '', $categoryName = '') { $this->cacheName = $cacheName; $this->cacheClass = $cacheClass; $this->categoryName = $categoryName; @@ -121,7 +115,7 @@ class OptionHandler implements IOptionHandler { $this->supportI18n = $supportI18n; // load cache on init - $this->readCache($loadActiveOptions); + $this->readCache(); } /** @@ -365,10 +359,8 @@ class OptionHandler implements IOptionHandler { /** * Gets all options and option categories from cache. - * - * @param boolean $loadActiveOptions */ - protected function readCache($loadActiveOptions) { + protected function readCache() { CacheHandler::getInstance()->addResource( $this->cacheName, WCF_DIR.'cache/cache.'.$this->cacheName.'.php', @@ -380,11 +372,16 @@ class OptionHandler implements IOptionHandler { $this->cachedOptions = CacheHandler::getInstance()->get($this->cacheName, 'options'); $this->cachedCategoryStructure = CacheHandler::getInstance()->get($this->cacheName, 'categoryStructure'); $this->cachedOptionToCategories = CacheHandler::getInstance()->get($this->cacheName, 'optionToCategories'); - - if ($loadActiveOptions) { + } + + /** + * Initializes active options. + */ + public function init() { + if (!$this->didInit) { // get active options $this->loadActiveOptions($this->categoryName); - + // mark options as initialized $this->didInit = true; } @@ -394,14 +391,8 @@ class OptionHandler implements IOptionHandler { * Creates a list of all active options. * * @param string $parentCategoryName - * @param array $ignoreCategories */ - protected function loadActiveOptions($parentCategoryName, array $ignoreCategories = array()) { - // skip ignored categories - if (in_array($parentCategoryName, $ignoreCategories)) { - return; - } - + protected function loadActiveOptions($parentCategoryName) { if (!isset($this->cachedCategories[$parentCategoryName]) || $this->checkCategory($this->cachedCategories[$parentCategoryName])) { if (isset($this->cachedOptionToCategories[$parentCategoryName])) { foreach ($this->cachedOptionToCategories[$parentCategoryName] as $optionName) { @@ -413,7 +404,7 @@ class OptionHandler implements IOptionHandler { if (isset($this->cachedCategoryStructure[$parentCategoryName])) { foreach ($this->cachedCategoryStructure[$parentCategoryName] as $categoryName) { - $this->loadActiveOptions($categoryName, $ignoreCategories); + $this->loadActiveOptions($categoryName); } } } @@ -463,7 +454,7 @@ class OptionHandler implements IOptionHandler { * @return boolean */ protected function checkOption(Option $option) { - if ($option->permissions && !$this->overrideVisibility) { + if ($option->permissions) { $hasPermission = false; $permissions = explode(',', $option->permissions); foreach ($permissions as $permission) { @@ -504,13 +495,6 @@ class OptionHandler implements IOptionHandler { * @return boolean */ protected function checkVisibility(Option $option) { - return $option->isVisible($this->overrideVisibility); - } - - /** - * Overrides option visibility for administrative purposes. - */ - public function overrideVisibility() { - $this->overrideVisibility = true; + return $option->isVisible(); } } diff --git a/wcfsetup/install/files/lib/system/option/user/UserOptionHandler.class.php b/wcfsetup/install/files/lib/system/option/user/UserOptionHandler.class.php index bbddf29b7f..1ee37c7d97 100644 --- a/wcfsetup/install/files/lib/system/option/user/UserOptionHandler.class.php +++ b/wcfsetup/install/files/lib/system/option/user/UserOptionHandler.class.php @@ -24,6 +24,12 @@ class UserOptionHandler extends OptionHandler { */ public $inRegistration = false; + /** + * true, if within edit mode + * @var boolean + */ + public $editMode = true; + /** * true, if empty options should be removed * @var boolean @@ -36,52 +42,55 @@ class UserOptionHandler extends OptionHandler { */ public $user = null; - /** - * Hides empty options. - */ - public function hideEmptyOptions() { - $this->removeEmptyOptions = true; - } - /** * Shows empty options. */ - public function showEmptyOptions() { - $this->removeEmptyOptions = false; + public function showEmptyOptions($show = true) { + $this->removeEmptyOptions = !$show; } /** - * Sets registration mode and disables visibility override. + * Sets registration mode. * * @param boolean $inRegistration */ - public function setInRegistration($inRegistration) { + public function setInRegistration($inRegistration = true) { $this->inRegistration = $inRegistration; - $this->overrideVisibility = false; + if ($inRegistration) $this->enableEditMode(); + } + + /** + * Enables edit mode. + * + * @param boolean $enable + */ + public function enableEditMode($enable = true) { + $this->editMode = $enable; } /** * Sets option values for a certain user. * * @param wcf\data\user\User $user - * @param array $ignoreCategories */ - public function setUser(User $user, array $ignoreCategories = array()) { + public function setUser(User $user) { $this->optionValues = array(); $this->user = $user; - if (!$this->didInit) { - $this->loadActiveOptions($this->categoryName, $ignoreCategories); - - $this->didInit = true; - } - + $this->init(); foreach ($this->options as $option) { $userOption = 'userOption' . $option->optionID; $this->optionValues[$option->optionName] = $this->user->{$userOption}; } } + /** + * Resets the option values. + */ + public function resetOptionValues() { + $this->optionValues = array(); + } + /** * @see wcf\system\option\OptionHandler::getOption() */ @@ -126,15 +135,24 @@ class UserOptionHandler extends OptionHandler { * @see wcf\system\option\OptionHandler::checkVisibility() */ protected function checkVisibility(Option $option) { - if ($this->user !== null) { - $option->setUser($this->user); + if ($this->inRegistration && !$option->askDuringRegistration && !$option->required) { + return false; } - if ($this->inRegistration && !$option->askDuringRegistration && !$option->required) { + if ($option->disabled) { return false; } - return $option->isVisible($this->overrideVisibility); + if ($this->user !== null) { + $option->setUser($this->user); + } + + if ($this->editMode) { + return $option->isEditable(); + } + else { + return $option->isVisible(); + } } /** @@ -154,4 +172,13 @@ class UserOptionHandler extends OptionHandler { return $options; } + + /** + * @see wcf\system\option\IOptionHandler::readData() + */ + public function readData() { + foreach ($this->options as $option) { + if (!isset($this->optionValues[$option->optionName])) $this->optionValues[$option->optionName] = $option->defaultValue; + } + } } -- 2.20.1