mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
a042e22481
Removed some common functions from other entities. Aligned implementation of getUrl() Cleaned phpdocs and added typehinting. Also extracted sibling search logic out of controller.
48 lines
1.4 KiB
PHP
48 lines
1.4 KiB
PHP
<?php namespace BookStack\Entities\Tools;
|
|
|
|
use BookStack\Entities\EntityProvider;
|
|
use BookStack\Entities\Models\Book;
|
|
use BookStack\Entities\Models\Bookshelf;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class SiblingFetcher
|
|
{
|
|
|
|
/**
|
|
* Search among the siblings of the entity of given type and id.
|
|
*/
|
|
public function fetch(string $entityType, int $entityId): Collection
|
|
{
|
|
$entity = (new EntityProvider)->get($entityType)->visible()->findOrFail($entityId);
|
|
$entities = [];
|
|
|
|
// Page in chapter
|
|
if ($entity->isA('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();
|
|
}
|
|
|
|
// Book
|
|
// Gets just the books in a shelf if shelf is in context
|
|
if ($entity->isA('book')) {
|
|
$contextShelf = (new ShelfContext)->getContextualShelfForBook($entity);
|
|
if ($contextShelf) {
|
|
$entities = $contextShelf->visibleBooks()->get();
|
|
} else {
|
|
$entities = Book::visible()->get();
|
|
}
|
|
}
|
|
|
|
// Shelve
|
|
if ($entity->isA('bookshelf')) {
|
|
$entities = Bookshelf::visible()->get();
|
|
}
|
|
|
|
return $entities;
|
|
}
|
|
}
|