| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | <?php namespace BookStack\Actions; | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | use BookStack\Entities\Entity; | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |  * Class CommentRepo | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  |  * @package BookStack\Repos | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  | class CommentRepo | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |      * @var \BookStack\Actions\Comment $comment | 
					
						
							| 
									
										
										
										
											2017-01-14 00:15:48 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected $comment; | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * CommentRepo constructor. | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |      * @param \BookStack\Actions\Comment $comment | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |     public function __construct(Comment $comment) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->comment = $comment; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get a comment by ID. | 
					
						
							|  |  |  |      * @param $id | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |      * @return \BookStack\Actions\Comment|\Illuminate\Database\Eloquent\Model | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getById($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->comment->newQuery()->findOrFail($id); | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Create a new comment on an entity. | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |      * @param \BookStack\Entities\Entity $entity | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |      * @param array $data | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |      * @return \BookStack\Actions\Comment | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |     public function create(Entity $entity, $data = []) | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |         $userId = user()->id; | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |         $comment = $this->comment->newInstance($data); | 
					
						
							|  |  |  |         $comment->created_by = $userId; | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |         $comment->updated_by = $userId; | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |         $comment->local_id = $this->getNextLocalId($entity); | 
					
						
							|  |  |  |         $entity->comments()->save($comment); | 
					
						
							| 
									
										
										
										
											2017-06-04 21:22:44 +08:00
										 |  |  |         return $comment; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update an existing comment. | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |      * @param \BookStack\Actions\Comment $comment | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |      * @param array $input | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function update($comment, $input) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $comment->updated_by = user()->id; | 
					
						
							|  |  |  |         $comment->update($input); | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |         return $comment; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-16 03:10:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Delete a comment from the system. | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |      * @param \BookStack\Actions\Comment $comment | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function delete($comment) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $comment->delete(); | 
					
						
							| 
									
										
										
										
											2017-04-27 05:05:29 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-05-16 03:10:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the next local ID relative to the linked entity. | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  |      * @param \BookStack\Entities\Entity $entity | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getNextLocalId(Entity $entity) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-09-09 22:56:24 +08:00
										 |  |  |         $comments = $entity->comments(false)->orderBy('local_id', 'desc')->first(); | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |         if ($comments === null) { | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-03 23:37:51 +08:00
										 |  |  |         return $comments->local_id + 1; | 
					
						
							| 
									
										
										
										
											2017-04-19 03:51:45 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  | } |