Added support for embedded objects
authorAlexander Ebert <ebert@woltlab.com>
Thu, 2 Mar 2017 17:09:15 +0000 (18:09 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 2 Mar 2017 17:09:15 +0000 (18:09 +0100)
wcfsetup/install/files/lib/data/comment/Comment.class.php
wcfsetup/install/files/lib/data/comment/CommentAction.class.php
wcfsetup/install/files/lib/data/comment/StructuredCommentList.class.php
wcfsetup/setup/db/install.sql

index 2452116218235f6489362e2cb908fe4aec3a2d49..93da45add3bff49dafe3774b27a15e828788b7e1 100644 (file)
@@ -3,7 +3,6 @@ namespace wcf\data\comment;
 use wcf\data\DatabaseObject;
 use wcf\data\IMessage;
 use wcf\data\TUserContent;
-use wcf\system\bbcode\SimpleMessageParser;
 use wcf\system\comment\CommentHandler;
 use wcf\system\html\output\HtmlOutputProcessor;
 use wcf\util\StringUtil;
@@ -26,6 +25,7 @@ use wcf\util\StringUtil;
  * @property-read      integer         $responses              number of responses on the comment
  * @property-read      string          $responseIDs            serialized array with the ids of the five latest comment responses
  * @property-read       integer         $enableHtml             is 1 if HTML will rendered in the comment, otherwise 0
+ * @property-read       integer         $hasEmbeddedObjects    number of embedded objects in the comment
  */
 class Comment extends DatabaseObject implements IMessage {
        use TUserContent;
index dc116785be0192f9f5d981be9267a315b2053b54..ddff7aebd26272a41372803796d7c780c665f878 100644 (file)
@@ -5,6 +5,7 @@ use wcf\data\comment\response\CommentResponseAction;
 use wcf\data\comment\response\CommentResponseEditor;
 use wcf\data\comment\response\CommentResponseList;
 use wcf\data\comment\response\StructuredCommentResponse;
+use wcf\data\IMessageInlineEditorAction;
 use wcf\data\object\type\ObjectType;
 use wcf\data\object\type\ObjectTypeCache;
 use wcf\data\AbstractDatabaseObjectAction;
@@ -17,6 +18,7 @@ use wcf\system\exception\SystemException;
 use wcf\system\exception\UserInputException;
 use wcf\system\html\input\HtmlInputProcessor;
 use wcf\system\like\LikeHandler;
+use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
 use wcf\system\user\activity\event\UserActivityEventHandler;
 use wcf\system\user\notification\object\type\ICommentUserNotificationObjectType;
 use wcf\system\user\notification\object\type\IMultiRecipientCommentUserNotificationObjectType;
@@ -39,7 +41,7 @@ use wcf\util\UserUtil;
  * @method     CommentEditor[]         getObjects()
  * @method     CommentEditor           getSingleObject()
  */
-class CommentAction extends AbstractDatabaseObjectAction {
+class CommentAction extends AbstractDatabaseObjectAction implements IMessageInlineEditorAction {
        /**
         * @inheritDoc
         */
@@ -255,6 +257,13 @@ class CommentAction extends AbstractDatabaseObjectAction {
                        'enableHtml' => 1
                ]);
                
+               // save embedded objects
+               $htmlInputProcessor->setObjectID($this->createdComment->commentID);
+               if (MessageEmbeddedObjectManager::getInstance()->registerObjects($htmlInputProcessor)) {
+                       (new CommentEditor($this->createdComment))->update(['hasEmbeddedObjects' => 1]);
+                       $this->createdComment = new Comment($this->createdComment->commentID);
+               }
+               
                // update counter
                $this->commentProcessor->updateCounter($this->parameters['data']['objectID'], 1);
                
@@ -540,6 +549,9 @@ class CommentAction extends AbstractDatabaseObjectAction {
                ];
        }
        
+       /**
+        * @inheritDoc
+        */
        public function validateBeginEdit() {
                $this->comment = $this->getSingleObject();
                
@@ -553,6 +565,9 @@ class CommentAction extends AbstractDatabaseObjectAction {
                }
        }
        
+       /**
+        * @inheritDoc
+        */
        public function beginEdit() {
                WCF::getTPL()->assign([
                        'comment' => $this->comment,
@@ -565,12 +580,18 @@ class CommentAction extends AbstractDatabaseObjectAction {
                ];
        }
        
+       /**
+        * @inheritDoc
+        */
        public function validateSave() {
                $this->validateBeginEdit();
                
                $this->validateMessage(true);
        }
        
+       /**
+        * @inheritDoc
+        */
        public function save() {
                /** @var HtmlInputProcessor $htmlInputProcessor */
                $htmlInputProcessor = $this->parameters['htmlInputProcessor'];
@@ -582,6 +603,16 @@ class CommentAction extends AbstractDatabaseObjectAction {
                ]);
                $action->executeAction();
                
+               if ($this->comment->hasEmbeddedObjects != MessageEmbeddedObjectManager::getInstance()->registerObjects($htmlInputProcessor)) {
+                       /** @noinspection PhpUndefinedMethodInspection */
+                       $this->comment->update([
+                               'hasEmbeddedObjects' => $this->comment->hasEmbeddedObjects ? 0 : 1
+                       ]);
+               }
+               
+               // load embedded objects
+               MessageEmbeddedObjectManager::getInstance()->loadObjects('com.woltlab.wcf.comment', [$this->comment->commentID]);
+               
                return [
                        'actionName' => 'save',
                        'message' => (new Comment($this->comment->commentID))->getFormattedMessage()
@@ -706,6 +737,10 @@ class CommentAction extends AbstractDatabaseObjectAction {
         * @return      string
         */
        protected function renderComment(Comment $comment) {
+               if ($comment->hasEmbeddedObjects) {
+                       MessageEmbeddedObjectManager::getInstance()->loadObjects('com.woltlab.wcf.comment', [$comment->commentID]);
+               }
+               
                $comment = new StructuredComment($comment);
                $comment->setIsDeletable($this->commentProcessor->canDeleteComment($comment->getDecoratedObject()));
                $comment->setIsEditable($this->commentProcessor->canEditComment($comment->getDecoratedObject()));
@@ -754,7 +789,7 @@ class CommentAction extends AbstractDatabaseObjectAction {
                
                if ($isComment) {
                        $this->setDisallowedBBCodes();
-                       $htmlInputProcessor = $this->getHtmlInputProcessor($this->parameters['data']['message']);
+                       $htmlInputProcessor = $this->getHtmlInputProcessor($this->parameters['data']['message'], ($this->comment !== null ? $this->comment->commentID : 0));
                        
                        // search for disallowed bbcodes
                        $disallowedBBCodes = $htmlInputProcessor->validate();
index 9e198cc65368c9ef7cb83d1937f9794b8a0debb8..e9d8870dd6f1d2ea08b667f768ac0ea2d9b87265 100644 (file)
@@ -6,6 +6,7 @@ use wcf\data\like\object\LikeObject;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
 use wcf\system\comment\manager\ICommentManager;
 use wcf\system\like\LikeHandler;
+use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
 
 /**
  * Provides a structured comment list fetching last responses for every comment.
@@ -66,6 +67,24 @@ class StructuredCommentList extends CommentList {
         */
        public $sqlOrderBy = 'comment.time DESC';
        
+       /**
+        * enables/disables the loading of responses
+        * @var boolean
+        */
+       public $responseLoading = true;
+       
+       /**
+        * enables/disables the loading of embedded objects
+        * @var boolean
+        */
+       protected $embeddedObjectLoading = true;
+       
+       /**
+        * ids of the comments with embedded objects
+        * @var integer[]
+        */
+       protected $embeddedObjectCommentIDs = [];
+       
        /**
         * Creates a new structured comment list.
         * 
@@ -93,12 +112,20 @@ class StructuredCommentList extends CommentList {
                
                // fetch response ids
                $responseIDs = $userIDs = [];
+               /** @var Comment $comment */
                foreach ($this->objects as $comment) {
                        if (!$this->minCommentTime || $comment->time < $this->minCommentTime) $this->minCommentTime = $comment->time;
-                       $commentResponseIDs = $comment->getResponseIDs();
-                       foreach ($commentResponseIDs as $responseID) {
-                               $this->responseIDs[] = $responseID;
-                               $responseIDs[$responseID] = $comment->commentID;
+                       
+                       if ($this->responseLoading) {
+                               $commentResponseIDs = $comment->getResponseIDs();
+                               foreach ($commentResponseIDs as $responseID) {
+                                       $this->responseIDs[] = $responseID;
+                                       $responseIDs[$responseID] = $comment->commentID;
+                               }
+                       }
+                       
+                       if ($this->embeddedObjectLoading && $comment->hasEmbeddedObjects) {
+                               $this->embeddedObjectCommentIDs[] = $comment->commentID;
                        }
                        
                        if ($comment->userID) {
@@ -110,7 +137,7 @@ class StructuredCommentList extends CommentList {
                }
                
                // fetch last responses
-               if (!empty($responseIDs)) {
+               if ( !empty($responseIDs)) {
                        $responseList = new CommentResponseList();
                        $responseList->setObjectIDs(array_keys($responseIDs));
                        $responseList->readObjects();
@@ -138,6 +165,10 @@ class StructuredCommentList extends CommentList {
                if (!empty($userIDs)) {
                        UserProfileRuntimeCache::getInstance()->cacheObjectIDs(array_unique($userIDs));
                }
+               
+               if ($this->embeddedObjectLoading) {
+                       $this->readEmbeddedObjects();
+               }
        }
        
        /**
@@ -179,4 +210,14 @@ class StructuredCommentList extends CommentList {
        public function getCommentManager() {
                return $this->commentManager;
        }
+       
+       /**
+        * Reads the embedded objects of the posts in the list.
+        */
+       public function readEmbeddedObjects() {
+               if (!empty($this->embeddedObjectCommentIDs)) {
+                       // load embedded objects
+                       MessageEmbeddedObjectManager::getInstance()->loadObjects('com.woltlab.wcf.comment', $this->embeddedObjectCommentIDs);
+               }
+       }
 }
index 958f3950e1be5d929b2fb481b1a217314f09ae72..d3d378d0f314c0ceadb8c2f6a7e4cb49925a2a70 100644 (file)
@@ -379,6 +379,7 @@ CREATE TABLE wcf1_comment (
        responses MEDIUMINT(7) NOT NULL DEFAULT '0',
        responseIDs VARCHAR(255) NOT NULL DEFAULT '',
        enableHtml TINYINT(1) NOT NULL DEFAULT 0,
+       hasEmbeddedObjects TINYINT(1) NOT NULL DEFAULT 0,
        
        KEY (objectTypeID, objectID, time),
        KEY lastCommentTime (userID, time)