3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\Models\View;
6 use BookStack\Activity\Tools\CommentTree;
7 use BookStack\Activity\Tools\UserEntityWatchOptions;
8 use BookStack\Entities\Models\Book;
9 use BookStack\Entities\Models\Chapter;
10 use BookStack\Entities\Queries\EntityQueries;
11 use BookStack\Entities\Queries\PageQueries;
12 use BookStack\Entities\Repos\PageRepo;
13 use BookStack\Entities\Tools\BookContents;
14 use BookStack\Entities\Tools\Cloner;
15 use BookStack\Entities\Tools\NextPreviousContentLocator;
16 use BookStack\Entities\Tools\PageContent;
17 use BookStack\Entities\Tools\PageEditActivity;
18 use BookStack\Entities\Tools\PageEditorData;
19 use BookStack\Exceptions\NotFoundException;
20 use BookStack\Exceptions\PermissionsException;
21 use BookStack\Http\Controller;
22 use BookStack\References\ReferenceFetcher;
24 use Illuminate\Database\Eloquent\Relations\BelongsTo;
25 use Illuminate\Http\Request;
26 use Illuminate\Validation\ValidationException;
29 class PageController extends Controller
31 public function __construct(
32 protected PageRepo $pageRepo,
33 protected PageQueries $queries,
34 protected EntityQueries $entityQueries,
35 protected ReferenceFetcher $referenceFetcher
40 * Show the form for creating a new page.
44 public function create(string $bookSlug, string $chapterSlug = null)
47 $parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
49 $parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
52 $this->checkOwnablePermission('page-create', $parent);
54 // Redirect to draft edit screen if signed in
55 if ($this->isSignedIn()) {
56 $draft = $this->pageRepo->getNewDraftPage($parent);
58 return redirect($draft->getUrl());
61 // Otherwise show the edit view if they're a guest
62 $this->setPageTitle(trans('entities.pages_new'));
64 return view('pages.guest-create', ['parent' => $parent]);
68 * Create a new page as a guest user.
70 * @throws ValidationException
72 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
74 $this->validate($request, [
75 'name' => ['required', 'string', 'max:255'],
79 $parent = $this->entityQueries->chapters->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
81 $parent = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
84 $this->checkOwnablePermission('page-create', $parent);
86 $page = $this->pageRepo->getNewDraftPage($parent);
87 $this->pageRepo->publishDraft($page, [
88 'name' => $request->get('name'),
91 return redirect($page->getUrl('/edit'));
95 * Show form to continue editing a draft page.
97 * @throws NotFoundException
99 public function editDraft(Request $request, string $bookSlug, int $pageId)
101 $draft = $this->queries->findVisibleByIdOrFail($pageId);
102 $this->checkOwnablePermission('page-create', $draft->getParent());
104 $editorData = new PageEditorData($draft, $this->entityQueries, $request->query('editor', ''));
105 $this->setPageTitle(trans('entities.pages_edit_draft'));
107 return view('pages.edit', $editorData->getViewData());
111 * Store a new page by changing a draft into a page.
113 * @throws NotFoundException
114 * @throws ValidationException
116 public function store(Request $request, string $bookSlug, int $pageId)
118 $this->validate($request, [
119 'name' => ['required', 'string', 'max:255'],
121 $draftPage = $this->queries->findVisibleByIdOrFail($pageId);
122 $this->checkOwnablePermission('page-create', $draftPage->getParent());
124 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
126 return redirect($page->getUrl());
130 * Display the specified page.
131 * If the page is not found via the slug the revisions are searched for a match.
133 * @throws NotFoundException
135 public function show(string $bookSlug, string $pageSlug)
138 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
139 } catch (NotFoundException $e) {
140 $revision = $this->entityQueries->revisions->findLatestVersionBySlugs($bookSlug, $pageSlug);
141 $page = $revision->page ?? null;
143 if (is_null($page)) {
147 return redirect($page->getUrl());
150 $this->checkOwnablePermission('page-view', $page);
152 $pageContent = (new PageContent($page));
153 $page->html = $pageContent->render();
154 $pageNav = $pageContent->getNavigation($page->html);
156 $sidebarTree = (new BookContents($page->book))->getTree();
157 $commentTree = (new CommentTree($page));
158 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
160 View::incrementFor($page);
161 $this->setPageTitle($page->getShortName());
163 return view('pages.show', [
165 'book' => $page->book,
167 'sidebarTree' => $sidebarTree,
168 'commentTree' => $commentTree,
169 'pageNav' => $pageNav,
170 'watchOptions' => new UserEntityWatchOptions(user(), $page),
171 'next' => $nextPreviousLocator->getNext(),
172 'previous' => $nextPreviousLocator->getPrevious(),
173 'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($page),
178 * Get page from an ajax request.
180 * @throws NotFoundException
182 public function getPageAjax(int $pageId)
184 $page = $this->queries->findVisibleByIdOrFail($pageId);
185 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
186 $page->makeHidden(['book']);
188 return response()->json($page);
192 * Show the form for editing the specified page.
194 * @throws NotFoundException
196 public function edit(Request $request, string $bookSlug, string $pageSlug)
198 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
199 $this->checkOwnablePermission('page-update', $page);
201 $editorData = new PageEditorData($page, $this->entityQueries, $request->query('editor', ''));
202 if ($editorData->getWarnings()) {
203 $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
206 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
208 return view('pages.edit', $editorData->getViewData());
212 * Update the specified page in storage.
214 * @throws ValidationException
215 * @throws NotFoundException
217 public function update(Request $request, string $bookSlug, string $pageSlug)
219 $this->validate($request, [
220 'name' => ['required', 'string', 'max:255'],
222 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
223 $this->checkOwnablePermission('page-update', $page);
225 $this->pageRepo->update($page, $request->all());
227 return redirect($page->getUrl());
231 * Save a draft update as a revision.
233 * @throws NotFoundException
235 public function saveDraft(Request $request, int $pageId)
237 $page = $this->queries->findVisibleByIdOrFail($pageId);
238 $this->checkOwnablePermission('page-update', $page);
240 if (!$this->isSignedIn()) {
241 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
244 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
245 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
247 return response()->json([
248 'status' => 'success',
249 'message' => trans('entities.pages_edit_draft_save_at'),
250 'warning' => implode("\n", $warnings),
251 'timestamp' => $draft->updated_at->timestamp,
256 * Redirect from a special link url which uses the page id rather than the name.
258 * @throws NotFoundException
260 public function redirectFromLink(int $pageId)
262 $page = $this->queries->findVisibleByIdOrFail($pageId);
264 return redirect($page->getUrl());
268 * Show the deletion page for the specified page.
270 * @throws NotFoundException
272 public function showDelete(string $bookSlug, string $pageSlug)
274 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
275 $this->checkOwnablePermission('page-delete', $page);
276 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
278 $this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
279 $this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
281 return view('pages.delete', [
282 'book' => $page->book,
285 'usedAsTemplate' => $usedAsTemplate,
290 * Show the deletion page for the specified page.
292 * @throws NotFoundException
294 public function showDeleteDraft(string $bookSlug, int $pageId)
296 $page = $this->queries->findVisibleByIdOrFail($pageId);
297 $this->checkOwnablePermission('page-update', $page);
298 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
300 $this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
301 $this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
303 return view('pages.delete', [
304 'book' => $page->book,
307 'usedAsTemplate' => $usedAsTemplate,
312 * Remove the specified page from storage.
314 * @throws NotFoundException
317 public function destroy(string $bookSlug, string $pageSlug)
319 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
320 $this->checkOwnablePermission('page-delete', $page);
321 $parent = $page->getParent();
323 $this->pageRepo->destroy($page);
325 return redirect($parent->getUrl());
329 * Remove the specified draft page from storage.
331 * @throws NotFoundException
334 public function destroyDraft(string $bookSlug, int $pageId)
336 $page = $this->queries->findVisibleByIdOrFail($pageId);
338 $chapter = $page->chapter;
339 $this->checkOwnablePermission('page-update', $page);
341 $this->pageRepo->destroy($page);
343 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
345 if ($chapter && userCan('view', $chapter)) {
346 return redirect($chapter->getUrl());
349 return redirect($book->getUrl());
353 * Show a listing of recently created pages.
355 public function showRecentlyUpdated()
357 $visibleBelongsScope = function (BelongsTo $query) {
358 $query->scopes('visible');
361 $pages = $this->queries->visibleForList()
362 ->addSelect('updated_by')
363 ->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
364 ->orderBy('updated_at', 'desc')
366 ->setPath(url('/pages/recently-updated'));
368 $this->setPageTitle(trans('entities.recently_updated_pages'));
370 return view('common.detailed-listing-paginated', [
371 'title' => trans('entities.recently_updated_pages'),
372 'entities' => $pages,
373 'showUpdatedBy' => true,
379 * Show the view to choose a new parent to move a page into.
381 * @throws NotFoundException
383 public function showMove(string $bookSlug, string $pageSlug)
385 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
386 $this->checkOwnablePermission('page-update', $page);
387 $this->checkOwnablePermission('page-delete', $page);
389 return view('pages.move', [
390 'book' => $page->book,
396 * Does the action of moving the location of a page.
398 * @throws NotFoundException
401 public function move(Request $request, string $bookSlug, string $pageSlug)
403 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
404 $this->checkOwnablePermission('page-update', $page);
405 $this->checkOwnablePermission('page-delete', $page);
407 $entitySelection = $request->get('entity_selection', null);
408 if ($entitySelection === null || $entitySelection === '') {
409 return redirect($page->getUrl());
413 $this->pageRepo->move($page, $entitySelection);
414 } catch (PermissionsException $exception) {
415 $this->showPermissionError();
416 } catch (Exception $exception) {
417 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
419 return redirect($page->getUrl('/move'));
422 return redirect($page->getUrl());
426 * Show the view to copy a page.
428 * @throws NotFoundException
430 public function showCopy(string $bookSlug, string $pageSlug)
432 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
433 $this->checkOwnablePermission('page-view', $page);
434 session()->flashInput(['name' => $page->name]);
436 return view('pages.copy', [
437 'book' => $page->book,
443 * Create a copy of a page within the requested target destination.
445 * @throws NotFoundException
448 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
450 $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
451 $this->checkOwnablePermission('page-view', $page);
453 $entitySelection = $request->get('entity_selection') ?: null;
454 $newParent = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $page->getParent();
456 if (!$newParent instanceof Book && !$newParent instanceof Chapter) {
457 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
459 return redirect($page->getUrl('/copy'));
462 $this->checkOwnablePermission('page-create', $newParent);
464 $newName = $request->get('name') ?: $page->name;
465 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
466 $this->showSuccessNotification(trans('entities.pages_copy_success'));
468 return redirect($pageCopy->getUrl());