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) {
36 $comment->updated_by = $userId;
37 $comment->fill($input);
42 public function getPageComments($pageId) {
43 $comments = $this->comment->getAllPageComments($pageId);
45 $totalComments = count($comments);
46 // normalizing the response.
47 foreach($comments as &$comment) {
48 $comment = $this->normalizeComment($comment);
49 $parentId = $comment->parent_id;
50 if (empty($parentId)) {
51 $index[$comment->id] = $comment;
55 if (empty($index[$parentId])) {
56 // weird condition should not happen.
59 if (empty($index[$parentId]->sub_comments)) {
60 $index[$parentId]->sub_comments = [];
62 array_push($index[$parentId]->sub_comments, $comment);
63 $index[$comment->id] = $comment;
66 'comments' => $comments,
67 'total' => $totalComments
71 public function getCommentById($commentId) {
72 return $this->normalizeComment($this->comment->getCommentById($commentId));
75 private function normalizeComment($comment) {
76 if (empty($comment)) {
79 $comment->createdBy->avatar_url = $comment->createdBy->getAvatar(50);
80 $comment->createdBy->profile_url = $comment->createdBy->getProfileUrl();
81 if (!empty($comment->updatedBy)) {
82 $comment->updatedBy->profile_url = $comment->updatedBy->getProfileUrl();