X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/222c665018cd7fc231d2970307e3a7423e4a377f..refs/pull/5591/head:/app/Entities/Queries/PageQueries.php diff --git a/app/Entities/Queries/PageQueries.php b/app/Entities/Queries/PageQueries.php index 1640dc2db..06298f470 100644 --- a/app/Entities/Queries/PageQueries.php +++ b/app/Entities/Queries/PageQueries.php @@ -8,6 +8,16 @@ use Illuminate\Database\Eloquent\Builder; class PageQueries implements ProvidesEntityQueries { + protected static array $contentAttributes = [ + 'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', + 'template', 'html', 'text', 'created_at', 'updated_at', 'priority', + 'created_by', 'updated_by', 'owned_by', + ]; + protected static array $listAttributes = [ + 'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft', + 'template', 'text', 'created_at', 'updated_at', 'priority', 'owned_by', + ]; + public function start(): Builder { return Page::query(); @@ -33,6 +43,7 @@ class PageQueries implements ProvidesEntityQueries { /** @var ?Page $page */ $page = $this->start()->with('book') + ->scopes('visible') ->whereHas('book', function (Builder $query) use ($bookSlug) { $query->where('slug', '=', $bookSlug); }) @@ -40,20 +51,41 @@ class PageQueries implements ProvidesEntityQueries ->first(); if (is_null($page)) { - throw new NotFoundException(trans('errors.chapter_not_found')); + throw new NotFoundException(trans('errors.page_not_found')); } return $page; } + public function usingSlugs(string $bookSlug, string $pageSlug): Builder + { + return $this->start() + ->where('slug', '=', $pageSlug) + ->whereHas('book', function (Builder $query) use ($bookSlug) { + $query->where('slug', '=', $bookSlug); + }); + } + public function visibleForList(): Builder { return $this->start() - ->select(array_merge(Page::$listAttributes, ['book_slug' => function ($builder) { - $builder->select('slug') - ->from('books') - ->whereColumn('books.id', '=', 'pages.book_id'); - }])); + ->scopes('visible') + ->select($this->mergeBookSlugForSelect(static::$listAttributes)); + } + + public function visibleForChapterList(int $chapterId): Builder + { + return $this->visibleForList() + ->where('chapter_id', '=', $chapterId) + ->orderBy('draft', 'desc') + ->orderBy('priority', 'asc'); + } + + public function visibleWithContents(): Builder + { + return $this->start() + ->scopes('visible') + ->select($this->mergeBookSlugForSelect(static::$contentAttributes)); } public function currentUserDraftsForList(): Builder @@ -68,4 +100,13 @@ class PageQueries implements ProvidesEntityQueries return $this->visibleForList() ->where('template', '=', true); } + + protected function mergeBookSlugForSelect(array $columns): array + { + return array_merge($columns, ['book_slug' => function ($builder) { + $builder->select('slug') + ->from('books') + ->whereColumn('books.id', '=', 'pages.book_id'); + }]); + } }