Added option to insert a new page into the main menu
authorMarcel Werk <burntime@woltlab.com>
Sun, 19 Mar 2017 13:02:55 +0000 (14:02 +0100)
committerMarcel Werk <burntime@woltlab.com>
Sun, 19 Mar 2017 13:03:07 +0000 (14:03 +0100)
Closes #2204

wcfsetup/install/files/acp/templates/pageAdd.tpl
wcfsetup/install/files/lib/acp/form/PageAddForm.class.php
wcfsetup/install/files/lib/data/menu/MenuCache.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index ac22caa14b6b8f50908b65f33fb7192d22166390..98375e7861de98f0a58f15a0356c927fb7e3c64a 100644 (file)
                                        </dd>
                                </dl>
                                
+                               {if $action == 'add'}
+                                       <dl>
+                                               <dt></dt>
+                                               <dd>
+                                                       <label><input type="checkbox" id="addPageToMainMenu" name="addPageToMainMenu" value="1"{if $addPageToMainMenu} checked{/if}> {lang}wcf.acp.page.addPageToMainMenu{/lang}</label>
+                                                       
+                                                       <script data-relocate="true">
+                                                               elById('addPageToMainMenu').addEventListener('change', function() {
+                                                                       if (this.checked) {
+                                                                               elShow(elById('parentMenuItemDl'));
+                                                                       }
+                                                                       else {
+                                                                               elHide(elById('parentMenuItemDl'));
+                                                                       }
+                                                               });
+                                                       </script>
+                                               </dd>
+                                       </dl>
+                                       
+                                       <dl id="parentMenuItemDl"{if $errorField == 'parentMenuItemID'} class="formError"{/if}{if !$addPageToMainMenu} style="display: none"{/if}>
+                                               <dt><label for="parentMenuItemID">{lang}wcf.acp.menu.item.parentItem{/lang}</label></dt>
+                                               <dd>
+                                                       <select name="parentMenuItemID" id="parentMenuItemID">
+                                                               <option value="0">{lang}wcf.global.noSelection{/lang}</option>
+                                                               
+                                                               {foreach from=$menuItemNodeList item=menuItemNode}
+                                                                       <option value="{@$menuItemNode->itemID}"{if $menuItemNode->itemID == $parentMenuItemID} selected{/if}>{if $menuItemNode->getDepth() > 1}{@"&nbsp;&nbsp;&nbsp;&nbsp;"|str_repeat:($menuItemNode->getDepth() - 1)}{/if}{lang}{$menuItemNode->title}{/lang}</option>
+                                                               {/foreach}
+                                                       </select>
+                                                       {if $errorField == 'parentMenuItemID'}
+                                                               <small class="innerError">
+                                                                       {if $errorType == 'empty'}
+                                                                               {lang}wcf.global.form.error.empty{/lang}
+                                                                       {else}
+                                                                               {lang}wcf.acp.page.parentMenuItem.error.{@$errorType}{/lang}
+                                                                       {/if}
+                                                               </small>
+                                                       {/if}
+                                               </dd>
+                                       </dl>
+                               {/if}
+                               
                                {event name='dataFields'}
                        </div>
                </div>
                                        {foreach from=$availableLanguages item=availableLanguage}
                                                <div id="language{@$availableLanguage->languageID}" class="tabMenuContent">
                                                        <div class="section">
-                                                               <dl{if $errorField == 'title'} class="formError"{/if}>
+                                                               {assign var='__errorFieldName' value='title_'|concat:$availableLanguage->languageID}
+                                                               <dl{if $errorField == $__errorFieldName} class="formError"{/if}>
                                                                        <dt><label for="title{@$availableLanguage->languageID}">{lang}wcf.global.title{/lang}</label></dt>
                                                                        <dd>
                                                                                <input type="text" id="title{@$availableLanguage->languageID}" name="title[{@$availableLanguage->languageID}]" value="{if !$title[$availableLanguage->languageID]|empty}{$title[$availableLanguage->languageID]}{/if}" class="long" maxlength="255">
