use BookStack\Activity\Models\View;
use BookStack\Activity\Tools\CommentTree;
-use BookStack\Entities\Models\Page;
+use BookStack\Activity\Tools\UserEntityWatchOptions;
+use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Chapter;
+use BookStack\Entities\Queries\EntityQueries;
+use BookStack\Entities\Queries\PageQueries;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\Cloner;
use BookStack\Entities\Tools\PageEditActivity;
use BookStack\Entities\Tools\PageEditorData;
use BookStack\Exceptions\NotFoundException;
+use BookStack\Exceptions\NotifyException;
use BookStack\Exceptions\PermissionsException;
use BookStack\Http\Controller;
use BookStack\References\ReferenceFetcher;
class PageController extends Controller
{
- protected PageRepo $pageRepo;
- protected ReferenceFetcher $referenceFetcher;
-
- /**
- * PageController constructor.
- */
- public function __construct(PageRepo $pageRepo, ReferenceFetcher $referenceFetcher)
- {
- $this->pageRepo = $pageRepo;
- $this->referenceFetcher = $referenceFetcher;
+ public function __construct(
+ protected PageRepo $pageRepo,
+ protected PageQueries $queries,
+ protected EntityQueries $entityQueries,
+ protected ReferenceFetcher $referenceFetcher
+ ) {
}
/**
*
* @throws Throwable
*/
- public function create(string $bookSlug, string $chapterSlug = null)
+ public function create(string $bookSlug, ?string $chapterSlug = null)
{
- $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
+ if ($chapterSlug) {
+ $parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
+ } else {
+ $parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
+ }
+
$this->checkOwnablePermission('page-create', $parent);
// Redirect to draft edit screen if signed in
*
* @throws ValidationException
*/
- public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
+ public function createAsGuest(Request $request, string $bookSlug, ?string $chapterSlug = null)
{
$this->validate($request, [
'name' => ['required', 'string', 'max:255'],
]);
- $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
+ if ($chapterSlug) {
+ $parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
+ } else {
+ $parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
+ }
+
$this->checkOwnablePermission('page-create', $parent);
$page = $this->pageRepo->getNewDraftPage($parent);
$this->pageRepo->publishDraft($page, [
'name' => $request->get('name'),
- 'html' => '',
]);
return redirect($page->getUrl('/edit'));
*/
public function editDraft(Request $request, string $bookSlug, int $pageId)
{
- $draft = $this->pageRepo->getById($pageId);
+ $draft = $this->queries->findVisibleByIdOrFail($pageId);
$this->checkOwnablePermission('page-create', $draft->getParent());
- $editorData = new PageEditorData($draft, $this->pageRepo, $request->query('editor', ''));
+ $editorData = new PageEditorData($draft, $this->entityQueries, $request->query('editor', ''));
$this->setPageTitle(trans('entities.pages_edit_draft'));
return view('pages.edit', $editorData->getViewData());
$this->validate($request, [
'name' => ['required', 'string', 'max:255'],
]);
- $draftPage = $this->pageRepo->getById($pageId);
+ $draftPage = $this->queries->findVisibleByIdOrFail($pageId);
$this->checkOwnablePermission('page-create', $draftPage->getParent());
$page = $this->pageRepo->publishDraft($draftPage, $request->all());
public function show(string $bookSlug, string $pageSlug)
{
try {
- $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+ $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
} catch (NotFoundException $e) {
- $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
+ $revision = $this->entityQueries->revisions->findLatestVersionBySlugs($bookSlug, $pageSlug);
+ $page = $revision->page ?? null;
- if ($page === null) {
+ if (is_null($page)) {
throw $e;
}
'sidebarTree' => $sidebarTree,
'commentTree' => $commentTree,
'pageNav' => $pageNav,
+ 'watchOptions' => new UserEntityWatchOptions(user(), $page),
'next' => $nextPreviousLocator->getNext(),
'previous' => $nextPreviousLocator->getPrevious(),
- 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($page),
+ 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($page),
]);
}
*/
public function getPageAjax(int $pageId)
{
- $page = $this->pageRepo->getById($pageId);
+ $page = $this->queries->findVisibleByIdOrFail($pageId);
$page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
$page->makeHidden(['book']);
*/
public function edit(Request $request, string $bookSlug, string $pageSlug)
{
- $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
- $this->checkOwnablePermission('page-update', $page);
+ $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
+ $this->checkOwnablePermission('page-update', $page, $page->getUrl());
- $editorData = new PageEditorData($page, $this->pageRepo, $request->query('editor', ''));
+ $editorData = new PageEditorData($page, $this->entityQueries, $request->query('editor', ''));
if ($editorData->getWarnings()) {
$this->showWarningNotification(implode("\n", $editorData->getWarnings()));
}
$this->validate($request, [
'name' => ['required', 'string', 'max:255'],
]);
- $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+ $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-update', $page);
$this->pageRepo->update($page, $request->all());
*/
public function saveDraft(Request $request, int $pageId)
{
- $page = $this->pageRepo->getById($pageId);
+ $page = $this->queries->findVisibleByIdOrFail($pageId);
$this->checkOwnablePermission('page-update', $page);
if (!$this->isSignedIn()) {
*/
public function redirectFromLink(int $pageId)
{
- $page = $this->pageRepo->getById($pageId);
+ $page = $this->queries->findVisibleByIdOrFail($pageId);
return redirect($page->getUrl());
}
*/
public function showDelete(string $bookSlug, string $pageSlug)
{
- $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+ $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-delete', $page);
$this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
+ $usedAsTemplate =
+ $this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
+ $this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
return view('pages.delete', [
'book' => $page->book,
'page' => $page,
'current' => $page,
+ 'usedAsTemplate' => $usedAsTemplate,
]);
}
*/
public function showDeleteDraft(string $bookSlug, int $pageId)
{
- $page = $this->pageRepo->getById($pageId);
+ $page = $this->queries->findVisibleByIdOrFail($pageId);
$this->checkOwnablePermission('page-update', $page);
$this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
+ $usedAsTemplate =
+ $this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
+ $this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
return view('pages.delete', [
'book' => $page->book,
'page' => $page,
'current' => $page,
+ 'usedAsTemplate' => $usedAsTemplate,
]);
}
*/
public function destroy(string $bookSlug, string $pageSlug)
{
- $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+ $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-delete', $page);
$parent = $page->getParent();
*/
public function destroyDraft(string $bookSlug, int $pageId)
{
- $page = $this->pageRepo->getById($pageId);
+ $page = $this->queries->findVisibleByIdOrFail($pageId);
$book = $page->book;
$chapter = $page->chapter;
$this->checkOwnablePermission('page-update', $page);
$query->scopes('visible');
};
- $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
+ $pages = $this->queries->visibleForList()
+ ->addSelect('updated_by')
+ ->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
->orderBy('updated_at', 'desc')
->paginate(20)
->setPath(url('/pages/recently-updated'));
*/
public function showMove(string $bookSlug, string $pageSlug)
{
- $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+ $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-update', $page);
$this->checkOwnablePermission('page-delete', $page);
*/
public function move(Request $request, string $bookSlug, string $pageSlug)
{
- $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+ $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-update', $page);
$this->checkOwnablePermission('page-delete', $page);
} catch (Exception $exception) {
$this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
- return redirect()->back();
+ return redirect($page->getUrl('/move'));
}
return redirect($page->getUrl());
*/
public function showCopy(string $bookSlug, string $pageSlug)
{
- $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+ $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-view', $page);
session()->flashInput(['name' => $page->name]);
*/
public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
{
- $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
+ $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-view', $page);
$entitySelection = $request->get('entity_selection') ?: null;
- $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
+ $newParent = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $page->getParent();
- if (is_null($newParent)) {
+ if (!$newParent instanceof Book && !$newParent instanceof Chapter) {
$this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
- return redirect()->back();
+ return redirect($page->getUrl('/copy'));
}
$this->checkOwnablePermission('page-create', $newParent);