Modified user options to reuse code
authorAlexander Ebert <ebert@woltlab.com>
Fri, 18 Nov 2011 16:21:36 +0000 (17:21 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 18 Nov 2011 16:21:36 +0000 (17:21 +0100)
wcfsetup/install/files/lib/data/user/option/IUserOptionOutput.class.php [deleted file]
wcfsetup/install/files/lib/data/user/option/UserOption.class.php
wcfsetup/install/files/lib/data/user/option/ViewableUserOption.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/option/user/IUserOptionOutput.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/option/user/IUserOptionOutputContactInformation.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/option/user/UserOptionHandler.class.php

diff --git a/wcfsetup/install/files/lib/data/user/option/IUserOptionOutput.class.php b/wcfsetup/install/files/lib/data/user/option/IUserOptionOutput.class.php
deleted file mode 100644 (file)
index eb4d781..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-<?php
-namespace wcf\data\user\option;
-use wcf\data\User;
-
-/**
- * Any user option output class should implement this interface.
- *
- * @author     Marcel Werk
- * @copyright  2001-2011 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @package    com.woltlab.wcf
- * @subpackage data.user.option
- * @category   Community Framework
- */
-interface IUserOptionOutput {
-       /**
-        * Returns a short version of the html code for the output of the given user option.
-        * 
-        * @param       User            $user
-        * @param       array           $optionData
-        * @param       string          $value
-        * @return      string
-        */
-       public function getShortOutput(User $user, $optionData, $value);
-       
-       /**
-        * Returns a medium version of the html code for the output of the given user option.
-        * 
-        * @param       User            $user
-        * @param       array           $optionData
-        * @param       string          $value
-        * @return      string
-        */
-       public function getMediumOutput(User $user, $optionData, $value);
-       
-       /**
-        * Returns the html code for the output of the given user option.
-        * 
-        * @param       User            $user
-        * @param       array           $optionData
-        * @param       string          $value
-        * @return      string
-        */
-       public function getOutput(User $user, $optionData, $value);
-}
index 5f1cb023b5ece25363dcc560c5cdf36bbc28c2bd..ce9732206a9abf1b3d83913ac597dd217ac68679 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\data\user\option;
 use wcf\data\option\Option;
+use wcf\system\option\user\IUserOptionOutput;
 
 /**
  * Represents a user option.
diff --git a/wcfsetup/install/files/lib/data/user/option/ViewableUserOption.class.php b/wcfsetup/install/files/lib/data/user/option/ViewableUserOption.class.php
new file mode 100644 (file)
index 0000000..b388a07
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+namespace wcf\data\user\option;
+use wcf\data\user\User;
+use wcf\data\DatabaseObjectDecorator;
+use wcf\system\option\user\IUserOptionOutputContactInformation;
+use wcf\util\ClassUtil;
+use wcf\util\StringUtil;
+
+class ViewableUserOption extends DatabaseObjectDecorator {
+       /**
+        * @see wcf\data\DatabaseObjectDecorator::$baseClass
+        */
+       protected static $baseClass = 'wcf\data\user\option\UserOption';
+       
+       /**
+        * list of output objects
+        * @var array<wcf\system\option\user\IUserOptionOutput>
+        */
+       public static $outputObjects = array();
+       
+       /**
+        * user option value
+        * @var string
+        */
+       public $optionValue = '';
+       
+       /**
+        * user option output data
+        * @var array
+        */
+       public $outputData = array();
+       
+       /**
+        * Sets option values for a specific user.
+        * 
+        * @param       wcf\data\user\User      $user
+        */
+       public function setOptionValue(User $user) {
+               $userOption = 'userOption' . $this->optionID;
+               $optionValue = $user->{$userOption};
+               
+               // use output class
+               if ($this->outputClass) {
+                       $outputObj = $this->getOutputObject($this->outputClass);
+                       
+                       if ($outputObj instanceof IUserOptionOutputContactInformation) {
+                               $this->outputData = $outputObj->getOutputData($user, $this->getDecoratedObject(), $optionValue);
+                       }
+                       
+                       if ($this->outputType == 'normal') $this->optionValue = $outputObj->getOutput($user, $this->getDecoratedObject(), $optionValue);
+                       else if ($this->outputType == 'short') $this->optionValue = $outputObj->getShortOutput($user, $this->getDecoratedObject(), $optionValue);
+                       else $this->optionValue = $outputObj->getMediumOutput($user, $this->getDecoratedObject(), $optionValue);
+               }
+               else {
+                       $this->optionValue = StringUtil::encodeHTML($optionValue);
+               }
+       }
+       
+       /**
+        * Returns the output object for current user option.
+        * 
+        * @return      wcf\system\option\user\IUserOptionOutput
+        */
+       public function getOutputObject() {
+               if (!isset(self::$outputObjects[$this->className])) {
+                       // create instance
+                       if (!class_exists($this->className)) {
+                               throw new SystemException("unable to find class '".$this->className."'");
+                       }
+                       
+                       // validate interface
+                       if (!ClassUtil::isInstanceOf($this->className, 'wcf\system\user\option\IUserOptionOutput')) {
+                               throw new SystemException("'".$this->className."' should implement wcf\system\user\option\IUserOptionOutput");
+                       }
+                       
+                       self::$outputObjects[$this->className] = new $this->className();
+               }
+               
+               return self::$outputObjects[$this->className];
+       }
+}
diff --git a/wcfsetup/install/files/lib/system/option/user/IUserOptionOutput.class.php b/wcfsetup/install/files/lib/system/option/user/IUserOptionOutput.class.php
new file mode 100644 (file)
index 0000000..0df3d17
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+namespace wcf\system\option\user;
+use wcf\data\user\option\UserOption;
+use wcf\data\user\User;
+
+/**
+ * Any user option output class should implement this interface.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2011 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.option.user
+ * @category   Community Framework
+ */
+interface IUserOptionOutput {
+       /**
+        * Returns a short version of the html code for the output of the given user option.
+        * 
+        * @param       wcf\data\user\User              $user
+        * @param       wcf\data\user\option\UserOption $option
+        * @param       string                          $value
+        * @return      string
+        */
+       public function getShortOutput(User $user, UserOption $option, $value);
+       
+       /**
+        * Returns a medium version of the html code for the output of the given user option.
+        * 
+        * @param       wcf\data\user\User              $user
+        * @param       wcf\data\user\option\UserOption $option
+        * @param       string                          $value
+        * @return      string
+        */
+       public function getMediumOutput(User $user, UserOption $option, $value);
+       
+       /**
+        * Returns the html code for the output of the given user option.
+        * 
+        * @param       wcf\data\user\User              $user
+        * @param       wcf\data\user\option\UserOption $option
+        * @param       string                          $value
+        * @return      string
+        */
+       public function getOutput(User $user, UserOption $option, $value);
+}
+?>
diff --git a/wcfsetup/install/files/lib/system/option/user/IUserOptionOutputContactInformation.class.php b/wcfsetup/install/files/lib/system/option/user/IUserOptionOutputContactInformation.class.php
new file mode 100644 (file)
index 0000000..419c695
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+namespace wcf\system\option\user;
+use wcf\data\user\option\UserOption;
+use wcf\data\user\User;
+
+/**
+ * Any user option output class should implement this interface.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2011 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.option.user
+ * @category   Community Framework
+ */
+interface IUserOptionOutputContactInformation {
+       /**
+        * Returns the output data of this user option.
+        *
+        * @param       wcf\data\user\User              $user
+        * @param       wcf\data\user\option\UserOption $optionData
+        * @param       string                          $value
+        * @return      array
+        */
+       public function getOutputData(User $user, UserOption $option, $value);
+} 
+?>
index 1b474eac2f5f05d0f11608542681613e7dfb0158..eb21035a39678198c7160f451c22c2ba9a4fd03f 100644 (file)
@@ -2,6 +2,7 @@
 namespace wcf\system\option\user;
 use wcf\data\option\category\OptionCategory;
 use wcf\data\option\Option;
