]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/SiblingFetcher.php
ZIP Imports: Added API examples, finished testing
[bookstack] / app / Entities / Tools / SiblingFetcher.php
index e9dad0e134f4f0629182e064e97f68d8b9193328..156209fd23e78ef641cb7324d477a1e469c54466 100644 (file)
@@ -5,10 +5,19 @@ namespace BookStack\Entities\Tools;
 use BookStack\Entities\EntityProvider;
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Bookshelf;
+use BookStack\Entities\Models\Chapter;
+use BookStack\Entities\Models\Page;
+use BookStack\Entities\Queries\EntityQueries;
 use Illuminate\Support\Collection;
 
 class SiblingFetcher
 {
+    public function __construct(
+        protected EntityQueries $queries,
+        protected ShelfContext $shelfContext,
+    ) {
+    }
+
     /**
      * Search among the siblings of the entity of given type and id.
      */
@@ -18,29 +27,29 @@ class SiblingFetcher
         $entities = [];
 
         // Page in chapter
-        if ($entity->isA('page') && $entity->chapter) {
+        if ($entity instanceof Page && $entity->chapter) {
             $entities = $entity->chapter->getVisiblePages();
         }
 
         // Page in book or chapter
-        if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
-            $entities = $entity->book->getDirectChildren();
+        if (($entity instanceof Page && !$entity->chapter) || $entity instanceof Chapter) {
+            $entities = $entity->book->getDirectVisibleChildren();
         }
 
         // Book
         // Gets just the books in a shelf if shelf is in context
-        if ($entity->isA('book')) {
-            $contextShelf = (new ShelfContext())->getContextualShelfForBook($entity);
+        if ($entity instanceof Book) {
+            $contextShelf = $this->shelfContext->getContextualShelfForBook($entity);
             if ($contextShelf) {
                 $entities = $contextShelf->visibleBooks()->get();
             } else {
-                $entities = Book::visible()->get();
+                $entities = $this->queries->books->visibleForList()->orderBy('name', 'asc')->get();
             }
         }
 
-        // Shelve
-        if ($entity->isA('bookshelf')) {
-            $entities = Bookshelf::visible()->get();
+        // Shelf
+        if ($entity instanceof Bookshelf) {
+            $entities = $this->queries->shelves->visibleForList()->orderBy('name', 'asc')->get();
         }
 
         return $entities;