2017-01-14 00:15:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack;
|
2017-04-19 03:51:45 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2017-01-14 00:15:48 +08:00
|
|
|
|
|
|
|
class Comment extends Ownable
|
|
|
|
{
|
2017-05-16 03:10:14 +08:00
|
|
|
protected $fillable = ['text', 'html', 'parent_id'];
|
|
|
|
|
2017-01-14 00:15:48 +08:00
|
|
|
/**
|
|
|
|
* Get the entity that this comment belongs to
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
|
|
|
*/
|
|
|
|
public function entity()
|
|
|
|
{
|
|
|
|
return $this->morphTo('entity');
|
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-01-14 00:15:48 +08:00
|
|
|
/**
|
|
|
|
* Get the page that this comment is in.
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function page()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Page::class);
|
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-01-14 00:15:48 +08:00
|
|
|
/**
|
|
|
|
* Get the owner of this comment.
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2017-05-16 03:10:14 +08:00
|
|
|
public function user()
|
2017-01-14 00:15:48 +08:00
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
|
|
|
public function getCommentsByPage($pageId, $commentId, $pageNum = 0, $limit = 0) {
|
|
|
|
|
2017-04-19 03:51:45 +08:00
|
|
|
$query = static::newQuery();
|
|
|
|
$query->join('users AS u', 'comments.created_by', '=', 'u.id');
|
|
|
|
$query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id');
|
|
|
|
$query->leftJoin('images AS i', 'i.id', '=', 'u.image_id');
|
|
|
|
$query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, comments.created_at, comments.updated_at, '
|
|
|
|
. 'u.name AS created_by_name, u1.name AS updated_by_name, '
|
2017-05-16 03:10:14 +08:00
|
|
|
. '(SELECT count(c.id) FROM bookstack.comments c WHERE c.parent_id = comments.id AND page_id = ?) AS cnt_sub_comments, i.url AS avatar ',
|
2017-04-19 03:51:45 +08:00
|
|
|
[$pageId]);
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-04-27 05:05:29 +08:00
|
|
|
if (empty($commentId)) {
|
|
|
|
$query->whereRaw('page_id = ? AND parent_id IS NULL', [$pageId]);
|
|
|
|
} else {
|
|
|
|
$query->whereRaw('page_id = ? AND parent_id = ?', [$pageId, $commentId]);
|
2017-05-16 03:10:14 +08:00
|
|
|
}
|
2017-04-19 03:51:45 +08:00
|
|
|
$query->orderBy('created_at');
|
|
|
|
return $query;
|
|
|
|
}
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|