| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\References; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  | use BookStack\Entities\Tools\MixedEntityListLoader; | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Permissions\PermissionApplicator; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ReferenceFetcher | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected PermissionApplicator $permissions, | 
					
						
							|  |  |  |         protected MixedEntityListLoader $mixedEntityListLoader, | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |      * Query and return the references pointing to the given entity. | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  |      * Loads the commonly required relations while taking permissions into account. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |     public function getReferencesToEntity(Entity $entity): Collection | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |         $references = $this->queryReferencesToEntity($entity)->get(); | 
					
						
							| 
									
										
										
										
											2024-02-04 22:39:01 +08:00
										 |  |  |         $this->mixedEntityListLoader->loadIntoRelations($references->all(), 'from', true); | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $references; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |      * Returns the count of references pointing to the given entity. | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  |      * Takes permissions into account. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |     public function getReferenceCountToEntity(Entity $entity): int | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |         return $this->queryReferencesToEntity($entity)->count(); | 
					
						
							| 
									
										
										
										
											2022-08-20 19:07:38 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |     protected function queryReferencesToEntity(Entity $entity): Builder | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |         $baseQuery = Reference::query() | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  |             ->where('to_type', '=', $entity->getMorphClass()) | 
					
						
							| 
									
										
										
										
											2023-12-19 00:23:40 +08:00
										 |  |  |             ->where('to_id', '=', $entity->id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->permissions->restrictEntityRelationQuery( | 
					
						
							|  |  |  |             $baseQuery, | 
					
						
							|  |  |  |             'references', | 
					
						
							|  |  |  |             'from_id', | 
					
						
							|  |  |  |             'from_type' | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-30 00:46:41 +08:00
										 |  |  | } |