Modified user options to support output type
authorAlexander Ebert <ebert@woltlab.com>
Fri, 18 Nov 2011 22:15:12 +0000 (23:15 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 18 Nov 2011 22:15:12 +0000 (23:15 +0100)
That was a joke, fat chance. User options are completely messed up now :/

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 7cc1277fd5107e7244d854c6763711b26773db2e..0620e69d9fed4eeb4c58a2c4528453b8052c5d6b 100644 (file)
@@ -26,11 +26,16 @@ 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_OTHER = 4;
-       const VISIBILITY_ALL = 7;
+       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()
index ce9732206a9abf1b3d83913ac597dd217ac68679..306857a7166385e56d7d55b4efb1e423521927db 100644 (file)
@@ -1,7 +1,9 @@
 <?php
 namespace wcf\data\user\option;
 use wcf\data\option\Option;
+use wcf\data\user\User;
 use wcf\system\option\user\IUserOptionOutput;
+use wcf\system\WCF;
 
 /**
  * Represents a user option.
@@ -36,31 +38,50 @@ class UserOption extends Option {
         */
        public $outputData = array();
        
+       /**
+        * user object
+        * @var wcf\data\user\User
+        */
+       public $user = null;
+       
+       /**
+        * Sets target user object.
+        * 
+        * @param       wcf\data\user\User      $user
+        */
+       public function setUser(User $user) {
+               $this->user = $user;
+       }
+       
        /**
         * @see wcf\data\option\Option::isVisible()
         */
        public function isVisible() {
-               $bitmask = $this->options[$optionName]->visible;
                // check if option is hidden
-               if ($bitmask & Option::VISIBILITY_NONE) {
-                       $visible = false;
+               if (!$this->visible) {
+                       return false;
                }
+               
                // proceed if option is visible for all
-               else if ($bitmask & Option::VISIBILITY_OTHER) {
+               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;
                }
                else {
                        $isAdmin = $isOwner = $visible = false;
                        // check admin permissions
-                       if ($bitmask & Option::VISIBILITY_ADMINISTRATOR) {
+                       if ($this->visible & Option::VISIBILITY_ADMINISTRATOR) {
                                if (WCF::getSession()->getPermission('admin.general.canViewPrivateUserOptions')) {
                                        $isAdmin = true;
                                }
                        }
                        
                        // check owner state
-                       if ($bitmask & Option::VISIBILITY_OWNER) {
-                               if ($user->userID == WCF::getUser()->userID) {
+                       if ($this->visible & Option::VISIBILITY_OWNER) {
+                               if ($this->user->userID == WCF::getUser()->userID) {
                                        $isOwner = true;
                                }
                        }
index 652fb3beb40f3c74ac1316b2282da9999f6b22c2..5ca9d1293ce71b57dd4425750251e61c88cd5b39 100644 (file)
@@ -54,7 +54,7 @@ class OptionHandler implements IOptionHandler {
         * option structure
         * @var array
         */
-       protected $cachedOptionToCategories = null;
+       public $cachedOptionToCategories = null;
        
        /**
         * Name of the active option category.
@@ -98,10 +98,16 @@ class OptionHandler implements IOptionHandler {
         */
        public $supportI18n = false;
        
+       /**
+        * cache initialization state
+        * @var boolean
+        */
+       public $didInit = false;
+       
        /**
         * @see wcf\system\option\IOptionHandler::__construct()
         */
-       public function __construct($cacheName, $cacheClass, $supportI18n, $languageItemPattern = '', $categoryName = '') {
+       public function __construct($cacheName, $cacheClass, $supportI18n, $languageItemPattern = '', $categoryName = '', $loadActiveOptions = true) {
                $this->cacheName = $cacheName;
                $this->cacheClass = $cacheClass;
                $this->categoryName = $categoryName;
@@ -109,7 +115,7 @@ class OptionHandler implements IOptionHandler {
                $this->supportI18n = $supportI18n;
                
                // load cache on init
-               $this->readCache();
+               $this->readCache($loadActiveOptions);
        }
        
        /**
@@ -334,8 +340,10 @@ class OptionHandler implements IOptionHandler {
        
        /**
         * Gets all options and option categories from cache.
+        * 
+        * @param       boolean         $loadActiveOptions
         */
-       protected function readCache() {
+       protected function readCache($loadActiveOptions) {
                $cacheName = $this->cacheName . '-' . PACKAGE_ID;
                CacheHandler::getInstance()->addResource($cacheName, WCF_DIR.'cache/cache.'.$cacheName.'.php', $this->cacheClass);
                
@@ -345,8 +353,13 @@ class OptionHandler implements IOptionHandler {
                $this->cachedCategoryStructure = CacheHandler::getInstance()->get($cacheName, 'categoryStructure');
                $this->cachedOptionToCategories = CacheHandler::getInstance()->get($cacheName, 'optionToCategories');
                
-               // get active options
-               $this->loadActiveOptions($this->categoryName);
+               if ($loadActiveOptions) {
+                       // get active options
+                       $this->loadActiveOptions($this->categoryName);
+                       
+                       // mark options as initialized
+                       $this->didInit = true;
+               }
        }
        
        /**
@@ -443,10 +456,20 @@ class OptionHandler implements IOptionHandler {
                        if (!$hasEnabledOption) return false;
                }
                
-               if (!$option->isVisible()) {
+               if (!$this->checkVisibility($option)) {
                        return false;
                }
                
                return true;
        }
+       
+       /**
+        * Checks visibility of an option.
+        * 
+        * @param       wcf\data\option\Option          $option
+        * @return      boolean
+        */
+       protected function checkVisibility(Option $option) {
+               return $option->isVisible();
+       }
 }
index eb21035a39678198c7160f451c22c2ba9a4fd03f..62c7502547b3435922667b44dff7862ff5b9955e 100644 (file)
@@ -37,6 +37,12 @@ class UserOptionHandler extends OptionHandler {
                        $userOption = 'userOption' . $option->optionID;
                        $this->optionValues[$option->optionName] = $this->user->{$userOption};
                }
+               
+               if (!$this->didInit) {
+                       $this->loadActiveOptions($this->categoryName);
+                       
+                       $this->didInit = true;
+               }
        }
        
        /**
@@ -69,4 +75,13 @@ class UserOptionHandler extends OptionHandler {
                
                return parent::checkCategory($category);
        }
+       
+       /**
+        * @see wcf\system\option\OptionHandler::checkVisibility()
+        */
+       protected function checkVisibility(Option $option) {
+               $option->setUser($this->user);
+               
+               return $option->isVisible();
+       }
 }