3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\Models\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\Exceptions\NotFoundException;
15 use BookStack\Exceptions\PermissionsException;
16 use BookStack\Http\Controllers\Controller;
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'),
81 return redirect($page->getUrl('/edit'));
85 * Show form to continue editing a draft page.
87 * @throws NotFoundException
89 public function editDraft(Request $request, string $bookSlug, int $pageId)
91 $draft = $this->pageRepo->getById($pageId);
92 $this->checkOwnablePermission('page-create', $draft->getParent());
94 $editorData = new PageEditorData($draft, $this->pageRepo, $request->query('editor', ''));
95 $this->setPageTitle(trans('entities.pages_edit_draft'));
97 return view('pages.edit', $editorData->getViewData());
101 * Store a new page by changing a draft into a page.
103 * @throws NotFoundException
104 * @throws ValidationException
106 public function store(Request $request, string $bookSlug, int $pageId)
108 $this->validate($request, [
109 'name' => ['required', 'string', 'max:255'],
111 $draftPage = $this->pageRepo->getById($pageId);
112 $this->checkOwnablePermission('page-create', $draftPage->getParent());
114 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
116 return redirect($page->getUrl());
120 * Display the specified page.
121 * If the page is not found via the slug the revisions are searched for a match.
123 * @throws NotFoundException
125 public function show(string $bookSlug, string $pageSlug)
128 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
129 } catch (NotFoundException $e) {
130 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
132 if ($page === null) {
136 return redirect($page->getUrl());
139 $this->checkOwnablePermission('page-view', $page);
141 $pageContent = (new PageContent($page));
142 $page->html = $pageContent->render();
143 $sidebarTree = (new BookContents($page->book))->getTree();
144 $pageNav = $pageContent->getNavigation($page->html);
146 // Check if page comments are enabled
147 $commentsEnabled = !setting('app-disable-comments');
148 if ($commentsEnabled) {
149 $page->load(['comments.createdBy']);
152 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
154 View::incrementFor($page);
155 $this->setPageTitle($page->getShortName());
157 return view('pages.show', [
159 'book' => $page->book,
161 'sidebarTree' => $sidebarTree,
162 'commentsEnabled' => $commentsEnabled,
163 'pageNav' => $pageNav,
164 'next' => $nextPreviousLocator->getNext(),
165 'previous' => $nextPreviousLocator->getPrevious(),
166 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($page),
171 * Get page from an ajax request.
173 * @throws NotFoundException
175 public function getPageAjax(int $pageId)
177 $page = $this->pageRepo->getById($pageId);
178 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
179 $page->makeHidden(['book']);
181 return response()->json($page);
185 * Show the form for editing the specified page.
187 * @throws NotFoundException
189 public function edit(Request $request, string $bookSlug, string $pageSlug)
191 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
192 $this->checkOwnablePermission('page-update', $page);
194 $editorData = new PageEditorData($page, $this->pageRepo, $request->query('editor', ''));
195 if ($editorData->getWarnings()) {
196 $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
199 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
201 return view('pages.edit', $editorData->getViewData());
205 * Update the specified page in storage.
207 * @throws ValidationException
208 * @throws NotFoundException
210 public function update(Request $request, string $bookSlug, string $pageSlug)
212 $this->validate($request, [
213 'name' => ['required', 'string', 'max:255'],
215 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
216 $this->checkOwnablePermission('page-update', $page);
218 $this->pageRepo->update($page, $request->all());
220 return redirect($page->getUrl());
224 * Save a draft update as a revision.
226 * @throws NotFoundException
228 public function saveDraft(Request $request, int $pageId)
230 $page = $this->pageRepo->getById($pageId);
231 $this->checkOwnablePermission('page-update', $page);
233 if (!$this->isSignedIn()) {
234 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
237 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
238 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
240 return response()->json([
241 'status' => 'success',
242 'message' => trans('entities.pages_edit_draft_save_at'),
243 'warning' => implode("\n", $warnings),
244 'timestamp' => $draft->updated_at->timestamp,
249 * Redirect from a special link url which uses the page id rather than the name.
251 * @throws NotFoundException
253 public function redirectFromLink(int $pageId)
255 $page = $this->pageRepo->getById($pageId);
257 return redirect($page->getUrl());
261 * Show the deletion page for the specified page.
263 * @throws NotFoundException
265 public function showDelete(string $bookSlug, string $pageSlug)
267 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
268 $this->checkOwnablePermission('page-delete', $page);
269 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
271 return view('pages.delete', [
272 'book' => $page->book,
279 * Show the deletion page for the specified page.
281 * @throws NotFoundException
283 public function showDeleteDraft(string $bookSlug, int $pageId)
285 $page = $this->pageRepo->getById($pageId);
286 $this->checkOwnablePermission('page-update', $page);
287 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
289 return view('pages.delete', [
290 'book' => $page->book,
297 * Remove the specified page from storage.
299 * @throws NotFoundException
302 public function destroy(string $bookSlug, string $pageSlug)
304 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
305 $this->checkOwnablePermission('page-delete', $page);
306 $parent = $page->getParent();
308 $this->pageRepo->destroy($page);
310 return redirect($parent->getUrl());
314 * Remove the specified draft page from storage.
316 * @throws NotFoundException
319 public function destroyDraft(string $bookSlug, int $pageId)
321 $page = $this->pageRepo->getById($pageId);
323 $chapter = $page->chapter;
324 $this->checkOwnablePermission('page-update', $page);
326 $this->pageRepo->destroy($page);
328 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
330 if ($chapter && userCan('view', $chapter)) {
331 return redirect($chapter->getUrl());
334 return redirect($book->getUrl());
338 * Show a listing of recently created pages.
340 public function showRecentlyUpdated()
342 $visibleBelongsScope = function (BelongsTo $query) {
343 $query->scopes('visible');
346 $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
347 ->orderBy('updated_at', 'desc')
349 ->setPath(url('/pages/recently-updated'));
351 $this->setPageTitle(trans('entities.recently_updated_pages'));
353 return view('common.detailed-listing-paginated', [
354 'title' => trans('entities.recently_updated_pages'),
355 'entities' => $pages,
356 'showUpdatedBy' => true,
362 * Show the view to choose a new parent to move a page into.
364 * @throws NotFoundException
366 public function showMove(string $bookSlug, string $pageSlug)
368 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
369 $this->checkOwnablePermission('page-update', $page);
370 $this->checkOwnablePermission('page-delete', $page);
372 return view('pages.move', [
373 'book' => $page->book,
379 * Does the action of moving the location of a page.
381 * @throws NotFoundException
384 public function move(Request $request, string $bookSlug, string $pageSlug)
386 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
387 $this->checkOwnablePermission('page-update', $page);
388 $this->checkOwnablePermission('page-delete', $page);
390 $entitySelection = $request->get('entity_selection', null);
391 if ($entitySelection === null || $entitySelection === '') {
392 return redirect($page->getUrl());
396 $parent = $this->pageRepo->move($page, $entitySelection);
397 } catch (PermissionsException $exception) {
398 $this->showPermissionError();
399 } catch (Exception $exception) {
400 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
402 return redirect()->back();
405 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
407 return redirect($page->getUrl());
411 * Show the view to copy a page.
413 * @throws NotFoundException
415 public function showCopy(string $bookSlug, string $pageSlug)
417 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
418 $this->checkOwnablePermission('page-view', $page);
419 session()->flashInput(['name' => $page->name]);
421 return view('pages.copy', [
422 'book' => $page->book,
428 * Create a copy of a page within the requested target destination.
430 * @throws NotFoundException
433 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
435 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
436 $this->checkOwnablePermission('page-view', $page);
438 $entitySelection = $request->get('entity_selection') ?: null;
439 $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
441 if (is_null($newParent)) {
442 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
444 return redirect()->back();
447 $this->checkOwnablePermission('page-create', $newParent);
449 $newName = $request->get('name') ?: $page->name;
450 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
451 $this->showSuccessNotification(trans('entities.pages_copy_success'));
453 return redirect($pageCopy->getUrl());