Overhauled language import form
authorMarcel Werk <burntime@woltlab.com>
Thu, 26 May 2016 17:21:07 +0000 (19:21 +0200)
committerMarcel Werk <burntime@woltlab.com>
Thu, 26 May 2016 17:21:14 +0000 (19:21 +0200)
CHANGELOG.md
wcfsetup/install/files/acp/templates/languageImport.tpl
wcfsetup/install/files/lib/acp/form/LanguageImportForm.class.php
wcfsetup/install/files/lib/data/language/LanguageEditor.class.php

index b7eb96edd1da34ae7eda99a963600c0cd4f896cf..76e45d787dd3d19a24e41d25e573b0eb09885fd0 100644 (file)
@@ -55,6 +55,7 @@
 * `\wcf\form\AbstractCaptchaForm`: added parameter to force captcha usage for registered users.
 * Added global disable switch for languages.
 * Overhauled page tracking in sessions / user online locations
+* Overhauled language import form
 
 #### CMS
 
index 89dc5e7b2722aada9f55f1252dd71b7545567ee4..d1c3c8cee715e11404271148a380600864d47d6c 100644 (file)
 
 <form enctype="multipart/form-data" method="post" action="{link controller='LanguageImport'}{/link}">
        <div class="section">
-               <dl{if $errorField == 'languageFile'} class="formError"{/if}>
-                       <dt><label for="languageFile">{lang}wcf.acp.language.import.source.file{/lang}</label></dt>
+               <dl{if $errorField == 'languageUpload'} class="formError"{/if}>
+                       <dt><label for="languageUpload">{lang}wcf.acp.language.import.source.upload{/lang}</label></dt>
                        <dd>
-                               <input type="text" id="languageFile" name="languageFile" value="{$languageFile}" class="long" />
-                               {if $errorField == 'languageFile'}
+                               <input type="file" id="languageUpload" name="languageUpload" />
+                               {if $errorField == 'languageUpload'}
                                        <small class="innerError">
                                                {if $errorType == 'empty'}
                                                        {lang}wcf.global.form.error.empty{/lang}
                                                {/if}
                                        </small>
                                {/if}
-                               <small>{lang}wcf.acp.language.import.source.file.description{/lang}</small>
                        </dd>
                </dl>
                
-               <dl{if $errorField == 'languageUpload'} class="formError"{/if}>
-                       <dt><label for="languageUpload">{lang}wcf.acp.language.import.source.upload{/lang}</label></dt>
+               <dl{if $errorField == 'sourceLanguageID'} class="formError"{/if}>
+                       <dt><label for="sourceLanguageID">{lang}wcf.acp.language.add.source{/lang}</label></dt>
                        <dd>
-                               <input type="file" id="languageUpload" name="languageUpload" />
-                               {if $errorField == 'languageUpload'}
+                               <select id="sourceLanguageID" name="sourceLanguageID">
+                                       {foreach from=$languages item=language}
+                                               <option value="{@$language->languageID}"{if $language->languageID == $sourceLanguageID} selected="selected"{/if}>{$language->languageName} ({$language->languageCode})</option>
+                                       {/foreach}
+                               </select>
+                               {if $errorField == 'sourceLanguageID'}
                                        <small class="innerError">
-                                               {lang}wcf.acp.language.import.error{/lang} {$errorType}
+                                               {if $errorType == 'empty'}
+                                                       {lang}wcf.global.form.error.empty{/lang}
+                                               {else}
+                                                       {lang}wcf.acp.language.add.source.error.{@$errorType}{/lang}
+                                               {/if}
                                        </small>
                                {/if}
+                               <small>{lang}wcf.acp.language.add.source.description{/lang}</small>
                        </dd>
                </dl>
                
