Added method to assign pages to boxes during package installation
authorMarcel Werk <burntime@woltlab.com>
Thu, 30 Jun 2016 18:19:02 +0000 (20:19 +0200)
committerMarcel Werk <burntime@woltlab.com>
Thu, 30 Jun 2016 18:19:02 +0000 (20:19 +0200)
wcfsetup/install/files/lib/system/box/BoxHandler.class.php

index d198196fff0d20c6d8c78fafd769b67e9c6732ad..f477a6c0ddfdf635e00cd5df813d045bfa63e34c 100644 (file)
@@ -3,6 +3,7 @@ namespace wcf\system\box;
 use wcf\data\box\Box;
 use wcf\data\box\BoxList;
 use wcf\data\condition\ConditionAction;
+use wcf\data\page\Page;
 use wcf\system\exception\SystemException;
 use wcf\system\request\RequestHandler;
 use wcf\system\SingletonFactory;
@@ -146,4 +147,49 @@ class BoxHandler extends SingletonFactory {
                
                return null;
        }
+       
+       /**
+        * Assigns pages to a certain box.
+        *
+        * Note: The primary use of this method is to be used during package installation.
+        *
+        * @param       string          $boxIdentifier
+        * @param       string[]        $pageIdentifiers
+        * @param       boolean         $visible
+        * @throws      \InvalidArgumentException
+        */
+       public function addBoxToPageAssignments($boxIdentifier, array $pageIdentifiers, $visible = true) {
+               $box = Box::getBoxByIdentifier($boxIdentifier);
+               if ($box === null) {
+                       throw new \InvalidArgumentException("Unknown box with identifier '{$boxIdentifier}'");
+               }
+               
+               $pages = [];
+               foreach ($pageIdentifiers as $pageIdentifier) {
+                       $page = Page::getPageByIdentifier($pageIdentifier);
+                       if ($page === null) {
+                               throw new \InvalidArgumentException("Unknown page with identifier '{$pageIdentifier}'");
+                       }
+                       $pages[] = $page;
+               }
+               
+               if (($visible && $box->visibleEverywhere) || (!$visible && !$box->visibleEverywhere)) {
+                       $sql = "DELETE FROM     wcf".WCF_N."_box_to_page
+                                       WHERE           boxID = ?
+                                                       AND pageID = ?";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       foreach ($pages as $page) {
+                               $statement->execute([$box->boxID, $page->pageID]);
+                       }
+               }
+               else {
+                       $sql = "REPLACE INTO    wcf".WCF_N."_box_to_page
+                                                       (boxID, pageID, visible)
+                                       VALUES          (?, ?, ?)";
+                       $statement = WCF::getDB()->prepareStatement($sql);
+                       foreach ($pages as $page) {
+                               $statement->execute([$box->boxID, $page->pageID, ($visible ? 1 : 0)]);
+                       }
+               }
+       }
 }