]> BookStack Code Mirror - bookstack/blob - app/Comment.php
Merge branch 'master' of https://github.com/Abijeet/BookStack
[bookstack] / app / Comment.php
1 <?php
2
3 namespace BookStack;
4 use Illuminate\Support\Facades\DB;
5
6 class Comment extends Ownable
7 {
8     protected $fillable = ['text', 'html'];
9     
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 getParentCommentsByPage($pageId, $pageNum = 0, $limit = 0) {        
38         $data = ['pageId' => $pageId];        
39         $query = static::newQuery();
40         $query->join('users AS u', 'comments.created_by', '=', 'u.id');
41         $query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id');
42         $query->leftJoin('images AS i', 'i.id', '=', 'u.image_id');
43         $query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, comments.created_at, comments.updated_at, '
44                 . 'u.name AS created_by_name, u1.name AS updated_by_name, '
45                 . '(SELECT count(c.id) FROM bookstack.comments c WHERE c.parent_id = comments.id AND page_id = ?) AS cnt_sub_comments, i.url AS avatar ', 
46                 [$pageId]);
47         $query->whereRaw('page_id = ? AND parent_id IS NULL', [$pageId]);
48         $query->orderBy('created_at');
49         return $query;
50     }
51 }