Added box to page assignments
authorMarcel Werk <burntime@woltlab.com>
Fri, 8 Apr 2016 14:06:59 +0000 (16:06 +0200)
committerMarcel Werk <burntime@woltlab.com>
Fri, 8 Apr 2016 14:07:10 +0000 (16:07 +0200)
wcfsetup/install/files/acp/templates/boxAdd.tpl
wcfsetup/install/files/acp/templates/menuAdd.tpl
wcfsetup/install/files/lib/acp/form/BoxAddForm.class.php
wcfsetup/install/files/lib/acp/form/BoxEditForm.class.php
wcfsetup/install/files/lib/acp/form/MenuAddForm.class.php
wcfsetup/install/files/lib/acp/form/MenuEditForm.class.php
wcfsetup/install/files/lib/data/box/Box.class.php
wcfsetup/install/files/lib/data/box/BoxAction.class.php
wcfsetup/install/files/lib/data/menu/MenuAction.class.php
wcfsetup/install/files/lib/system/box/BoxHandler.class.php
wcfsetup/setup/db/install.sql

index 75578026fdda59a45befb5acbcc22318be99a2d9..e77a9bed1ef9eb1830d89f7421e9c046c3848553 100644 (file)
                        </dd>
                </dl>
                
+               <dl>
+                       <dt><label for="pageIDs">{lang}wcf.acp.box.pageIDs{/lang}</label></dt>
+                       <dd>
+                               <select name="pageIDs[]" id="pageIDs" multiple="multiple" size="20">
+                                       {foreach from=$pageNodeList item=pageNode}
+                                               <option value="{@$pageNode->getPage()->pageID}"{if $pageNode->getPage()->pageID|in_array:$pageIDs} selected="selected"{/if}>{if $pageNode->getDepth() > 1}{@"&nbsp;&nbsp;&nbsp;&nbsp;"|str_repeat:($pageNode->getDepth() - 1)}{/if}{$pageNode->getPage()->name}</option>
+                                       {/foreach}
+                               </select>
+                       </dd>
+               </dl>
+               
                {event name='dataFields'}
        </section>
        
index 835015a4051f5d35e739e3242bf00d5856ad5380..c9b28c23e70ed0cd5b56657658115e5ab9c9ce4d 100644 (file)
                                        <label><input type="checkbox" id="visibleEverywhere" name="visibleEverywhere" value="1" {if $visibleEverywhere}checked="checked" {/if}/> {lang}wcf.acp.box.visibleEverywhere{/lang}</label>
                                </dd>
                        </dl>
+                       
+                       <dl>
+                               <dt><label for="pageIDs">{lang}wcf.acp.box.pageIDs{/lang}</label></dt>
+                               <dd>
+                                       <select name="pageIDs[]" id="pageIDs" multiple="multiple" size="20">
+                                               {foreach from=$pageNodeList item=pageNode}
+                                                       <option value="{@$pageNode->getPage()->pageID}"{if $pageNode->getPage()->pageID|in_array:$pageIDs} selected="selected"{/if}>{if $pageNode->getDepth() > 1}{@"&nbsp;&nbsp;&nbsp;&nbsp;"|str_repeat:($pageNode->getDepth() - 1)}{/if}{$pageNode->getPage()->name}</option>
+                                               {/foreach}
+                                       </select>
+                               </dd>
+                       </dl>
                {/if}
                
                {event name='dataFields'}
index 807948f0d92539807d23131b0c297d159d0b551e..cc867941e66b479e0b4955e82d30f973b744b403 100644 (file)
@@ -5,7 +5,9 @@ use wcf\data\box\BoxAction;
 use wcf\data\box\BoxEditor;
 use wcf\data\media\Media;
 use wcf\data\media\ViewableMediaList;
+use wcf\data\page\PageNodeTree;
 use wcf\form\AbstractForm;
+use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\UserInputException;
 use wcf\system\language\LanguageFactory;
 use wcf\system\WCF;
