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