Implemented i18n for options
authorAlexander Ebert <ebert@woltlab.com>
Wed, 28 Sep 2011 19:58:42 +0000 (21:58 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 28 Sep 2011 19:58:42 +0000 (21:58 +0200)
These changes may not be completely compatible with user groups, check that!

com.woltlab.wcf/option.xml
wcfsetup/install/files/acp/templates/optionTypeTextI18n.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/acp/form/AbstractOptionListForm.class.php
wcfsetup/install/files/lib/acp/form/OptionForm.class.php
wcfsetup/install/files/lib/system/language/I18nHandler.class.php
wcfsetup/install/files/lib/system/option/AbstractOptionType.class.php
wcfsetup/install/files/lib/system/option/IOptionType.class.php
wcfsetup/install/files/lib/system/option/TextI18nOptionType.class.php [new file with mode: 0644]

index fd4d387793d330b9d41ea021cf0dc0190787b65b..17fcb33fe1e7007f74688df33365f87d7359d4a3 100644 (file)
                        <!-- general.page -->
                        <option name="page_title">
                                <categoryname>general.page</categoryname>
-                               <optiontype>text</optiontype>
+                               <optiontype>texti18n</optiontype>
+                               <supporti18n>1</supporti18n>
                        </option>
                        <option name="page_description">
                                <categoryname>general.page</categoryname>
diff --git a/wcfsetup/install/files/acp/templates/optionTypeTextI18n.tpl b/wcfsetup/install/files/acp/templates/optionTypeTextI18n.tpl
new file mode 100644 (file)
index 0000000..7f808f6
--- /dev/null
@@ -0,0 +1,10 @@
+<script type="text/javascript">
+       //<![CDATA[
+       $(function() {
+               var $availableLanguages = { {implode from=$availableLanguages key=languageID item=languageName}{@$languageID}: '{$languageName}'{/implode} };
+               var $optionValues = { {implode from=$i18nValues[$option->optionName] key=languageID item=value}'{@$languageID}': '{$value}'{/implode} };
+               new WCF.MultipleLanguageInput('{$option->optionName}', false, $optionValues, $availableLanguages);
+       });
+       //]]>
+</script>
+<input type="{@$inputType}" id="{$option->optionName}" name="{$option->optionName}" value="{$i18nPlainValues[$option->optionName]}" class="medium" />
index 1b3df00b1c0488142bc26ee658e734f6c83daf89..679953a46f771128cad3887640d42c0ec02badb5 100755 (executable)
@@ -6,6 +6,7 @@ use wcf\form\AbstractForm;
 use wcf\system\cache\CacheHandler;
 use wcf\system\exception\SystemException;
 use wcf\system\exception\UserInputException;
+use wcf\system\language\I18nHandler;
 use wcf\util\ClassUtil;
 use wcf\util\StringUtil;
 
@@ -95,6 +96,12 @@ abstract class AbstractOptionListForm extends AbstractForm {
         * @var array
         */
        public $typeObjects = array();
+       
+       /**
+        * language item pattern
+        * @var string
+        */
+       protected $languageItemPattern = '';
                
        /**
         * @see wcf\form\IForm::readFormParameters()
@@ -103,6 +110,14 @@ abstract class AbstractOptionListForm extends AbstractForm {
                parent::readFormParameters();
                
                if (isset($_POST['values']) && is_array($_POST['values'])) $this->rawValues = $_POST['values'];
+               
+               foreach ($this->options as $option) {
+                       if ($option->supportI18n) {
+                               I18nHandler::getInstance()->register($option->optionName);
+                               I18nHandler::getInstance()->setOptions($option->optionName, $option->packageID, $option->optionValue, $this->languageItemPattern);
+                       }
+               }
+               I18nHandler::getInstance()->readValues();
        }
 
        /**
@@ -331,7 +346,7 @@ abstract class AbstractOptionListForm extends AbstractForm {
                                }
                        }
                }
-       
+               
                return $tree;
        }
        
@@ -378,4 +393,17 @@ abstract class AbstractOptionListForm extends AbstractForm {
                
                return $children;
        }
+       
+       public function readData() {
+               parent::readData();
+               
+               if (!count($_POST)) {
+                       foreach ($this->options as $option) {
+                               if ($option->supportI18n) {
+                                       I18nHandler::getInstance()->register($option->optionName);
+                                       I18nHandler::getInstance()->setOptions($option->optionName, $option->packageID, $option->optionValue, $this->languageItemPattern);
+                               }
+                       }
+               }
+       }
 }
index c171ae6271f3212fef0f3f342d133d61bc2b0741..4cc31b202203ac5066cd51f9184b7ba36be501e3 100644 (file)
@@ -5,6 +5,7 @@ use wcf\data\option\category\OptionCategory;
 use wcf\data\option\OptionAction;
 use wcf\data\option\Option;
 use wcf\system\exception\IllegalLinkException;
+use wcf\system\language\I18nHandler;
 use wcf\system\WCF;
 use wcf\system\WCFACP;
 
@@ -48,6 +49,11 @@ class OptionForm extends AbstractOptionListForm {
         */
        public $optionTree = array();
        
+       /**
+        * @see wcf\acp\form\AbstractOptionListForm::$languageItemPattern
+        */
+       protected $languageItemPattern = 'wcf.option.option\d+';
+       
        /**
         * @see wcf\page\IPage::readParameters()
         */
@@ -80,7 +86,20 @@ class OptionForm extends AbstractOptionListForm {
                // save options
                $saveOptions = array();
                foreach ($this->options as $option) {
-                       $saveOptions[$option->optionID] = $this->optionValues[$option->optionName];
+                       // handle i18n support
+                       if ($option->supportI18n) {
+                               if (I18nHandler::getInstance()->isPlainValue($option->optionName)) {
+                                       I18nHandler::getInstance()->remove('wcf.option.option' . $option->optionID, $option->packageID);
+                                       $saveOptions[$option->optionID] = I18nHandler::getInstance()->getValue($option->optionName);
+                               }
+                               else {
+                                       I18nHandler::getInstance()->save($option->optionName, 'wcf.option.option' . $option->optionID, 'wcf.option', $option->packageID);
+                                       $saveOptions[$option->optionID] = 'wcf.option.option' . $option->optionID;
+                               }
+                       }
+                       else {
+                               $saveOptions[$option->optionID] = $this->optionValues[$option->optionName];
+                       }
                }
                $optionAction = new OptionAction(array(), 'updateAll', array('data' => $saveOptions));
                $optionAction->executeAction();
@@ -102,7 +121,10 @@ class OptionForm extends AbstractOptionListForm {
                                $this->optionValues[$option->optionName] = $option->optionValue;
                        }
                }
+               
+               // load option tree
                $this->optionTree = $this->getOptionTree($this->category->categoryName);
+               
                if (!count($_POST)) {
                        $this->activeTabMenuItem = $this->optionTree[0]['object']->categoryName;
                }
index 41f0b5b36658e1654e1608ed970890e05d3e3004..eccf763ce838aeb3fda4ab1e43cee4be746352e3 100644 (file)
@@ -129,6 +129,9 @@ class I18nHandler extends SingletonFactory {
                                return false;
                        }
                }
+               else if (!isset($this->i18nValues[$elementID]) || empty($this->i18nValues[$elementID])) {
+                       return false;
+               }
                else {
                        foreach ($this->i18nValues[$elementID] as $value) {
                                if (empty($value)) {
@@ -251,8 +254,9 @@ class I18nHandler extends SingletonFactory {
         * @param       string          $value
         * @param       string          $pattern
         */
-       public function setOptions($elementID, $value, $pattern) {
+       public function setOptions($elementID, $packageID, $value, $pattern) {
                $this->elementOptions[$elementID] = array(
+                       'packageID' => $packageID,
                        'pattern' => $pattern,
                        'value' => $value
                );
@@ -260,28 +264,29 @@ class I18nHandler extends SingletonFactory {
        
        /**
         * Assigns element values to template. Using request data once reading
-        * initial database data is explicitly disallowed. Restrictions on package
-        * id apply as explained in save().
+        * initial database data is explicitly disallowed.
         * 
-        * @param       integer         $packageID
         * @param       boolean         $useRequestData
         */
-       public function assignVariables($packageID, $useRequestData = true) {
+       public function assignVariables($useRequestData = true) {
+               $elementValues = array();
+               $elementValuesI18n = array();
+               
                foreach ($this->elementIDs as $elementID) {
-                       $elementValue = '';
-                       $elementI18nValues = array();
+                       $value = '';
+                       $i18nValues = array();
                        
                        // use POST values instead of querying database
                        if ($useRequestData) {
                                if ($this->isPlainValue($elementID)) {
-                                       $elementValue = $this->getValue($elementID);
+                                       $value = $this->getValue($elementID);
                                }
                                else {
                                        if ($this->hasI18nValues($elementID)) {
-                                               $elementI18nValues = $this->i18nValues[$elementID];
+                                               $i18nValues = $this->i18nValues[$elementID];
                                        }
                                        else {
-                                               $elementI18nValues = array();
+                                               $i18nValues = array();
                                        }
                                }
                        }
@@ -295,24 +300,26 @@ class I18nHandler extends SingletonFactory {
                                        $statement = WCF::getDB()->prepareStatement($sql);
                                        $statement->execute(array(
                                                $this->elementOptions[$elementID]['value'],
-                                               $packageID
-                                               ));
+                                               $this->elementOptions[$elementID]['packageID']
+                                       ));
                                        while ($row = $statement->fetchArray()) {
-                                               $elementI18nValues[$row['languageID']] = $row['languageItemValue'];
+                                               $i18nValues[$row['languageID']] = $row['languageItemValue'];
                                        }
-                                       
                                }
                                else {
                                        // use data provided by setOptions()
-                                       $elementValue = $this->elementOptions[$elementID]['value'];
+                                       $value = $this->elementOptions[$elementID]['value'];
                                }
                        }
                        
-                       WCF::getTPL()->assign(array(
-                               'availableLanguages' => LanguageFactory::getInstance()->getLanguages(),
-                               $elementID => $elementValue,
-                               $elementID.'_i18n' => $elementI18nValues
-                       ));
+                       $elementValues[$elementID] = $value;
+                       $elementValuesI18n[$elementID] = $i18nValues;
                }
+               
+               WCF::getTPL()->assign(array(
+                       'availableLanguages' => LanguageFactory::getInstance()->getLanguages(),
+                       'i18nPlainValues' => $elementValues,
+                       'i18nValues' => $elementValuesI18n
+               ));
        }
 }
index 96a9982e7672aee21715cb46dc8d1e835afb8a5f..695e0de0898f62af829bea62301d3d4481a12f24 100644 (file)
@@ -13,6 +13,12 @@ use wcf\data\option\Option;
  * @category   Community Framework
  */
 abstract class AbstractOptionType implements IOptionType {
+       /**
+        * internationalization support
+        * @var boolean
+        */
+       protected $supportI18n = false;
+       
        /**
         * @see wcf\system\option\IOptionType::validate()
         */
@@ -31,4 +37,11 @@ abstract class AbstractOptionType implements IOptionType {
        public function getCSSClassName() {
                return '';
        }
+       
+       /**
+        * @see wcf\system\option\IOptionType::supportI18n()
+        */
+       public function supportI18n() {
+               return $this->supportI18n;
+       }
 }
index 4b0da9be1c3c2fa80c5148b10cf2b01231dd7e98..b44ae870238c117e9c1644741be3241c5d60bbc5 100644 (file)
@@ -48,4 +48,11 @@ interface IOptionType {
         * @return      string
         */
        public function getCSSClassName();
+       
+       /**
+        * Returns true, if options supports internationalization .
+        * 
+        * @return      boolean
+        */
+       public function supportI18n();
 }
diff --git a/wcfsetup/install/files/lib/system/option/TextI18nOptionType.class.php b/wcfsetup/install/files/lib/system/option/TextI18nOptionType.class.php
new file mode 100644 (file)
index 0000000..05a2bf2
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+namespace wcf\system\option;
+use wcf\data\option\Option;
+use wcf\system\language\I18nHandler;
+use wcf\system\WCF;
+
+class TextI18nOptionType extends TextOptionType {
+       /**
+        * @see wcf\system\option\AbstractOptionType::$supportI18n
+        */
+       protected $supportI18n = true;
+       
+       /**
+        * @see wcf\system\option\IOptionType::getFormElement()
+        */
+       public function getFormElement(Option $option, $value) {
+               $useRequestData = (count($_POST)) ? true : false;
+               I18nHandler::getInstance()->assignVariables($useRequestData);
+               
+               WCF::getTPL()->assign(array(
+                       'option' => $option,
+                       'inputType' => $this->inputType,
+                       'value' => $value
+               ));
+               return WCF::getTPL()->fetch('optionTypeTextI18n');
+       }
+       
+       /**
+        * @see wcf\system\option\IOptionType::validate()
+        */
+       public function validate(Option $option, $newValue) {
+               if (!I18nHandler::getInstance()->validateValue($option->optionName)) {
+                       throw new UserInputException($option->optionName, 'validationFailed');
+               }
+       }
+}