3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\View;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Entities\Tools\BookContents;
9 use BookStack\Entities\Tools\Cloner;
10 use BookStack\Entities\Tools\NextPreviousContentLocator;
11 use BookStack\Entities\Tools\PageContent;
12 use BookStack\Entities\Tools\PageEditActivity;
13 use BookStack\Entities\Tools\PageEditorData;
14 use BookStack\Entities\Tools\PermissionsUpdater;
15 use BookStack\Exceptions\NotFoundException;
16 use BookStack\Exceptions\PermissionsException;
18 use Illuminate\Database\Eloquent\Relations\BelongsTo;
19 use Illuminate\Http\Request;
20 use Illuminate\Validation\ValidationException;
23 class PageController extends Controller
25 protected PageRepo $pageRepo;
28 * PageController constructor.
30 public function __construct(PageRepo $pageRepo)
32 $this->pageRepo = $pageRepo;
36 * Show the form for creating a new page.
40 public function create(string $bookSlug, string $chapterSlug = null)
42 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
43 $this->checkOwnablePermission('page-create', $parent);
45 // Redirect to draft edit screen if signed in
46 if ($this->isSignedIn()) {
47 $draft = $this->pageRepo->getNewDraftPage($parent);
49 return redirect($draft->getUrl());
52 // Otherwise show the edit view if they're a guest
53 $this->setPageTitle(trans('entities.pages_new'));
55 return view('pages.guest-create', ['parent' => $parent]);
59 * Create a new page as a guest user.
61 * @throws ValidationException
63 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
65 $this->validate($request, [
66 'name' => ['required', 'string', 'max:255'],
69 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
70 $this->checkOwnablePermission('page-create', $parent);
72 $page = $this->pageRepo->getNewDraftPage($parent);
73 $this->pageRepo->publishDraft($page, [
74 'name' => $request->get('name'),
78 return redirect($page->getUrl('/edit'));
82 * Show form to continue editing a draft page.
84 * @throws NotFoundException
86 public function editDraft(Request $request, string $bookSlug, int $pageId)
88 $draft = $this->pageRepo->getById($pageId);
89 $this->checkOwnablePermission('page-create', $draft->getParent());
91 $editorData = new PageEditorData($draft, $this->pageRepo, $request->query('editor', ''));
92 $this->setPageTitle(trans('entities.pages_edit_draft'));
94 return view('pages.edit', $editorData->getViewData());
98 * Store a new page by changing a draft into a page.
100 * @throws NotFoundException
101 * @throws ValidationException
103 public function store(Request $request, string $bookSlug, int $pageId)
105 $this->validate($request, [
106 'name' => ['required', 'string', 'max:255'],
108 $draftPage = $this->pageRepo->getById($pageId);
109 $this->checkOwnablePermission('page-create', $draftPage->getParent());
111 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
113 return redirect($page->getUrl());
117 * Display the specified page.
118 * If the page is not found via the slug the revisions are searched for a match.
120 * @throws NotFoundException
122 public function show(string $bookSlug, string $pageSlug)
125 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
126 } catch (NotFoundException $e) {
127 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
129 if ($page === null) {
133 return redirect($page->getUrl());
136 $this->checkOwnablePermission('page-view', $page);
138 $pageContent = (new PageContent($page));
139 $page->html = $pageContent->render();
140 $sidebarTree = (new BookContents($page->book))->getTree();
141 $pageNav = $pageContent->getNavigation($page->html);
143 // Check if page comments are enabled
144 $commentsEnabled = !setting('app-disable-comments');
145 if ($commentsEnabled) {
146 $page->load(['comments.createdBy']);
149 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
151 View::incrementFor($page);
152 $this->setPageTitle($page->getShortName());
154 return view('pages.show', [
156 'book' => $page->book,
158 'sidebarTree' => $sidebarTree,
159 'commentsEnabled' => $commentsEnabled,
160 'pageNav' => $pageNav,
161 'next' => $nextPreviousLocator->getNext(),
162 'previous' => $nextPreviousLocator->getPrevious(),
167 * Get page from an ajax request.
169 * @throws NotFoundException
171 public function getPageAjax(int $pageId)
173 $page = $this->pageRepo->getById($pageId);
174 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
175 $page->makeHidden(['book']);
177 return response()->json($page);
181 * Show the form for editing the specified page.
183 * @throws NotFoundException
185 public function edit(Request $request, string $bookSlug, string $pageSlug)
187 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
188 $this->checkOwnablePermission('page-update', $page);
190 $editorData = new PageEditorData($page, $this->pageRepo, $request->query('editor', ''));
191 if ($editorData->getWarnings()) {
192 $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
195 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
197 return view('pages.edit', $editorData->getViewData());
201 * Update the specified page in storage.
203 * @throws ValidationException
204 * @throws NotFoundException
206 public function update(Request $request, string $bookSlug, string $pageSlug)
208 $this->validate($request, [
209 'name' => ['required', 'string', 'max:255'],
211 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
212 $this->checkOwnablePermission('page-update', $page);
214 $this->pageRepo->update($page, $request->all());
216 return redirect($page->getUrl());
220 * Save a draft update as a revision.
222 * @throws NotFoundException
224 public function saveDraft(Request $request, int $pageId)
226 $page = $this->pageRepo->getById($pageId);
227 $this->checkOwnablePermission('page-update', $page);
229 if (!$this->isSignedIn()) {
230 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
233 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
234 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
236 return response()->json([
237 'status' => 'success',
238 'message' => trans('entities.pages_edit_draft_save_at'),
239 'warning' => implode("\n", $warnings),
240 'timestamp' => $draft->updated_at->timestamp,
245 * Redirect from a special link url which uses the page id rather than the name.
247 * @throws NotFoundException
249 public function redirectFromLink(int $pageId)
251 $page = $this->pageRepo->getById($pageId);
253 return redirect($page->getUrl());
257 * Show the deletion page for the specified page.
259 * @throws NotFoundException
261 public function showDelete(string $bookSlug, string $pageSlug)
263 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
264 $this->checkOwnablePermission('page-delete', $page);
265 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
267 return view('pages.delete', [
268 'book' => $page->book,
275 * Show the deletion page for the specified page.
277 * @throws NotFoundException
279 public function showDeleteDraft(string $bookSlug, int $pageId)
281 $page = $this->pageRepo->getById($pageId);
282 $this->checkOwnablePermission('page-update', $page);
283 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
285 return view('pages.delete', [
286 'book' => $page->book,
293 * Remove the specified page from storage.
295 * @throws NotFoundException
298 public function destroy(string $bookSlug, string $pageSlug)
300 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
301 $this->checkOwnablePermission('page-delete', $page);
302 $parent = $page->getParent();
304 $this->pageRepo->destroy($page);
306 return redirect($parent->getUrl());
310 * Remove the specified draft page from storage.
312 * @throws NotFoundException
315 public function destroyDraft(string $bookSlug, int $pageId)
317 $page = $this->pageRepo->getById($pageId);
319 $chapter = $page->chapter;
320 $this->checkOwnablePermission('page-update', $page);
322 $this->pageRepo->destroy($page);
324 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
326 if ($chapter && userCan('view', $chapter)) {
327 return redirect($chapter->getUrl());
330 return redirect($book->getUrl());
334 * Show a listing of recently created pages.
336 public function showRecentlyUpdated()
338 $visibleBelongsScope = function (BelongsTo $query) {
339 $query->scopes('visible');
342 $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
343 ->orderBy('updated_at', 'desc')
345 ->setPath(url('/pages/recently-updated'));
347 $this->setPageTitle(trans('entities.recently_updated_pages'));
349 return view('common.detailed-listing-paginated', [
350 'title' => trans('entities.recently_updated_pages'),
351 'entities' => $pages,
352 'showUpdatedBy' => true,
358 * Show the view to choose a new parent to move a page into.
360 * @throws NotFoundException
362 public function showMove(string $bookSlug, string $pageSlug)
364 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
365 $this->checkOwnablePermission('page-update', $page);
366 $this->checkOwnablePermission('page-delete', $page);
368 return view('pages.move', [
369 'book' => $page->book,
375 * Does the action of moving the location of a page.
377 * @throws NotFoundException
380 public function move(Request $request, string $bookSlug, string $pageSlug)
382 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
383 $this->checkOwnablePermission('page-update', $page);
384 $this->checkOwnablePermission('page-delete', $page);
386 $entitySelection = $request->get('entity_selection', null);
387 if ($entitySelection === null || $entitySelection === '') {
388 return redirect($page->getUrl());
392 $parent = $this->pageRepo->move($page, $entitySelection);
393 } catch (PermissionsException $exception) {
394 $this->showPermissionError();
395 } catch (Exception $exception) {
396 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
398 return redirect()->back();
401 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
403 return redirect($page->getUrl());
407 * Show the view to copy a page.
409 * @throws NotFoundException
411 public function showCopy(string $bookSlug, string $pageSlug)
413 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
414 $this->checkOwnablePermission('page-view', $page);
415 session()->flashInput(['name' => $page->name]);
417 return view('pages.copy', [
418 'book' => $page->book,
424 * Create a copy of a page within the requested target destination.
426 * @throws NotFoundException
429 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
431 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
432 $this->checkOwnablePermission('page-view', $page);
434 $entitySelection = $request->get('entity_selection') ?: null;
435 $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
437 if (is_null($newParent)) {
438 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
440 return redirect()->back();
443 $this->checkOwnablePermission('page-create', $newParent);
445 $newName = $request->get('name') ?: $page->name;
446 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
447 $this->showSuccessNotification(trans('entities.pages_copy_success'));
449 return redirect($pageCopy->getUrl());
453 * Show the Permissions view.
455 * @throws NotFoundException
457 public function showPermissions(string $bookSlug, string $pageSlug)
459 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
460 $this->checkOwnablePermission('restrictions-manage', $page);
462 return view('pages.permissions', [
468 * Set the permissions for this page.
470 * @throws NotFoundException
473 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
475 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
476 $this->checkOwnablePermission('restrictions-manage', $page);
478 $permissionsUpdater->updateFromPermissionsForm($page, $request);
480 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
482 return redirect($page->getUrl());