]> BookStack Code Mirror - bookstack/blob - app/Comment.php
#47 - Changes the way we are handling fetching of data for the comment section.
[bookstack] / app / Comment.php
1 <?php
2
3 namespace BookStack;
4
5 class Comment extends Ownable
6 {
7     public $sub_comments = [];
8     protected $fillable = ['text', 'html', 'parent_id'];
9     protected $appends = ['created', 'updated', 'sub_comments'];
10     /**
11      * Get the entity that this comment belongs to
12      * @return \Illuminate\Database\Eloquent\Relations\MorphTo
13      */
14     public function entity()
15     {
16         return $this->morphTo('entity');
17     }
18
19     /**
20      * Get the page that this comment is in.
21      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
22      */
23     public function page()
24     {
25         return $this->belongsTo(Page::class);
26     }
27
28     /**
29      * Get the owner of this comment.
30      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
31      */
32     public function user()
33     {
34         return $this->belongsTo(User::class);
35     }
36
37     public function getPageComments($pageId) {
38         $query = static::newQuery();
39         $query->join('users AS u', 'comments.created_by', '=', 'u.id');
40         $query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id');
41         $query->leftJoin('images AS i', 'i.id', '=', 'u.image_id');
42         $query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, '
43                 . 'comments.created_at, comments.updated_at, comments.parent_id, '
44                 . 'u.name AS created_by_name, u1.name AS updated_by_name, '
45                 . 'i.url AS avatar ');
46         $query->whereRaw('page_id = ?', [$pageId]);
47         $query->orderBy('created_at');
48         return $query->get();
49     }
50
51     public function getAllPageComments($pageId) {
52         return self::where('page_id', '=', $pageId)->with(['createdBy' => function($query) {
53             $query->select('id', 'name', 'image_id');
54         }, 'updatedBy' => function($query) {
55             $query->select('id', 'name');
56         }, 'createdBy.avatar' => function ($query) {
57             $query->select('id', 'path', 'url');
58         }])->get();
59     }
60
61     public function getCommentById($commentId) {
62         return self::where('id', '=', $commentId)->with(['createdBy' => function($query) {
63             $query->select('id', 'name', 'image_id');
64         }, 'updatedBy' => function($query) {
65             $query->select('id', 'name');
66         }, 'createdBy.avatar' => function ($query) {
67             $query->select('id', 'path', 'url');
68         }])->first();
69     }
70
71     public function getCreatedAttribute() {
72         $created = [
73             'day_time_str' => $this->created_at->toDayDateTimeString(),
74             'diff' => $this->created_at->diffForHumans()
75         ];
76         return $created;
77     }
78
79     public function getUpdatedAttribute() {
80         if (empty($this->updated_at)) {
81             return null;
82         }
83         $updated = [
84             'day_time_str' => $this->updated_at->toDayDateTimeString(),
85             'diff' => $this->updated_at->diffForHumans()
86         ];
87         return $updated;
88     }
89
90     public function getSubCommentsAttribute() {
91         return $this->sub_comments;
92     }
93 }