Added support for application based option types
authorAlexander Ebert <ebert@woltlab.com>
Wed, 26 Dec 2012 21:14:12 +0000 (22:14 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 26 Dec 2012 21:14:12 +0000 (22:14 +0100)
wcfsetup/install/files/lib/system/option/OptionHandler.class.php
wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php

index 72229eaecd5e5105e738c2054b81718af56b2c3a..b2acaa5d2da886366fb103716fbdaccd9f62a5ec 100644 (file)
@@ -2,12 +2,14 @@
 namespace wcf\system\option;
 use wcf\data\option\category\OptionCategory;
 use wcf\data\option\Option;
+use wcf\system\application\ApplicationHandler;
 use wcf\system\cache\CacheHandler;
 use wcf\system\exception\SystemException;
 use wcf\system\exception\UserInputException;
 use wcf\system\language\I18nHandler;
 use wcf\system\WCF;
 use wcf\util\ClassUtil;
+use wcf\util\StringUtil;
 
 /**
  * Handles options.
@@ -20,6 +22,12 @@ use wcf\util\ClassUtil;
  * @category   Community Framework
  */
 class OptionHandler implements IOptionHandler {
+       /**
+        * list of application abbreviations
+        * @var array<string>
+        */
+       protected $abbreviations = null;
+       
        /**
         * cache name
         * @var string
@@ -340,16 +348,42 @@ class OptionHandler implements IOptionHandler {
        /**
         * Returns class name for option type.
         * 
-        * @param       string          $type
+        * @param       string          $optionType
         * @return      string
         */
-       protected function getClassName($type) {
-               $className = 'wcf\system\option\\'.ucfirst($type).'OptionType';
+       protected function getClassName($optionType) {
+               $optionType = StringUtil::firstCharToUpperCase($optionType);
+               
+               // attempt to validate against WCF first
+               $isValid = false;
+               $className = 'wcf\system\option\\'.$optionType.'OptionType';
+               if (class_exists($className)) {
+                       $isValid = true;
+               }
+               else {
+                       if ($this->abbreviations === null) {
+                               $this->abbreviations = array();
+                               
+                               $applications = ApplicationHandler::getInstance()->getApplications();
+                               foreach ($applications as $application) {
+                                       $this->abbreviations[] = ApplicationHandler::getInstance()->getAbbreviation($application->packageID);
+                               }
+                       }
+                       
+                       foreach ($this->abbreviations as $abbreviation) {
+                               $className = $abbreviation.'\system\option\\'.$optionType.'OptionType';
+                               if (class_exists($className)) {
+                                       $isValid = true;
+                                       break;
+                               }
+                       }
+               }
                
                // validate class
-               if (!class_exists($className)) {
+               if (!$isValid) {
                        return null;
                }
+               
                if (!ClassUtil::isInstanceOf($className, 'wcf\system\option\IOptionType')) {
                        throw new SystemException("'".$className."' does not implement 'wcf\system\option\IOptionType'");
                }
index 8fc4f3c599b33a99517346d21d716084454653a7..3fca93e1495dde41bd03d402de51ee434ac33100 100644 (file)
@@ -2,6 +2,7 @@
 namespace wcf\system\package\plugin;
 use wcf\data\option\Option;
 use wcf\data\option\OptionEditor;
+use wcf\system\application\ApplicationHandler;
 use wcf\system\exception\SystemException;
 use wcf\system\WCF;
 use wcf\util\StringUtil;
@@ -17,6 +18,12 @@ use wcf\util\StringUtil;
  * @category   Community Framework
  */
 class OptionPackageInstallationPlugin extends AbstractOptionPackageInstallationPlugin {
+       /**
+        * list of application abbreviations
+        * @var array<string>
+        */
+       protected static $abbreviations = null;
+       
        /**
         * @see wcf\system\package\plugin\AbstractPackageInstallationPlugin::$tableName
         */
@@ -53,9 +60,8 @@ class OptionPackageInstallationPlugin extends AbstractOptionPackageInstallationP
                if (isset($option['requirei18n'])) $requireI18n = $option['requirei18n'];
                
                // check if optionType exists
-               $className = 'wcf\system\option\\'.StringUtil::firstCharToUpperCase($optionType).'OptionType';
-               if (!class_exists($className)) {
-                       throw new SystemException("unable to find class '".$className."'");
+               if (!$this->validateOptionType($optionType)) {
+                       throw new SystemException("unable to find option type '".$optionType."'");
                }
                
                // collect additional tags and their values
@@ -107,4 +113,39 @@ class OptionPackageInstallationPlugin extends AbstractOptionPackageInstallationP
                        $optionEditor->update($data);
                }
        }
+       
+       /**
+        * Returns true, if option type is valid.
+        * 
+        * @param       integer         $optionType
+        * @return      boolean
+        */
+       protected function validateOptionType($optionType) {
+               $optionType = StringUtil::firstCharToUpperCase($optionType);
+               
+               // attempt to validate against WCF first
+               $className = 'wcf\system\option\\'.$optionType.'OptionType';
+               if (class_exists($className)) {
+                       return true;
+               }
+               
+               // check for applications
+               if (self::$abbreviations === null) {
+                       self::$abbreviations = array();
+                       
+                       $applications = ApplicationHandler::getInstance()->getApplications();
+                       foreach ($applications as $application) {
+                               self::$abbreviations[] = ApplicationHandler::getInstance()->getAbbreviation($application->packageID);
+                       }
+               }
+               
+               foreach (self::$abbreviations as $abbreviation) {
+                       $className = $abbreviation.'\system\option\\'.$optionType.'OptionType';
+                       if (class_exists($className)) {
+                               return true;
+                       }
+               }
+               
+               return false;
+       }
 }