Comment code cleanups

This commit is contained in:
Lucas Bartholemy 2020-07-01 11:32:06 +02:00
parent 57de3afa8c
commit 74d21a9b60
17 changed files with 162 additions and 130 deletions

View File

@ -8,6 +8,7 @@
namespace humhub\modules\comment;
use humhub\modules\content\components\ContentActiveRecord;
use Yii;
use humhub\modules\comment\models\Comment;
use humhub\modules\search\events\SearchAttributesEvent;
@ -30,7 +31,10 @@ class Events extends Component
*/
public static function onContentDelete($event)
{
foreach (Comment::find()->where(['object_model' => $event->sender->className(), 'object_id' => $event->sender->id])->all() as $comment) {
/** @var Comment|ContentActiveRecord $sender */
$sender = $event->sender;
foreach (Comment::find()->where(['object_model' => get_class($sender), 'object_id' => $sender->getPrimaryKey()])->all() as $comment) {
$comment->delete();
}
}

View File

@ -4,6 +4,7 @@ namespace humhub\modules\comment;
use humhub\modules\comment\models\Comment;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\space\models\Space;
use Yii;
/**
@ -27,7 +28,7 @@ class Module extends \humhub\components\Module
*/
public function getPermissions($contentContainer = null)
{
if ($contentContainer instanceof \humhub\modules\space\models\Space) {
if ($contentContainer instanceof Space) {
return [
new permissions\CreateComment()
];
@ -69,7 +70,7 @@ class Module extends \humhub\components\Module
$content = $object->content;
if ($content->container instanceof \humhub\modules\space\models\Space) {
if ($content->container instanceof Space) {
$space = $content->container;
if (!$space->permissionManager->can(new permissions\CreateComment())) {
return false;

View File

@ -13,7 +13,7 @@ use humhub\modules\activity\components\BaseActivity;
use humhub\modules\activity\interfaces\ConfigurableActivityInterface;
/**
* Description of NewComment
* NewComment activity
*
* @author luke
*/

View File

@ -8,6 +8,7 @@ use humhub\modules\content\widgets\WallEntryAddons;
use humhub\modules\content\widgets\WallEntryLinks;
use humhub\modules\search\engine\Search;
/** @noinspection MissedFieldInspection */
return [
'id' => 'comment',
'class' => \humhub\modules\comment\Module::class,

View File

@ -16,6 +16,7 @@ use humhub\modules\content\models\Content;
use Yii;
use yii\data\Pagination;
use yii\web\HttpException;
use yii\helpers\Url;
use humhub\components\behaviors\AccessControl;
use humhub\modules\comment\models\Comment;
use humhub\modules\comment\widgets\Comment as CommentWidget;
@ -70,8 +71,8 @@ class CommentController extends Controller
*/
public function beforeAction($action)
{
$modelClass = Yii::$app->request->get('object_model', Yii::$app->request->post('object_model'));
$modelPk = (int)Yii::$app->request->get('object_id', Yii::$app->request->post('object_id'));
$modelClass = Yii::$app->request->get('objectModel', Yii::$app->request->post('objectModel'));
$modelPk = (int)Yii::$app->request->get('objectId', Yii::$app->request->post('objectId'));
Helpers::CheckClassType($modelClass, [Comment::class, ContentActiveRecord::class]);
$this->target = $modelClass::findOne(['id' => $modelPk]);
@ -124,7 +125,7 @@ class CommentController extends Controller
*/
public function actionPost()
{
if (Yii::$app->user->isGuest || !$this->module->canComment($this->content)) {
if (Yii::$app->user->isGuest || !$this->module->canComment($this->target)) {
throw new ForbiddenHttpException(Yii::t('CommentModule.base', 'You are not allowed to comment.'));
}
@ -151,56 +152,55 @@ class CommentController extends Controller
public function actionEdit()
{
$this->loadContentAddon(Comment::class, Yii::$app->request->get('id'));
$comment = Comment::findOne(['id' => Yii::$app->request->get('id')]);
if (!$this->contentAddon->canEdit()) {
if (!$comment->canEdit()) {
throw new HttpException(403, Yii::t('CommentModule.base', 'Access denied!'));
}
if ($this->contentAddon->load(Yii::$app->request->post()) && $this->contentAddon->save()) {
if ($comment->load(Yii::$app->request->post()) && $comment->save()) {
// Reload comment to get populated updated_at field
$this->contentAddon = Comment::findOne(['id' => $this->contentAddon->id]);
$comment = Comment::findOne(['id' => $comment->id]);
return $this->renderAjaxContent(CommentWidget::widget([
'comment' => $this->contentAddon,
'comment' => $comment,
'justEdited' => true
]));
} else if (Yii::$app->request->post()) {
Yii::$app->response->statusCode = 400;
}
$submitUrl = Url::to(['/comment/comment/edit',
'id' => $comment->id, 'objectModel' => $comment->object_model, 'objectId' => $comment->object_id]);
return $this->renderAjax('edit', [
'comment' => $this->contentAddon,
'contentModel' => $this->contentAddon->object_model,
'contentId' => $this->contentAddon->object_id
'comment' => $comment,
'objectModel' => $comment->object_model,
'objectId' => $comment->object_id,
'submitUrl' => $submitUrl
]);
}
public function actionLoad()
{
$this->loadContentAddon(Comment::class, Yii::$app->request->get('id'));
$comment = Comment::findOne(['id' => Yii::$app->request->get('id')]);
if (!$this->contentAddon->canRead()) {
if (!$comment->canRead()) {
throw new HttpException(403, Yii::t('CommentModule.base', 'Access denied!'));
}
return $this->renderAjaxContent(CommentWidget::widget(['comment' => $this->contentAddon]));
return $this->renderAjaxContent(CommentWidget::widget(['comment' => $comment]));
}
/**
* Handles AJAX Request for Comment Deletion.
* Currently this is only allowed for the Comment Owner.
*/
public function actionDelete()
{
$this->forcePostRequest();
$this->loadContentAddon(Comment::class, Yii::$app->request->get('id'));
Yii::$app->response->format = 'json';
if ($this->contentAddon->canDelete()) {
$this->contentAddon->delete();
return ['success' => true];
$comment = Comment::findOne(['id' => Yii::$app->request->get('id')]);
if ($comment !== null && $comment->canDelete()) {
$comment->delete();
return $this->asJson(['success' => true]);
} else {
throw new HttpException(500, Yii::t('CommentModule.base', 'Insufficent permissions!'));
}

View File

@ -5,57 +5,58 @@ use humhub\modules\file\widgets\UploadButton;
use humhub\widgets\Button;
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
use humhub\modules\content\widgets\richtext\RichTextField;
/* @var $contentModel string */
/* @var $contentId integer */
/* @var $this \humhub\modules\ui\view\components\View */
/* @var $objectModel string */
/* @var $objectId integer */
/* @var $comment \humhub\modules\comment\models\Comment */
/* @var $submitUrl string */
/** @var \humhub\modules\content\Module $contentModule */
$contentModule = Yii::$app->getModule('content');
$submitUrl = Url::to(['/comment/comment/edit', 'id' => $comment->id, 'contentModel' => $comment->object_model, 'contentId' => $comment->object_id]);
?>
<div class="content_edit input-container" id="comment_edit_<?= $comment->id; ?>">
<?php $form = ActiveForm::begin(); ?>
<?= Html::hiddenInput('contentModel', $contentModel); ?>
<?= Html::hiddenInput('contentId', $contentId); ?>
<?= Html::hiddenInput('objectModel', $objectModel); ?>
<?= Html::hiddenInput('objectId', $objectId); ?>
<div class="comment-create-input-group">
<?= $form->field($comment, 'message')->widget(RichTextField::class, [
'id' => 'comment_input_'.$comment->id,
'layout' => RichTextField::LAYOUT_INLINE,
'pluginOptions' => ['maxHeight' => '300px'],
'placeholder' => Yii::t('CommentModule.base', 'Edit your comment...'),
'focus' => true,
'events' => [
'scroll-active' => 'comment.scrollActive',
'scroll-inactive' => 'comment.scrollInactive'
]
])->label(false) ?>
<div class="comment-create-input-group">
<?= $form->field($comment, 'message')->widget(RichTextField::class, [
'id' => 'comment_input_' . $comment->id,
'layout' => RichTextField::LAYOUT_INLINE,
'pluginOptions' => ['maxHeight' => '300px'],
'placeholder' => Yii::t('CommentModule.base', 'Edit your comment...'),
'focus' => true,
'events' => [
'scroll-active' => 'comment.scrollActive',
'scroll-inactive' => 'comment.scrollInactive'
]
])->label(false) ?>
<div class="comment-buttons">
<div class="comment-buttons">
<?= UploadButton::widget([
'id' => 'comment_upload_' . $comment->id,
'model' => $comment,
'dropZone' => '#comment_' . $comment->id,
'preview' => '#comment_upload_preview_' . $comment->id,
'progress' => '#comment_upload_progress_' . $comment->id,
'max' => $contentModule->maxAttachedFiles
]); ?>
<?= UploadButton::widget([
'id' => 'comment_upload_' . $comment->id,
'model' => $comment,
'dropZone' => '#comment_'.$comment->id,
'preview' => '#comment_upload_preview_'.$comment->id,
'progress' => '#comment_upload_progress_'.$comment->id,
'max' => Yii::$app->getModule('content')->maxAttachedFiles
]); ?>
<?= Button::defaultType(Yii::t('base', 'Save'))->cssClass('btn-comment-submit')->action('editSubmit', $submitUrl)->submit()->sm() ?>
</div>
<?= Button::defaultType(Yii::t('base', 'Save'))->cssClass('btn-comment-submit')->action('editSubmit', $submitUrl)->submit()->sm() ?>
</div>
</div>
<div id="comment_upload_progress_<?= $comment->id ?>" style="display:none; margin:10px 0;"></div>
<div id="comment_upload_progress_<?= $comment->id ?>" style="display:none; margin:10px 0;"></div>
<?= FilePreview::widget([
'id' => 'comment_upload_preview_'.$comment->id,
'options' => ['style' => 'margin-top:10px'],
'model' => $comment,
'edit' => true
]); ?>
<?= FilePreview::widget([
'id' => 'comment_upload_preview_' . $comment->id,
'options' => ['style' => 'margin-top:10px'],
'model' => $comment,
'edit' => true
]); ?>
<?php ActiveForm::end(); ?>
</div>
</div>

View File

@ -4,6 +4,9 @@ use humhub\libs\Html;
use humhub\widgets\ModalDialog;
use humhub\modules\comment\widgets\Form;
/* @var $this \humhub\modules\ui\view\components\View */
/* @var $object \humhub\modules\content\components\ContentActiveRecord */
?>
<?php ModalDialog::begin(['header' => Yii::t('CommentModule.base', 'Comments')]) ?>

View File

@ -33,9 +33,12 @@ class Comment extends Widget
*/
public function run()
{
$deleteUrl = Url::to(['/comment/comment/delete', 'contentModel' => $this->comment->object_model, 'contentId' => $this->comment->object_id, 'id' => $this->comment->id]);
$editUrl = Url::to(['/comment/comment/edit', 'contentModel' => $this->comment->object_model, 'contentId' => $this->comment->object_id, 'id' => $this->comment->id]);
$loadUrl = Url::to(['/comment/comment/load', 'contentModel' => $this->comment->object_model, 'contentId' => $this->comment->object_id, 'id' => $this->comment->id]);
$deleteUrl = Url::to(['/comment/comment/delete',
'objectModel' => $this->comment->object_model, 'objectId' => $this->comment->object_id, 'id' => $this->comment->id]);
$editUrl = Url::to(['/comment/comment/edit',
'objectModel' => $this->comment->object_model, 'objectId' => $this->comment->object_id, 'id' => $this->comment->id]);
$loadUrl = Url::to(['/comment/comment/load',
'objectModel' => $this->comment->object_model, 'objectId' => $this->comment->object_id, 'id' => $this->comment->id]);
$updatedAt = null;
if (!empty($this->comment->updated_at) && $this->comment->created_at != $this->comment->updated_at) {
@ -43,16 +46,16 @@ class Comment extends Widget
}
return $this->render('showComment', [
'comment' => $this->comment,
'user' => $this->comment->user,
'justEdited' => $this->justEdited,
'deleteUrl' => $deleteUrl,
'editUrl' => $editUrl,
'loadUrl' => $loadUrl,
'createdAt' => $this->comment->created_at,
'updatedAt' => $updatedAt,
'canEdit' => $this->comment->canEdit(),
'canDelete' => $this->comment->canDelete(),
'comment' => $this->comment,
'user' => $this->comment->user,
'justEdited' => $this->justEdited,
'deleteUrl' => $deleteUrl,
'editUrl' => $editUrl,
'loadUrl' => $loadUrl,
'createdAt' => $this->comment->created_at,
'updatedAt' => $updatedAt,
'canEdit' => $this->comment->canEdit(),
'canDelete' => $this->comment->canDelete(),
]);
}

View File

@ -12,6 +12,7 @@ use humhub\components\ActiveRecord;
use humhub\components\Widget;
use humhub\modules\comment\models\Comment as CommentModel;
use humhub\modules\comment\Module;
use humhub\modules\content\components\ContentActiveRecord;
use Yii;
/**
@ -26,7 +27,7 @@ class CommentLink extends Widget
const MODE_POPUP = 'popup';
/**
* @var ActiveRecord
* @var CommentModel|ContentActiveRecord
*/
public $object;
@ -50,7 +51,7 @@ class CommentLink extends Widget
/** @var Module $module */
$module = Yii::$app->getModule('comment');
if ($this->mode == "") {
if (empty($this->mode)) {
$this->mode = self::MODE_INLINE;
}
@ -58,18 +59,11 @@ class CommentLink extends Widget
return '';
}
$objectModel = $this->object->content->object_model;
$objectId = $this->object->content->object_id;
if ($this->object instanceof CommentModel) {
$objectModel = CommentModel::class;
$objectId = $this->object->id;
}
return $this->render('link', [
'id' => $this->object->getUniqueId(),
'mode' => $this->mode,
'objectModel' => $objectModel,
'objectId' => $objectId,
'objectModel' => get_class($this->object),
'objectId' => $this->object->getPrimaryKey(),
]);
}

View File

@ -39,8 +39,8 @@ class Comments extends \yii\base\Widget
return $this->render('comments', [
'object' => $this->object,
'comments' => $comments,
'modelName' => $objectModel,
'modelId' => $objectId,
'objectModel' => $objectModel,
'objectId' => $objectId,
'id' => $this->object->getUniqueId(),
'isLimited' => $isLimited,
'total' => $commentCount

View File

@ -9,8 +9,9 @@
namespace humhub\modules\comment\widgets;
use humhub\modules\comment\Module;
use humhub\modules\content\components\ContentActiveRecord;
use Yii;
use humhub\modules\comment\models\Comment as CommentModel;
use humhub\modules\comment\models\Comment as CommentModel;
/**
* This widget is used include the comments functionality to a wall entry.
@ -22,9 +23,8 @@ use humhub\modules\comment\models\Comment as CommentModel;
*/
class Form extends \yii\base\Widget
{
/**
* Content Object
* @var CommentModel|ContentActiveRecord
*/
public $object;
@ -33,7 +33,6 @@ class Form extends \yii\base\Widget
*/
public function run()
{
if (Yii::$app->user->isGuest) {
return '';
}
@ -45,12 +44,9 @@ class Form extends \yii\base\Widget
return '';
}
$objectModel = get_class($this->object);
$objectId = $this->object->getPrimaryKey();
return $this->render('form', [
'modelName' => $objectModel,
'modelId' => $objectId,
'objectModel' => get_class($this->object),
'objectId' => $this->object->getPrimaryKey(),
'id' => $this->object->getUniqueId(),
]);
}

View File

@ -2,10 +2,11 @@
namespace humhub\modules\comment\widgets;
use yii\helpers\Url;
/**
* CommentsShowMoreWidget
*
* @package humhub.modules_core.comment
* @since 0.11
* @author luke
*/
@ -27,15 +28,15 @@ class ShowMore extends \yii\base\Widget
*/
public function run()
{
if (!$this->pagination->totalCount || $this->pagination->pageCount == $this->pagination->page + 1) {
return;
return '';
}
$showMoreUrl = \yii\helpers\Url::to([
$showMoreUrl = Url::to([
'/comment/comment/show',
'contentModel' => get_class($this->object),
'contentId' => $this->object->getPrimaryKey(),
'objectModel' => get_class($this->object),
'objectId' => $this->object->getPrimaryKey(),
'page' => $this->pagination->page + 2
]);

View File

@ -5,18 +5,27 @@ use humhub\modules\comment\widgets\Comment;
use yii\helpers\Url;
use humhub\libs\Html;
/* @var $this \humhub\modules\ui\view\components\View */
/* @var $object \humhub\modules\content\components\ContentActiveRecord */
/* @var $comments \humhub\modules\comment\models\Comment[] */
/* @var $objectModel string */
/* @var $objectId int */
/* @var $id string unqiue object id */
/* @var $isLimited boolean */
/* @var $total int */
?>
<div class="well well-small comment-container" style="display:none;" id="comment_<?= $id; ?>">
<div class="comment <?php if (Yii::$app->user->isGuest): ?>guest-mode<?php endif; ?>"
id="comments_area_<?= $id; ?>">
<?php if ($isLimited): ?>
<a href="#" class="show show-all-link" data-ui-loader data-action-click="comment.showAll"
data-action-url="<?= Url::to(['/comment/comment/show', 'contentModel' => $modelName, 'contentId' => $modelId]) ?>">
data-action-url="<?= Url::to(['/comment/comment/show', 'objectModel' => $objectModel, 'objectId' => $objectId]) ?>">
<?= Yii::t('CommentModule.base', 'Show all {total} comments.', ['{total}' => $total]) ?>
</a>
<hr>
<?php endif;
?>
<?php endif; ?>
<?php foreach ($comments as $comment) : ?>
<?= Comment::widget(['comment' => $comment]); ?>
@ -24,14 +33,12 @@ use humhub\libs\Html;
</div>
<?= Form::widget(['object' => $object]); ?>
</div>
<script <?= Html::nonce() ?>>
<?php if (count($comments) != 0) { ?>
<?php if (count($comments) != 0): ?>
// make comments visible at this point to fixing autoresizing issue for textareas in Firefox
$('#comment_<?php echo $id; ?>').show();
<?php } ?>
$('#comment_<?= $id; ?>').show();
<?php endif; ?>
</script>

View File

@ -7,9 +7,13 @@ use humhub\modules\content\widgets\richtext\RichTextField;
use humhub\modules\file\widgets\UploadButton;
use humhub\modules\file\widgets\FilePreview;
/* @var $modelName string */
/* @var $modelId integer */
/* @var $this \humhub\modules\ui\view\components\View */
/* @var $objectModel string */
/* @var $objectId integer */
/* @var $id string unique object id */
/** @var \humhub\modules\content\Module $contentModule */
$contentModule = Yii::$app->getModule('content');
$submitUrl = Url::to(['/comment/comment/post']);
?>
@ -17,8 +21,8 @@ $submitUrl = Url::to(['/comment/comment/post']);
<div id="comment_create_form_<?= $id; ?>" class="comment_create" data-ui-widget="comment.Form">
<?= Html::beginForm('#'); ?>
<?= Html::hiddenInput('object_model', $modelName); ?>
<?= Html::hiddenInput('object_id', $modelId); ?>
<?= Html::hiddenInput('objectModel', $objectModel); ?>
<?= Html::hiddenInput('objectId', $objectId); ?>
<div class="comment-create-input-group">
<?= RichTextField::widget([
@ -39,8 +43,8 @@ $submitUrl = Url::to(['/comment/comment/post']);
'options' => ['class' => 'main_comment_upload'],
'progress' => '#comment_create_upload_progress_' . $id,
'preview' => '#comment_create_upload_preview_' . $id,
'dropZone' => '#comment_create_form_'.$id,
'max' => Yii::$app->getModule('content')->maxAttachedFiles
'dropZone' => '#comment_create_form_' . $id,
'max' => $contentModule->maxAttachedFiles
]); ?>
<?= Button::defaultType(Yii::t('CommentModule.base', 'Send'))

View File

@ -5,6 +5,12 @@ use humhub\widgets\Button;
use yii\helpers\Html;
use yii\helpers\Url;
/* @var $this \humhub\modules\ui\view\components\View */
/* @var $objectModel string */
/* @var $objectId integer */
/* @var $id string unique object id */
/* @var $mode string */
$commentCount = $this->context->getCommentsCount();
$hasComments = ($commentCount > 0);
$commentCountSpan = Html::tag('span', ' (' . $commentCount . ')', [
@ -12,11 +18,10 @@ $commentCountSpan = Html::tag('span', ' (' . $commentCount . ')', [
'data-count' => $commentCount,
'style' => ($hasComments) ? null : 'display:none'
]);
?>
<?php if ($mode == CommentLink::MODE_POPUP): ?>
<?php $url = Url::to(['/comment/comment/show', 'object_model' => $objectModel, 'object_id' => $objectId, 'mode' => 'popup']); ?>
<?php $url = Url::to(['/comment/comment/show', 'objectModel' => $objectModel, 'objectId' => $objectId, 'mode' => 'popup']); ?>
<a href="#" data-action-click="ui.modal.load" data-action-url="<?= $url ?>">
<?= Yii::t('CommentModule.base', "Comment") . ' (' . $this->context->getCommentsCount() . ')' ?>
</a>

View File

@ -9,6 +9,7 @@ use humhub\modules\like\widgets\LikeLink;
use humhub\modules\comment\widgets\CommentLink;
use humhub\modules\comment\widgets\Comments;
/* @var $this \humhub\modules\ui\view\components\View */
/* @var $comment \humhub\modules\comment\models\Comment */
/* @var $deleteUrl string */
/* @var $editUrl string */
@ -18,6 +19,10 @@ use humhub\modules\comment\widgets\Comments;
/* @var $canDelete bool */
/* @var $createdAt string */
/* @var $updatedAt string */
/** @var \humhub\modules\comment\Module $module */
$module = Yii::$app->getModule('comment');
?>
<div class="media" id="comment_<?= $comment->id; ?>"
@ -80,12 +85,12 @@ use humhub\modules\comment\widgets\Comments;
<div class="wall-entry-controls">
<?= LikeLink::widget(['object' => $comment]); ?>
&middot;
<?= CommentLink::widget(['object' => $comment]); ?>
<div style="border:1px solid red;padding-top:12px">
<?= Comments::widget(['object' => $comment]); ?>
</div>
<?php if ($module->canComment($comment)): ?>
&middot; <?= CommentLink::widget(['object' => $comment]); ?>
<?php endif; ?>
</div>
<div style="margin-left:42px">
<?= Comments::widget(['object' => $comment]); ?>
</div>
</div>

View File

@ -1,6 +1,13 @@
<?php
/* @var $this \humhub\modules\ui\view\components\View */
/* @var $moreCount int */
/* @var $showMoreUrl string */
?>
<div class="showMore">
<a href="#" data-action-click="comment.showMore" data-action-url="<?= $showMoreUrl ?>">
<?= Yii::t('CommentModule.base', "Show {count} more comments", ['{count}' => $moreCount]) ?>
</a>
<hr />
<hr/>
</div>