Simplify rendering of the comments widget
authorMarcel Werk <burntime@woltlab.com>
Tue, 2 Jan 2024 16:52:01 +0000 (17:52 +0100)
committerMarcel Werk <burntime@woltlab.com>
Tue, 2 Jan 2024 16:52:01 +0000 (17:52 +0100)
com.woltlab.wcf/templates/commentsView.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/view/CommentsView.class.php [new file with mode: 0644]

diff --git a/com.woltlab.wcf/templates/commentsView.tpl b/com.woltlab.wcf/templates/commentsView.tpl
new file mode 100644 (file)
index 0000000..6db9924
--- /dev/null
@@ -0,0 +1,19 @@
+{if $commentsView->isVisible()}
+       {if $commentsView->showSection}
+               <section id="comments" class="section sectionContainerList">
+                       <h2 class="sectionTitle">{lang}wcf.global.comments{/lang}{if $commentsView->totalComments} <span class="badge">{#$commentsView->totalComments}</span>{/if}</h2>
+       {/if}
+
+       {assign var='commentContainerID' value=$commentsView->commentContainerID}
+       {assign var='commentObjectID' value=$commentsView->objectID}
+       {assign var='commentCanAdd' value=$commentsView->canAddComments}
+       {assign var='commentList' value=$commentsView->getCommentList()}
+       {assign var='commentObjectTypeID' value=$commentsView->getObjectTypeID()}
+       {assign var='lastCommentTime' value=$commentsView->getLastCommentTime()}
+       {assign var='likeData' value=$commentsView->getLikeData()}
+       {include file='comments'}
+
+       {if $commentsView->showSection}
+               </section>
+       {/if}
+{/if}
diff --git a/wcfsetup/install/files/lib/system/view/CommentsView.class.php b/wcfsetup/install/files/lib/system/view/CommentsView.class.php
new file mode 100644 (file)
index 0000000..0300fec
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+
+namespace wcf\system\view;
+
+use SystemException;
+use wcf\data\comment\StructuredCommentList;
+use wcf\system\comment\CommentHandler;
+use wcf\system\comment\manager\ICommentManager;
+
+/**
+ * Represents a comments view.
+ *
+ * @author      Marcel Werk
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.1
+ */
+final class CommentsView
+{
+    private int $objectTypeID;
+    private StructuredCommentList $commentList;
+    private ICommentManager $commentManager;
+
+    public function __construct(
+        public readonly string $objectTypeName,
+        public readonly int $objectID,
+        public readonly string $commentContainerID,
+        public readonly bool $canAddComments = true,
+        public readonly int $totalComments = 0,
+        public readonly bool $showSection = true
+    ) {
+        $this->init();
+    }
+
+    private function init(): void
+    {
+        $objectTypeID = CommentHandler::getInstance()
+            ->getObjectTypeID($this->objectTypeName);
+        if (!$objectTypeID) {
+            throw new SystemException("Unable to find object type '{$this->objectTypeName}'");
+        }
+
+        $this->objectTypeID = $objectTypeID;
+
+        $this->commentManager = CommentHandler::getInstance()
+            ->getObjectType($this->objectTypeID)
+            ->getProcessor();
+        $this->commentList = CommentHandler::getInstance()
+            ->getCommentList($this->commentManager, $this->objectTypeID, $this->objectID);
+    }
+
+    public function getObjectTypeID(): int
+    {
+        return $this->objectTypeID;
+    }
+
+    public function getCommentManager(): ICommentManager
+    {
+        return $this->commentManager;
+    }
+
+    public function getCommentList(): StructuredCommentList
+    {
+        return $this->commentList;
+    }
+
+    public function getLastCommentTime(): int
+    {
+        return $this->commentList->getMinCommentTime();
+    }
+
+    public function getLikeData(): array
+    {
+        if (!MODULE_LIKE) {
+            return [];
+        }
+
+        return $this->commentList->getLikeData();
+    }
+
+    public function isVisible(): bool
+    {
+        return $this->canAddComments || \count($this->getCommentList());
+    }
+}