Improved box to page management
authorMarcel Werk <burntime@woltlab.com>
Sat, 16 Apr 2016 13:37:22 +0000 (15:37 +0200)
committerMarcel Werk <burntime@woltlab.com>
Sat, 16 Apr 2016 13:37:29 +0000 (15:37 +0200)
wcfsetup/install/files/acp/templates/pageAdd.tpl
wcfsetup/install/files/lib/acp/form/PageAddForm.class.php
wcfsetup/install/files/lib/acp/form/PageEditForm.class.php
wcfsetup/install/files/lib/data/page/Page.class.php
wcfsetup/install/files/lib/data/page/PageAction.class.php

index 37ff9dc5b629bc9d0380c9c25644a48b63311c42..4d1b85d80fdbb8d57a63e4af4fc1ca7dd1eca410 100644 (file)
                        </dd>
                </dl>
                
+               <dl{if $errorField == 'boxIDs'} class="formError"{/if}>
+                       <dt><label for="boxIDs">{lang}wcf.acp.page.boxIDs{/lang}</label></dt>
+                       <dd>
+                               <select name="boxIDs[]" id="boxIDs" multiple="multiple" size="20">
+                                       {foreach from=$availableBoxes item=availableBox}
+                                               <option value="{@$availableBox->boxID}"{if $availableBox->boxID|in_array:$boxIDs} selected="selected"{/if}>{$availableBox->name}</option>
+                                       {/foreach}
+                               </select>
+                               {if $errorField == 'boxIDs'}
+                                       <small class="innerError">
+                                               {if $errorType == 'empty'}
+                                                       {lang}wcf.global.form.error.empty{/lang}
+                                               {else}
+                                                       {lang}wcf.acp.page.boxIDs.error.{@$errorType}{/lang}
+                                               {/if}
+                                       </small>
+                               {/if}
+                       </dd>
+               </dl>
+               
                {event name='dataFields'}
        </div>
        
index 16a5b43e63e928cf5e7189e21ed5638346f947eb..36072c5da07ff3e8c78bcb5e4035a5d20d312130 100644 (file)
@@ -2,6 +2,8 @@
 namespace wcf\acp\form;
 use wcf\data\application\Application;
 use wcf\data\application\ApplicationList;
+use wcf\data\box\Box;
+use wcf\data\box\BoxList;
 use wcf\data\page\Page;
 use wcf\data\page\PageAction;
 use wcf\data\page\PageEditor;
