Added PIP for menus
authorAlexander Ebert <ebert@woltlab.com>
Mon, 30 Nov 2015 13:51:04 +0000 (14:51 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 30 Nov 2015 13:51:04 +0000 (14:51 +0100)
wcfsetup/install/files/lib/data/menu/MenuEditor.class.php
wcfsetup/install/files/lib/system/package/plugin/MenuPackageInstallationPlugin.class.php [new file with mode: 0644]
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index ec2d8f1c9da9d690548fea6cdd913089291d0e86..a7d166aa326acfba024839517ccfaf96f1e1f546 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 namespace wcf\data\menu;
 use wcf\data\DatabaseObjectEditor;
+use wcf\system\language\LanguageFactory;
+use wcf\system\WCF;
 
 /**
  * Provides functions to edit menus.
@@ -17,4 +19,57 @@ class MenuEditor extends DatabaseObjectEditor {
         * @see \wcf\data\DatabaseObjectDecorator::$baseClass
         */
        protected static $baseClass = 'wcf\data\menu\Menu';
+       
+       /**
+        * @inheritDoc
+        */
+       public static function create(array $parameters = []) {
+               $title = '';
+               if (is_array($parameters['title'])) {
+                       $title = $parameters['title'];
+                       $parameters['title'] = '';
+               }
+               
+               $menu = parent::create($parameters);
+               
+               if (is_array($title)) {
+                       if (count($title) > 1) {
+                               $sql = "SELECT  languageCategoryID
+                                       FROM    wcf".WCF_N."_language_category
+                                       WHERE   languageCategory = ?";
+                               $statement = WCF::getDB()->prepareStatement($sql, 1);
+                               $statement->execute(['wcf.menu']);
+                               $languageCategoryID = $statement->fetchSingleColumn();
+                               
+                               $sql = "INSERT INTO     wcf".WCF_N."_language_item
+                                                       (languageID, languageItem, languageItemValue, languageItemOriginIsSystem, languageCategoryID, packageID)
+                                       VALUES          (?, ?, ?, ?, ?, ?)";
+                               $statement = WCF::getDB()->prepareStatement($sql);
+                               
+                               WCF::getDB()->beginTransaction();
+                               foreach ($title as $languageCode => $value) {
+                                       $statement->execute([
+                                               LanguageFactory::getInstance()->getLanguageByCode($languageCode)->languageID,
+                                               'wcf.menu.menu' . $menu->menuID,
+                                               $value,
+                                               1,
+                                               $languageCategoryID,
+                                               $menu->packageID
+                                       ]);
+                               }
+                               WCF::getDB()->commitTransaction();
+                               
+                               $title = 'wcf.menu.menu' . $menu->menuID;
+                       }
+                       else {
+                               $title = reset($title);
+                       }
+                       
+                       //$menuEditor = new MenuItemEditor($menu);
+                       $menuEditor = new self($menu);
+                       $menuEditor->update(['title' => $title]);
+               }
+               
+               return $menu;
+       }
 }
diff --git a/wcfsetup/install/files/lib/system/package/plugin/MenuPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/MenuPackageInstallationPlugin.class.php
new file mode 100644 (file)
index 0000000..9bb9e0f
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+namespace wcf\system\package\plugin;
+use wcf\data\menu\Menu;
+use wcf\data\menu\MenuEditor;
+use wcf\system\exception\SystemException;
+use wcf\system\WCF;
+
+/**
+ * Installs, updates and deletes menus.
+ * 
+ * @author     Alexander Ebert
+ * @copyright  2001-2015 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage acp.package.plugin
+ * @category   Community Framework
+ */
+class MenuPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin {
+       /**
+        * @inheritDoc
+        */
+       public $className = MenuEditor::class;
+       
+       /**
+        * @inheritDoc
+        */
+       public $tagName = 'menu';
+       
+       /**
+        * @inheritDoc
+        */
+       protected function handleDelete(array $items) {
+               $sql = "DELETE FROM     wcf".WCF_N."_menu
+                       WHERE           identifier = ?
+                                       AND packageID = ?";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               
+               WCF::getDB()->beginTransaction();
+               foreach ($items as $item) {
+                       $statement->execute([
+                               $item['attributes']['identifier'],
+                               $this->installation->getPackageID()
+                       ]);
+               }
+               WCF::getDB()->commitTransaction();
+       }
+       
+       /**
+        * @inheritDoc
+        * @throws      SystemException
+        */
+       protected function getElement(\DOMXPath $xpath, array &$elements, \DOMElement $element) {
+               $nodeValue = $element->nodeValue;
+               
+               if ($element->tagName === 'title') {
+                       if (empty($element->getAttribute('language'))) {
+                               throw new SystemException("Missing required attribute 'language' for menu '" . $element->parentNode->getAttribute('identifier') . "'");
+                       }
+                       
+                       // <title> can occur multiple times using the `language` attribute
+                       if (!isset($elements['title'])) $elements['title'] = [];
+                       
+                       $elements['title'][$element->getAttribute('language')] = $element->nodeValue;
+               }
+               else {
+                       $elements[$element->tagName] = $nodeValue;
+               }
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       protected function prepareImport(array $data) {
+               return [
+                       'identifier' => $data['attributes']['identifier'],
+                       'title' => $this->getI18nValues($data['elements']['title']),
+                       'originIsSystem' => 1
+               ];
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       protected function findExistingItem(array $data) {
+               $sql = "SELECT  *
+                       FROM    wcf".WCF_N."_menu
+                       WHERE   identifier = ?
+                               AND packageID = ?";
+               $parameters = array(
+                       $data['identifier'],
+                       $this->installation->getPackageID()
+               );
+               
+               return array(
+                       'sql' => $sql,
+                       'parameters' => $parameters
+               );
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       protected function import(array $row, array $data) {
+               // updating menus is not supported because the only modifiable data is the
+               // title and overwriting it could conflict with user changes
+               if (!empty($row)) {
+                       return new Menu(null, $row);
+               }
+               
+               return parent::import($row, $data);
+       }
+}
index 910028319cf64d4229341b645634fbd62b1ce457..81dfc259755e364b9ccbb0bfbf1f5c04983b0b0f 100644 (file)
@@ -2380,6 +2380,10 @@ Fehler sind beispielsweise:
                <item name="wcf.message.error.tooLong"><![CDATA[Ihre Nachricht ist zu lang. Es stehen maximal {#$maxTextLength} Zeichen zur Verfügung.]]></item>
        </category>
        
+       <category name="wcf.menu">
+               <!-- category for menus and menu items -->
+       </category>
+       
        <category name="wcf.moderation">
                <item name="wcf.moderation.assignedUser"><![CDATA[Zugewiesener Benutzer]]></item>
                <item name="wcf.moderation.assignedUser.change"><![CDATA[(Ändern)]]></item>
index fdb396d3582ca9dd4958b9597e3dd14519a8c843..8b74c302ea0e6aa188cbc333b68ed3c5418a25a3 100644 (file)
@@ -2377,6 +2377,10 @@ Errors are:
                <item name="wcf.message.error.tooLong"><![CDATA[Message is too long, must be below {#$maxTextLength} characters.]]></item>
        </category>
        
+       <category name="wcf.menu">
+               <!-- category for menus and menu items -->
+       </category>
+       
        <category name="wcf.moderation">
                <item name="wcf.moderation.assignedUser"><![CDATA[Assigned User]]></item>
                <item name="wcf.moderation.assignedUser.change"><![CDATA[(Change)]]></item>