4d53b9fbdcffb05aa7201fac177eab18ac735339
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace wcf\system\message\embedded\object;
4
5 use wcf\data\article\AccessibleArticleList;
6 use wcf\data\article\Article;
7 use wcf\data\article\content\ViewableArticleContentList;
8 use wcf\system\cache\runtime\ViewableArticleRuntimeCache;
9 use wcf\system\html\input\HtmlInputProcessor;
10
11 /**
12 * Parses embedded articles and outputs their link or title.
13 *
14 * @author Alexander Ebert
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\System\Message\Embedded\Object
18 */
19 class ArticleMessageEmbeddedObjectHandler extends AbstractSimpleMessageEmbeddedObjectHandler
20 {
21 /**
22 * @inheritDoc
23 */
24 public function parse(HtmlInputProcessor $htmlInputProcessor, array $embeddedData)
25 {
26 $articleIDs = [];
27 if (!empty($embeddedData['wsa'])) {
28 for ($i = 0, $length = \count($embeddedData['wsa']); $i < $length; $i++) {
29 $articleIDs[] = \intval($embeddedData['wsa'][$i][0]);
30 }
31 }
32
33 return \array_unique($articleIDs);
34 }
35
36 /**
37 * @inheritDoc
38 */
39 public function loadObjects(array $objectIDs)
40 {
41 $viewableArticles = ViewableArticleRuntimeCache::getInstance()->getObjects($objectIDs);
42 $contentLanguageID = MessageEmbeddedObjectManager::getInstance()->getContentLanguageID();
43 if ($contentLanguageID !== null) {
44 $articleIDs = [];
45 foreach ($viewableArticles as $article) {
46 if (
47 $article !== null
48 && $article->getArticleContent()->languageID
49 && $article->getArticleContent()->languageID != $contentLanguageID
50 ) {
51 $articleIDs[] = $article->articleID;
52 }
53 }
54
55 if (!empty($articleIDs)) {
56 $list = new ViewableArticleContentList();
57 $list->getConditionBuilder()->add("articleID IN (?)", [$articleIDs]);
58 $list->getConditionBuilder()->add("languageID = ?", [$contentLanguageID]);
59 $list->readObjects();
60
61 foreach ($list->getObjects() as $articleContent) {
62 $viewableArticles[$articleContent->articleID]->setArticleContent($articleContent);
63 }
64 }
65 }
66
67 return $viewableArticles;
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function validateValues($objectType, $objectID, array $values)
74 {
75 $articleList = new AccessibleArticleList();
76 $articleList->getConditionBuilder()->add('article.articleID IN (?)', [$values]);
77 $articleList->readObjects();
78 $articles = $articleList->getObjects();
79
80 return \array_filter($values, static function ($value) use ($articles) {
81 return isset($articles[$value]);
82 });
83 }
84
85 /**
86 * @inheritDoc
87 */
88 public function replaceSimple($objectType, $objectID, $value, array $attributes)
89 {
90 /** @var Article $article */
91 $article = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.article', $value);
92 if ($article === null) {
93 return;
94 }
95
96 $return = (!empty($attributes['return'])) ? $attributes['return'] : 'link';
97 switch ($return) {
98 case 'title':
99 return $article->getTitle();
100 break;
101
102 case 'link':
103 default:
104 return $article->getLink();
105 break;
106 }
107 }
108 }