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