]> BookStack Code Mirror - bookstack/blobdiff - app/References/ReferenceFetcher.php
ZIP Imports: Added API examples, finished testing
[bookstack] / app / References / ReferenceFetcher.php
index fef2744d7dd7d01011e515eaa5381c45d7266916..1c9664f45a9979d8c5e88627b4bb6eaa7547dd1a 100644 (file)
@@ -2,61 +2,53 @@
 
 namespace BookStack\References;
 
-use BookStack\Auth\Permissions\PermissionApplicator;
 use BookStack\Entities\Models\Entity;
-use BookStack\Entities\Models\Page;
+use BookStack\Entities\Tools\MixedEntityListLoader;
+use BookStack\Permissions\PermissionApplicator;
+use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Collection;
-use Illuminate\Database\Eloquent\Relations\Relation;
 
 class ReferenceFetcher
 {
-    protected PermissionApplicator $permissions;
-
-    public function __construct(PermissionApplicator $permissions)
-    {
-        $this->permissions = $permissions;
+    public function __construct(
+        protected PermissionApplicator $permissions,
+        protected MixedEntityListLoader $mixedEntityListLoader,
+    ) {
     }
 
     /**
-     * Query and return the page references pointing to the given entity.
+     * Query and return the references pointing to the given entity.
      * Loads the commonly required relations while taking permissions into account.
      */
-    public function getPageReferencesToEntity(Entity $entity): Collection
+    public function getReferencesToEntity(Entity $entity): Collection
     {
-        $baseQuery = $entity->referencesTo()
-            ->where('from_type', '=', (new Page())->getMorphClass())
-            ->with([
-                'from' => fn(Relation $query) => $query->select(Page::$listAttributes),
-                'from.book' => fn(Relation $query) => $query->scopes('visible'),
-                'from.chapter' => fn(Relation $query) => $query->scopes('visible')
-            ]);
-
-        $references = $this->permissions->restrictEntityRelationQuery(
-            $baseQuery,
-            'references',
-            'from_id',
-            'from_type'
-        )->get();
+        $references = $this->queryReferencesToEntity($entity)->get();
+        $this->mixedEntityListLoader->loadIntoRelations($references->all(), 'from', true);
 
         return $references;
     }
 
     /**
-     * Returns the count of page references pointing to the given entity.
+     * Returns the count of references pointing to the given entity.
      * Takes permissions into account.
      */
-    public function getPageReferenceCountToEntity(Entity $entity): int
+    public function getReferenceCountToEntity(Entity $entity): int
     {
-        $baseQuery = $entity->referencesTo()
-            ->where('from_type', '=', (new Page())->getMorphClass());
+        return $this->queryReferencesToEntity($entity)->count();
+    }
 
-        $count = $this->permissions->restrictEntityRelationQuery(
+    protected function queryReferencesToEntity(Entity $entity): Builder
+    {
+        $baseQuery = Reference::query()
+            ->where('to_type', '=', $entity->getMorphClass())
+            ->where('to_id', '=', $entity->id)
+            ->whereHas('from');
+
+        return $this->permissions->restrictEntityRelationQuery(
             $baseQuery,
             'references',
             'from_id',
             'from_type'
-        )->count();
-
-        return $count;
+        );
     }
-}
\ No newline at end of file
+}