Append (instead of prepend) menu item during page creation
authorMatthias Schmidt <gravatronics@live.com>
Sun, 28 Jul 2019 13:17:59 +0000 (15:17 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 28 Jul 2019 13:17:59 +0000 (15:17 +0200)
See #2998

wcfsetup/install/files/lib/acp/form/PageAddForm.class.php

index 63d4cc71f42b5918d7878f7225acc0b253df1f41..ce2b54da9d40d1e6137716837b68976a0960c18a 100644 (file)
@@ -17,6 +17,7 @@ use wcf\data\page\PageNodeTree;
 use wcf\data\smiley\SmileyCache;
 use wcf\form\AbstractForm;
 use wcf\system\acl\simple\SimpleAclHandler;
+use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\UserInputException;
 use wcf\system\html\input\HtmlInputProcessor;
@@ -589,12 +590,29 @@ class PageAddForm extends AbstractForm {
                
                // add page to main menu
                if ($this->addPageToMainMenu) {
+                       // select maximum showOrder value so that new menu item will be appened
+                       $conditionBuilder = new PreparedStatementConditionBuilder();
+                       if ($this->parentMenuItemID) {
+                               $conditionBuilder->add('parentItemID = ?', [$this->parentMenuItemID]);
+                       }
+                       else {
+                               $conditionBuilder->add('parentItemID IS NULL');
+                       }
+                       
+                       $sql = "SELECT  MAX(showOrder)
+                               FROM    wcf" . WCF_N . "_menu_item
+                               " . $conditionBuilder;
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute($conditionBuilder->getParameters());
+                       $maxShowOrder = $statement->fetchSingleColumn() ?? 0;
+                       
                        $menuItemAction = new MenuItemAction([], 'create', ['data' => [
                                'isDisabled' => $this->isDisabled ? 1 : 0,
                                'title' => (!$this->isMultilingual ? $this->title[0] : ''),
                                'pageID' => $page->pageID,
                                'menuID' => MenuCache::getInstance()->getMainMenuID(),
                                'parentItemID' => $this->parentMenuItemID,
+                               'showOrder' => $maxShowOrder + 1,
                                'identifier' => StringUtil::getRandomID(),
                                'packageID' => 1
                        ]]);