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