a7b808392beba3474d2898eaa2410998a45ab534
[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\system\html\input\HtmlInputProcessor;
8
9 /**
10 * Parses embedded articles and outputs their link or title.
11 *
12 * @author Alexander Ebert
13 * @copyright 2001-2019 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package WoltLabSuite\Core\System\Message\Embedded\Object
16 */
17 class ArticleMessageEmbeddedObjectHandler extends AbstractSimpleMessageEmbeddedObjectHandler
18 {
19 /**
20 * @inheritDoc
21 */
22 public function parse(HtmlInputProcessor $htmlInputProcessor, array $embeddedData)
23 {
24 $articleIDs = [];
25 if (!empty($embeddedData['wsa'])) {
26 for ($i = 0, $length = \count($embeddedData['wsa']); $i < $length; $i++) {
27 $articleIDs[] = \intval($embeddedData['wsa'][$i][0]);
28 }
29 }
30
31 return \array_unique($articleIDs);
32 }
33
34 /**
35 * @inheritDoc
36 */
37 public function loadObjects(array $objectIDs)
38 {
39 $articleList = new AccessibleArticleList();
40 $articleList->getConditionBuilder()->add('article.articleID IN (?)', [$objectIDs]);
41 $articleList->readObjects();
42
43 return $articleList->getObjects();
44 }
45
46 /**
47 * @inheritDoc
48 */
49 public function validateValues($objectType, $objectID, array $values)
50 {
51 $articleList = new AccessibleArticleList();
52 $articleList->getConditionBuilder()->add('article.articleID IN (?)', [$values]);
53 $articleList->readObjects();
54 $articles = $articleList->getObjects();
55
56 return \array_filter($values, static function ($value) use ($articles) {
57 return isset($articles[$value]);
58 });
59 }
60
61 /**
62 * @inheritDoc
63 */
64 public function replaceSimple($objectType, $objectID, $value, array $attributes)
65 {
66 /** @var Article $article */
67 $article = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.article', $value);
68 if ($article === null) {
69 return;
70 }
71
72 $return = (!empty($attributes['return'])) ? $attributes['return'] : 'link';
73 switch ($return) {
74 case 'title':
75 return $article->getTitle();
76 break;
77
78 case 'link':
79 default:
80 return $article->getLink();
81 break;
82 }
83 }
84 }