- public function getGalleryFiltered($filter, $pageId, $pageNum = 0, $pageSize = 24)
- {
- $images = $this->image->where('type', '=', 'gallery');
-
- $page = $this->page->findOrFail($pageId);
-
- if ($filter === 'page') {
- $images = $images->where('uploaded_to', '=', $page->id);
- } elseif ($filter === 'book') {
- $validPageIds = $page->book->pages->pluck('id')->toArray();
- $images = $images->whereIn('uploaded_to', $validPageIds);
+ public function getEntityFiltered(
+ string $type,
+ string $filterType = null,
+ int $page = 0,
+ int $pageSize = 24,
+ int $uploadedTo = null,
+ string $search = null
+ ): array {
+ /** @var Page $contextPage */
+ $contextPage = Page::visible()->findOrFail($uploadedTo);
+ $parentFilter = null;
+
+ if ($filterType === 'book' || $filterType === 'page') {
+ $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
+ if ($filterType === 'page') {
+ $query->where('uploaded_to', '=', $contextPage->id);
+ } elseif ($filterType === 'book') {
+ $validPageIds = $contextPage->book->pages()
+ ->scopes('visible')
+ ->pluck('id')
+ ->toArray();
+ $query->whereIn('uploaded_to', $validPageIds);
+ }
+ };