Improved box handler
authorMarcel Werk <burntime@woltlab.com>
Tue, 26 Jul 2016 15:37:05 +0000 (17:37 +0200)
committerMarcel Werk <burntime@woltlab.com>
Tue, 26 Jul 2016 15:37:05 +0000 (17:37 +0200)
wcfsetup/install/files/lib/data/box/Box.class.php
wcfsetup/install/files/lib/data/box/BoxList.class.php
wcfsetup/install/files/lib/data/box/content/BoxContent.class.php
wcfsetup/install/files/lib/data/box/content/BoxContentList.class.php
wcfsetup/install/files/lib/system/box/BoxHandler.class.php

index c5cfb688113d6966878c2aca194f52a2ca1b2aff..14d7aaf660254e0067e2ef3476203f26bf5e6b8b 100644 (file)
@@ -178,6 +178,15 @@ class Box extends DatabaseObject {
                return $this->boxContents;
        }
        
+       /**
+        * Sets the box's content.
+        *
+        * @param       BoxContent[]    $boxContents
+        */
+       public function setBoxContents($boxContents) {
+               $this->boxContents = $boxContents;
+       }
+       
        /**
         * Returns the title of the box as set in the box content database table.
         * 
@@ -334,11 +343,11 @@ class Box extends DatabaseObject {
                                $this->getBoxContents();
                                if ($this->isMultilingual) {
                                        if (isset($this->boxContents[WCF::getLanguage()->languageID]) && $this->boxContents[WCF::getLanguage()->languageID]->imageID) {
-                                               $this->image = ViewableMedia::getMedia($this->boxContents[WCF::getLanguage()->languageID]->imageID);
+                                               $this->image = $this->boxContents[WCF::getLanguage()->languageID]->getImage();
                                        }
                                }
                                else if (isset($this->boxContents[0]) && $this->boxContents[0]->imageID) {
-                                       $this->image = ViewableMedia::getMedia($this->boxContents[0]->imageID);
+                                       $this->image = $this->boxContents[0]->getImage();
                                }
                        }
                }
index cfec8e3c6237554cc1bddb12534d8c3f8f622e56..61dba7100fff034997d8be093ebc51ad927b1a6c 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 namespace wcf\data\box;
+use wcf\data\box\content\BoxContentList;
 use wcf\data\DatabaseObjectList;
+use wcf\system\WCF;
 
 /**
  * Represents a list of boxes.
@@ -21,4 +23,47 @@ class BoxList extends DatabaseObjectList {
         * @inheritDoc
         */
        public $className = Box::class;
+       
+       /**
+        * enables/disables the loading of box content objects
+        * @var boolean
+        */
+       protected $contentLoading = false;
+       
+       /**
+        * @inheritDoc
+        */
+       public function readObjects() {
+               parent::readObjects();
+               
+               // get box content
+               if ($this->contentLoading) {
+                       $boxIDs = [];
+                       foreach ($this->getObjects() as $box) {
+                               if ($box->boxType != 'system' && $box->boxType != 'menu') {
+                                       $boxIDs[] = $box->boxID;
+                               }
+                       }
+                       
+                       if (!empty($boxIDs)) {
+                               $contentList = new BoxContentList();
+                               $contentList->enableImageLoading();
+                               $contentList->getConditionBuilder()->add('box_content.boxID IN (?)', [$this->objectIDs]);
+                               $contentList->getConditionBuilder()->add('(box_content.languageID IS NULL OR box_content.languageID = ?)', [WCF::getLanguage()->languageID]);
+                               $contentList->readObjects();
+                               foreach ($contentList as $boxContent) {
+                                       $this->objects[$boxContent->boxID]->setBoxContents([($boxContent->languageID ?: 0) => $boxContent]);
+                               }
+                       }
+               }
+       }
+       
+       /**
+        * Enables/disables the loading of box content objects.
+        *
+        * @param       boolean         $enable
+        */
+       public function enableContentLoading($enable = true) {
+               $this->contentLoading = $enable;
+       }
 }
index 3685c5716f62ff09b6163e1b2bd55925e9a3c45c..73a514a43db968fa12e7c1fa4389a92cb72d5d93 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\data\box\content;
 use wcf\data\DatabaseObject;
+use wcf\data\media\ViewableMedia;
 use wcf\system\WCF;
 
 /**
@@ -31,6 +32,12 @@ class BoxContent extends DatabaseObject {
         */
        protected static $databaseTableIndexName = 'boxContentID';
        
+       /**
+        * image media object
+        * @var ViewableMedia
+        */
+       protected $image;
+       
        /**
         * Returns a certain box content.
         *
@@ -62,4 +69,28 @@ class BoxContent extends DatabaseObject {
                
                return null;
        }
+       
+       /**
+        * Returns the image of this box content.
+        *
+        * @return      ViewableMedia|null
+        */
+       public function getImage() {
+               if ($this->image === null) {
+                       if ($this->imageID) {
+                               $this->image = ViewableMedia::getMedia($this->imageID);
+                       }
+               }
+               
+               return $this->image;
+       }
+       
+       /**
+        * Sets the image of this box content.
+        * 
+        * @param       ViewableMedia   $image
+        */
+       public function setImage(ViewableMedia $image) {
+               $this->image = $image;
+       }
 }
index abdbafbba0645226693d160a22c65388d7c63e90..d56cd8195b5d54cacb73e9853af3da1496306478 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\data\box\content;
 use wcf\data\DatabaseObjectList;
+use wcf\data\media\ViewableMediaList;
 
 /**
  * Represents a list of box content.
@@ -21,4 +22,50 @@ class BoxContentList extends DatabaseObjectList {
         * @inheritDoc
         */
        public $className = BoxContent::class;
+       
+       /**
+        * enables/disables the loading of box content images
+        * @var boolean
+        */
+       protected $imageLoading = false;
+       
+       /**
+        * @inheritDoc
+        */
+       public function readObjects() {
+               parent::readObjects();
+               
+               if ($this->imageLoading) {
+                       $imageIDs = [];
+                       foreach ($this->getObjects() as $boxContent) {
+                               if ($boxContent->imageID) {
+                                       $imageIDs[] = $boxContent->imageID;
+                               }
+                       }
+                       
+                       // cache images
+                       if (!empty($imageIDs)) {
+                               $mediaList = new ViewableMediaList();
+                               $mediaList->setObjectIDs($imageIDs);
+                               $mediaList->readObjects();
+                               $images = $mediaList->getObjects();
+                               
+                               foreach ($this->getObjects() as $boxContent) {
+                                       if ($boxContent->imageID && isset($images[$boxContent->imageID])) {
+                                               $boxContent->setImage($images[$boxContent->imageID]);
+                                       }
+                               }
+                       }
+               }
+       }
+       
+       
+       /**
+        * Enables/disables the loading of box content images.
+        *
+        * @param       boolean         $enable
+        */
+       public function enableImageLoading($enable = true) {
+               $this->imageLoading = $enable;
+       }
 }
index 02743d1008b124d0ebf28785316d8eac19f9f9fc..83a0cff86bd2b7b65fb83bba236057da1062cb46 100644 (file)
@@ -48,6 +48,7 @@ class BoxHandler extends SingletonFactory {
                
                // load box layout for active page
                $boxList = new BoxList();
+               $boxList->enableContentLoading();
                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 = ?)', [1, $pageID, 0, $pageID, 1]);
                else $boxList->getConditionBuilder()->add('box.visibleEverywhere = ?', [1]);
                $boxList->sqlOrderBy = 'showOrder';