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 public function __construct(
28 protected PageRepo $pageRepo,
29 protected ReferenceFetcher $referenceFetcher
34 * Show the form for creating a new page.
38 public function create(string $bookSlug, string $chapterSlug = null)
40 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
41 $this->checkOwnablePermission('page-create', $parent);
43 // Redirect to draft edit screen if signed in
44 if ($this->isSignedIn()) {
45 $draft = $this->pageRepo->getNewDraftPage($parent);
47 return redirect($draft->getUrl());
50 // Otherwise show the edit view if they're a guest
51 $this->setPageTitle(trans('entities.pages_new'));
53 return view('pages.guest-create', ['parent' => $parent]);
57 * Create a new page as a guest user.
59 * @throws ValidationException
61 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
63 $this->validate($request, [
64 'name' => ['required', 'string', 'max:255'],
67 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
68 $this->checkOwnablePermission('page-create', $parent);
70 $page = $this->pageRepo->getNewDraftPage($parent);
71 $this->pageRepo->publishDraft($page, [
72 'name' => $request->get('name'),
76 return redirect($page->getUrl('/edit'));
80 * Show form to continue editing a draft page.
82 * @throws NotFoundException
84 public function editDraft(Request $request, string $bookSlug, int $pageId)
86 $draft = $this->pageRepo->getById($pageId);
87 $this->checkOwnablePermission('page-create', $draft->getParent());
89 $editorData = new PageEditorData($draft, $this->pageRepo, $request->query('editor', ''));
90 $this->setPageTitle(trans('entities.pages_edit_draft'));
92 return view('pages.edit', $editorData->getViewData());
96 * Store a new page by changing a draft into a page.
98 * @throws NotFoundException
99 * @throws ValidationException
101 public function store(Request $request, string $bookSlug, int $pageId)
103 $this->validate($request, [
104 'name' => ['required', 'string', 'max:255'],
106 $draftPage = $this->pageRepo->getById($pageId);
107 $this->checkOwnablePermission('page-create', $draftPage->getParent());
109 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
111 return redirect($page->getUrl());
115 * Display the specified page.
116 * If the page is not found via the slug the revisions are searched for a match.
118 * @throws NotFoundException
120 public function show(string $bookSlug, string $pageSlug)
123 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
124 } catch (NotFoundException $e) {
125 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
127 if ($page === null) {
131 return redirect($page->getUrl());
134 $this->checkOwnablePermission('page-view', $page);
136 $pageContent = (new PageContent($page));
137 $page->html = $pageContent->render();
138 $pageNav = $pageContent->getNavigation($page->html);
140 $sidebarTree = (new BookContents($page->book))->getTree();
141 $commentTree = (new CommentTree($page));
142 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
144 View::incrementFor($page);
145 $this->setPageTitle($page->getShortName());
147 return view('pages.show', [
149 'book' => $page->book,
151 'sidebarTree' => $sidebarTree,
152 'commentTree' => $commentTree,
153 'pageNav' => $pageNav,
154 'next' => $nextPreviousLocator->getNext(),
155 'previous' => $nextPreviousLocator->getPrevious(),
156 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($page),
161 * Get page from an ajax request.
163 * @throws NotFoundException
165 public function getPageAjax(int $pageId)
167 $page = $this->pageRepo->getById($pageId);
168 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
169 $page->makeHidden(['book']);
171 return response()->json($page);
175 * Show the form for editing the specified page.
177 * @throws NotFoundException
179 public function edit(Request $request, string $bookSlug, string $pageSlug)
181 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
182 $this->checkOwnablePermission('page-update', $page);
184 $editorData = new PageEditorData($page, $this->pageRepo, $request->query('editor', ''));
185 if ($editorData->getWarnings()) {
186 $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
189 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
191 return view('pages.edit', $editorData->getViewData());
195 * Update the specified page in storage.
197 * @throws ValidationException
198 * @throws NotFoundException
200 public function update(Request $request, string $bookSlug, string $pageSlug)
202 $this->validate($request, [
203 'name' => ['required', 'string', 'max:255'],
205 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
206 $this->checkOwnablePermission('page-update', $page);
208 $this->pageRepo->update($page, $request->all());
210 return redirect($page->getUrl());
214 * Save a draft update as a revision.
216 * @throws NotFoundException
218 public function saveDraft(Request $request, int $pageId)
220 $page = $this->pageRepo->getById($pageId);
221 $this->checkOwnablePermission('page-update', $page);
223 if (!$this->isSignedIn()) {
224 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
227 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
228 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
230 return response()->json([
231 'status' => 'success',
232 'message' => trans('entities.pages_edit_draft_save_at'),
233 'warning' => implode("\n", $warnings),
234 'timestamp' => $draft->updated_at->timestamp,
239 * Redirect from a special link url which uses the page id rather than the name.
241 * @throws NotFoundException
243 public function redirectFromLink(int $pageId)
245 $page = $this->pageRepo->getById($pageId);
247 return redirect($page->getUrl());
251 * Show the deletion page for the specified page.
253 * @throws NotFoundException
255 public function showDelete(string $bookSlug, string $pageSlug)
257 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
258 $this->checkOwnablePermission('page-delete', $page);
259 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
261 return view('pages.delete', [
262 'book' => $page->book,
269 * Show the deletion page for the specified page.
271 * @throws NotFoundException
273 public function showDeleteDraft(string $bookSlug, int $pageId)
275 $page = $this->pageRepo->getById($pageId);
276 $this->checkOwnablePermission('page-update', $page);
277 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
279 return view('pages.delete', [
280 'book' => $page->book,
287 * Remove the specified page from storage.
289 * @throws NotFoundException
292 public function destroy(string $bookSlug, string $pageSlug)
294 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
295 $this->checkOwnablePermission('page-delete', $page);
296 $parent = $page->getParent();
298 $this->pageRepo->destroy($page);
300 return redirect($parent->getUrl());
304 * Remove the specified draft page from storage.
306 * @throws NotFoundException
309 public function destroyDraft(string $bookSlug, int $pageId)
311 $page = $this->pageRepo->getById($pageId);
313 $chapter = $page->chapter;
314 $this->checkOwnablePermission('page-update', $page);
316 $this->pageRepo->destroy($page);
318 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
320 if ($chapter && userCan('view', $chapter)) {
321 return redirect($chapter->getUrl());
324 return redirect($book->getUrl());
328 * Show a listing of recently created pages.
330 public function showRecentlyUpdated()
332 $visibleBelongsScope = function (BelongsTo $query) {
333 $query->scopes('visible');
336 $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
337 ->orderBy('updated_at', 'desc')
339 ->setPath(url('/pages/recently-updated'));
341 $this->setPageTitle(trans('entities.recently_updated_pages'));
343 return view('common.detailed-listing-paginated', [
344 'title' => trans('entities.recently_updated_pages'),
345 'entities' => $pages,
346 'showUpdatedBy' => true,
352 * Show the view to choose a new parent to move a page into.
354 * @throws NotFoundException
356 public function showMove(string $bookSlug, string $pageSlug)
358 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
359 $this->checkOwnablePermission('page-update', $page);
360 $this->checkOwnablePermission('page-delete', $page);
362 return view('pages.move', [
363 'book' => $page->book,
369 * Does the action of moving the location of a page.
371 * @throws NotFoundException
374 public function move(Request $request, string $bookSlug, string $pageSlug)
376 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
377 $this->checkOwnablePermission('page-update', $page);
378 $this->checkOwnablePermission('page-delete', $page);
380 $entitySelection = $request->get('entity_selection', null);
381 if ($entitySelection === null || $entitySelection === '') {
382 return redirect($page->getUrl());
386 $this->pageRepo->move($page, $entitySelection);
387 } catch (PermissionsException $exception) {
388 $this->showPermissionError();
389 } catch (Exception $exception) {
390 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
392 return redirect()->back();
395 return redirect($page->getUrl());
399 * Show the view to copy a page.
401 * @throws NotFoundException
403 public function showCopy(string $bookSlug, string $pageSlug)
405 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
406 $this->checkOwnablePermission('page-view', $page);
407 session()->flashInput(['name' => $page->name]);
409 return view('pages.copy', [
410 'book' => $page->book,
416 * Create a copy of a page within the requested target destination.
418 * @throws NotFoundException
421 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
423 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
424 $this->checkOwnablePermission('page-view', $page);
426 $entitySelection = $request->get('entity_selection') ?: null;
427 $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
429 if (is_null($newParent)) {
430 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
432 return redirect()->back();
435 $this->checkOwnablePermission('page-create', $newParent);
437 $newName = $request->get('name') ?: $page->name;
438 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
439 $this->showSuccessNotification(trans('entities.pages_copy_success'));
441 return redirect($pageCopy->getUrl());