ee591d94ddc14103618d86cc9a8c0ffa803d39f5
[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\database\util\PreparedStatementConditionBuilder;
7 use wcf\system\html\input\HtmlInputProcessor;
8 use wcf\system\WCF;
9 use wcf\util\ArrayUtil;
10
11 /**
12 * IMessageEmbeddedObjectHandler implementation for shared media.
13 *
14 * @author Alexander Ebert
15 * @copyright 2001-2017 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\System\Message\Embedded\Object
18 */
19 class MediaMessageEmbeddedObjectHandler extends AbstractSimpleMessageEmbeddedObjectHandler {
20 /**
21 * @inheritDoc
22 */
23 public function parse(HtmlInputProcessor $htmlInputProcessor, array $embeddedData) {
24 $mediaIDs = [];
25 foreach (['wsm', 'wsmg'] as $name) {
26 if (empty($embeddedData[$name])) {
27 continue;
28 }
29
30 for ($i = 0, $length = count($embeddedData[$name]); $i < $length; $i++) {
31 $parsedIDs = ArrayUtil::toIntegerArray(explode(',', $embeddedData[$name][$i][0]));
32
33 $mediaIDs = array_merge($mediaIDs, $parsedIDs);
34 }
35 }
36
37 return $mediaIDs;
38 }
39
40 /**
41 * @inheritDoc
42 */
43 public function loadObjects(array $objectIDs) {
44 $viewableMedia = ViewableMediaRuntimeCache::getInstance()->getObjects($objectIDs);
45 $contentLanguageID = MessageEmbeddedObjectManager::getInstance()->getContentLanguageID();
46 if ($contentLanguageID !== null) {
47 $mediaIDs = [];
48 foreach ($viewableMedia as $media) {
49 if ($media->localizedLanguageID != $contentLanguageID) {
50 $mediaIDs[] = $media->getDecoratedObject()->mediaID;
51 }
52 }
53
54 if (!empty($mediaIDs)) {
55 $conditions = new PreparedStatementConditionBuilder();
56 $conditions->add("mediaID IN (?)", [$mediaIDs]);
57 $conditions->add("languageID = ?", [$contentLanguageID]);
58
59 $sql = "SELECT *
60 FROM wcf".WCF_N."_media_content
61 ".$conditions;
62 $statement = WCF::getDB()->prepareStatement($sql);
63 $statement->execute($conditions->getParameters());
64 while ($row = $statement->fetchArray()) {
65 $viewableMedia[$row['mediaID']]->setLocalizedContent($row['languageID'], $row);
66 }
67 }
68 }
69
70 return $viewableMedia;
71 }
72
73 /**
74 * @inheritDoc
75 */
76 public function validateValues($objectType, $objectID, array $values) {
77 $mediaList = new MediaList();
78 $mediaList->getConditionBuilder()->add("media.mediaID IN (?)", [$values]);
79 $mediaList->readObjectIDs();
80
81 return $mediaList->getObjectIDs();
82 }
83
84 /**
85 * @inheritDoc
86 */
87 public function replaceSimple($objectType, $objectID, $value, array $attributes) {
88 /** @var Media $media */
89 $media = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.media', $value);
90 if ($media === null) {
91 return null;
92 }
93
94 $return = (!empty($attributes['return'])) ? $attributes['return'] : 'link';
95 switch ($return) {
96 case 'title':
97 return $media->getTitle();
98 break;
99
100 case 'link':
101 default:
102 $size = (!empty($attributes['size'])) ? $attributes['size'] : 'original';
103 switch ($size) {
104 case 'small':
105 case 'medium':
106 case 'large':
107 return $media->getThumbnailLink($size);
108 break;
109
110 case 'original':
111 default:
112 return $media->getLink();
113 break;
114 }
115
116 break;
117 }
118 }
119
120 }