]> BookStack Code Mirror - bookstack/blob - app/Repos/CommentRepo.php
Added Italian language
[bookstack] / app / Repos / CommentRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Comment;
4 use BookStack\Page;
5
6 /**
7  * Class TagRepo
8  * @package BookStack\Repos
9  */
10 class CommentRepo {
11     /**
12      *
13      * @var Comment $comment
14      */
15     protected $comment;
16
17     public function __construct(Comment $comment)
18     {
19         $this->comment = $comment;
20     }
21
22     public function create (Page $page, $data = []) {
23         $userId = user()->id;
24         $comment = $this->comment->newInstance();
25         $comment->fill($data);
26         // new comment
27         $comment->page_id = $page->id;
28         $comment->created_by = $userId;
29         $comment->updated_at = null;
30         $comment->save();
31         return $comment;
32     }
33
34     public function update($comment, $input, $activeOnly = true) {
35         $userId = user()->id;
36         $comment->updated_by = $userId;
37         $comment->fill($input);
38
39         // only update active comments by default.
40         $whereClause = ['active' => 1];
41         if (!$activeOnly) {
42             $whereClause = [];
43         }
44         $comment->update($whereClause);
45         return $comment;
46     }
47
48     public function delete($comment) {
49         $comment->text = trans('entities.comment_deleted');
50         $comment->html = trans('entities.comment_deleted');
51         $comment->active = false;
52         $userId = user()->id;
53         $comment->updated_by = $userId;
54         $comment->save();
55         return $comment;
56     }
57
58     public function getPageComments($pageId) {
59         $comments = $this->comment->getAllPageComments($pageId);
60         $index = [];
61         $totalComments = count($comments);
62         $finalCommentList = [];
63
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;
71                 continue;
72             }
73
74             if (empty($index[$parentId])) {
75                 // weird condition should not happen.
76                 continue;
77             }
78             if (empty($index[$parentId]->sub_comments)) {
79                 $index[$parentId]->sub_comments = [];
80             }
81             array_push($index[$parentId]->sub_comments, $comment);
82             $index[$comment->id] = $comment;
83         }
84         return [
85             'comments' => $finalCommentList,
86             'total' => $totalComments
87         ];
88     }
89
90     public function getCommentById($commentId) {
91         return $this->normalizeComment($this->comment->getCommentById($commentId));
92     }
93
94     private function normalizeComment($comment) {
95         if (empty($comment)) {
96             return;
97         }
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();
102         }
103         return $comment;
104     }
105 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.