2017-01-13 17:15:48 +01:00
|
|
|
<?php namespace BookStack\Repos;
|
|
|
|
|
|
|
|
use BookStack\Comment;
|
2017-04-18 21:51:45 +02:00
|
|
|
use BookStack\Page;
|
2017-01-13 17:15:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TagRepo
|
|
|
|
* @package BookStack\Repos
|
|
|
|
*/
|
|
|
|
class CommentRepo {
|
|
|
|
/**
|
|
|
|
*
|
2017-05-15 21:10:14 +02:00
|
|
|
* @var Comment $comment
|
2017-01-13 17:15:48 +01:00
|
|
|
*/
|
|
|
|
protected $comment;
|
2017-04-18 21:51:45 +02: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-15 21:10:14 +02:00
|
|
|
$comment->created_by = $userId;
|
2017-05-25 04:32:49 +02:00
|
|
|
$comment->updated_at = null;
|
2017-04-18 21:51:45 +02:00
|
|
|
$comment->save();
|
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
2017-06-04 15:22:44 +02:00
|
|
|
public function update($comment, $input, $activeOnly = true) {
|
2017-04-18 21:51:45 +02:00
|
|
|
$userId = user()->id;
|
|
|
|
$comment->updated_by = $userId;
|
|
|
|
$comment->fill($input);
|
2017-06-04 15:22:44 +02:00
|
|
|
|
|
|
|
// only update active comments by default.
|
|
|
|
$whereClause = ['active' => 1];
|
|
|
|
if (!$activeOnly) {
|
|
|
|
$whereClause = [];
|
|
|
|
}
|
|
|
|
$comment->update($whereClause);
|
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($comment) {
|
2017-06-04 16:42:01 +02:00
|
|
|
$comment->text = trans('activities.comment_deleted');
|
|
|
|
$comment->html = trans('activities.comment_deleted');
|
2017-06-04 15:22:44 +02:00
|
|
|
$comment->active = false;
|
|
|
|
$userId = user()->id;
|
|
|
|
$comment->updated_by = $userId;
|
2017-04-18 21:51:45 +02:00
|
|
|
$comment->save();
|
|
|
|
return $comment;
|
|
|
|
}
|
2017-05-15 21:10:14 +02:00
|
|
|
|
2017-05-30 05:32:47 +02:00
|
|
|
public function getPageComments($pageId) {
|
|
|
|
$comments = $this->comment->getAllPageComments($pageId);
|
|
|
|
$index = [];
|
|
|
|
$totalComments = count($comments);
|
2017-06-04 07:47:14 +02:00
|
|
|
$finalCommentList = [];
|
|
|
|
|
2017-05-30 05:32:47 +02:00
|
|
|
// normalizing the response.
|
2017-06-04 07:47:14 +02:00
|
|
|
for ($i = 0; $i < count($comments); ++$i) {
|
|
|
|
$comment = $this->normalizeComment($comments[$i]);
|
2017-05-30 05:32:47 +02:00
|
|
|
$parentId = $comment->parent_id;
|
|
|
|
if (empty($parentId)) {
|
2017-06-04 07:47:14 +02:00
|
|
|
$finalCommentList[] = $comment;
|
2017-05-30 05:32:47 +02: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 07:47:14 +02:00
|
|
|
'comments' => $finalCommentList,
|
2017-05-30 05:32:47 +02:00
|
|
|
'total' => $totalComments
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommentById($commentId) {
|
|
|
|
return $this->normalizeComment($this->comment->getCommentById($commentId));
|
2017-04-26 23:05:29 +02:00
|
|
|
}
|
2017-05-15 21:10:14 +02:00
|
|
|
|
2017-05-30 05:32:47 +02: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-18 21:51:45 +02:00
|
|
|
}
|
2017-01-13 17:15:48 +01:00
|
|
|
}
|