Prototype to render files as HTML elements
authorAlexander Ebert <ebert@woltlab.com>
Thu, 28 Mar 2024 11:34:56 +0000 (12:34 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 8 Jun 2024 10:19:38 +0000 (12:19 +0200)
com.woltlab.wcf/templates/shared_messageFormAttachments.tpl
wcfsetup/install/files/lib/data/attachment/Attachment.class.php
wcfsetup/install/files/lib/data/attachment/AttachmentList.class.php
wcfsetup/install/files/lib/data/file/File.class.php

index 851dd4cb4f68a68ad5f9d095965e66bbba263cf3..958dd19742ee99a9fcc659fe2117fca1873dc307 100644 (file)
@@ -1,13 +1,17 @@
 <div class="messageTabMenuContent" id="attachments_{if $wysiwygSelector|isset}{$wysiwygSelector}{else}text{/if}">
-       {@$attachmentHandler->getHtmlElement()}
+       {unsafe:$attachmentHandler->getHtmlElement()}
        <dl class="wide">
                <dt></dt>
                <dd>
-                       <div data-max-size="{@$attachmentHandler->getMaxSize()}"></div>
+                       <div data-max-size="{$attachmentHandler->getMaxSize()}"></div>
                        <small>{lang}wcf.attachment.upload.limits{/lang}</small>
                </dd>
        </dl>
 
+       {foreach from=$attachmentHandler->getAttachmentList() item=attachment}
+               {unsafe:$attachment->toHtmlElement()}
+       {/foreach}
+
        <script data-relocate="true">
                require(["WoltLabSuite/Core/Component/Attachment/List"], ({ setup }) => {
                        setup("{if $wysiwygSelector|isset}{$wysiwygSelector}{else}text{/if}");
index 30ae35d43352c21fa00454e7573cb62132545407..e5d7e9cc53a6f28414a407f9e2e6a4e9921924fd 100644 (file)
@@ -6,6 +6,7 @@ use wcf\data\DatabaseObject;
 use wcf\data\ILinkableObject;
 use wcf\data\IThumbnailFile;
 use wcf\data\file\File;
+use wcf\data\file\thumbnail\FileThumbnailList;
 use wcf\data\object\type\ObjectTypeCache;
 use wcf\system\request\IRouteController;
 use wcf\system\request\LinkHandler;
@@ -361,6 +362,13 @@ class Attachment extends DatabaseObject implements ILinkableObject, IRouteContro
 
         if (!isset($this->file)) {
             $this->file = new File($fileID);
+
+            $thumbnailList = new FileThumbnailList();
+            $thumbnailList->getConditionBuilder()->add("fileID = ?", [$this->file->fileID]);
+            $thumbnailList->readObjects();
+            foreach ($thumbnailList as $thumbnail) {
+                $this->file->addThumbnail($thumbnail);
+            }
         }
 
         return $this->file;
@@ -406,6 +414,11 @@ class Attachment extends DatabaseObject implements ILinkableObject, IRouteContro
         };
     }
 
+    public function toHtmlElement(): ?string
+    {
+        return $this->getFile()?->toHtmlElement();
+    }
+
     public static function findByFileID(int $fileID): ?Attachment
     {
         $sql = "SELECT  *
index dfcc2b7476add5c771954315aa12da6de5041628..f4b35143c15ca961e68eca90b2605b46217b437f 100644 (file)
@@ -26,11 +26,20 @@ class AttachmentList extends DatabaseObjectList
      */
     public $className = Attachment::class;
 
+    public $enableFileLoading = true;
+
     #[\Override]
     public function readObjects()
     {
         parent::readObjects();
 
+        if ($this->enableFileLoading) {
+            $this->loadFiles();
+        }
+    }
+
+    private function loadFiles(): void
+    {
         $fileIDs = [];
         foreach ($this->objects as $attachment) {
             if ($attachment->fileID) {
index 455f41b4320f44b4751f37da14fd74d2ebceeb8f..279c20bf532bdded632de62c5e8a9688ea35794d 100644 (file)
@@ -8,6 +8,7 @@ use wcf\data\file\thumbnail\FileThumbnail;
 use wcf\system\file\processor\FileProcessor;
 use wcf\system\file\processor\IFileProcessor;
 use wcf\system\request\LinkHandler;
+use wcf\util\StringUtil;
 
 /**
  * @author Alexander Ebert
@@ -91,4 +92,31 @@ class File extends DatabaseObject
     {
         return $this->thumbnails[$identifier] ?? null;
     }
+
+    public function toHtmlElement(): string
+    {
+        $thumbnails = [];
+        foreach ($this->thumbnails as $thumbnail) {
+            $thumbnails[] = [
+                'identifier' => $thumbnail->identifier,
+                'link' => $thumbnail->getLink(),
+            ];
+        }
+
+        // TODO: Icon and preview url is missing.
+        return \sprintf(
+            <<<'EOT'
+                <woltlab-core-file
+                    file-id="%d"
+                    data-filename="%s"
+                    data-mime-type="%s"
+                    data-thumbnails="%s"
+                ></woltlab-core-file>
+                EOT,
+            $this->fileID,
+            StringUtil::encodeHTML($this->filename),
+            StringUtil::encodeHTML($this->mimeType),
+            StringUtil::encodeHTML(\json_encode($thumbnails)),
+        );
+    }
 }