+use wcf\data\user\option\ViewableUserOption;
 use wcf\data\user\User;
 use wcf\system\exception\UserInputException;
 use wcf\system\option\OptionHandler;
@@ -17,15 +18,36 @@ use wcf\system\option\OptionHandler;
  * @category   Community Framework
  */
 class UserOptionHandler extends OptionHandler {
+       /**
+        * current user
+        * @var wcf\data\user\User
+        */
+       public $user = null;
+       
+       /**
+        * Sets option values for a certain user.
+        * 
+        * @param       wcf\data\user\User
+        */
        public function setUser(User $user) {
                $this->optionValues = array();
+               $this->user = $user;
                
                foreach ($this->options as $option) {
                        $userOption = 'userOption' . $option->optionID;
-                       $this->optionValues[$option->optionName] = $user->{$userOption};
+                       $this->optionValues[$option->optionName] = $this->user->{$userOption};
                }
        }
        
+       /**
+        * @see wcf\system\option\OptionHandler::getCategoryOptions()
+        */
+       public function getCategoryOptions($categoryName = '', $inherit = true) {
+               $options = parent::getCategoryOptions($categoryName, $inherit);
+               
+               die('<pre>'.print_r($options, true));
+       }
+       
        /**
         * @see wcf\system\option\OptionHandler::validateOption()
         */