3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\Models\View;
6 use BookStack\Activity\Tools\CommentTree;
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\Http\Controller;
18 use BookStack\References\ReferenceFetcher;
20 use Illuminate\Database\Eloquent\Relations\BelongsTo;
21 use Illuminate\Http\Request;
22 use Illuminate\Validation\ValidationException;
25 class PageController extends Controller
27 protected PageRepo $pageRepo;
28 protected ReferenceFetcher $referenceFetcher;
31 * PageController constructor.
33 public function __construct(PageRepo $pageRepo, ReferenceFetcher $referenceFetcher)
35 $this->pageRepo = $pageRepo;
36 $this->referenceFetcher = $referenceFetcher;
40 * Show the form for creating a new page.
44 public function create(string $bookSlug, string $chapterSlug = null)
46 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
47 $this->checkOwnablePermission('page-create', $parent);
49 // Redirect to draft edit screen if signed in
50 if ($this->isSignedIn()) {
51 $draft = $this->pageRepo->getNewDraftPage($parent);
53 return redirect($draft->getUrl());
56 // Otherwise show the edit view if they're a guest
57 $this->setPageTitle(trans('entities.pages_new'));
59 return view('pages.guest-create', ['parent' => $parent]);
63 * Create a new page as a guest user.
65 * @throws ValidationException
67 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
69 $this->validate($request, [
70 'name' => ['required', 'string', 'max:255'],
73 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
74 $this->checkOwnablePermission('page-create', $parent);
76 $page = $this->pageRepo->getNewDraftPage($parent);
77 $this->pageRepo->publishDraft($page, [
78 'name' => $request->get('name'),
82 return redirect($page->getUrl('/edit'));
86 * Show form to continue editing a draft page.
88 * @throws NotFoundException
90 public function editDraft(Request $request, string $bookSlug, int $pageId)
92 $draft = $this->pageRepo->getById($pageId);
93 $this->checkOwnablePermission('page-create', $draft->getParent());
95 $editorData = new PageEditorData($draft, $this->pageRepo, $request->query('editor', ''));
96 $this->setPageTitle(trans('entities.pages_edit_draft'));
98 return view('pages.edit', $editorData->getViewData());
102 * Store a new page by changing a draft into a page.
104 * @throws NotFoundException
105 * @throws ValidationException
107 public function store(Request $request, string $bookSlug, int $pageId)
109 $this->validate($request, [
110 'name' => ['required', 'string', 'max:255'],
112 $draftPage = $this->pageRepo->getById($pageId);
113 $this->checkOwnablePermission('page-create', $draftPage->getParent());
115 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
117 return redirect($page->getUrl());
121 * Display the specified page.
122 * If the page is not found via the slug the revisions are searched for a match.
124 * @throws NotFoundException
126 public function show(string $bookSlug, string $pageSlug)
129 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
130 } catch (NotFoundException $e) {
131 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
133 if ($page === null) {
137 return redirect($page->getUrl());
140 $this->checkOwnablePermission('page-view', $page);
142 $pageContent = (new PageContent($page));
143 $page->html = $pageContent->render();
144 $pageNav = $pageContent->getNavigation($page->html);
146 $sidebarTree = (new BookContents($page->book))->getTree();
147 $commentTree = (new CommentTree($page));
148 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
150 View::incrementFor($page);
151 $this->setPageTitle($page->getShortName());
153 return view('pages.show', [
155 'book' => $page->book,
157 'sidebarTree' => $sidebarTree,
158 'commentTree' => $commentTree,
159 'pageNav' => $pageNav,
160 'next' => $nextPreviousLocator->getNext(),
161 'previous' => $nextPreviousLocator->getPrevious(),
162 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($page),
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());