2017-01-14 00:15:48 +08:00
|
|
|
<?php namespace BookStack\Repos;
|
|
|
|
|
|
|
|
use BookStack\Comment;
|
2017-04-19 03:51:45 +08:00
|
|
|
use BookStack\Page;
|
2017-01-14 00:15:48 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TagRepo
|
|
|
|
* @package BookStack\Repos
|
|
|
|
*/
|
|
|
|
class CommentRepo {
|
|
|
|
/**
|
|
|
|
*
|
2017-05-16 03:10:14 +08:00
|
|
|
* @var Comment $comment
|
2017-01-14 00:15:48 +08:00
|
|
|
*/
|
|
|
|
protected $comment;
|
2017-04-19 03:51:45 +08:00
|
|
|
|
|
|
|
public function __construct(Comment $comment)
|
|
|
|
{
|
|
|
|
$this->comment = $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create (Page $page, $data = []) {
|
|
|
|
$userId = user()->id;
|
|
|
|
$comment = $this->comment->newInstance();
|
|
|
|
$comment->fill($data);
|
|
|
|
// new comment
|
|
|
|
$comment->page_id = $page->id;
|
2017-05-16 03:10:14 +08:00
|
|
|
$comment->created_by = $userId;
|
2017-05-25 10:32:49 +08:00
|
|
|
$comment->updated_at = null;
|
2017-04-19 03:51:45 +08:00
|
|
|
$comment->save();
|
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:22:44 +08:00
|
|
|
public function update($comment, $input, $activeOnly = true) {
|
2017-04-19 03:51:45 +08:00
|
|
|
$userId = user()->id;
|
|
|
|
$comment->updated_by = $userId;
|
|
|
|
$comment->fill($input);
|
2017-06-04 21:22:44 +08:00
|
|
|
|
|
|
|
// only update active comments by default.
|
|
|
|
$whereClause = ['active' => 1];
|
|
|
|
if (!$activeOnly) {
|
|
|
|
$whereClause = [];
|
|
|
|
}
|
|
|
|
$comment->update($whereClause);
|
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($comment) {
|
|
|
|
$comment->text = trans('errors.cannot_add_comment_to_draft');
|
|
|
|
$comment->html = trans('errors.cannot_add_comment_to_draft');
|
|
|
|
$comment->active = false;
|
|
|
|
$userId = user()->id;
|
|
|
|
$comment->updated_by = $userId;
|
2017-04-19 03:51:45 +08:00
|
|
|
$comment->save();
|
|
|
|
return $comment;
|
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-05-30 11:32:47 +08:00
|
|
|
public function getPageComments($pageId) {
|
|
|
|
$comments = $this->comment->getAllPageComments($pageId);
|
|
|
|
$index = [];
|
|
|
|
$totalComments = count($comments);
|
2017-06-04 13:47:14 +08:00
|
|
|
$finalCommentList = [];
|
|
|
|
|
2017-05-30 11:32:47 +08:00
|
|
|
// normalizing the response.
|
2017-06-04 13:47:14 +08:00
|
|
|
for ($i = 0; $i < count($comments); ++$i) {
|
|
|
|
$comment = $this->normalizeComment($comments[$i]);
|
2017-05-30 11:32:47 +08:00
|
|
|
$parentId = $comment->parent_id;
|
|
|
|
if (empty($parentId)) {
|
2017-06-04 13:47:14 +08:00
|
|
|
$finalCommentList[] = $comment;
|
2017-05-30 11:32:47 +08:00
|
|
|
$index[$comment->id] = $comment;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($index[$parentId])) {
|
|
|
|
// weird condition should not happen.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (empty($index[$parentId]->sub_comments)) {
|
|
|
|
$index[$parentId]->sub_comments = [];
|
|
|
|
}
|
|
|
|
array_push($index[$parentId]->sub_comments, $comment);
|
|
|
|
$index[$comment->id] = $comment;
|
|
|
|
}
|
|
|
|
return [
|
2017-06-04 13:47:14 +08:00
|
|
|
'comments' => $finalCommentList,
|
2017-05-30 11:32:47 +08:00
|
|
|
'total' => $totalComments
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommentById($commentId) {
|
|
|
|
return $this->normalizeComment($this->comment->getCommentById($commentId));
|
2017-04-27 05:05:29 +08:00
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-05-30 11:32:47 +08:00
|
|
|
private function normalizeComment($comment) {
|
|
|
|
if (empty($comment)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$comment->createdBy->avatar_url = $comment->createdBy->getAvatar(50);
|
|
|
|
$comment->createdBy->profile_url = $comment->createdBy->getProfileUrl();
|
|
|
|
if (!empty($comment->updatedBy)) {
|
|
|
|
$comment->updatedBy->profile_url = $comment->updatedBy->getProfileUrl();
|
|
|
|
}
|
|
|
|
return $comment;
|
2017-04-19 03:51:45 +08:00
|
|
|
}
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|