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\PermissionsUpdater;
14 use BookStack\Exceptions\NotFoundException;
15 use BookStack\Exceptions\PermissionsException;
17 use Illuminate\Database\Eloquent\Relations\BelongsTo;
18 use Illuminate\Http\Request;
19 use Illuminate\Validation\ValidationException;
22 class PageController extends Controller
27 * PageController constructor.
29 public function __construct(PageRepo $pageRepo)
31 $this->pageRepo = $pageRepo;
35 * Show the form for creating a new page.
39 public function create(string $bookSlug, string $chapterSlug = null)
41 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
42 $this->checkOwnablePermission('page-create', $parent);
44 // Redirect to draft edit screen if signed in
45 if ($this->isSignedIn()) {
46 $draft = $this->pageRepo->getNewDraftPage($parent);
48 return redirect($draft->getUrl());
51 // Otherwise show the edit view if they're a guest
52 $this->setPageTitle(trans('entities.pages_new'));
54 return view('pages.guest-create', ['parent' => $parent]);
58 * Create a new page as a guest user.
60 * @throws ValidationException
62 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
64 $this->validate($request, [
65 'name' => ['required', 'string', 'max:255'],
68 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
69 $this->checkOwnablePermission('page-create', $parent);
71 $page = $this->pageRepo->getNewDraftPage($parent);
72 $this->pageRepo->publishDraft($page, [
73 'name' => $request->get('name'),
77 return redirect($page->getUrl('/edit'));
81 * Show form to continue editing a draft page.
83 * @throws NotFoundException
85 public function editDraft(string $bookSlug, int $pageId)
87 $draft = $this->pageRepo->getById($pageId);
88 $this->checkOwnablePermission('page-create', $draft->getParent());
89 $this->setPageTitle(trans('entities.pages_edit_draft'));
91 $draftsEnabled = $this->isSignedIn();
92 $templates = $this->pageRepo->getTemplates(10);
94 return view('pages.edit', [
96 'book' => $draft->book,
98 'draftsEnabled' => $draftsEnabled,
99 'templates' => $templates,
100 'editor' => setting('app-editor') === 'wysiwyg' ? 'wysiwyg' : 'markdown',
105 * Store a new page by changing a draft into a page.
107 * @throws NotFoundException
108 * @throws ValidationException
110 public function store(Request $request, string $bookSlug, int $pageId)
112 $this->validate($request, [
113 'name' => ['required', 'string', 'max:255'],
115 $draftPage = $this->pageRepo->getById($pageId);
116 $this->checkOwnablePermission('page-create', $draftPage->getParent());
118 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
120 return redirect($page->getUrl());
124 * Display the specified page.
125 * If the page is not found via the slug the revisions are searched for a match.
127 * @throws NotFoundException
129 public function show(string $bookSlug, string $pageSlug)
132 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
133 } catch (NotFoundException $e) {
134 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
136 if ($page === null) {
140 return redirect($page->getUrl());
143 $this->checkOwnablePermission('page-view', $page);
145 $pageContent = (new PageContent($page));
146 $page->html = $pageContent->render();
147 $sidebarTree = (new BookContents($page->book))->getTree();
148 $pageNav = $pageContent->getNavigation($page->html);
150 // Check if page comments are enabled
151 $commentsEnabled = !setting('app-disable-comments');
152 if ($commentsEnabled) {
153 $page->load(['comments.createdBy']);
156 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
158 View::incrementFor($page);
159 $this->setPageTitle($page->getShortName());
161 return view('pages.show', [
163 'book' => $page->book,
165 'sidebarTree' => $sidebarTree,
166 'commentsEnabled' => $commentsEnabled,
167 'pageNav' => $pageNav,
168 'next' => $nextPreviousLocator->getNext(),
169 'previous' => $nextPreviousLocator->getPrevious(),
174 * Get page from an ajax request.
176 * @throws NotFoundException
178 public function getPageAjax(int $pageId)
180 $page = $this->pageRepo->getById($pageId);
181 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
182 $page->makeHidden(['book']);
184 return response()->json($page);
188 * Show the form for editing the specified page.
190 * @throws NotFoundException
192 public function edit(string $bookSlug, string $pageSlug)
194 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
195 $this->checkOwnablePermission('page-update', $page);
197 $page->isDraft = false;
198 $editActivity = new PageEditActivity($page);
200 // Check for active editing
202 if ($editActivity->hasActiveEditing()) {
203 $warnings[] = $editActivity->activeEditingMessage();
206 // Check for a current draft version for this user
207 $userDraft = $this->pageRepo->getUserDraft($page);
208 if ($userDraft !== null) {
209 $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
210 $page->isDraft = true;
211 $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
214 if (count($warnings) > 0) {
215 $this->showWarningNotification(implode("\n", $warnings));
218 $templates = $this->pageRepo->getTemplates(10);
219 $draftsEnabled = $this->isSignedIn();
220 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
222 return view('pages.edit', [
224 'book' => $page->book,
226 'draftsEnabled' => $draftsEnabled,
227 'templates' => $templates,
228 'editor' => setting('app-editor') === 'wysiwyg' ? 'wysiwyg' : 'markdown',
233 * Update the specified page in storage.
235 * @throws ValidationException
236 * @throws NotFoundException
238 public function update(Request $request, string $bookSlug, string $pageSlug)
240 $this->validate($request, [
241 'name' => ['required', 'string', 'max:255'],
243 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
244 $this->checkOwnablePermission('page-update', $page);
246 $this->pageRepo->update($page, $request->all());
248 return redirect($page->getUrl());
252 * Save a draft update as a revision.
254 * @throws NotFoundException
256 public function saveDraft(Request $request, int $pageId)
258 $page = $this->pageRepo->getById($pageId);
259 $this->checkOwnablePermission('page-update', $page);
261 if (!$this->isSignedIn()) {
262 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
265 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
266 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
268 return response()->json([
269 'status' => 'success',
270 'message' => trans('entities.pages_edit_draft_save_at'),
271 'warning' => implode("\n", $warnings),
272 'timestamp' => $draft->updated_at->timestamp,
277 * Redirect from a special link url which uses the page id rather than the name.
279 * @throws NotFoundException
281 public function redirectFromLink(int $pageId)
283 $page = $this->pageRepo->getById($pageId);
285 return redirect($page->getUrl());
289 * Show the deletion page for the specified page.
291 * @throws NotFoundException
293 public function showDelete(string $bookSlug, string $pageSlug)
295 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
296 $this->checkOwnablePermission('page-delete', $page);
297 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
299 return view('pages.delete', [
300 'book' => $page->book,
307 * Show the deletion page for the specified page.
309 * @throws NotFoundException
311 public function showDeleteDraft(string $bookSlug, int $pageId)
313 $page = $this->pageRepo->getById($pageId);
314 $this->checkOwnablePermission('page-update', $page);
315 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
317 return view('pages.delete', [
318 'book' => $page->book,
325 * Remove the specified page from storage.
327 * @throws NotFoundException
330 public function destroy(string $bookSlug, string $pageSlug)
332 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
333 $this->checkOwnablePermission('page-delete', $page);
334 $parent = $page->getParent();
336 $this->pageRepo->destroy($page);
338 return redirect($parent->getUrl());
342 * Remove the specified draft page from storage.
344 * @throws NotFoundException
347 public function destroyDraft(string $bookSlug, int $pageId)
349 $page = $this->pageRepo->getById($pageId);
351 $chapter = $page->chapter;
352 $this->checkOwnablePermission('page-update', $page);
354 $this->pageRepo->destroy($page);
356 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
358 if ($chapter && userCan('view', $chapter)) {
359 return redirect($chapter->getUrl());
362 return redirect($book->getUrl());
366 * Show a listing of recently created pages.
368 public function showRecentlyUpdated()
370 $visibleBelongsScope = function (BelongsTo $query) {
371 $query->scopes('visible');
374 $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
375 ->orderBy('updated_at', 'desc')
377 ->setPath(url('/pages/recently-updated'));
379 $this->setPageTitle(trans('entities.recently_updated_pages'));
381 return view('common.detailed-listing-paginated', [
382 'title' => trans('entities.recently_updated_pages'),
383 'entities' => $pages,
384 'showUpdatedBy' => true,
390 * Show the view to choose a new parent to move a page into.
392 * @throws NotFoundException
394 public function showMove(string $bookSlug, string $pageSlug)
396 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
397 $this->checkOwnablePermission('page-update', $page);
398 $this->checkOwnablePermission('page-delete', $page);
400 return view('pages.move', [
401 'book' => $page->book,
407 * Does the action of moving the location of a page.
409 * @throws NotFoundException
412 public function move(Request $request, string $bookSlug, string $pageSlug)
414 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
415 $this->checkOwnablePermission('page-update', $page);
416 $this->checkOwnablePermission('page-delete', $page);
418 $entitySelection = $request->get('entity_selection', null);
419 if ($entitySelection === null || $entitySelection === '') {
420 return redirect($page->getUrl());
424 $parent = $this->pageRepo->move($page, $entitySelection);
425 } catch (PermissionsException $exception) {
426 $this->showPermissionError();
427 } catch (Exception $exception) {
428 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
430 return redirect()->back();
433 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
435 return redirect($page->getUrl());
439 * Show the view to copy a page.
441 * @throws NotFoundException
443 public function showCopy(string $bookSlug, string $pageSlug)
445 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
446 $this->checkOwnablePermission('page-view', $page);
447 session()->flashInput(['name' => $page->name]);
449 return view('pages.copy', [
450 'book' => $page->book,
456 * Create a copy of a page within the requested target destination.
458 * @throws NotFoundException
461 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
463 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
464 $this->checkOwnablePermission('page-view', $page);
466 $entitySelection = $request->get('entity_selection') ?: null;
467 $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
469 if (is_null($newParent)) {
470 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
472 return redirect()->back();
475 $this->checkOwnablePermission('page-create', $newParent);
477 $newName = $request->get('name') ?: $page->name;
478 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
479 $this->showSuccessNotification(trans('entities.pages_copy_success'));
481 return redirect($pageCopy->getUrl());
485 * Show the Permissions view.
487 * @throws NotFoundException
489 public function showPermissions(string $bookSlug, string $pageSlug)
491 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
492 $this->checkOwnablePermission('restrictions-manage', $page);
494 return view('pages.permissions', [
500 * Set the permissions for this page.
502 * @throws NotFoundException
505 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
507 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
508 $this->checkOwnablePermission('restrictions-manage', $page);
510 $permissionsUpdater->updateFromPermissionsForm($page, $request);
512 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
514 return redirect($page->getUrl());