@@ -112,6 +114,12 @@ class BoxAddForm extends AbstractForm {
         */
        public $images = [];
        
+       /**
+        * page ids
+        * @var integer[]
+        */
+       public $pageIDs = [];
+       
        /**
         * @inheritDoc
         */
@@ -136,6 +144,7 @@ class BoxAddForm extends AbstractForm {
                if (isset($_POST['cssClassName'])) $this->cssClassName = StringUtil::trim($_POST['cssClassName']);
                if (isset($_POST['showHeader'])) $this->showHeader = intval($_POST['showHeader']);
                if (isset($_POST['className'])) $this->className = StringUtil::trim($_POST['className']);
+               if (isset($_POST['pageIDs']) && is_array($_POST['pageIDs'])) $this->pageIDs = ArrayUtil::toIntegerArray($_POST['pageIDs']);
                
                if (isset($_POST['title']) && is_array($_POST['title'])) $this->title = ArrayUtil::trim($_POST['title']);
                if (isset($_POST['content']) && is_array($_POST['content'])) $this->content = ArrayUtil::trim($_POST['content']);
@@ -193,6 +202,21 @@ class BoxAddForm extends AbstractForm {
                        // @todo check class
                }
                
+               // validate page ids
+               if (!empty($this->pageIDs)) {
+                       $conditionBuilder = new PreparedStatementConditionBuilder();
+                       $conditionBuilder->add('pageID IN (?)', [$this->pageIDs]);
+                       $sql = "SELECT  pageID
+                               FROM    wcf".WCF_N."_page
+                               " . $conditionBuilder;
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute($conditionBuilder->getParameters());
+                       $this->pageIDs = [];
+                       while ($row = $statement->fetchArray()) {
+                               $this->pageIDs[] = $row['pageID'];
+                       }
+               }
+               
                // validate images
                if (WCF::getSession()->getPermission('admin.content.cms.canUseMedia')) {
                        foreach ($this->imageID as $languageID => $imageID) {
@@ -251,7 +275,7 @@ class BoxAddForm extends AbstractForm {
                        'showHeader' => $this->showHeader,
                        'className' => $this->className,
                        'identifier' => ''
-               ]), 'content' => $content]);
+               ]), 'content' => $content, 'pageIDs' => $this->pageIDs ]);
                $returnValues = $this->objectAction->executeAction();
                
                // set generic box identifier
@@ -294,9 +318,11 @@ class BoxAddForm extends AbstractForm {
                        'content' => $this->content,
                        'imageID' => $this->imageID,
                        'images' => $this->images,
+                       'pageIDs' => $this->pageIDs,
                        'availableLanguages' => LanguageFactory::getInstance()->getLanguages(),
                        'availableBoxTypes' => Box::$availableBoxTypes,
-                       'availablePositions' => Box::$availablePositions
+                       'availablePositions' => Box::$availablePositions,
+                       'pageNodeList' => (new PageNodeTree())->getNodeList()
                ]);
        }
 }
index dd4a8e9be962afc2d1042738260f32554365eebc..2093691a9b54c381c3e1b71ba0c31a85a0c9ba77 100644 (file)
@@ -97,7 +97,7 @@ class BoxEditForm extends BoxAddForm {
                        'cssClassName' => $this->cssClassName,
                        'showHeader' => $this->showHeader,
                        'className' => $this->className
-               ]), 'content' => $content]);
+               ]), 'content' => $content, 'pageIDs' => $this->pageIDs]);
                $this->objectAction->executeAction();
                
                // call saved event
