Added preview max parameter

This commit is contained in:
Lucas Bartholemy 2020-07-13 13:59:10 +02:00
parent 02bc617202
commit 8fbad5c8fb
6 changed files with 36 additions and 22 deletions

View File

@ -23,6 +23,11 @@ class Module extends \humhub\components\Module
*/
public $commentsBlockLoadSize = 10;
/**
* @var int maximum comments to show initially
*/
public $commentsPreviewMax = 2;
/**
* @inheritdoc
*/

View File

@ -98,10 +98,10 @@ class CommentController extends Controller
{
$query = Comment::find();
$query->orderBy('created_at DESC');
$query->where(['object_model' => $this->target->className(), 'object_id' => $this->target->getPrimaryKey()]);
$query->where(['object_model' => get_class($this->target), 'object_id' => $this->target->getPrimaryKey()]);
$pagination = new Pagination([
'totalCount' => Comment::GetCommentCount($this->target->className(), $this->target->getPrimaryKey()),
'totalCount' => Comment::GetCommentCount(get_class($this->target), $this->target->getPrimaryKey()),
'pageSize' => $this->module->commentsBlockLoadSize
]);

View File

@ -8,6 +8,7 @@
namespace humhub\modules\comment\models;
use humhub\modules\comment\Module;
use Yii;
use yii\base\Exception;
use yii\db\ActiveRecord;
@ -19,7 +20,6 @@ use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentAddonActiveRecord;
use humhub\modules\content\interfaces\ContentOwner;
use humhub\modules\content\widgets\richtext\RichText;
use humhub\modules\post\models\Post;
use humhub\modules\search\libs\SearchHelper;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
@ -201,12 +201,18 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner
*
* @param $model
* @param $id
* @param int $limit
* @param int|null $limit when null the default limit will used
*
* @return Comment[] the comments
*/
public static function GetCommentsLimited($model, $id, $limit = 2)
public static function GetCommentsLimited($model, $id, $limit = null)
{
if ($limit === null) {
/** @var Module $module */
$module = Yii::$app->getModule('comment');
$limit = $module->commentsPreviewMax;
}
$cacheID = sprintf("commentsLimited_%s_%s", $model, $id);
$comments = Yii::$app->cache->get($cacheID);

View File

@ -64,17 +64,8 @@ class CommentLink extends Widget
'mode' => $this->mode,
'objectModel' => get_class($this->object),
'objectId' => $this->object->getPrimaryKey(),
'commentCount' => CommentModel::GetCommentCount(get_class($this->object), $this->object->getPrimaryKey())
]);
}
/**
* Returns count of existing comments
*
* @return Int the total amount of comments
*/
public function getCommentsCount()
{
return CommentModel::GetCommentCount(get_class($this->object), $this->object->getPrimaryKey());
}
}

View File

@ -3,8 +3,10 @@
namespace humhub\modules\comment\widgets;
use humhub\modules\comment\models\Comment as CommentModel;
use humhub\modules\comment\Module;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\components\Widget;
use Yii;
/**
* This widget is used include the comments functionality to a wall entry.
@ -28,14 +30,20 @@ class Comments extends Widget
*/
public function run()
{
/** @var Module $module */
$module = Yii::$app->getModule('comment');
$objectModel = get_class($this->object);
$objectId = $this->object->getPrimaryKey();
// Count all Comments
$commentCount = CommentModel::GetCommentCount($objectModel, $objectId);
$comments = CommentModel::GetCommentsLimited($objectModel, $objectId, 2);
$comments = [];
if ($commentCount !== 0) {
$comments = CommentModel::GetCommentsLimited($objectModel, $objectId, $module->commentsPreviewMax);
}
$isLimited = ($commentCount > 2);
$isLimited = ($commentCount > $module->commentsPreviewMax);
return $this->render('comments', [
'object' => $this->object,

View File

@ -9,9 +9,9 @@ use yii\helpers\Url;
/* @var $objectModel string */
/* @var $objectId integer */
/* @var $id string unique object id */
/* @var $commentCount integer */
/* @var $mode string */
$commentCount = $this->context->getCommentsCount();
$hasComments = ($commentCount > 0);
$commentCountSpan = Html::tag('span', ' (' . $commentCount . ')', [
'class' => 'comment-count',
@ -23,10 +23,14 @@ $commentCountSpan = Html::tag('span', ' (' . $commentCount . ')', [
<?php if ($mode == CommentLink::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() . ')' ?>
<?= Yii::t('CommentModule.base', "Comment") . ' (' . $commentCount . ')' ?>
</a>
<?php elseif (Yii::$app->user->isGuest): ?>
<?= Html::a(Yii::t('CommentModule.base', "Comment") . $commentCountSpan, Yii::$app->user->loginUrl, ['data-target' => '#globalModal']) ?>
<?= Html::a(
Yii::t('CommentModule.base', "Comment") . $commentCountSpan,
Yii::$app->user->loginUrl,
['data-target' => '#globalModal']) ?>
<?php else : ?>
<?= Button::asLink(Yii::t('CommentModule.base', "Comment") . $commentCountSpan)->action('comment.toggleComment', null, '#comment_' . $id) ?>
<?= Button::asLink(Yii::t('CommentModule.base', "Comment") . $commentCountSpan)
->action('comment.toggleComment', null, '#comment_' . $id) ?>
<?php endif; ?>