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