User option handler overhaul
authorMarcel Werk <burntime@woltlab.com>
Fri, 14 Dec 2012 22:51:47 +0000 (23:51 +0100)
committerMarcel Werk <burntime@woltlab.com>
Fri, 14 Dec 2012 22:51:47 +0000 (23:51 +0100)
wcfsetup/install/files/lib/acp/form/AbstractOptionListForm.class.php
wcfsetup/install/files/lib/acp/form/UserAddForm.class.php
wcfsetup/install/files/lib/acp/form/UserEditForm.class.php
wcfsetup/install/files/lib/data/option/Option.class.php
wcfsetup/install/files/lib/data/user/option/UserOption.class.php
wcfsetup/install/files/lib/system/option/OptionHandler.class.php
wcfsetup/install/files/lib/system/option/user/UserOptionHandler.class.php

index 1c33c9f6e3cc8a1b45729c8ae4df266c6a5f0f01..b003ae030297a70b5006eb1749bc525d52e98e70 100755 (executable)
@@ -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();
        }
        
        /**
index 0ab204f3c012cefbe10cd11761a1f0f350e6e445..968811f8fbd2d44e7cdf45770bc783d13ac4aaee 100644 (file)
@@ -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();
        }
        
        /**
index dc6a1ce7020dc8fcded6547cb4408a17b235e0ab..d2e7c39ff2871fd0b1e47a701775a44439305822 100755 (executable)
@@ -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.
         */
index 90bb5799d95f50355c637728882d0d2ae7c1a4eb..09ab90dfe23c07da55a62ede35b8476baac2caef 100644 (file)
@@ -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;
        }
        
index abbbfee1108ea1c6b9b47e1cbaf00bcf494f219a..34fb450d873a96b9afd3f62f4a5f8faff7d4c4ff 100644 (file)
@@ -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;
        }
 }
index 98912517805e56ed1971f372ecdd13220df6c2e2..005adbaad5b04ca4d35178cb9b83bfd7fc66e141 100644 (file)
@@ -86,12 +86,6 @@ class OptionHandler implements IOptionHandler {
         */
        public $optionValues = array();
        
-       /**
-        * visibility override
-        * @var boolean
-        */
-       public $overrideVisibility = false;
-       
        /**
         * raw option values
         * @var array<mixed>
@@ -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<string>   $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();
        }
 }
index bbddf29b7fcc09d331d7b57a97a4409da93b4fb4..1ee37c7d975fc7e11e701a7f3cdd50646aa22f79 100644 (file)
@@ -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<string>           $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;
+               }
+       }
 }