Added support for embedded media in page content
authorAlexander Ebert <ebert@woltlab.com>
Wed, 6 Jul 2016 14:40:09 +0000 (16:40 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 6 Jul 2016 14:40:15 +0000 (16:40 +0200)
wcfsetup/install/files/lib/system/message/embedded/object/MediaMessageEmbeddedObjectHandler.class.php

index 1514103408c7fcf829f70181eaca267509a89868..fd11d4ad9af045a1398de381b5920216e3ce27e8 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 namespace wcf\system\message\embedded\object;
+use wcf\data\media\Media;
+use wcf\data\media\MediaList;
 use wcf\system\cache\runtime\MediaRuntimeCache;
 use wcf\system\html\input\HtmlInputProcessor;
 use wcf\util\ArrayUtil;
@@ -12,7 +14,7 @@ use wcf\util\ArrayUtil;
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System\Message\Embedded\Object
  */
-class MediaMessageEmbeddedObjectHandler extends AbstractMessageEmbeddedObjectHandler {
+class MediaMessageEmbeddedObjectHandler extends AbstractSimpleMessageEmbeddedObjectHandler {
        /**
         * @inheritDoc
         */
@@ -39,4 +41,52 @@ class MediaMessageEmbeddedObjectHandler extends AbstractMessageEmbeddedObjectHan
        public function loadObjects(array $objectIDs) {
                return MediaRuntimeCache::getInstance()->getObjects($objectIDs);
        }
+       
+       /**
+        * @inheritDoc
+        */
+       public function validateValues($objectType, $objectID, array $values) {
+               $mediaList = new MediaList();
+               $mediaList->getConditionBuilder()->add("media.mediaID IN (?)", [$values]);
+               $mediaList->readObjectIDs();
+               
+               return $mediaList->getObjectIDs();
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function replaceSimple($objectType, $objectID, $value, array $attributes) {
+               /** @var Media $media */
+               $media = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.media', $value);
+               if ($media === null) {
+                       return null;
+               }
+               
+               $return = (!empty($attributes['return'])) ? $attributes['return'] : 'link';
+               switch ($return) {
+                       case 'title':
+                               return $media->getTitle();
+                               break;
+                       
+                       case 'link':
+                       default:
+                               $size = (!empty($attributes['size'])) ? $attributes['size'] : 'original';
+                               switch ($size) {
+                                       case 'small':
+                                       case 'medium':
+                                       case 'large':
+                                               return $media->getThumbnailLink($size);
+                                               break;
+                                       
+                                       case 'original':
+                                       default:
+                                               return $media->getLink();
+                                               break;
+                               }
+                               
+                               break;
+               }
+       }
+       
 }