Fetch articles in ViewableArticleContentList
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / article / content / ViewableArticleContentList.class.php
1 <?php
2 namespace wcf\data\article\content;
3 use wcf\data\article\ViewableArticleList;
4 use wcf\data\media\ViewableMediaList;
5 use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
6
7 /**
8 * Represents a list of viewable article contents.
9 *
10 * @author Marcel Werk
11 * @copyright 2001-2019 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package WoltLabSuite\Core\Data\Article\Content
14 * @since 3.0
15 *
16 * @method ViewableArticleContent current()
17 * @method ViewableArticleContent[] getObjects()
18 * @method ViewableArticleContent|null search($objectID)
19 * @property ViewableArticleContent[] $objects
20 */
21 class ViewableArticleContentList extends ArticleContentList {
22 /**
23 * @inheritDoc
24 */
25 public $decoratorClassName = ViewableArticleContent::class;
26
27 /**
28 * @inheritDoc
29 */
30 public function readObjects() {
31 parent::readObjects();
32
33 $imageIDs = $embeddedObjectPostIDs = $articleIDs = [];
34 foreach ($this->getObjects() as $articleContent) {
35 if ($articleContent->imageID) {
36 $imageIDs[] = $articleContent->imageID;
37 }
38 if ($articleContent->thumbnailImageID) {
39 $imageIDs[] = $articleContent->thumbnailImageID;
40 }
41 if ($articleContent->hasEmbeddedObjects) {
42 $embeddedObjectPostIDs[] = $articleContent->articleContentID;
43 }
44
45 $articleIDs[] = $articleContent->articleID;
46 }
47
48 // cache images
49 if (!empty($imageIDs)) {
50 $mediaList = new ViewableMediaList();
51 $mediaList->setObjectIDs($imageIDs);
52 $mediaList->readObjects();
53 $images = $mediaList->getObjects();
54 }
55
56 // load embedded objects
57 if (!empty($embeddedObjectPostIDs)) {
58 $contentLanguageID = null;
59 if (count($embeddedObjectPostIDs) === 1) $contentLanguageID = reset($this->objects)->languageID;
60
61 MessageEmbeddedObjectManager::getInstance()->loadObjects('com.woltlab.wcf.article.content', $embeddedObjectPostIDs, $contentLanguageID);
62 }
63
64 if (!empty($articleIDs)) {
65 $articleList = new ViewableArticleList();
66 // to prevent an infinity loop, because the list loads otherwise the article content
67 $articleList->enableContentLoading(false);
68 $articleList->setObjectIDs($articleIDs);
69 $articleList->readObjects();
70 }
71
72 foreach ($this->getObjects() as $articleContent) {
73 if (isset($images)) {
74 if ($articleContent->imageID && isset($images[$articleContent->imageID])) {
75 $articleContent->setImage($images[$articleContent->imageID]);
76 }
77
78 if ($articleContent->teaserImageID && isset($images[$articleContent->teaserImageID])) {
79 $articleContent->setTeaserImage($images[$articleContent->teaserImageID]);
80 }
81 }
82
83 if (isset($articleList)) {
84 if ($articleList->search($articleContent->articleID) !== null) {
85 $articleContent->setArticle($articleList->search($articleContent->articleID));
86 }
87 else {
88 throw new \LogicException('Unable to find article with id "'. $articleContent->articleID .'".');
89 }
90 }
91 }
92 }
93 }