3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\View;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Entities\Tools\BookContents;
10 use BookStack\Entities\Tools\Cloner;
11 use BookStack\Entities\Tools\NextPreviousContentLocator;
12 use BookStack\Entities\Tools\PageContent;
13 use BookStack\Entities\Tools\PageEditActivity;
14 use BookStack\Entities\Tools\PageEditorData;
15 use BookStack\Exceptions\NotFoundException;
16 use BookStack\Exceptions\PermissionsException;
17 use BookStack\References\ReferenceFetcher;
19 use Illuminate\Database\Eloquent\Relations\BelongsTo;
20 use Illuminate\Http\Request;
21 use Illuminate\Validation\ValidationException;
24 class PageController extends Controller
26 protected PageRepo $pageRepo;
27 protected ReferenceFetcher $referenceFetcher;
30 * PageController constructor.
32 public function __construct(PageRepo $pageRepo, ReferenceFetcher $referenceFetcher)
34 $this->pageRepo = $pageRepo;
35 $this->referenceFetcher = $referenceFetcher;
39 * Show the form for creating a new page.
43 public function create(string $bookSlug, string $chapterSlug = null)
45 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
46 $this->checkOwnablePermission('page-create', $parent);
48 // Redirect to draft edit screen if signed in
49 if ($this->isSignedIn()) {
50 $draft = $this->pageRepo->getNewDraftPage($parent);
52 return redirect($draft->getUrl());
55 // Otherwise show the edit view if they're a guest
56 $this->setPageTitle(trans('entities.pages_new'));
58 return view('pages.guest-create', ['parent' => $parent]);
62 * Create a new page as a guest user.
64 * @throws ValidationException
66 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
68 $this->validate($request, [
69 'name' => ['required', 'string', 'max:255'],
72 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
73 $this->checkOwnablePermission('page-create', $parent);
75 $page = $this->pageRepo->getNewDraftPage($parent);
76 $this->pageRepo->publishDraft($page, [
77 'name' => $request->get('name'),
80 return redirect($page->getUrl('/edit'));
84 * Show form to continue editing a draft page.
86 * @throws NotFoundException
88 public function editDraft(Request $request, string $bookSlug, int $pageId)
90 $draft = $this->pageRepo->getById($pageId);
91 $this->checkOwnablePermission('page-create', $draft->getParent());
93 $editorData = new PageEditorData($draft, $this->pageRepo, $request->query('editor', ''));
94 $this->setPageTitle(trans('entities.pages_edit_draft'));
96 return view('pages.edit', $editorData->getViewData());
100 * Store a new page by changing a draft into a page.
102 * @throws NotFoundException
103 * @throws ValidationException
105 public function store(Request $request, string $bookSlug, int $pageId)
107 $this->validate($request, [
108 'name' => ['required', 'string', 'max:255'],
110 $draftPage = $this->pageRepo->getById($pageId);
111 $this->checkOwnablePermission('page-create', $draftPage->getParent());
113 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
115 return redirect($page->getUrl());
119 * Display the specified page.
120 * If the page is not found via the slug the revisions are searched for a match.
122 * @throws NotFoundException
124 public function show(string $bookSlug, string $pageSlug)
127 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
128 } catch (NotFoundException $e) {
129 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
131 if ($page === null) {
135 return redirect($page->getUrl());
138 $this->checkOwnablePermission('page-view', $page);
140 $pageContent = (new PageContent($page));
141 $page->html = $pageContent->render();
142 $sidebarTree = (new BookContents($page->book))->getTree();
143 $pageNav = $pageContent->getNavigation($page->html);
145 // Check if page comments are enabled
146 $commentsEnabled = !setting('app-disable-comments');
147 if ($commentsEnabled) {
148 $page->load(['comments.createdBy']);
151 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
153 View::incrementFor($page);
154 $this->setPageTitle($page->getShortName());
156 return view('pages.show', [
158 'book' => $page->book,
160 'sidebarTree' => $sidebarTree,
161 'commentsEnabled' => $commentsEnabled,
162 'pageNav' => $pageNav,
163 'next' => $nextPreviousLocator->getNext(),
164 'previous' => $nextPreviousLocator->getPrevious(),
165 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($page),
170 * Get page from an ajax request.
172 * @throws NotFoundException
174 public function getPageAjax(int $pageId)
176 $page = $this->pageRepo->getById($pageId);
177 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
178 $page->makeHidden(['book']);
180 return response()->json($page);
184 * Show the form for editing the specified page.
186 * @throws NotFoundException
188 public function edit(Request $request, string $bookSlug, string $pageSlug)
190 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
191 $this->checkOwnablePermission('page-update', $page);
193 $editorData = new PageEditorData($page, $this->pageRepo, $request->query('editor', ''));
194 if ($editorData->getWarnings()) {
195 $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
198 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
200 return view('pages.edit', $editorData->getViewData());
204 * Update the specified page in storage.
206 * @throws ValidationException
207 * @throws NotFoundException
209 public function update(Request $request, string $bookSlug, string $pageSlug)
211 $this->validate($request, [
212 'name' => ['required', 'string', 'max:255'],
214 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
215 $this->checkOwnablePermission('page-update', $page);
217 $this->pageRepo->update($page, $request->all());
219 return redirect($page->getUrl());
223 * Save a draft update as a revision.
225 * @throws NotFoundException
227 public function saveDraft(Request $request, int $pageId)
229 $page = $this->pageRepo->getById($pageId);
230 $this->checkOwnablePermission('page-update', $page);
232 if (!$this->isSignedIn()) {
233 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
236 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
237 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
239 return response()->json([
240 'status' => 'success',
241 'message' => trans('entities.pages_edit_draft_save_at'),
242 'warning' => implode("\n", $warnings),
243 'timestamp' => $draft->updated_at->timestamp,
248 * Redirect from a special link url which uses the page id rather than the name.
250 * @throws NotFoundException
252 public function redirectFromLink(int $pageId)
254 $page = $this->pageRepo->getById($pageId);
256 return redirect($page->getUrl());
260 * Show the deletion page for the specified page.
262 * @throws NotFoundException
264 public function showDelete(string $bookSlug, string $pageSlug)
266 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
267 $this->checkOwnablePermission('page-delete', $page);
268 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
269 $times_used_as_template = Book::where('default_template', '=', $page->id)->count();
271 return view('pages.delete', [
272 'book' => $page->book,
275 'times_used_as_template' => $times_used_as_template,
280 * Show the deletion page for the specified page.
282 * @throws NotFoundException
284 public function showDeleteDraft(string $bookSlug, int $pageId)
286 $page = $this->pageRepo->getById($pageId);
287 $this->checkOwnablePermission('page-update', $page);
288 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
290 return view('pages.delete', [
291 'book' => $page->book,
298 * Remove the specified page from storage.
300 * @throws NotFoundException
303 public function destroy(string $bookSlug, string $pageSlug)
305 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
306 $this->checkOwnablePermission('page-delete', $page);
307 $parent = $page->getParent();
309 $this->pageRepo->destroy($page);
311 return redirect($parent->getUrl());
315 * Remove the specified draft page from storage.
317 * @throws NotFoundException
320 public function destroyDraft(string $bookSlug, int $pageId)
322 $page = $this->pageRepo->getById($pageId);
324 $chapter = $page->chapter;
325 $this->checkOwnablePermission('page-update', $page);
327 $this->pageRepo->destroy($page);
329 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
331 if ($chapter && userCan('view', $chapter)) {
332 return redirect($chapter->getUrl());
335 return redirect($book->getUrl());
339 * Show a listing of recently created pages.
341 public function showRecentlyUpdated()
343 $visibleBelongsScope = function (BelongsTo $query) {
344 $query->scopes('visible');
347 $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
348 ->orderBy('updated_at', 'desc')
350 ->setPath(url('/pages/recently-updated'));
352 $this->setPageTitle(trans('entities.recently_updated_pages'));
354 return view('common.detailed-listing-paginated', [
355 'title' => trans('entities.recently_updated_pages'),
356 'entities' => $pages,
357 'showUpdatedBy' => true,
363 * Show the view to choose a new parent to move a page into.
365 * @throws NotFoundException
367 public function showMove(string $bookSlug, string $pageSlug)
369 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
370 $this->checkOwnablePermission('page-update', $page);
371 $this->checkOwnablePermission('page-delete', $page);
373 return view('pages.move', [
374 'book' => $page->book,
380 * Does the action of moving the location of a page.
382 * @throws NotFoundException
385 public function move(Request $request, string $bookSlug, string $pageSlug)
387 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
388 $this->checkOwnablePermission('page-update', $page);
389 $this->checkOwnablePermission('page-delete', $page);
391 $entitySelection = $request->get('entity_selection', null);
392 if ($entitySelection === null || $entitySelection === '') {
393 return redirect($page->getUrl());
397 $parent = $this->pageRepo->move($page, $entitySelection);
398 } catch (PermissionsException $exception) {
399 $this->showPermissionError();
400 } catch (Exception $exception) {
401 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
403 return redirect()->back();
406 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
408 return redirect($page->getUrl());
412 * Show the view to copy a page.
414 * @throws NotFoundException
416 public function showCopy(string $bookSlug, string $pageSlug)
418 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
419 $this->checkOwnablePermission('page-view', $page);
420 session()->flashInput(['name' => $page->name]);
422 return view('pages.copy', [
423 'book' => $page->book,
429 * Create a copy of a page within the requested target destination.
431 * @throws NotFoundException
434 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
436 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
437 $this->checkOwnablePermission('page-view', $page);
439 $entitySelection = $request->get('entity_selection') ?: null;
440 $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
442 if (is_null($newParent)) {
443 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
445 return redirect()->back();
448 $this->checkOwnablePermission('page-create', $newParent);
450 $newName = $request->get('name') ?: $page->name;
451 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
452 $this->showSuccessNotification(trans('entities.pages_copy_success'));
454 return redirect($pageCopy->getUrl());