]> BookStack Code Mirror - bookstack/blob - app/Comment.php
Added Italian language
[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     /*
38      * Not being used, but left here because might be used in the future for performance reasons.
39      */
40     public function getPageComments($pageId) {
41         $query = static::newQuery();
42         $query->join('users AS u', 'comments.created_by', '=', 'u.id');
43         $query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id');
44         $query->leftJoin('images AS i', 'i.id', '=', 'u.image_id');
45         $query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, '
46                 . 'comments.created_at, comments.updated_at, comments.parent_id, '
47                 . 'u.name AS created_by_name, u1.name AS updated_by_name, '
48                 . 'i.url AS avatar ');
49         $query->whereRaw('page_id = ?', [$pageId]);
50         $query->orderBy('created_at');
51         return $query->get();
52     }
53
54     public function getAllPageComments($pageId) {
55         return self::where('page_id', '=', $pageId)->with(['createdBy' => function($query) {
56             $query->select('id', 'name', 'image_id');
57         }, 'updatedBy' => function($query) {
58             $query->select('id', 'name');
59         }, 'createdBy.avatar' => function ($query) {
60             $query->select('id', 'path', 'url');
61         }])->get();
62     }
63
64     public function getCommentById($commentId) {
65         return self::where('id', '=', $commentId)->with(['createdBy' => function($query) {
66             $query->select('id', 'name', 'image_id');
67         }, 'updatedBy' => function($query) {
68             $query->select('id', 'name');
69         }, 'createdBy.avatar' => function ($query) {
70             $query->select('id', 'path', 'url');
71         }])->first();
72     }
73
74     public function getCreatedAttribute() {
75         $created = [
76             'day_time_str' => $this->created_at->toDayDateTimeString(),
77             'diff' => $this->created_at->diffForHumans()
78         ];
79         return $created;
80     }
81
82     public function getUpdatedAttribute() {
83         if (empty($this->updated_at)) {
84             return null;
85         }
86         $updated = [
87             'day_time_str' => $this->updated_at->toDayDateTimeString(),
88             'diff' => $this->updated_at->diffForHumans()
89         ];
90         return $updated;
91     }
92
93     public function getSubCommentsAttribute() {
94         return $this->sub_comments;
95     }
96 }