| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Comment extends Ownable | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-05-30 11:32:47 +08:00
										 |  |  |     public $sub_comments = []; | 
					
						
							| 
									
										
										
										
											2017-05-16 03:10:14 +08:00
										 |  |  |     protected $fillable = ['text', 'html', 'parent_id']; | 
					
						
							| 
									
										
										
										
											2017-05-30 11:32:47 +08:00
										 |  |  |     protected $appends = ['created', 'updated', 'sub_comments']; | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the entity that this comment belongs to | 
					
						
							|  |  |  |      * @return \Illuminate\Database\Eloquent\Relations\MorphTo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function entity() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->morphTo('entity'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-16 03:10:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the page that this comment is in. | 
					
						
							|  |  |  |      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function page() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->belongsTo(Page::class); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-16 03:10:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the owner of this comment. | 
					
						
							|  |  |  |      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-05-16 03:10:14 +08:00
										 |  |  |     public function user() | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->belongsTo(User::class); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-16 03:10:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 04:16:59 +08:00
										 |  |  |     /* | 
					
						
							|  |  |  |      * Not being used, but left here because might be used in the future for performance reasons. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-05-30 11:32:47 +08:00
										 |  |  |     public function getPageComments($pageId) { | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |         $query = static::newQuery(); | 
					
						
							|  |  |  |         $query->join('users AS u', 'comments.created_by', '=', 'u.id'); | 
					
						
							|  |  |  |         $query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id'); | 
					
						
							|  |  |  |         $query->leftJoin('images AS i', 'i.id', '=', 'u.image_id'); | 
					
						
							| 
									
										
										
										
											2017-05-30 11:32:47 +08:00
										 |  |  |         $query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, ' | 
					
						
							|  |  |  |                 . 'comments.created_at, comments.updated_at, comments.parent_id, ' | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |                 . 'u.name AS created_by_name, u1.name AS updated_by_name, ' | 
					
						
							| 
									
										
										
										
											2017-05-30 11:32:47 +08:00
										 |  |  |                 . 'i.url AS avatar '); | 
					
						
							|  |  |  |         $query->whereRaw('page_id = ?', [$pageId]); | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |         $query->orderBy('created_at'); | 
					
						
							| 
									
										
										
										
											2017-05-30 11:32:47 +08:00
										 |  |  |         return $query->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getAllPageComments($pageId) { | 
					
						
							|  |  |  |         return self::where('page_id', '=', $pageId)->with(['createdBy' => function($query) { | 
					
						
							|  |  |  |             $query->select('id', 'name', 'image_id'); | 
					
						
							|  |  |  |         }, 'updatedBy' => function($query) { | 
					
						
							|  |  |  |             $query->select('id', 'name'); | 
					
						
							|  |  |  |         }, 'createdBy.avatar' => function ($query) { | 
					
						
							|  |  |  |             $query->select('id', 'path', 'url'); | 
					
						
							|  |  |  |         }])->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getCommentById($commentId) { | 
					
						
							|  |  |  |         return self::where('id', '=', $commentId)->with(['createdBy' => function($query) { | 
					
						
							|  |  |  |             $query->select('id', 'name', 'image_id'); | 
					
						
							|  |  |  |         }, 'updatedBy' => function($query) { | 
					
						
							|  |  |  |             $query->select('id', 'name'); | 
					
						
							|  |  |  |         }, 'createdBy.avatar' => function ($query) { | 
					
						
							|  |  |  |             $query->select('id', 'path', 'url'); | 
					
						
							|  |  |  |         }])->first(); | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-25 10:33:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function getCreatedAttribute() { | 
					
						
							|  |  |  |         $created = [ | 
					
						
							|  |  |  |             'day_time_str' => $this->created_at->toDayDateTimeString(), | 
					
						
							|  |  |  |             'diff' => $this->created_at->diffForHumans() | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         return $created; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getUpdatedAttribute() { | 
					
						
							|  |  |  |         if (empty($this->updated_at)) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $updated = [ | 
					
						
							|  |  |  |             'day_time_str' => $this->updated_at->toDayDateTimeString(), | 
					
						
							|  |  |  |             'diff' => $this->updated_at->diffForHumans() | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         return $updated; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-30 11:32:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function getSubCommentsAttribute() { | 
					
						
							|  |  |  |         return $this->sub_comments; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  | } |