mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
222c665018
Started new class for PageRevisions too as part of these changes
72 lines
1.9 KiB
PHP
72 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Entities\Queries;
|
|
|
|
use BookStack\Entities\Models\Page;
|
|
use BookStack\Exceptions\NotFoundException;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class PageQueries implements ProvidesEntityQueries
|
|
{
|
|
public function start(): Builder
|
|
{
|
|
return Page::query();
|
|
}
|
|
|
|
public function findVisibleById(int $id): ?Page
|
|
{
|
|
return $this->start()->scopes('visible')->find($id);
|
|
}
|
|
|
|
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')
|
|
->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;
|
|
}
|
|
|
|
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');
|
|
}]));
|
|
}
|
|
|
|
public function currentUserDraftsForList(): Builder
|
|
{
|
|
return $this->visibleForList()
|
|
->where('draft', '=', true)
|
|
->where('created_by', '=', user()->id);
|
|
}
|
|
|
|
public function visibleTemplates(): Builder
|
|
{
|
|
return $this->visibleForList()
|
|
->where('template', '=', true);
|
|
}
|
|
}
|