Attempt to fix show order for page menu items on create/update
authorAlexander Ebert <ebert@woltlab.com>
Thu, 16 May 2013 21:01:51 +0000 (23:01 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 16 May 2013 21:01:51 +0000 (23:01 +0200)
Fixes #1120
Fixes #1193

wcfsetup/install/files/lib/system/package/plugin/AbstractMenuPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/AbstractXMLPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/PageMenuPackageInstallationPlugin.class.php

index 99dbf0a7e033b2bf7fd7f10e15c250f3b2ea914b..d1777f3b749cef15918b3052f1908d69b80b8577 100644 (file)
@@ -7,7 +7,7 @@ use wcf\system\WCF;
  * Abstract implementation of a package installation plugin for menu items.
  * 
  * @author     Alexander Ebert
- * @copyright  2001-2012 WoltLab GmbH
+ * @copyright  2001-2013 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    com.woltlab.wcf
  * @subpackage system.package.plugin
index 6862f462b7c8c6f8ed47ed1c0e1aefc29d1c9cbf..b0af1d24c3933740a20721b10b3cc3a0026cd632 100644 (file)
@@ -10,8 +10,8 @@ use wcf\util\XML;
 /**
  * Abstract implementation of a package installation plugin using a XML file.
  * 
- * @author     Marcel Werk
- * @copyright  2001-2012 WoltLab GmbH
+ * @author     Alexander Ebert
+ * @copyright  2001-2013 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    com.woltlab.wcf
  * @subpackage system.package.plugin
index cd1037e858f16dbe94f3dcb255f1f4b8d371b19b..dab7b2f05534671cef343a8e028be1e8bda8ceb0 100644 (file)
@@ -1,7 +1,9 @@
 <?php
 namespace wcf\system\package\plugin;
 use wcf\data\page\menu\item\PageMenuItemEditor;
+use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\SystemException;
+use wcf\system\WCF;
 
 /**
  * Installs, updates and deletes page page menu items.
@@ -48,10 +50,74 @@ class PageMenuPackageInstallationPlugin extends AbstractMenuPackageInstallationP
                PageMenuItemEditor::updateLandingPage();
        }
        
+       /**
+        * @see wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::import()
+        */
+       protected function import(array $row, array $data) {
+               if (!empty($row)) {
+                       // ignore show order if null
+                       if ($data['showOrder'] === null) {
+                               unset($data['showOrder']);
+                       }
+                       else if ($data['showOrder'] != $row['showOrder']) {
+                               $data['showOrder'] = $this->getMenuItemPosition($data);
+                       }
+               }
+               else {
+                       $data['showOrder'] = $this->getMenuItemPosition($data);
+               }
+               
+               parent::import($row, $data);
+       }
+       
        /**
         * @see wcf\system\package\plugin\AbstractXMLPackageInstallationPlugin::getShowOrder()
         */
        protected function getShowOrder($showOrder, $parentName = null, $columnName = null, $tableNameExtension = '') {
+               // will be recalculated anyway
                return $showOrder;
        }
+       
+       /**
+        * Returns menu item position.
+        * 
+        * @param       array           $data
+        * @return      integer
+        */
+       protected function getMenuItemPosition(array $data) {
+               if ($data['showOrder'] === null) {
+                       // get greatest showOrder value
+                       $conditions = new PreparedStatementConditionBuilder();
+                       $conditions->add("menuPosition = ?", array($data['menuPosition']));
+                       if ($data['parentMenuItem']) $conditions->add("parentMenuItem = ?", array($data['parentMenuItem']));
+                       
+                       $sql = "SELECT  MAX(showOrder) AS showOrder
+                               FROM    wcf".WCF_N."_".$this->tableName."
+                               ".$conditions;
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute($conditions->getParameters());
+                       $maxShowOrder = $statement->fetchArray();
+                       return (!$maxShowOrder) ? 1 : ($maxShowOrder['showOrder'] + 1);
+               }
+               else {
+                       // increase all showOrder values which are >= $showOrder
+                       $sql = "UPDATE  wcf".WCF_N."_".$this->tableName."
+                               SET     showOrder = showOrder + 1
+                               WHERE   showOrder >= ?
+                                       AND menuPosition = ?
+                               ".($data['parentMenuItem'] ? "AND parentMenuItem = ?" : "");
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       
+                       $data = array(
+                               $data['showOrder'],
+                               $data['menuPosition']
+                       );
+                       if ($data['parentMenuItem']) $data[] = $data['parentMenuItem'];
+                       
+                       $statement->execute($data);
+                       
+                       // return the wanted showOrder level
+                       return $data['showOrder'];
+               }
+       }
 }