]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/SiblingFetcher.php
Minor capitalisation fix for Estonian
[bookstack] / app / Entities / Tools / SiblingFetcher.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Entities\EntityProvider;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use Illuminate\Support\Collection;
9
10 class SiblingFetcher
11 {
12     /**
13      * Search among the siblings of the entity of given type and id.
14      */
15     public function fetch(string $entityType, int $entityId): Collection
16     {
17         $entity = (new EntityProvider())->get($entityType)->visible()->findOrFail($entityId);
18         $entities = [];
19
20         // Page in chapter
21         if ($entity->isA('page') && $entity->chapter) {
22             $entities = $entity->chapter->getVisiblePages();
23         }
24
25         // Page in book or chapter
26         if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
27             $entities = $entity->book->getDirectChildren();
28         }
29
30         // Book
31         // Gets just the books in a shelf if shelf is in context
32         if ($entity->isA('book')) {
33             $contextShelf = (new ShelfContext())->getContextualShelfForBook($entity);
34             if ($contextShelf) {
35                 $entities = $contextShelf->visibleBooks()->get();
36             } else {
37                 $entities = Book::visible()->get();
38             }
39         }
40
41         // Shelve
42         if ($entity->isA('bookshelf')) {
43             $entities = Bookshelf::visible()->get();
44         }
45
46         return $entities;
47     }
48 }