3 namespace BookStack\Entities\Tools;
5 use BookStack\Entities\EntityProvider;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Queries\EntityQueries;
11 use Illuminate\Support\Collection;
15 public function __construct(
16 protected EntityQueries $queries,
21 * Search among the siblings of the entity of given type and id.
23 public function fetch(string $entityType, int $entityId): Collection
25 $entity = (new EntityProvider())->get($entityType)->visible()->findOrFail($entityId);
29 if ($entity instanceof Page && $entity->chapter) {
30 $entities = $entity->chapter->getVisiblePages();
33 // Page in book or chapter
34 if (($entity instanceof Page && !$entity->chapter) || $entity instanceof Chapter) {
35 $entities = $entity->book->getDirectVisibleChildren();
39 // Gets just the books in a shelf if shelf is in context
40 if ($entity instanceof Book) {
41 $contextShelf = (new ShelfContext())->getContextualShelfForBook($entity);
43 $entities = $contextShelf->visibleBooks()->get();
45 $entities = $this->queries->books->visibleForList()->get();
50 if ($entity instanceof Bookshelf) {
51 $entities = $this->queries->shelves->visibleForList()->get();