2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Tools;
|
2020-11-21 20:20:38 -05:00
|
|
|
|
|
|
|
use BookStack\Entities\EntityProvider;
|
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
2021-11-20 09:03:56 -05:00
|
|
|
use BookStack\Entities\Models\Chapter;
|
2021-11-05 20:32:01 -04:00
|
|
|
use BookStack\Entities\Models\Page;
|
2024-02-07 11:37:36 -05:00
|
|
|
use BookStack\Entities\Queries\EntityQueries;
|
2020-11-21 20:20:38 -05:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
class SiblingFetcher
|
|
|
|
{
|
2024-02-07 11:37:36 -05:00
|
|
|
public function __construct(
|
|
|
|
protected EntityQueries $queries,
|
2024-02-08 12:18:03 -05:00
|
|
|
protected ShelfContext $shelfContext,
|
2024-02-07 11:37:36 -05:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2020-11-21 20:20:38 -05:00
|
|
|
/**
|
|
|
|
* Search among the siblings of the entity of given type and id.
|
|
|
|
*/
|
|
|
|
public function fetch(string $entityType, int $entityId): Collection
|
|
|
|
{
|
2021-06-26 11:23:15 -04:00
|
|
|
$entity = (new EntityProvider())->get($entityType)->visible()->findOrFail($entityId);
|
2020-11-21 20:20:38 -05:00
|
|
|
$entities = [];
|
|
|
|
|
|
|
|
// Page in chapter
|
2021-11-05 20:32:01 -04:00
|
|
|
if ($entity instanceof Page && $entity->chapter) {
|
2020-11-21 20:20:38 -05:00
|
|
|
$entities = $entity->chapter->getVisiblePages();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Page in book or chapter
|
2021-11-20 09:03:56 -05:00
|
|
|
if (($entity instanceof Page && !$entity->chapter) || $entity instanceof Chapter) {
|
2024-02-07 11:37:36 -05:00
|
|
|
$entities = $entity->book->getDirectVisibleChildren();
|
2020-11-21 20:20:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Book
|
|
|
|
// Gets just the books in a shelf if shelf is in context
|
2021-11-05 20:32:01 -04:00
|
|
|
if ($entity instanceof Book) {
|
2024-02-08 12:18:03 -05:00
|
|
|
$contextShelf = $this->shelfContext->getContextualShelfForBook($entity);
|
2020-11-21 20:20:38 -05:00
|
|
|
if ($contextShelf) {
|
|
|
|
$entities = $contextShelf->visibleBooks()->get();
|
|
|
|
} else {
|
2024-03-09 10:24:44 -05:00
|
|
|
$entities = $this->queries->books->visibleForList()->orderBy('name', 'asc')->get();
|
2020-11-21 20:20:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:32:01 -04:00
|
|
|
// Shelf
|
|
|
|
if ($entity instanceof Bookshelf) {
|
2024-03-09 10:24:44 -05:00
|
|
|
$entities = $this->queries->shelves->visibleForList()->orderBy('name', 'asc')->get();
|
2020-11-21 20:20:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
}
|