1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Actions\View;
4 use BookStack\Entities\Tools\BookContents;
5 use BookStack\Entities\Tools\NextPreviousContentLocator;
6 use BookStack\Entities\Tools\PageContent;
7 use BookStack\Entities\Tools\PageEditActivity;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Repos\PageRepo;
10 use BookStack\Entities\Tools\PermissionsUpdater;
11 use BookStack\Exceptions\NotFoundException;
12 use BookStack\Exceptions\PermissionsException;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
18 class PageController extends Controller
24 * PageController constructor.
26 public function __construct(PageRepo $pageRepo)
28 $this->pageRepo = $pageRepo;
32 * Show the form for creating a new page.
35 public function create(string $bookSlug, string $chapterSlug = null)
37 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
38 $this->checkOwnablePermission('page-create', $parent);
40 // Redirect to draft edit screen if signed in
41 if ($this->isSignedIn()) {
42 $draft = $this->pageRepo->getNewDraftPage($parent);
43 return redirect($draft->getUrl());
46 // Otherwise show the edit view if they're a guest
47 $this->setPageTitle(trans('entities.pages_new'));
48 return view('pages.guest-create', ['parent' => $parent]);
52 * Create a new page as a guest user.
53 * @throws ValidationException
55 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
57 $this->validate($request, [
58 'name' => 'required|string|max:255'
61 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
62 $this->checkOwnablePermission('page-create', $parent);
64 $page = $this->pageRepo->getNewDraftPage($parent);
65 $this->pageRepo->publishDraft($page, [
66 'name' => $request->get('name'),
70 return redirect($page->getUrl('/edit'));
74 * Show form to continue editing a draft page.
75 * @throws NotFoundException
77 public function editDraft(string $bookSlug, int $pageId)
79 $draft = $this->pageRepo->getById($pageId);
80 $this->checkOwnablePermission('page-create', $draft->getParent());
81 $this->setPageTitle(trans('entities.pages_edit_draft'));
83 $draftsEnabled = $this->isSignedIn();
84 $templates = $this->pageRepo->getTemplates(10);
86 return view('pages.edit', [
88 'book' => $draft->book,
90 'draftsEnabled' => $draftsEnabled,
91 'templates' => $templates,
96 * Store a new page by changing a draft into a page.
97 * @throws NotFoundException
98 * @throws ValidationException
100 public function store(Request $request, string $bookSlug, int $pageId)
102 $this->validate($request, [
103 'name' => 'required|string|max:255'
105 $draftPage = $this->pageRepo->getById($pageId);
106 $this->checkOwnablePermission('page-create', $draftPage->getParent());
108 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
110 return redirect($page->getUrl());
114 * Display the specified page.
115 * If the page is not found via the slug the revisions are searched for a match.
116 * @throws NotFoundException
118 public function show(string $bookSlug, string $pageSlug)
121 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
122 } catch (NotFoundException $e) {
123 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
125 if ($page === null) {
129 return redirect($page->getUrl());
132 $this->checkOwnablePermission('page-view', $page);
134 $pageContent = (new PageContent($page));
135 $page->html = $pageContent->render();
136 $sidebarTree = (new BookContents($page->book))->getTree();
137 $pageNav = $pageContent->getNavigation($page->html);
139 // Check if page comments are enabled
140 $commentsEnabled = !setting('app-disable-comments');
141 if ($commentsEnabled) {
142 $page->load(['comments.createdBy']);
145 $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
147 View::incrementFor($page);
148 $this->setPageTitle($page->getShortName());
149 return view('pages.show', [
151 'book' => $page->book,
153 'sidebarTree' => $sidebarTree,
154 'commentsEnabled' => $commentsEnabled,
155 'pageNav' => $pageNav,
156 'next' => $nextPreviousLocator->getNext(),
157 'previous' => $nextPreviousLocator->getPrevious(),
162 * 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->addHidden(['book']);
170 return response()->json($page);
174 * Show the form for editing the specified page.
175 * @throws NotFoundException
177 public function edit(string $bookSlug, string $pageSlug)
179 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
180 $this->checkOwnablePermission('page-update', $page);
182 $page->isDraft = false;
183 $editActivity = new PageEditActivity($page);
185 // Check for active editing
187 if ($editActivity->hasActiveEditing()) {
188 $warnings[] = $editActivity->activeEditingMessage();
191 // Check for a current draft version for this user
192 $userDraft = $this->pageRepo->getUserDraft($page);
193 if ($userDraft !== null) {
194 $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
195 $page->isDraft = true;
196 $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
199 if (count($warnings) > 0) {
200 $this->showWarningNotification(implode("\n", $warnings));
203 $templates = $this->pageRepo->getTemplates(10);
204 $draftsEnabled = $this->isSignedIn();
205 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
206 return view('pages.edit', [
208 'book' => $page->book,
210 'draftsEnabled' => $draftsEnabled,
211 'templates' => $templates,
216 * Update the specified page in storage.
217 * @throws ValidationException
218 * @throws NotFoundException
220 public function update(Request $request, string $bookSlug, string $pageSlug)
222 $this->validate($request, [
223 'name' => 'required|string|max:255'
225 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
226 $this->checkOwnablePermission('page-update', $page);
228 $this->pageRepo->update($page, $request->all());
230 return redirect($page->getUrl());
234 * Save a draft update as a revision.
235 * @throws NotFoundException
237 public function saveDraft(Request $request, int $pageId)
239 $page = $this->pageRepo->getById($pageId);
240 $this->checkOwnablePermission('page-update', $page);
242 if (!$this->isSignedIn()) {
243 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
246 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
248 $updateTime = $draft->updated_at->timestamp;
249 return response()->json([
250 'status' => 'success',
251 'message' => trans('entities.pages_edit_draft_save_at'),
252 'timestamp' => $updateTime
257 * Redirect from a special link url which uses the page id rather than the name.
258 * @throws NotFoundException
260 public function redirectFromLink(int $pageId)
262 $page = $this->pageRepo->getById($pageId);
263 return redirect($page->getUrl());
267 * Show the deletion page for the specified page.
268 * @throws NotFoundException
270 public function showDelete(string $bookSlug, string $pageSlug)
272 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
273 $this->checkOwnablePermission('page-delete', $page);
274 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
275 return view('pages.delete', [
276 'book' => $page->book,
283 * Show the deletion page for the specified page.
284 * @throws NotFoundException
286 public function showDeleteDraft(string $bookSlug, int $pageId)
288 $page = $this->pageRepo->getById($pageId);
289 $this->checkOwnablePermission('page-update', $page);
290 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
291 return view('pages.delete', [
292 'book' => $page->book,
299 * 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.
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());
333 return redirect($book->getUrl());
337 * Show a listing of recently created pages.
339 public function showRecentlyUpdated()
341 $pages = Page::visible()->orderBy('updated_at', 'desc')
343 ->setPath(url('/pages/recently-updated'));
345 return view('common.detailed-listing-paginated', [
346 'title' => trans('entities.recently_updated_pages'),
352 * Show the view to choose a new parent to move a page into.
353 * @throws NotFoundException
355 public function showMove(string $bookSlug, string $pageSlug)
357 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
358 $this->checkOwnablePermission('page-update', $page);
359 $this->checkOwnablePermission('page-delete', $page);
360 return view('pages.move', [
361 'book' => $page->book,
367 * Does the action of moving the location of a page.
368 * @throws NotFoundException
371 public function move(Request $request, string $bookSlug, string $pageSlug)
373 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
374 $this->checkOwnablePermission('page-update', $page);
375 $this->checkOwnablePermission('page-delete', $page);
377 $entitySelection = $request->get('entity_selection', null);
378 if ($entitySelection === null || $entitySelection === '') {
379 return redirect($page->getUrl());
383 $parent = $this->pageRepo->move($page, $entitySelection);
384 } catch (Exception $exception) {
385 if ($exception instanceof PermissionsException) {
386 $this->showPermissionError();
389 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
390 return redirect()->back();
393 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
394 return redirect($page->getUrl());
398 * Show the view to copy a page.
399 * @throws NotFoundException
401 public function showCopy(string $bookSlug, string $pageSlug)
403 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
404 $this->checkOwnablePermission('page-view', $page);
405 session()->flashInput(['name' => $page->name]);
406 return view('pages.copy', [
407 'book' => $page->book,
414 * Create a copy of a page within the requested target destination.
415 * @throws NotFoundException
418 public function copy(Request $request, string $bookSlug, string $pageSlug)
420 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
421 $this->checkOwnablePermission('page-view', $page);
423 $entitySelection = $request->get('entity_selection', null) ?? null;
424 $newName = $request->get('name', null);
427 $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
428 } catch (Exception $exception) {
429 if ($exception instanceof PermissionsException) {
430 $this->showPermissionError();
433 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
434 return redirect()->back();
437 $this->showSuccessNotification(trans('entities.pages_copy_success'));
438 return redirect($pageCopy->getUrl());
442 * Show the Permissions view.
443 * @throws NotFoundException
445 public function showPermissions(string $bookSlug, string $pageSlug)
447 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
448 $this->checkOwnablePermission('restrictions-manage', $page);
449 return view('pages.permissions', [
455 * Set the permissions for this page.
456 * @throws NotFoundException
459 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
461 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
462 $this->checkOwnablePermission('restrictions-manage', $page);
464 $permissionsUpdater->updateFromPermissionsForm($page, $request);
466 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
467 return redirect($page->getUrl());