2024-02-04 09:39:01 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Queries;
|
|
|
|
|
|
|
|
use BookStack\Entities\Models\Page;
|
2024-02-05 12:35:49 -05:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2024-02-04 09:39:01 -05:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
2024-02-05 10:59:20 -05:00
|
|
|
class PageQueries implements ProvidesEntityQueries
|
2024-02-04 09:39:01 -05:00
|
|
|
{
|
2024-02-04 12:35:16 -05:00
|
|
|
public function start(): Builder
|
2024-02-04 09:39:01 -05:00
|
|
|
{
|
|
|
|
return Page::query();
|
|
|
|
}
|
|
|
|
|
2024-02-05 10:59:20 -05:00
|
|
|
public function findVisibleById(int $id): ?Page
|
|
|
|
{
|
|
|
|
return $this->start()->scopes('visible')->find($id);
|
|
|
|
}
|
|
|
|
|
2024-02-05 12:35:49 -05:00
|
|
|
public function findVisibleByIdOrFail(int $id): Page
|
|
|
|
{
|
|
|
|
$page = $this->findVisibleById($id);
|
|
|
|
|
|
|
|
if (is_null($page)) {
|
|
|
|
throw new NotFoundException(trans('errors.page_not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findVisibleBySlugsOrFail(string $bookSlug, string $pageSlug): Page
|
|
|
|
{
|
|
|
|
/** @var ?Page $page */
|
|
|
|
$page = $this->start()->with('book')
|
2024-02-07 11:37:36 -05:00
|
|
|
->scopes('visible')
|
2024-02-05 12:35:49 -05:00
|
|
|
->whereHas('book', function (Builder $query) use ($bookSlug) {
|
|
|
|
$query->where('slug', '=', $bookSlug);
|
|
|
|
})
|
|
|
|
->where('slug', '=', $pageSlug)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if (is_null($page)) {
|
|
|
|
throw new NotFoundException(trans('errors.chapter_not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:37:36 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:35:16 -05:00
|
|
|
public function visibleForList(): Builder
|
2024-02-04 09:39:01 -05:00
|
|
|
{
|
2024-02-04 12:35:16 -05:00
|
|
|
return $this->start()
|
2024-02-07 11:37:36 -05:00
|
|
|
->scopes('visible')
|
2024-02-04 09:39:01 -05:00
|
|
|
->select(array_merge(Page::$listAttributes, ['book_slug' => function ($builder) {
|
|
|
|
$builder->select('slug')
|
|
|
|
->from('books')
|
|
|
|
->whereColumn('books.id', '=', 'pages.book_id');
|
|
|
|
}]));
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:37:36 -05:00
|
|
|
public function visibleWithContents(): Builder
|
|
|
|
{
|
|
|
|
return $this->start()
|
|
|
|
->scopes('visible')
|
|
|
|
->select(array_merge(Page::$contentAttributes, ['book_slug' => function ($builder) {
|
|
|
|
$builder->select('slug')
|
|
|
|
->from('books')
|
|
|
|
->whereColumn('books.id', '=', 'pages.book_id');
|
|
|
|
}]));
|
|
|
|
}
|
|
|
|
|
2024-02-04 12:35:16 -05:00
|
|
|
public function currentUserDraftsForList(): Builder
|
2024-02-04 09:39:01 -05:00
|
|
|
{
|
2024-02-04 12:35:16 -05:00
|
|
|
return $this->visibleForList()
|
2024-02-04 09:39:01 -05:00
|
|
|
->where('draft', '=', true)
|
|
|
|
->where('created_by', '=', user()->id);
|
|
|
|
}
|
2024-02-05 12:35:49 -05:00
|
|
|
|
|
|
|
public function visibleTemplates(): Builder
|
|
|
|
{
|
|
|
|
return $this->visibleForList()
|
|
|
|
->where('template', '=', true);
|
|
|
|
}
|
2024-02-04 09:39:01 -05:00
|
|
|
}
|