1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Entities\Tools\BookContents;
4 use BookStack\Entities\Tools\PageContent;
5 use BookStack\Entities\Tools\PageEditActivity;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Entities\Tools\PermissionsUpdater;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Exceptions\PermissionsException;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
17 class PageController extends Controller
23 * PageController constructor.
25 public function __construct(PageRepo $pageRepo)
27 $this->pageRepo = $pageRepo;
31 * Show the form for creating a new page.
34 public function create(string $bookSlug, string $chapterSlug = null)
36 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
37 $this->checkOwnablePermission('page-create', $parent);
39 // Redirect to draft edit screen if signed in
40 if ($this->isSignedIn()) {
41 $draft = $this->pageRepo->getNewDraftPage($parent);
42 return redirect($draft->getUrl());
45 // Otherwise show the edit view if they're a guest
46 $this->setPageTitle(trans('entities.pages_new'));
47 return view('pages.guest-create', ['parent' => $parent]);
51 * Create a new page as a guest user.
52 * @throws ValidationException
54 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
56 $this->validate($request, [
57 'name' => 'required|string|max:255'
60 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
61 $this->checkOwnablePermission('page-create', $parent);
63 $page = $this->pageRepo->getNewDraftPage($parent);
64 $this->pageRepo->publishDraft($page, [
65 'name' => $request->get('name'),
69 return redirect($page->getUrl('/edit'));
73 * Show form to continue editing a draft page.
74 * @throws NotFoundException
76 public function editDraft(string $bookSlug, int $pageId)
78 $draft = $this->pageRepo->getById($pageId);
79 $this->checkOwnablePermission('page-create', $draft->getParent());
80 $this->setPageTitle(trans('entities.pages_edit_draft'));
82 $draftsEnabled = $this->isSignedIn();
83 $templates = $this->pageRepo->getTemplates(10);
85 return view('pages.edit', [
87 'book' => $draft->book,
89 'draftsEnabled' => $draftsEnabled,
90 'templates' => $templates,
95 * Store a new page by changing a draft into a page.
96 * @throws NotFoundException
97 * @throws ValidationException
99 public function store(Request $request, string $bookSlug, int $pageId)
101 $this->validate($request, [
102 'name' => 'required|string|max:255'
104 $draftPage = $this->pageRepo->getById($pageId);
105 $this->checkOwnablePermission('page-create', $draftPage->getParent());
107 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
109 return redirect($page->getUrl());
113 * Display the specified page.
114 * If the page is not found via the slug the revisions are searched for a match.
115 * @throws NotFoundException
117 public function show(string $bookSlug, string $pageSlug)
120 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
121 } catch (NotFoundException $e) {
122 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
124 if ($page === null) {
128 return redirect($page->getUrl());
131 $this->checkOwnablePermission('page-view', $page);
133 $pageContent = (new PageContent($page));
134 $page->html = $pageContent->render();
135 $sidebarTree = (new BookContents($page->book))->getTree();
136 $pageNav = $pageContent->getNavigation($page->html);
138 // Check if page comments are enabled
139 $commentsEnabled = !setting('app-disable-comments');
140 if ($commentsEnabled) {
141 $page->load(['comments.createdBy']);
145 $this->setPageTitle($page->getShortName());
146 return view('pages.show', [
148 'book' => $page->book,
150 'sidebarTree' => $sidebarTree,
151 'commentsEnabled' => $commentsEnabled,
152 'pageNav' => $pageNav
157 * Get page from an ajax request.
158 * @throws NotFoundException
160 public function getPageAjax(int $pageId)
162 $page = $this->pageRepo->getById($pageId);
163 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
164 $page->addHidden(['book']);
165 return response()->json($page);
169 * Show the form for editing the specified page.
170 * @throws NotFoundException
172 public function edit(string $bookSlug, string $pageSlug)
174 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
175 $this->checkOwnablePermission('page-update', $page);
177 $page->isDraft = false;
178 $editActivity = new PageEditActivity($page);
180 // Check for active editing
182 if ($editActivity->hasActiveEditing()) {
183 $warnings[] = $editActivity->activeEditingMessage();
186 // Check for a current draft version for this user
187 $userDraft = $this->pageRepo->getUserDraft($page);
188 if ($userDraft !== null) {
189 $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
190 $page->isDraft = true;
191 $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
194 if (count($warnings) > 0) {
195 $this->showWarningNotification(implode("\n", $warnings));
198 $templates = $this->pageRepo->getTemplates(10);
199 $draftsEnabled = $this->isSignedIn();
200 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
201 return view('pages.edit', [
203 'book' => $page->book,
205 'draftsEnabled' => $draftsEnabled,
206 'templates' => $templates,
211 * Update the specified page in storage.
212 * @throws ValidationException
213 * @throws NotFoundException
215 public function update(Request $request, string $bookSlug, string $pageSlug)
217 $this->validate($request, [
218 'name' => 'required|string|max:255'
220 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
221 $this->checkOwnablePermission('page-update', $page);
223 $this->pageRepo->update($page, $request->all());
225 return redirect($page->getUrl());
229 * Save a draft update as a revision.
230 * @throws NotFoundException
232 public function saveDraft(Request $request, int $pageId)
234 $page = $this->pageRepo->getById($pageId);
235 $this->checkOwnablePermission('page-update', $page);
237 if (!$this->isSignedIn()) {
238 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
241 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
243 $updateTime = $draft->updated_at->timestamp;
244 return response()->json([
245 'status' => 'success',
246 'message' => trans('entities.pages_edit_draft_save_at'),
247 'timestamp' => $updateTime
252 * Redirect from a special link url which uses the page id rather than the name.
253 * @throws NotFoundException
255 public function redirectFromLink(int $pageId)
257 $page = $this->pageRepo->getById($pageId);
258 return redirect($page->getUrl());
262 * 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()]));
270 return view('pages.delete', [
271 'book' => $page->book,
278 * Show the deletion page for the specified page.
279 * @throws NotFoundException
281 public function showDeleteDraft(string $bookSlug, int $pageId)
283 $page = $this->pageRepo->getById($pageId);
284 $this->checkOwnablePermission('page-update', $page);
285 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
286 return view('pages.delete', [
287 'book' => $page->book,
294 * 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.
311 * @throws NotFoundException
314 public function destroyDraft(string $bookSlug, int $pageId)
316 $page = $this->pageRepo->getById($pageId);
318 $chapter = $page->chapter;
319 $this->checkOwnablePermission('page-update', $page);
321 $this->pageRepo->destroy($page);
323 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
325 if ($chapter && userCan('view', $chapter)) {
326 return redirect($chapter->getUrl());
328 return redirect($book->getUrl());
332 * Show a listing of recently created pages.
334 public function showRecentlyUpdated()
336 $pages = Page::visible()->orderBy('updated_at', 'desc')
338 ->setPath(url('/pages/recently-updated'));
340 return view('pages.detailed-listing', [
341 'title' => trans('entities.recently_updated_pages'),
347 * Show the view to choose a new parent to move a page into.
348 * @throws NotFoundException
350 public function showMove(string $bookSlug, string $pageSlug)
352 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
353 $this->checkOwnablePermission('page-update', $page);
354 $this->checkOwnablePermission('page-delete', $page);
355 return view('pages.move', [
356 'book' => $page->book,
362 * Does the action of moving the location of a page.
363 * @throws NotFoundException
366 public function move(Request $request, string $bookSlug, string $pageSlug)
368 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
369 $this->checkOwnablePermission('page-update', $page);
370 $this->checkOwnablePermission('page-delete', $page);
372 $entitySelection = $request->get('entity_selection', null);
373 if ($entitySelection === null || $entitySelection === '') {
374 return redirect($page->getUrl());
378 $parent = $this->pageRepo->move($page, $entitySelection);
379 } catch (Exception $exception) {
380 if ($exception instanceof PermissionsException) {
381 $this->showPermissionError();
384 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
385 return redirect()->back();
388 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
389 return redirect($page->getUrl());
393 * Show the view to copy a page.
394 * @throws NotFoundException
396 public function showCopy(string $bookSlug, string $pageSlug)
398 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
399 $this->checkOwnablePermission('page-view', $page);
400 session()->flashInput(['name' => $page->name]);
401 return view('pages.copy', [
402 'book' => $page->book,
409 * Create a copy of a page within the requested target destination.
410 * @throws NotFoundException
413 public function copy(Request $request, string $bookSlug, string $pageSlug)
415 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
416 $this->checkOwnablePermission('page-view', $page);
418 $entitySelection = $request->get('entity_selection', null) ?? null;
419 $newName = $request->get('name', null);
422 $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
423 } catch (Exception $exception) {
424 if ($exception instanceof PermissionsException) {
425 $this->showPermissionError();
428 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
429 return redirect()->back();
432 $this->showSuccessNotification(trans('entities.pages_copy_success'));
433 return redirect($pageCopy->getUrl());
437 * Show the Permissions view.
438 * @throws NotFoundException
440 public function showPermissions(string $bookSlug, string $pageSlug)
442 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
443 $this->checkOwnablePermission('restrictions-manage', $page);
444 return view('pages.permissions', [
450 * Set the permissions for this page.
451 * @throws NotFoundException
454 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
456 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
457 $this->checkOwnablePermission('restrictions-manage', $page);
459 $permissionsUpdater->updateFromPermissionsForm($page, $request);
461 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
462 return redirect($page->getUrl());