diff --git a/protected/humhub/docs/CHANGELOG.md b/protected/humhub/docs/CHANGELOG.md index 17701cd6c0..56faa1ac65 100644 --- a/protected/humhub/docs/CHANGELOG.md +++ b/protected/humhub/docs/CHANGELOG.md @@ -1,10 +1,6 @@ HumHub Changelog ================ -1.6.5 (Unreleased) ------------------- -- Fix #4396: Submitting only picture in comment results in debug error. - 1.6.4 (Unreleased) ------------------ @@ -14,7 +10,7 @@ This release also brings a [security update](https://github.com/yiisoft/yii2/sec - Fix #4361: Added missing nonce attribute in inline marketplace script - Fix #4371: Word break issue in notification dropdown - Fix #4384: Upgrade to Yii 2.0.38 - +- Fix #4396: Submitting only picture in comment results in debug error. 1.6.3 (September 9, 2020) ----------------------- diff --git a/protected/humhub/modules/comment/controllers/CommentController.php b/protected/humhub/modules/comment/controllers/CommentController.php index aa99fd3355..a8f2dda504 100644 --- a/protected/humhub/modules/comment/controllers/CommentController.php +++ b/protected/humhub/modules/comment/controllers/CommentController.php @@ -10,6 +10,7 @@ namespace humhub\modules\comment\controllers; use humhub\components\Controller; use humhub\libs\Helpers; +use humhub\modules\comment\models\forms\CommentForm; use humhub\modules\comment\Module; use humhub\modules\content\components\ContentActiveRecord; use humhub\modules\content\models\Content; @@ -21,7 +22,6 @@ use humhub\components\behaviors\AccessControl; use humhub\modules\comment\models\Comment; use humhub\modules\comment\widgets\Comment as CommentWidget; use humhub\modules\comment\widgets\ShowMore; -use yii\web\BadRequestHttpException; use yii\web\ForbiddenHttpException; /** @@ -130,22 +130,15 @@ class CommentController extends Controller } return Comment::getDb()->transaction(function ($db) { - $message = Yii::$app->request->post('message'); - $files = Yii::$app->request->post('fileList'); - if (empty(trim($message)) && empty($files)) { - throw new BadRequestHttpException(Yii::t('CommentModule.base', 'The comment must not be empty!')); + $form = new CommentForm($this->target); + + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + $comment = $form->save(); + return $this->renderAjaxContent(CommentWidget::widget(['comment' => $comment])); + } else { + return false; } - - $comment = new Comment(['message' => $message]); - $comment->setPolyMorphicRelation($this->target); - $comment->save(); - $comment->fileManager->attach($files); - - // Reload comment to get populated created_at field - $comment->refresh(); - - return $this->renderAjaxContent(CommentWidget::widget(['comment' => $comment])); }); } @@ -154,14 +147,26 @@ class CommentController extends Controller { $comment = Comment::findOne(['id' => Yii::$app->request->get('id')]); + // Find all file attached to this comment + $files = $comment->fileManager->find()->asArray()->all(); + $fileList = []; + $i = 0; + + // Fill up files array + foreach ($files as $file) { + $fileList[$i] = $file['guid']; + $i++; + } + if (!$comment->canEdit()) { throw new HttpException(403, Yii::t('CommentModule.base', 'Access denied!')); } - if ($comment->load(Yii::$app->request->post()) && $comment->save()) { + $form = new CommentForm($this->target, $fileList); - // Reload comment to get populated updated_at field - $comment = Comment::findOne(['id' => $comment->id]); + if ($form->load(Yii::$app->request->post(), '') && $form->validate()) { + + $comment = $form->update($comment); return $this->renderAjaxContent(CommentWidget::widget([ 'comment' => $comment, diff --git a/protected/humhub/modules/comment/models/Comment.php b/protected/humhub/modules/comment/models/Comment.php index f9e856fddf..96f405fc99 100644 --- a/protected/humhub/modules/comment/models/Comment.php +++ b/protected/humhub/modules/comment/models/Comment.php @@ -57,9 +57,6 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner { return [ [['message'], 'safe'], - [['message'], 'required', 'when' => function ($comment) { - return !$comment->fileManager->find()->exists(); - }], ]; } diff --git a/protected/humhub/modules/comment/models/forms/CommentForm.php b/protected/humhub/modules/comment/models/forms/CommentForm.php new file mode 100644 index 0000000000..26ce05f23c --- /dev/null +++ b/protected/humhub/modules/comment/models/forms/CommentForm.php @@ -0,0 +1,122 @@ +target = $target; + $this->fileList = $fileList; + parent::__construct(); + } + + /** + * @return array validation rules for model attributes. + */ + public function rules() + { + return [ + [['message'], 'safe'], + [['message'], 'required', 'isEmpty' => function ($message) { + $hasFile = !is_null($this->fileList) && !empty($this->fileList) ? true : false; + + //check if message empty and attached file exists + if (empty($message) && !$hasFile) { + throw new ServerErrorHttpException(Yii::t('CommentModule.base', 'The comment must not be empty!')); + } + }], + [['fileList'], 'safe'], + ]; + } + + /** + * @inheritDoc + */ + public function load($data, $formName = null) + { + // When user updates comment $data contain 'Comment', otherwise not + if (isset($data['Comment'])) { + $data['message'] = $data['Comment']['message']; + unset($data['Comment']); + } + + return parent::load($data, $formName); + } + + /** + * @inheritDoc + */ + public function formName() + { + return ''; + } + + /** + * Updates the form + * + * @param Comment $comment + * @return Comment|boolean + */ + public function update($comment) + { + + $comment->message = $this->message; + $comment->setPolyMorphicRelation($this->target); + + //check if model saved + if (!$comment->save()) { + return false; + } + + $comment->fileManager->attach($this->fileList); + + // Reload comment to get populated created_at field + $comment->refresh(); + return $comment; + } + + + /** + * Saves the form + * + * @return Comment|boolean + */ + public function save() + { + + $comment = new Comment(['message' => $this->message]); + $comment->setPolyMorphicRelation($this->target); + + //check if model saved + if (!$comment->save()) { + return false; + } + + $comment->fileManager->attach($this->fileList); + + // Reload comment to get populated created_at field + $comment->refresh(); + return $comment; + } + +}