@@ -130,6 +130,8 @@ class BoxEditForm extends BoxAddForm {
                        $this->className = $this->box->className;
                        if ($this->box->showHeader) $this->showHeader = 1;
                        if ($this->box->visibleEverywhere) $this->visibleEverywhere = 1;
+                       else $this->visibleEverywhere = 0;
+                       $this->pageIDs = $this->box->getPageIDs();
                        
                        foreach ($this->box->getBoxContent() as $languageID => $content) {
                                $this->title[$languageID] = $content['title'];
index 1f9a181a4c9e3c5c6f71757ad9b63bf1ebd4b076..3d3a07c8e2add7245df28be72d42f464eff20821 100644 (file)
@@ -3,10 +3,13 @@ namespace wcf\acp\form;
 use wcf\data\box\Box;
 use wcf\data\menu\MenuAction;
 use wcf\data\menu\MenuEditor;
+use wcf\data\page\PageNodeTree;
 use wcf\form\AbstractForm;
+use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\UserInputException;
 use wcf\system\language\I18nHandler;
 use wcf\system\WCF;
+use wcf\util\ArrayUtil;
 use wcf\util\StringUtil;
 
 /**
@@ -67,6 +70,12 @@ class MenuAddForm extends AbstractForm {
         */
        public $showHeader = 1;
        
+       /**
+        * page ids
+        * @var integer[]
+        */
+       public $pageIDs = [];
+       
        /**
         * @inheritDoc
         */
@@ -92,6 +101,7 @@ class MenuAddForm extends AbstractForm {
                if (isset($_POST['visibleEverywhere'])) $this->visibleEverywhere = intval($_POST['visibleEverywhere']);
                if (isset($_POST['cssClassName'])) $this->cssClassName = StringUtil::trim($_POST['cssClassName']);
                if (isset($_POST['showHeader'])) $this->showHeader = intval($_POST['showHeader']);
+               if (isset($_POST['pageIDs']) && is_array($_POST['pageIDs'])) $this->pageIDs = ArrayUtil::toIntegerArray($_POST['pageIDs']);
        }
        
        /**
@@ -112,6 +122,21 @@ class MenuAddForm extends AbstractForm {
                
                // validate box position
                $this->validatePosition();
+               
+               // validate page ids
+               if (!empty($this->pageIDs)) {
+                       $conditionBuilder = new PreparedStatementConditionBuilder();
+                       $conditionBuilder->add('pageID IN (?)', [$this->pageIDs]);
+                       $sql = "SELECT  pageID
+                               FROM    wcf".WCF_N."_page
+                               " . $conditionBuilder;
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute($conditionBuilder->getParameters());
+                       $this->pageIDs = [];
+                       while ($row = $statement->fetchArray()) {
+                               $this->pageIDs[] = $row['pageID'];
+                       }
+               }
        }
        
        /**
@@ -145,7 +170,7 @@ class MenuAddForm extends AbstractForm {
                        'showOrder' => $this->showOrder,
                        'cssClassName' => $this->cssClassName,
                        'packageID' => 1
-               )));
+               ), 'pageIDs' => $this->pageIDs));
                $returnValues = $this->objectAction->executeAction();
                // set generic identifier
                $menuEditor = new MenuEditor($returnValues['returnValues']);
@@ -190,7 +215,9 @@ class MenuAddForm extends AbstractForm {
                        'showOrder' => $this->showOrder,
                        'visibleEverywhere' => $this->visibleEverywhere,
                        'showHeader' => $this->showHeader,
-                       'availablePositions' => Box::$availableMenuPositions
+                       'pageIDs' => $this->pageIDs,
+                       'availablePositions' => Box::$availableMenuPositions,
+                       'pageNodeList' => (new PageNodeTree())->getNodeList()
                ));
        }
 }
index 93ad21cdbc46ca4a25d5a7dd9cec35f1be00e944..11e8e86b47a1882d0d867471069c121065fc3568 100644 (file)
@@ -88,7 +88,7 @@ class MenuEditForm extends MenuAddForm {
                                'showHeader' => ($this->showHeader) ? 1 : 0,
                                'showOrder' => $this->showOrder,
                                'cssClassName' => $this->cssClassName
-                       ))));
+                       )), 'pageIDs' => $this->pageIDs));
                        $boxAction->executeAction();
                }
                
@@ -114,6 +114,7 @@ class MenuEditForm extends MenuAddForm {
                        $this->cssClassName = $this->menu->getBox()->cssClassName;
                        $this->showOrder = $this->menu->getBox()->showOrder;
                        $this->visibleEverywhere = $this->menu->getBox()->visibleEverywhere;
+                       $this->pageIDs = $this->menu->getBox()->getPageIDs();
                        $this->showHeader = $this->menu->getBox()->showHeader;
                }
        }
index 84a9c8d725c46f435e277fe45481ae2c1b7f65ad..fd32df450eb83c0bd89a5b6e6c8c1757e8cf83d0 100644 (file)
@@ -38,13 +38,13 @@ class Box extends DatabaseObject {
         * box content grouped by language id
         * @var string[][]
         */
-       protected $boxContent = null;
+       protected $boxContent;
        
        /**
         * image media object
         * @var ViewableMedia
         */
-       protected $image = null;
+       protected $image;
        
        /**
         * @inheritDoc
@@ -60,7 +60,7 @@ class Box extends DatabaseObject {
         * available box types
         * @var string[]
         */
-       public static $availableBoxTypes = ['text', 'html', 'system'];
+       public static $availableBoxTypes = ['text', 'html', 'tpl', 'system'];
        
        /**
         * available box positions
@@ -80,6 +80,12 @@ class Box extends DatabaseObject {
         */
        protected $menu;
        
+       /**
+        * box to page assignments
+        * @var integer[]
+        */
+       protected $pageIDs;
+       
        /**
         * Returns true if the active user can delete this box.
         * 
@@ -291,6 +297,27 @@ class Box extends DatabaseObject {
                return false;
        }
        
+       /**
+        * Returns box to page assignments.
+        * 
+        * @return      integer[]
+        */
+       public function getPageIDs() {
+               if ($this->pageIDs === null) {
+                       $this->pageIDs = [];
+                       $sql = "SELECT  pageID
+                               FROM    wcf" . WCF_N . "_box_to_page
+                               WHERE   boxID = ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       $statement->execute([$this->boxID]);
+                       while ($row = $statement->fetchArray()) {
+                               $this->pageIDs[] = $row['pageID'];
+                       }
+               }
+               
+               return $this->pageIDs;
+       }
+       
        /**
         * Returns the box with the given name.
         *
index dc8911b629806b3e790dc164550775c7f10e4316..389451200ba4a3a5971f9ad4a292dd3dc1712e99 100644 (file)
@@ -65,6 +65,22 @@ class BoxAction extends AbstractDatabaseObjectAction {
                        }
                }
                
+               // save box to page
+               if (!empty($this->parameters['pageIDs'])) {
+                       $sql = "INSERT INTO     wcf".WCF_N."_box_to_page
+                                               (boxID, pageID, visible)
+                               VALUES          (?, ?, ?)";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       
+                       foreach ($this->parameters['pageIDs'] as $pageID) {
+                               $statement->execute([
+                                       $box->boxID,
+                                       $pageID,
+                                       ($box->visibleEverywhere ? 0 : 1)
+                               ]);
+                       }
+               }
+               
                return $box;
        }
        
@@ -99,6 +115,26 @@ class BoxAction extends AbstractDatabaseObjectAction {
                                }
                        }
                }
+               
+               // save box to page
+               if (isset($this->parameters['pageIDs'])) {
+                       $sql = "DELETE FROM     wcf".WCF_N."_box_to_page
+                               WHERE           boxID = ?";
+                       $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 $box) {
+                               $deleteStatement->execute([$box->boxID]);
+                               
+                               foreach ($this->parameters['pageIDs'] as $pageID) {
+                                       $insertStatement->execute([$box->boxID, $pageID, ($box->visibleEverywhere ? 0 : 1)]);
+                               }
+                       }
+               }
        }
        
        /**
index 0cb47ce384117127ffe708fd7e00c8c76d793e61..e50f5122f435ebc290f0d1efd438f1d78d1ac0b8 100644 (file)
@@ -53,7 +53,7 @@ class MenuAction extends AbstractDatabaseObjectAction {
                $boxData = $this->parameters['boxData'];
                $boxData['menuID'] = $menu->menuID;
                $boxData['identifier'] = '';
-               $boxAction = new BoxAction([], 'create', ['data' => $boxData]);
+               $boxAction = new BoxAction([], 'create', ['data' => $boxData, 'pageIDs' => (isset($this->parameters['pageIDs']) ? $this->parameters['pageIDs'] : [])]);
                $returnValues = $boxAction->executeAction();
                
                // set generic box identifier
index dce7219d68a257aa12b9e246022757c85ce52bce..934a9954e09f4cfb2c327f0dcbcc97d16eaec5c8 100644 (file)
@@ -2,6 +2,7 @@
 namespace wcf\system\box;
 use wcf\data\box\Box;
 use wcf\data\box\BoxList;
+use wcf\system\request\RequestHandler;
 use wcf\system\SingletonFactory;
 
 /**
@@ -26,8 +27,13 @@ class BoxHandler extends SingletonFactory {
         * @inheritDoc
         */
        protected function init() {
+               // get active page id
+               $pageID = RequestHandler::getInstance()->getActiveRequest()->getPageID();
+               
                // load box layout for active page
                $boxList = new BoxList();
+               if ($pageID) $boxList->getConditionBuilder()->add('(box.visibleEverywhere = ? AND boxID NOT IN (SELECT boxID FROM wcf'.WCF_N.'_box_to_page WHERE pageID = ? AND visible = ?)) OR boxID IN (SELECT boxID FROM wcf'.WCF_N.'_box_to_page WHERE pageID = ? AND visible = ?)', array(1, $pageID, 0, $pageID, 1));
+               else $boxList->getConditionBuilder()->add('box.visibleEverywhere = ?', array(1));
                $boxList->sqlOrderBy = 'showOrder';
                $boxList->readObjects();
                foreach ($boxList as $box) {
index b0d106d2d05899672124af4ea189e563147237b7..d925d0da8e300dec8526a487c57eaa77780a9a8c 100644 (file)
@@ -252,6 +252,16 @@ CREATE TABLE wcf1_box_content (
        KEY (boxID, languageID)
 );
 
+DROP TABLE IF EXISTS wcf1_box_to_page;
+CREATE TABLE wcf1_box_to_page (
+       boxID INT(10) NOT NULL,
+       pageID INT(10) NOT NULL,
+       visible TINYINT(1) NOT NULL DEFAULT 1,
+       
+       UNIQUE KEY (pageID, boxID),
+       KEY (pageID, visible)
+);
+
 DROP TABLE IF EXISTS wcf1_captcha_question;
 CREATE TABLE wcf1_captcha_question (
        questionID INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
@@ -1650,6 +1660,9 @@ ALTER TABLE wcf1_box_content ADD FOREIGN KEY (boxID) REFERENCES wcf1_box (boxID)
 ALTER TABLE wcf1_box_content ADD FOREIGN KEY (languageID) REFERENCES wcf1_language (languageID) ON DELETE CASCADE;
 ALTER TABLE wcf1_box_content ADD FOREIGN KEY (imageID) REFERENCES wcf1_media (mediaID) ON DELETE SET NULL;
 
+ALTER TABLE wcf1_box_to_page ADD FOREIGN KEY (boxID) REFERENCES wcf1_box (boxID) ON DELETE CASCADE;
+ALTER TABLE wcf1_box_to_page ADD FOREIGN KEY (pageID) REFERENCES wcf1_page (pageID) ON DELETE CASCADE;
+
 ALTER TABLE wcf1_category ADD FOREIGN KEY (objectTypeID) REFERENCES wcf1_object_type (objectTypeID) ON DELETE CASCADE;
 
 ALTER TABLE wcf1_cli_history ADD FOREIGN KEY (userID) REFERENCES wcf1_user (userID) ON DELETE CASCADE;