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