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\Exceptions\NotFoundException;
9 use BookStack\Exceptions\NotifyException;
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
297 * @throws NotifyException
299 public function destroy(string $bookSlug, string $pageSlug)
301 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
302 $this->checkOwnablePermission('page-delete', $page);
303 $parent = $page->getParent();
305 $this->pageRepo->destroy($page);
307 return redirect($parent->getUrl());
311 * Remove the specified draft page from storage.
312 * @throws NotFoundException
313 * @throws NotifyException
316 public function destroyDraft(string $bookSlug, int $pageId)
318 $page = $this->pageRepo->getById($pageId);
320 $chapter = $page->chapter;
321 $this->checkOwnablePermission('page-update', $page);
323 $this->pageRepo->destroy($page);
325 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
327 if ($chapter && userCan('view', $chapter)) {
328 return redirect($chapter->getUrl());
330 return redirect($book->getUrl());
334 * Show a listing of recently created pages.
336 public function showRecentlyUpdated()
338 $pages = Page::visible()->orderBy('updated_at', 'desc')
340 ->setPath(url('/pages/recently-updated'));
342 return view('pages.detailed-listing', [
343 'title' => trans('entities.recently_updated_pages'),
349 * Show the view to choose a new parent to move a page into.
350 * @throws NotFoundException
352 public function showMove(string $bookSlug, string $pageSlug)
354 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
355 $this->checkOwnablePermission('page-update', $page);
356 $this->checkOwnablePermission('page-delete', $page);
357 return view('pages.move', [
358 'book' => $page->book,
364 * Does the action of moving the location of a page.
365 * @throws NotFoundException
368 public function move(Request $request, string $bookSlug, string $pageSlug)
370 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
371 $this->checkOwnablePermission('page-update', $page);
372 $this->checkOwnablePermission('page-delete', $page);
374 $entitySelection = $request->get('entity_selection', null);
375 if ($entitySelection === null || $entitySelection === '') {
376 return redirect($page->getUrl());
380 $parent = $this->pageRepo->move($page, $entitySelection);
381 } catch (Exception $exception) {
382 if ($exception instanceof PermissionsException) {
383 $this->showPermissionError();
386 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
387 return redirect()->back();
390 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
391 return redirect($page->getUrl());
395 * Show the view to copy a page.
396 * @throws NotFoundException
398 public function showCopy(string $bookSlug, string $pageSlug)
400 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
401 $this->checkOwnablePermission('page-view', $page);
402 session()->flashInput(['name' => $page->name]);
403 return view('pages.copy', [
404 'book' => $page->book,
411 * Create a copy of a page within the requested target destination.
412 * @throws NotFoundException
415 public function copy(Request $request, string $bookSlug, string $pageSlug)
417 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
418 $this->checkOwnablePermission('page-view', $page);
420 $entitySelection = $request->get('entity_selection', null) ?? null;
421 $newName = $request->get('name', null);
424 $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
425 } catch (Exception $exception) {
426 if ($exception instanceof PermissionsException) {
427 $this->showPermissionError();
430 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
431 return redirect()->back();
434 $this->showSuccessNotification(trans('entities.pages_copy_success'));
435 return redirect($pageCopy->getUrl());
439 * Show the Permissions view.
440 * @throws NotFoundException
442 public function showPermissions(string $bookSlug, string $pageSlug)
444 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
445 $this->checkOwnablePermission('restrictions-manage', $page);
446 return view('pages.permissions', [
452 * Set the permissions for this page.
453 * @throws NotFoundException
456 public function permissions(Request $request, string $bookSlug, string $pageSlug)
458 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
459 $this->checkOwnablePermission('restrictions-manage', $page);
461 $restricted = $request->get('restricted') === 'true';
462 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
463 $this->pageRepo->updatePermissions($page, $restricted, $permissions);
465 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
466 return redirect($page->getUrl());