3 namespace BookStack\Entities\Controllers;
5 use BookStack\Activity\Models\View;
6 use BookStack\Activity\Tools\CommentTree;
7 use BookStack\Activity\Tools\UserEntityWatchOptions;
8 use BookStack\Entities\Models\Book;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Repos\PageRepo;
11 use BookStack\Entities\Tools\BookContents;
12 use BookStack\Entities\Tools\Cloner;
13 use BookStack\Entities\Tools\NextPreviousContentLocator;
14 use BookStack\Entities\Tools\PageContent;
15 use BookStack\Entities\Tools\PageEditActivity;
16 use BookStack\Entities\Tools\PageEditorData;
17 use BookStack\Exceptions\NotFoundException;
18 use BookStack\Exceptions\PermissionsException;
19 use BookStack\Http\Controller;
20 use BookStack\References\ReferenceFetcher;
22 use Illuminate\Database\Eloquent\Relations\BelongsTo;
23 use Illuminate\Http\Request;
24 use Illuminate\Validation\ValidationException;
27 class PageController extends Controller
29 public function __construct(
30 protected PageRepo $pageRepo,
31 protected ReferenceFetcher $referenceFetcher
36 * Show the form for creating a new page.
40 public function create(string $bookSlug, string $chapterSlug = null)
42 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
43 $this->checkOwnablePermission('page-create', $parent);
45 // Redirect to draft edit screen if signed in
46 if ($this->isSignedIn()) {
47 $draft = $this->pageRepo->getNewDraftPage($parent);
49 return redirect($draft->getUrl());
52 // Otherwise show the edit view if they're a guest
53 $this->setPageTitle(trans('entities.pages_new'));
55 return view('pages.guest-create', ['parent' => $parent]);
59 * Create a new page as a guest user.
61 * @throws ValidationException
63 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
65 $this->validate($request, [
66 'name' => ['required', 'string', 'max:255'],
69 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
70 $this->checkOwnablePermission('page-create', $parent);
72 $page = $this->pageRepo->getNewDraftPage($parent);
73 $this->pageRepo->publishDraft($page, [
74 '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(Request $request, string $bookSlug, int $pageId)
87 $draft = $this->pageRepo->getById($pageId);
88 $this->checkOwnablePermission('page-create', $draft->getParent());
90 $editorData = new PageEditorData($draft, $this->pageRepo, $request->query('editor', ''));
91 $this->setPageTitle(trans('entities.pages_edit_draft'));
93 return view('pages.edit', $editorData->getViewData());
97 * Store a new page by changing a draft into a page.
99 * @throws NotFoundException
100 * @throws ValidationException
102 public function store(Request $request, string $bookSlug, int $pageId)
104 $this->validate($request, [
105 'name' => ['required', 'string', 'max:255'],
107 $draftPage = $this->pageRepo->getById($pageId);
108 $this->checkOwnablePermission('page-create', $draftPage->getParent());
110 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
112 return redirect($page->getUrl());
116 * Display the specified page.
117 * If the page is not found via the slug the revisions are searched for a match.
119 * @throws NotFoundException
121 public function show(string $bookSlug, string $pageSlug)
124 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
125 } catch (NotFoundException $e) {
126 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
128 if ($page === null) {
132 return redirect($page->getUrl());
135 $this->checkOwnablePermission('page-view', $page);
137 $pageContent = (new PageContent($page));
138 $page->html = $pageContent->render();
139 $pageNav = $pageContent->getNavigation($page->html);
141 $sidebarTree = (new BookContents($page->book))->getTree();
142 $commentTree = (new CommentTree($page));
143 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
145 View::incrementFor($page);
146 $this->setPageTitle($page->getShortName());
148 return view('pages.show', [
150 'book' => $page->book,
152 'sidebarTree' => $sidebarTree,
153 'commentTree' => $commentTree,
154 'pageNav' => $pageNav,
155 'watchOptions' => new UserEntityWatchOptions(user(), $page),
156 'next' => $nextPreviousLocator->getNext(),
157 'previous' => $nextPreviousLocator->getPrevious(),
158 'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($page),
163 * Get page from an ajax request.
165 * @throws NotFoundException
167 public function getPageAjax(int $pageId)
169 $page = $this->pageRepo->getById($pageId);
170 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
171 $page->makeHidden(['book']);
173 return response()->json($page);
177 * Show the form for editing the specified page.
179 * @throws NotFoundException
181 public function edit(Request $request, string $bookSlug, string $pageSlug)
183 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
184 $this->checkOwnablePermission('page-update', $page);
186 $editorData = new PageEditorData($page, $this->pageRepo, $request->query('editor', ''));
187 if ($editorData->getWarnings()) {
188 $this->showWarningNotification(implode("\n", $editorData->getWarnings()));
191 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
193 return view('pages.edit', $editorData->getViewData());
197 * Update the specified page in storage.
199 * @throws ValidationException
200 * @throws NotFoundException
202 public function update(Request $request, string $bookSlug, string $pageSlug)
204 $this->validate($request, [
205 'name' => ['required', 'string', 'max:255'],
207 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
208 $this->checkOwnablePermission('page-update', $page);
210 $this->pageRepo->update($page, $request->all());
212 return redirect($page->getUrl());
216 * Save a draft update as a revision.
218 * @throws NotFoundException
220 public function saveDraft(Request $request, int $pageId)
222 $page = $this->pageRepo->getById($pageId);
223 $this->checkOwnablePermission('page-update', $page);
225 if (!$this->isSignedIn()) {
226 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
229 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
230 $warnings = (new PageEditActivity($page))->getWarningMessagesForDraft($draft);
232 return response()->json([
233 'status' => 'success',
234 'message' => trans('entities.pages_edit_draft_save_at'),
235 'warning' => implode("\n", $warnings),
236 'timestamp' => $draft->updated_at->timestamp,
241 * Redirect from a special link url which uses the page id rather than the name.
243 * @throws NotFoundException
245 public function redirectFromLink(int $pageId)
247 $page = $this->pageRepo->getById($pageId);
249 return redirect($page->getUrl());
253 * Show the deletion page for the specified page.
255 * @throws NotFoundException
257 public function showDelete(string $bookSlug, string $pageSlug)
259 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
260 $this->checkOwnablePermission('page-delete', $page);
261 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
262 $times_used_as_template = Book::where('default_template', '=', $page->id)->count();
264 return view('pages.delete', [
265 'book' => $page->book,
268 'times_used_as_template' => $times_used_as_template,
273 * Show the deletion page for the specified page.
275 * @throws NotFoundException
277 public function showDeleteDraft(string $bookSlug, int $pageId)
279 $page = $this->pageRepo->getById($pageId);
280 $this->checkOwnablePermission('page-update', $page);
281 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
283 return view('pages.delete', [
284 'book' => $page->book,
291 * Remove the specified page from storage.
293 * @throws NotFoundException
296 public function destroy(string $bookSlug, string $pageSlug)
298 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
299 $this->checkOwnablePermission('page-delete', $page);
300 $parent = $page->getParent();
302 $this->pageRepo->destroy($page);
304 return redirect($parent->getUrl());
308 * Remove the specified draft page from storage.
310 * @throws NotFoundException
313 public function destroyDraft(string $bookSlug, int $pageId)
315 $page = $this->pageRepo->getById($pageId);
317 $chapter = $page->chapter;
318 $this->checkOwnablePermission('page-update', $page);
320 $this->pageRepo->destroy($page);
322 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
324 if ($chapter && userCan('view', $chapter)) {
325 return redirect($chapter->getUrl());
328 return redirect($book->getUrl());
332 * Show a listing of recently created pages.
334 public function showRecentlyUpdated()
336 $visibleBelongsScope = function (BelongsTo $query) {
337 $query->scopes('visible');
340 $pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
341 ->orderBy('updated_at', 'desc')
343 ->setPath(url('/pages/recently-updated'));
345 $this->setPageTitle(trans('entities.recently_updated_pages'));
347 return view('common.detailed-listing-paginated', [
348 'title' => trans('entities.recently_updated_pages'),
349 'entities' => $pages,
350 'showUpdatedBy' => true,
356 * Show the view to choose a new parent to move a page into.
358 * @throws NotFoundException
360 public function showMove(string $bookSlug, string $pageSlug)
362 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
363 $this->checkOwnablePermission('page-update', $page);
364 $this->checkOwnablePermission('page-delete', $page);
366 return view('pages.move', [
367 'book' => $page->book,
373 * Does the action of moving the location of a page.
375 * @throws NotFoundException
378 public function move(Request $request, string $bookSlug, string $pageSlug)
380 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
381 $this->checkOwnablePermission('page-update', $page);
382 $this->checkOwnablePermission('page-delete', $page);
384 $entitySelection = $request->get('entity_selection', null);
385 if ($entitySelection === null || $entitySelection === '') {
386 return redirect($page->getUrl());
390 $this->pageRepo->move($page, $entitySelection);
391 } catch (PermissionsException $exception) {
392 $this->showPermissionError();
393 } catch (Exception $exception) {
394 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
396 return redirect($page->getUrl('/move'));
399 return redirect($page->getUrl());
403 * Show the view to copy a page.
405 * @throws NotFoundException
407 public function showCopy(string $bookSlug, string $pageSlug)
409 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
410 $this->checkOwnablePermission('page-view', $page);
411 session()->flashInput(['name' => $page->name]);
413 return view('pages.copy', [
414 'book' => $page->book,
420 * Create a copy of a page within the requested target destination.
422 * @throws NotFoundException
425 public function copy(Request $request, Cloner $cloner, string $bookSlug, string $pageSlug)
427 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
428 $this->checkOwnablePermission('page-view', $page);
430 $entitySelection = $request->get('entity_selection') ?: null;
431 $newParent = $entitySelection ? $this->pageRepo->findParentByIdentifier($entitySelection) : $page->getParent();
433 if (is_null($newParent)) {
434 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
436 return redirect($page->getUrl('/copy'));
439 $this->checkOwnablePermission('page-create', $newParent);
441 $newName = $request->get('name') ?: $page->name;
442 $pageCopy = $cloner->clonePage($page, $newParent, $newName);
443 $this->showSuccessNotification(trans('entities.pages_copy_success'));
445 return redirect($pageCopy->getUrl());