Added i18n import capabilities
authorAlexander Ebert <ebert@woltlab.com>
Fri, 21 Jul 2017 15:35:21 +0000 (17:35 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 21 Jul 2017 15:35:28 +0000 (17:35 +0200)
See WoltLab/com.woltlab.wcf.exporter#24

wcfsetup/install/files/lib/system/importer/AbstractImporter.class.php
wcfsetup/install/files/lib/system/importer/UserGroupImporter.class.php

index 497f4bcd34ddf02dcbfd490806b27e0e8514b684..299f515ddc6a47815ebbfca62bf5855da0089beb 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 namespace wcf\system\importer;
+use wcf\data\package\PackageCache;
+use wcf\system\WCF;
 
 /**
  * Basic implementation of IImporter.
@@ -22,4 +24,57 @@ abstract class AbstractImporter implements IImporter {
        public function getClassName() {
                return $this->className;
        }
+       
+       /**
+        * Imports a list of language items.
+        * 
+        * @param       string[][]      $items
+        * @param       string          $languageCategory
+        * @param       string          $package
+        */
+       protected function importI18nValues(array $items, $languageCategory, $package) {
+               // get package id
+               $packageID = PackageCache::getInstance()->getPackageID($package);
+               
+               $sql = "INSERT INTO             wcf".WCF_N."_language_item
+                                               (languageID, languageItem, languageItemValue, languageCategoryID, packageID)
+                       VALUES                  (?, ?, ?, ?, ?)
+                       ON DUPLICATE KEY
+                       UPDATE                  languageItemValue = VALUES(languageItemValue)";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               WCF::getDB()->beginTransaction();
+               foreach ($items as $itemData) {
+                       $statement->execute([
+                               $itemData['languageID'],
+                               $itemData['languageItem'],
+                               $itemData['languageItemValue'],
+                               $this->getLanguageCategoryID($languageCategory),
+                               $packageID
+                       ]);
+               }
+               WCF::getDB()->commitTransaction();
+       }
+       
+       /**
+        * Returns the language category id.
+        * 
+        * @param       string          $languageCategory
+        * @return      integer
+        */
+       protected function getLanguageCategoryID($languageCategory) {
+               static $languageCategoryIDs = [];
+               
+               if (!isset($languageCategoryIDs[$languageCategory])) {
+                       // get language category id
+                       $sql = "SELECT  languageCategoryID
+                               FROM    wcf" . WCF_N . "_language_category
+                               WHERE   languageCategory = ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute([$languageCategory]);
+                       
+                       $languageCategoryIDs[$languageCategory] = $statement->fetchSingleColumn();
+               }
+               
+               return $languageCategoryIDs[$languageCategory];
+       }
 }
index e85b840f4f5c0001b62a6da30a138688d6d045c1..58decfdb79166d9918fcb39e4d643b319bec6de4 100644 (file)
@@ -2,11 +2,12 @@
 namespace wcf\system\importer;
 use wcf\data\user\group\UserGroup;
 use wcf\data\user\group\UserGroupAction;
+use wcf\data\user\group\UserGroupEditor;
 
 /**
  * Imports user groups.
  * 
- * @author     Marcel Werk
+ * @author     Alexander Ebert, Marcel Werk
  * @copyright  2001-2017 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System\Importer
@@ -29,7 +30,40 @@ class UserGroupImporter extends AbstractImporter {
                                'data' => $data
                        ]);
                        $returnValues = $action->executeAction();
-                       $newGroupID = $returnValues['returnValues']->groupID;
+                       $group = $returnValues['returnValues'];
+                       $newGroupID = $group->groupID;
+                       
+                       // handle i18n values
+                       if (!empty($additionalData['i18n'])) {
+                               $values = [];
+                               
+                               foreach (['groupName', 'groupDescription'] as $property) {
+                                       if (isset($additionalData['i18n'][$property])) {
+                                               $values[$property] = $additionalData['i18n'][$property];
+                                       }
+                               }
+                               
+                               if (!empty($values)) {
+                                       $updateData = [];
+                                       if (isset($values['groupName'])) $updateData['groupName'] = 'wcf.acp.group.group' . $newGroupID;
+                                       if (isset($values['groupDescription'])) $updateData['groupDescription'] = 'wcf.acp.group.groupDescription' . $newGroupID;
+                                       
+                                       $items = [];
+                                       foreach ($values as $property => $propertyValues) {
+                                               foreach ($propertyValues as $languageID => $languageItemValue) {
+                                                       $items[] = [
+                                                               'languageID' => $languageID,
+                                                               'languageItem' => 'wcf.acp.group.' . ($property === 'description' ? 'groupDescription' : 'group') . $newGroupID,
+                                                               'languageItemValue' => $languageItemValue
+                                                       ];
+                                               }
+                                       }
+                                       
+                                       $this->importI18nValues($items, 'wcf.acp.group', 'com.woltlab.wcf');
+                                       
+                                       (new UserGroupEditor($group))->update($updateData);
+                               }
+                       }
                }
                
                ImportHandler::getInstance()->saveNewID('com.woltlab.wcf.user.group', $oldID, $newGroupID);