index c9d5a016e6165ce9eb9ebd63d80d0ce534eea702..bcd7e9fd537aafb64c84e144c4b1c8453d0b8266 100644 (file)
@@ -38,28 +38,28 @@ class LanguageImportForm extends AbstractForm {
        public $filename = '';
        
        /**
-        * import field
-        * @var string
+        * language object
+        * @var Language
         */
-       public $importField = 'languageFile';
+       public $language;
        
        /**
-        * language object
-        * @var \wcf\data\language\Language
+        * list of available languages
+        * @var Language[]
         */
-       public $language = null;
+       public $languages = [];
        
        /**
-        * import language file
-        * @var string
+        * source language object
+        * @var Language
         */
-       public $languageFile = '';
+       public $sourceLanguage;
        
        /**
-        * list of available languages
-        * @var Language[]
+        * source language id
+        * @var integer
         */
-       public $languages = [];
+       public $sourceLanguageID = 0;
        
        /**
         * @inheritDoc
@@ -67,15 +67,10 @@ class LanguageImportForm extends AbstractForm {
        public function readFormParameters() {
                parent::readFormParameters();
                
-               // import
-               if (isset($_POST['languageFile']) && !empty($_POST['languageFile'])) {
-                       $this->languageFile = $_POST['languageFile'];
-                       $this->filename = $_POST['languageFile'];
-               }
                if (isset($_FILES['languageUpload']) && !empty($_FILES['languageUpload']['tmp_name'])) {
-                       $this->importField = 'languageUpload';
                        $this->filename = $_FILES['languageUpload']['tmp_name'];
                }
+               if (isset($_POST['sourceLanguageID'])) $this->sourceLanguageID = intval($_POST['sourceLanguageID']);
        }
        
        /**
@@ -86,7 +81,17 @@ class LanguageImportForm extends AbstractForm {
                
                // check file
                if (!file_exists($this->filename)) {
-                       throw new UserInputException('languageFile');
+                       throw new UserInputException('languageUpload');
+               }
+               
+               if (empty($this->sourceLanguageID)) {
+                       throw new UserInputException('sourceLanguageID');
+               }
+               
+               // get language
+               $this->sourceLanguage = LanguageFactory::getInstance()->getLanguage($this->sourceLanguageID);
+               if (!$this->sourceLanguage->languageID) {
+                       throw new UserInputException('sourceLanguageID');
                }
                
                // try to import
@@ -96,10 +101,10 @@ class LanguageImportForm extends AbstractForm {
                        $xml->load($this->filename);
                        
                        // import xml document
-                       $this->language = LanguageEditor::importFromXML($xml, -1);
+                       $this->language = LanguageEditor::importFromXML($xml, -1, $this->sourceLanguage);
                }
                catch (SystemException $e) {
-                       throw new UserInputException($this->importField, $e->getMessage());
+                       throw new UserInputException('languageUpload', $e->getMessage());
                }
        }
        
@@ -117,6 +122,15 @@ class LanguageImportForm extends AbstractForm {
                WCF::getTPL()->assign('success', true);
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function readData() {
+               parent::readData();
+               
+               $this->languages = LanguageFactory::getInstance()->getLanguages();
+       }
+       
        /**
         * @inheritDoc
         */
@@ -125,7 +139,7 @@ class LanguageImportForm extends AbstractForm {
                
                WCF::getTPL()->assign([
                        'languages' => $this->languages,
-                       'languageFile' => $this->languageFile
+                       'sourceLanguageID' => $this->sourceLanguageID
                ]);
        }
        
index 41f81e28fbd94171894bb9e046a55e17f2e9c89f..c296a9f2060bfd79bd0ad08d7150d38c8b338a97 100644 (file)
@@ -367,11 +367,12 @@ class LanguageEditor extends DatabaseObjectEditor implements IEditableCachedObje
         * Imports language items from an XML file into a new or a current language.
         * Updates the relevant language files automatically.
         * 
-        * @param       XML     $xml
+        * @param       XML             $xml
         * @param       integer         $packageID
+        * @param       Language        $source
         * @return      LanguageEditor
         */
-       public static function importFromXML(XML $xml, $packageID) {
+       public static function importFromXML(XML $xml, $packageID, Language $source = null) {
                $languageCode = self::readLanguageCodeFromXML($xml);
                
                // try to find an existing language with the given language code
@@ -386,6 +387,11 @@ class LanguageEditor extends DatabaseObjectEditor implements IEditableCachedObje
                                'languageCode' => $languageCode,
                                'languageName' => $languageName
                        ]);
+                       
+                       if ($source) {
+                               $sourceEditor = new LanguageEditor($source);
+                               $sourceEditor->copy($language);
+                       }
                }
                
                // import xml