| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\References; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Auth\Permissions\PermissionApplicator; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Page; | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Builder; | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Collection; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Relations\Relation; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ReferenceFetcher | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected PermissionApplicator $permissions; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(PermissionApplicator $permissions) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->permissions = $permissions; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Query and return the page references pointing to the given entity. | 
					
						
							|  |  |  |      * Loads the commonly required relations while taking permissions into account. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getPageReferencesToEntity(Entity $entity): Collection | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  |         $baseQuery = $this->queryPageReferencesToEntity($entity) | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  |             ->with([ | 
					
						
							| 
									
										
										
										
											2022-08-30 00:46:41 +08:00
										 |  |  |                 'from'         => fn (Relation $query) => $query->select(Page::$listAttributes), | 
					
						
							|  |  |  |                 'from.book'    => fn (Relation $query) => $query->scopes('visible'), | 
					
						
							|  |  |  |                 'from.chapter' => fn (Relation $query) => $query->scopes('visible'), | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  |             ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $references = $this->permissions->restrictEntityRelationQuery( | 
					
						
							|  |  |  |             $baseQuery, | 
					
						
							|  |  |  |             'references', | 
					
						
							|  |  |  |             'from_id', | 
					
						
							|  |  |  |             'from_type' | 
					
						
							|  |  |  |         )->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $references; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Returns the count of page references pointing to the given entity. | 
					
						
							|  |  |  |      * Takes permissions into account. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getPageReferenceCountToEntity(Entity $entity): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $count = $this->permissions->restrictEntityRelationQuery( | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  |             $this->queryPageReferencesToEntity($entity), | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  |             'references', | 
					
						
							|  |  |  |             'from_id', | 
					
						
							|  |  |  |             'from_type' | 
					
						
							|  |  |  |         )->count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $count; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     protected function queryPageReferencesToEntity(Entity $entity): Builder | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Reference::query() | 
					
						
							|  |  |  |             ->where('to_type', '=', $entity->getMorphClass()) | 
					
						
							|  |  |  |             ->where('to_id', '=', $entity->id) | 
					
						
							|  |  |  |             ->where('from_type', '=', (new Page())->getMorphClass()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-30 00:46:41 +08:00
										 |  |  | } |