1 <?php namespace BookStack\Repos;
8 * @package BookStack\Repos
13 * @var Comment $comment
17 public function __construct(Comment $comment)
19 $this->comment = $comment;
22 public function create (Page $page, $data = []) {
24 $comment = $this->comment->newInstance();
25 $comment->fill($data);
27 $comment->page_id = $page->id;
28 $comment->created_by = $userId;
29 $comment->updated_at = null;
34 public function update($comment, $input, $activeOnly = true) {
36 $comment->updated_by = $userId;
37 $comment->fill($input);
39 // only update active comments by default.
40 $whereClause = ['active' => 1];
44 $comment->update($whereClause);
48 public function delete($comment) {
49 $comment->text = trans('entities.comment_deleted');
50 $comment->html = trans('entities.comment_deleted');
51 $comment->active = false;
53 $comment->updated_by = $userId;
58 public function getPageComments($pageId) {
59 $comments = $this->comment->getAllPageComments($pageId);
61 $totalComments = count($comments);
62 $finalCommentList = [];
64 // normalizing the response.
65 for ($i = 0; $i < count($comments); ++$i) {
66 $comment = $this->normalizeComment($comments[$i]);
67 $parentId = $comment->parent_id;
68 if (empty($parentId)) {
69 $finalCommentList[] = $comment;
70 $index[$comment->id] = $comment;
74 if (empty($index[$parentId])) {
75 // weird condition should not happen.
78 if (empty($index[$parentId]->sub_comments)) {
79 $index[$parentId]->sub_comments = [];
81 array_push($index[$parentId]->sub_comments, $comment);
82 $index[$comment->id] = $comment;
85 'comments' => $finalCommentList,
86 'total' => $totalComments
90 public function getCommentById($commentId) {
91 return $this->normalizeComment($this->comment->getCommentById($commentId));
94 private function normalizeComment($comment) {
95 if (empty($comment)) {
98 $comment->createdBy->avatar_url = $comment->createdBy->getAvatar(50);
99 $comment->createdBy->profile_url = $comment->createdBy->getProfileUrl();
100 if (!empty($comment->updatedBy)) {
101 $comment->updatedBy->profile_url = $comment->updatedBy->getProfileUrl();