aad97f6699f58ab45215047304f31be3e9dde722
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\message\embedded\object;
3 use wcf\data\media\Media;
4 use wcf\data\media\MediaList;
5 use wcf\system\cache\runtime\ViewableMediaRuntimeCache;
6 use wcf\system\html\input\HtmlInputProcessor;
7 use wcf\util\ArrayUtil;
8
9 /**
10 * IMessageEmbeddedObjectHandler implementation for shared media.
11 *
12 * @author Alexander Ebert
13 * @copyright 2001-2016 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 MediaMessageEmbeddedObjectHandler extends AbstractSimpleMessageEmbeddedObjectHandler {
18 /**
19 * @inheritDoc
20 */
21 public function parse(HtmlInputProcessor $htmlInputProcessor, array $embeddedData) {
22 $mediaIDs = [];
23 foreach (['wsm', 'wsmg'] as $name) {
24 if (empty($embeddedData[$name])) {
25 continue;
26 }
27
28 for ($i = 0, $length = count($embeddedData[$name]); $i < $length; $i++) {
29 $parsedIDs = ArrayUtil::toIntegerArray(explode(',', $embeddedData[$name][$i][0]));
30
31 $mediaIDs = array_merge($mediaIDs, $parsedIDs);
32 }
33 }
34
35 return $mediaIDs;
36 }
37
38 /**
39 * @inheritDoc
40 */
41 public function loadObjects(array $objectIDs) {
42 return ViewableMediaRuntimeCache::getInstance()->getObjects($objectIDs);
43 }
44
45 /**
46 * @inheritDoc
47 */
48 public function validateValues($objectType, $objectID, array $values) {
49 $mediaList = new MediaList();
50 $mediaList->getConditionBuilder()->add("media.mediaID IN (?)", [$values]);
51 $mediaList->readObjectIDs();
52
53 return $mediaList->getObjectIDs();
54 }
55
56 /**
57 * @inheritDoc
58 */
59 public function replaceSimple($objectType, $objectID, $value, array $attributes) {
60 /** @var Media $media */
61 $media = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.media', $value);
62 if ($media === null) {
63 return null;
64 }
65
66 $return = (!empty($attributes['return'])) ? $attributes['return'] : 'link';
67 switch ($return) {
68 case 'title':
69 return $media->getTitle();
70 break;
71
72 case 'link':
73 default:
74 $size = (!empty($attributes['size'])) ? $attributes['size'] : 'original';
75 switch ($size) {
76 case 'small':
77 case 'medium':
78 case 'large':
79 return $media->getThumbnailLink($size);
80 break;
81
82 case 'original':
83 default:
84 return $media->getLink();
85 break;
86 }
87
88 break;
89 }
90 }
91
92 }