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-07 17:41:45 -05:00
|
|
|
protected static array $contentAttributes = [
|
|
|
|
'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
|
2024-02-08 12:18:03 -05:00
|
|
|
'template', 'html', 'text', 'created_at', 'updated_at', 'priority',
|
|
|
|
'created_by', 'updated_by', 'owned_by',
|
2024-02-07 17:41:45 -05:00
|
|
|
];
|
|
|
|
protected static array $listAttributes = [
|
|
|
|
'name', 'id', 'slug', 'book_id', 'chapter_id', 'draft',
|
2024-02-08 12:18:03 -05:00
|
|
|
'template', 'text', 'created_at', 'updated_at', 'priority', 'owned_by',
|
2024-02-07 17:41:45 -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)) {
|
2024-02-08 11:39:59 -05:00
|
|
|
throw new NotFoundException(trans('errors.page_not_found'));
|
2024-02-05 12:35:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07 17:41:45 -05:00
|
|
|
->select($this->mergeBookSlugForSelect(static::$listAttributes));
|
2024-02-04 09:39:01 -05:00
|
|
|
}
|
|
|
|
|
2024-02-11 10:42:37 -05:00
|
|
|
public function visibleForChapterList(int $chapterId): Builder
|
|
|
|
{
|
|
|
|
return $this->visibleForList()
|
|
|
|
->where('chapter_id', '=', $chapterId)
|
|
|
|
->orderBy('draft', 'desc')
|
|
|
|
->orderBy('priority', 'asc');
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:37:36 -05:00
|
|
|
public function visibleWithContents(): Builder
|
|
|
|
{
|
|
|
|
return $this->start()
|
|
|
|
->scopes('visible')
|
2024-02-07 17:41:45 -05:00
|
|
|
->select($this->mergeBookSlugForSelect(static::$contentAttributes));
|
2024-02-07 11:37:36 -05:00
|
|
|
}
|
|
|
|
|
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-07 17:41:45 -05:00
|
|
|
|
|
|
|
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');
|
|
|
|
}]);
|
|
|
|
}
|
2024-02-04 09:39:01 -05:00
|
|
|
}
|