-                                                                               {if $errorField == 'title'}
+                                                                               {if $errorField == $__errorFieldName}
                                                                                        <small class="innerError">
                                                                                                {if $errorType == 'empty'}
                                                                                                        {lang}wcf.global.form.error.empty{/lang}
                                                                </dl>
                                                                
                                                                {if $pageType != 'system'}
-                                                                       <dl{if $errorField == 'content'} class="formError"{/if}>
+                                                                       {assign var='__errorFieldName' value='content_'|concat:$availableLanguage->languageID}
+                                                                       <dl{if $errorField == $__errorFieldName} class="formError"{/if}>
                                                                                <dt><label for="content{@$availableLanguage->languageID}">{lang}wcf.acp.page.content{/lang}</label></dt>
                                                                                <dd>
                                                                                        {include file='__pageAddContent' languageID=$availableLanguage->languageID}
                                                                                        
-                                                                                       {if $errorField == 'content'}
+                                                                                       {if $errorField == $__errorFieldName}
                                                                                                <small class="innerError">
                                                                                                        {if $errorType == 'empty'}
                                                                                                                {lang}wcf.global.form.error.empty{/lang}
                                                                                </dd>
                                                                        </dl>
                                                                        
-                                                                       <dl{if $errorField == 'metaDescription'} class="formError"{/if}>
+                                                                       {assign var='__errorFieldName' value='metaDescription_'|concat:$availableLanguage->languageID}
+                                                                       <dl{if $errorField == $__errorFieldName} class="formError"{/if}>
                                                                                <dt><label for="metaDescription{@$availableLanguage->languageID}">{lang}wcf.acp.page.metaDescription{/lang}</label></dt>
                                                                                <dd>
                                                                                        <input type="text" class="long" name="metaDescription[{@$availableLanguage->languageID}]" id="metaDescription{@$availableLanguage->languageID}" value="{if !$metaDescription[$availableLanguage->languageID]|empty}{$metaDescription[$availableLanguage->languageID]}{/if}">
-                                                                                       {if $errorField == 'metaDescription'}
+                                                                                       {if $errorField == $__errorFieldName}
                                                                                                <small class="innerError">
                                                                                                        {if $errorType == 'empty'}
                                                                                                                {lang}wcf.global.form.error.empty{/lang}
                                                                                </dd>
                                                                        </dl>
                                                                        
-                                                                       <dl{if $errorField == 'metaKeywords'} class="formError"{/if}>
+                                                                       {assign var='__errorFieldName' value='metaKeywords_'|concat:$availableLanguage->languageID}
+                                                                       <dl{if $errorField == $__errorFieldName} class="formError"{/if}>
                                                                                <dt><label for="metaKeywords{@$availableLanguage->languageID}">{lang}wcf.acp.page.metaKeywords{/lang}</label></dt>
                                                                                <dd>
                                                                                        <input type="text" class="long" name="metaKeywords[{@$availableLanguage->languageID}]" id="metaKeywords{@$availableLanguage->languageID}" value="{if !$metaKeywords[$availableLanguage->languageID]|empty}{$metaKeywords[$availableLanguage->languageID]}{/if}">
-                                                                                       {if $errorField == 'metaKeywords'}
+                                                                                       {if $errorField == $__errorFieldName}
                                                                                                <small class="innerError">
                                                                                                        {if $errorType == 'empty'}
                                                                                                                {lang}wcf.global.form.error.empty{/lang}
index 02bcf7ec639028a9b370b382988601721242687e..1715eb70544041089ba05932eab07686c7ab3638 100644 (file)
@@ -5,6 +5,11 @@ use wcf\data\application\ApplicationList;
 use wcf\data\box\Box;
 use wcf\data\box\BoxList;
 use wcf\data\language\Language;
+use wcf\data\menu\item\MenuItem;
+use wcf\data\menu\item\MenuItemAction;
+use wcf\data\menu\item\MenuItemEditor;
+use wcf\data\menu\item\MenuItemNodeTree;
+use wcf\data\menu\MenuCache;
 use wcf\data\page\Page;
 use wcf\data\page\PageAction;
 use wcf\data\page\PageEditor;