@@ -84,6 +86,12 @@ class PageAddForm extends AbstractForm {
         */
        public $availableApplications = [];
        
+       /**
+        * list of available boxes
+        * @var Box[]
+        */
+       public $availableBoxes = [];
+       
        /**
         * page custom URL
         * @var string[]
@@ -120,6 +128,12 @@ class PageAddForm extends AbstractForm {
         */
        public $metaKeywords = [];
        
+       /**
+        * list of box ids
+        * @var integer[]
+        */
+       public $boxIDs = [];
+       
        /**
         * @inheritDoc
         */
@@ -132,6 +146,12 @@ class PageAddForm extends AbstractForm {
                $applicationList = new ApplicationList();
                $applicationList->readObjects();
                $this->availableApplications = $applicationList->getObjects();
+               
+               // get boxes
+               $boxList = new BoxList();
+               $boxList->sqlOrderBy = 'box.name';
+               $boxList->readObjects();
+               $this->availableBoxes = $boxList->getObjects();
        }
        
        /**
@@ -153,6 +173,7 @@ class PageAddForm extends AbstractForm {
                if (isset($_POST['content']) && is_array($_POST['content'])) $this->content = ArrayUtil::trim($_POST['content']);
                if (isset($_POST['metaDescription']) && is_array($_POST['metaDescription'])) $this->metaDescription = ArrayUtil::trim($_POST['metaDescription']);
                if (isset($_POST['metaKeywords']) && is_array($_POST['metaKeywords'])) $this->metaKeywords = ArrayUtil::trim($_POST['metaKeywords']);
+               if (isset($_POST['boxIDs']) && is_array($_POST['boxIDs'])) $this->boxIDs = ArrayUtil::toIntegerArray($_POST['boxIDs']);
        }
        
        /**
@@ -172,6 +193,8 @@ class PageAddForm extends AbstractForm {
                $this->validateController();
                
                $this->validateCustomUrl();
+               
+               $this->validateBoxIDs();
        }
        
        /**
@@ -242,12 +265,53 @@ class PageAddForm extends AbstractForm {
                }
        }
        
+       /**
+        * Validates box ids.
+        */
+       protected function validateBoxIDs() {
+               foreach ($this->boxIDs as $boxID) {
+                       if (!isset($this->availableBoxes[$boxID])) {
+                               throw new UserInputException('boxIDs');
+                       }
+               }
+       }
+       
+       /**
+        * Prepares box to page assignments
+        * 
+        * @return      mixed[]
+        */
+       protected function getBoxToPage() {
+               $boxToPage = [];
+               foreach ($this->availableBoxes as $box) {
+                       if ($box->visibleEverywhere) {
+                               if (!in_array($box->boxID, $this->boxIDs)) {
+                                       $boxToPage[] = [
+                                               'boxID' => $box->boxID,
+                                               'visible' => 0
+                                       ];
+                               }
+                       }
+                       else {
+                               if (in_array($box->boxID, $this->boxIDs)) {
+                                       $boxToPage[] = [
+                                               'boxID' => $box->boxID,
+                                               'visible' => 1
+                                       ];
+                               }
+                       }
+               }
+               
+               return $boxToPage;
+       }
+       
        /**
         * @inheritDoc
         */
        public function save() {
                parent::save();
                
+               // prepare page content
                $content = [];
                if ($this->isMultilingual) {
                        foreach (LanguageFactory::getInstance()->getLanguages() as $language) {
@@ -281,7 +345,7 @@ class PageAddForm extends AbstractForm {
                        'isMultilingual' => $this->isMultilingual,
                        'identifier' => '',
                        'controller' => $this->controller
-               ]), 'content' => $content]);
+               ]), 'content' => $content, 'boxToPage' => $this->getBoxToPage()]);
                
                /** @var Page $page */
                $page = $this->objectAction->executeAction()['returnValues'];
@@ -310,6 +374,20 @@ class PageAddForm extends AbstractForm {
                $this->customURL = $this->title = $this->content = $this->metaDescription = $this->metaKeywords = [];
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function readData() {
+               parent::readData();
+               
+               // set default values
+               if (empty($_POST)) {
+                       foreach ($this->availableBoxes as $box) {
+                               if ($box->visibleEverywhere) $this->boxIDs[] = $box->boxID;
+                       }
+               }
+       }
+       
        /**
         * @inheritDoc
         */
@@ -333,9 +411,11 @@ class PageAddForm extends AbstractForm {
                        'content' => $this->content,
                        'metaDescription' => $this->metaDescription,
                        'metaKeywords' => $this->metaKeywords,
+                       'boxIDs' => $this->boxIDs,
                        'availableApplications' => $this->availableApplications,
                        'availableLanguages' => LanguageFactory::getInstance()->getLanguages(),
                        'availablePageTypes' => $availablePageTypes,
+                       'availableBoxes' => $this->availableBoxes,
                        'pageNodeList' => (new PageNodeTree())->getNodeList()
                ]);
        }
index b77c24768bb5e64e7b32aa587ffc40d32dab6174..78858b5916d9c0eb844572c4fe14ab0a2348b282 100644 (file)
@@ -120,7 +120,7 @@ class PageEditForm extends PageAddForm {
                                );
                        }
                        
-                       $this->objectAction = new PageAction(array($this->page), 'update', array('data' => array_merge($this->additionalFields, $data), 'content' => $content));
+                       $this->objectAction = new PageAction(array($this->page), 'update', array('data' => array_merge($this->additionalFields, $data), 'content' => $content, 'boxToPage' => $this->getBoxToPage()));
                        $this->objectAction->executeAction();
                }
                