@@ -14,6 +19,7 @@ use wcf\system\acl\simple\SimpleAclHandler;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\UserInputException;
 use wcf\system\html\input\HtmlInputProcessor;
+use wcf\system\language\I18nHandler;
 use wcf\system\language\LanguageFactory;
 use wcf\system\request\LinkHandler;
 use wcf\system\request\RouteHandler;
@@ -167,6 +173,24 @@ class PageAddForm extends AbstractForm {
         */
        public $allowSpidersToIndex = 1;
        
+       /**
+        * true if page should be added to the main menu
+        * @var boolean
+        */
+       public $addPageToMainMenu = 0;
+       
+       /**
+        * parent menu item id
+        * @var integer
+        */
+       public $parentMenuItemID = null;
+       
+       /**
+        * menu item node tree
+        * @var MenuItemNodeTree
+        */
+       public $menuItems = null;
+       
        /**
         * @inheritDoc
         */
@@ -227,7 +251,9 @@ class PageAddForm extends AbstractForm {
                if (isset($_POST['isLandingPage'])) $this->isLandingPage = 1;
                if (isset($_POST['availableDuringOfflineMode'])) $this->availableDuringOfflineMode = 1;
                if (isset($_POST['allowSpidersToIndex'])) $this->allowSpidersToIndex = 1;
+               if (isset($_POST['addPageToMainMenu'])) $this->addPageToMainMenu = 1;
                if (isset($_POST['applicationPackageID'])) $this->applicationPackageID = intval($_POST['applicationPackageID']);
+               if (!empty($_POST['parentMenuItemID'])) $this->parentMenuItemID = intval($_POST['parentMenuItemID']);
                
                if (isset($_POST['customURL']) && is_array($_POST['customURL'])) $this->customURL = array_map('mb_strtolower', ArrayUtil::trim($_POST['customURL']));
                if (isset($_POST['title']) && is_array($_POST['title'])) $this->title = ArrayUtil::trim($_POST['title']);
@@ -255,8 +281,12 @@ class PageAddForm extends AbstractForm {
                
                $this->validateApplicationPackageID();
                
+               $this->validateParentMenuItemID();
+               
                $this->validateCustomUrls();
                
+               $this->validateTitle();
+               
                $this->validateBoxIDs();
                
                if ($this->pageType == 'text') {
@@ -381,6 +411,42 @@ class PageAddForm extends AbstractForm {
                }
        }
        
+       /**
+        * Validates page title.
+        * 
+        * @throws UserInputException
+        */
+       protected function validateTitle() {
+               if ($this->addPageToMainMenu) {
+                       if ($this->isMultilingual) {
+                               foreach ($this->availableLanguages as $language) {
+                                       if (empty($this->title[$language->languageID])) {
+                                               throw new UserInputException('title_' . $language->languageID);
+                                       }
+                               }
+                       }
+                       else {
+                               if (empty($this->title[0])) {
+                                       throw new UserInputException('title');
+                               }
+                       }
+               }
+       }
+       
+       /**
+        * Validates parent menu item id.
+        *
+        * @throws      UserInputException
+        */
+       protected function validateParentMenuItemID() {
+               if ($this->addPageToMainMenu && $this->parentMenuItemID) {
+                       $parentMenuItem = new MenuItem($this->parentMenuItemID);
+                       if (!$parentMenuItem->itemID || $parentMenuItem->menuID != MenuCache::getInstance()->getMainMenuID()) {
+                               throw new UserInputException('parentMenuItemID', 'invalid');
+                       }
+               }
+       }
+       
        /**
         * Validates box ids.
         * 
@@ -486,6 +552,33 @@ class PageAddForm extends AbstractForm {
                // save acl
                SimpleAclHandler::getInstance()->setValues('com.woltlab.wcf.page', $page->pageID, $this->aclValues);
                
+               // add page to main menu
+               if ($this->addPageToMainMenu) {
+                       $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,
+                               'identifier' => StringUtil::getRandomID(),
+                               'packageID' => 1
+                       ]]);
+                       $menuItemAction->executeAction();
+                       
+                       if ($this->isMultilingual) {
+                               $returnValues = $menuItemAction->getReturnValues();
+                               $menuItem = $returnValues['returnValues'];
+                               
+                               $data = ['identifier' => 'com.woltlab.wcf.generic' . $menuItem->itemID];
+                               $data['title'] = 'wcf.menu.item.' . $data['identifier'];
+                               I18nHandler::getInstance()->setValues('title', $this->title);
+                               I18nHandler::getInstance()->save('title', $data['title'], 'wcf.menu');
+                               
+                               $menuItemEditor = new MenuItemEditor($menuItem);
+                               $menuItemEditor->update($data);
+                       }
+               }
+               
                // call saved event
                $this->saved();
                
@@ -511,6 +604,8 @@ class PageAddForm extends AbstractForm {
                                if ($box->visibleEverywhere) $this->boxIDs[] = $box->boxID;
                        }
                }
+               
+               $this->menuItems = new MenuItemNodeTree(MenuCache::getInstance()->getMainMenuID(), null, false);
        }
        
        /**
@@ -541,7 +636,10 @@ class PageAddForm extends AbstractForm {
                        'availableLanguages' => $this->availableLanguages,
                        'availableBoxes' => $this->availableBoxes,
                        'pageNodeList' => (new PageNodeTree())->getNodeList(),
-                       'aclValues' => SimpleAclHandler::getInstance()->getOutputValues($this->aclValues)
+                       'aclValues' => SimpleAclHandler::getInstance()->getOutputValues($this->aclValues),
+                       'addPageToMainMenu' => $this->addPageToMainMenu,
+                       'parentMenuItemID' => $this->parentMenuItemID,
+                       'menuItemNodeList' => $this->menuItems->getNodeList()
                ]);
        }
 }
index bab28f1de50c5f0893041a4a9fc620821aa1452c..ec82279391d2f09e0f95c4ead875ef90f0396853 100644 (file)
@@ -66,6 +66,15 @@ class MenuCache extends SingletonFactory {
         * @return      Menu|null       menu object
         */
        public function getMainMenu() {
-               return $this->getMenuByID(MenuCacheBuilder::getInstance()->getData([], 'mainMenuID'));
+               return $this->getMenuByID($this->getMainMenuID());
+       }
+       
+       /**
+        * Returns the id of the main menu.
+        * 
+        * @return      integer
+        */
+       public function getMainMenuID() {
+               return MenuCacheBuilder::getInstance()->getData([], 'mainMenuID');
        }
 }
index 02a105bc19ab5009ef27276c6943ff2b6a3e4da0..5c98005c613f12a19f9157a0edd4757f1ea0d960 100644 (file)
@@ -1427,6 +1427,7 @@ GmbH=Gesellschaft mit beschränkter Haftung]]></item>
                <item name="wcf.acp.page.cssClassName"><![CDATA[CSS-Klassen]]></item>
                <item name="wcf.acp.page.availableDuringOfflineMode"><![CDATA[Seite ist im Wartungsmodus aufrufbar]]></item>
                <item name="wcf.acp.page.allowSpidersToIndex"><![CDATA[Seite darf von Suchmaschinen-Robotern indiziert werden]]></item>
+               <item name="wcf.acp.page.addPageToMainMenu"><![CDATA[Seiten automatisch in das Hauptmenü aufnehmen]]></item>
        </category>
        
        <category name="wcf.acp.paidSubscription">
index d5aad08461104d77c89f94e2ff0b4c0efbcca78d..495d8fe81173fdf15155b2930e58e62a6cfd2d37 100644 (file)
@@ -2898,6 +2898,7 @@ Errors are:
                <item name="wcf.acp.page.cssClassName"><![CDATA[CSS Class Name]]></item>
                <item name="wcf.acp.page.availableDuringOfflineMode"><![CDATA[Page is available during maintenance mode]]></item>
                <item name="wcf.acp.page.allowSpidersToIndex"><![CDATA[Allow search spiders to index this page]]></item>
+               <item name="wcf.acp.page.addPageToMainMenu"><![CDATA[Add page to main menu]]></item>
        </category>
        
        <category name="wcf.paidSubscription">