@@ -158,6 +158,20 @@ class PageEditForm extends PageAddForm {
                                $this->metaKeywords[$languageID] = $content['metaKeywords'];
                                $this->customURL[$languageID] = $content['customURL'];
                        }
+                       
+                       $this->boxIDs = [];
+                       foreach ($this->availableBoxes as $box) {
+                               if ($box->visibleEverywhere) {
+                                       if (!in_array($box->boxID, $this->page->getBoxIDs())) {
+                                               $this->boxIDs[] = $box->boxID;
+                                       }
+                               }
+                               else {
+                                       if (in_array($box->boxID, $this->page->getBoxIDs())) {
+                                               $this->boxIDs[] = $box->boxID;
+                                       }
+                               }
+                       }
                }
        }
        
index 739f3f0f4d3b0e3d01d6bc2773d1a7daaa848d50..92634afc7dd42dd0409d8388de912f233ce24501 100644 (file)
@@ -62,6 +62,12 @@ class Page extends DatabaseObject {
         */
        protected $pageHandler;
        
+       /**
+        * box to page assignments
+        * @var integer[]
+        */
+       protected $boxIDs;
+       
        /**
         * Returns true if the active user can delete this page.
         * 
@@ -243,6 +249,27 @@ class Page extends DatabaseObject {
                return $this->name;
        }
        
+       /**
+        * Returns box to page assignments.
+        *
+        * @return      integer[]
+        */
+       public function getBoxIDs() {
+               if ($this->boxIDs === null) {
+                       $this->boxIDs = [];
+                       $sql = "SELECT  boxID
+                               FROM    wcf" . WCF_N . "_box_to_page
+                               WHERE   pageID = ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute([$this->pageID]);
+                       while ($row = $statement->fetchArray()) {
+                               $this->boxIDs[] = $row['boxID'];
+                       }
+               }
+               
+               return $this->boxIDs;
+       }
+       
        /**
         * Returns the page with the given identifier.
         * 
index 7a4ff9b4868b8754ddb496a48410efe7190ef0fe..9091a0d1eb9c6ad1b9b2ce735cdede55ff611259 100644 (file)
@@ -76,6 +76,22 @@ class PageAction extends AbstractDatabaseObjectAction implements ISearchAction,
                        }
                }
                
+               // save box to page assignments
+               if (!empty($this->parameters['boxToPage'])) {
+                       $sql = "INSERT INTO     wcf".WCF_N."_box_to_page
+                                               (boxID, pageID, visible)
+                               VALUES          (?, ?, ?)";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       
+                       foreach ($this->parameters['boxToPage'] as $boxData) {
+                               $statement->execute([
+                                       $boxData['boxID'],
+                                       $page->pageID,
+                                       $boxData['visible']
+                               ]);
+                       }
+               }
+               
                return $page;
        }
        
@@ -112,6 +128,30 @@ class PageAction extends AbstractDatabaseObjectAction implements ISearchAction,
                                }
                        }
                }
+               
+               // save box to page assignments
+               if (!empty($this->parameters['boxToPage'])) {
+                       $sql = "DELETE FROM     wcf".WCF_N."_box_to_page
+                               WHERE           pageID = ?";
+                       $deleteStatement = WCF::getDB()->prepareStatement($sql);
+                       
+                       $sql = "INSERT INTO     wcf".WCF_N."_box_to_page
+                                               (boxID, pageID, visible)
+                               VALUES          (?, ?, ?)";
+                       $insertStatement = WCF::getDB()->prepareStatement($sql);
+                       
+                       foreach ($this->objects as $page) {
+                               $deleteStatement->execute([$page->pageID]);
+                               
+                               foreach ($this->parameters['boxToPage'] as $boxData) {
+                                       $insertStatement->execute([
+                                               $boxData['boxID'],
+                                               $page->pageID,
+                                               $boxData['visible']
+                                       ]);
+                               }
+                       }
+               }
        }
        
        /**