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\NotifyException;
11 use BookStack\Exceptions\PermissionsException;
13 use Illuminate\Http\Request;
14 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']);
146 $this->setPageTitle($page->getShortName());
147 return view('pages.show', [
149 'book' => $page->book,
151 'sidebarTree' => $sidebarTree,
152 'commentsEnabled' => $commentsEnabled,
153 'pageNav' => $pageNav
158 * Get page from an ajax request.
159 * @throws NotFoundException
161 public function getPageAjax(int $pageId)
163 $page = $this->pageRepo->getById($pageId);
164 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
165 $page->addHidden(['book']);
166 return response()->json($page);
170 * Show the form for editing the specified page.
171 * @throws NotFoundException
173 public function edit(string $bookSlug, string $pageSlug)
175 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
176 $this->checkOwnablePermission('page-update', $page);
178 $page->isDraft = false;
179 $editActivity = new PageEditActivity($page);
181 // Check for active editing
183 if ($editActivity->hasActiveEditing()) {
184 $warnings[] = $editActivity->activeEditingMessage();
187 // Check for a current draft version for this user
188 $userDraft = $this->pageRepo->getUserDraft($page);
189 if ($userDraft !== null) {
190 $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
191 $page->isDraft = true;
192 $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
195 if (count($warnings) > 0) {
196 $this->showWarningNotification(implode("\n", $warnings));
199 $templates = $this->pageRepo->getTemplates(10);
200 $draftsEnabled = $this->isSignedIn();
201 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
202 return view('pages.edit', [
204 'book' => $page->book,
206 'draftsEnabled' => $draftsEnabled,
207 'templates' => $templates,
212 * Update the specified page in storage.
213 * @throws ValidationException
214 * @throws NotFoundException
216 public function update(Request $request, string $bookSlug, string $pageSlug)
218 $this->validate($request, [
219 'name' => 'required|string|max:255'
221 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
222 $this->checkOwnablePermission('page-update', $page);
224 $this->pageRepo->update($page, $request->all());
226 return redirect($page->getUrl());
230 * Save a draft update as a revision.
231 * @throws NotFoundException
233 public function saveDraft(Request $request, int $pageId)
235 $page = $this->pageRepo->getById($pageId);
236 $this->checkOwnablePermission('page-update', $page);
238 if (!$this->isSignedIn()) {
239 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
242 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
244 $updateTime = $draft->updated_at->timestamp;
245 return response()->json([
246 'status' => 'success',
247 'message' => trans('entities.pages_edit_draft_save_at'),
248 'timestamp' => $updateTime
253 * Redirect from a special link url which uses the page id rather than the name.
254 * @throws NotFoundException
256 public function redirectFromLink(int $pageId)
258 $page = $this->pageRepo->getById($pageId);
259 return redirect($page->getUrl());
263 * Show the deletion page for the specified page.
264 * @throws NotFoundException
266 public function showDelete(string $bookSlug, string $pageSlug)
268 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
269 $this->checkOwnablePermission('page-delete', $page);
270 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
271 return view('pages.delete', [
272 'book' => $page->book,
279 * Show the deletion page for the specified page.
280 * @throws NotFoundException
282 public function showDeleteDraft(string $bookSlug, int $pageId)
284 $page = $this->pageRepo->getById($pageId);
285 $this->checkOwnablePermission('page-update', $page);
286 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
287 return view('pages.delete', [
288 'book' => $page->book,
295 * Remove the specified page from storage.
296 * @throws NotFoundException
298 * @throws NotifyException
300 public function destroy(string $bookSlug, string $pageSlug)
302 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
303 $this->checkOwnablePermission('page-delete', $page);
304 $parent = $page->getParent();
306 $this->pageRepo->destroy($page);
308 return redirect($parent->getUrl());
312 * Remove the specified draft page from storage.
313 * @throws NotFoundException
314 * @throws NotifyException
317 public function destroyDraft(string $bookSlug, int $pageId)
319 $page = $this->pageRepo->getById($pageId);
321 $chapter = $page->chapter;
322 $this->checkOwnablePermission('page-update', $page);
324 $this->pageRepo->destroy($page);
326 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
328 if ($chapter && userCan('view', $chapter)) {
329 return redirect($chapter->getUrl());
331 return redirect($book->getUrl());
335 * Show a listing of recently created pages.
337 public function showRecentlyUpdated()
339 $pages = Page::visible()->orderBy('updated_at', 'desc')
341 ->setPath(url('/pages/recently-updated'));
343 return view('pages.detailed-listing', [
344 'title' => trans('entities.recently_updated_pages'),
350 * Show the view to choose a new parent to move a page into.
351 * @throws NotFoundException
353 public function showMove(string $bookSlug, string $pageSlug)
355 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
356 $this->checkOwnablePermission('page-update', $page);
357 $this->checkOwnablePermission('page-delete', $page);
358 return view('pages.move', [
359 'book' => $page->book,
365 * Does the action of moving the location of a page.
366 * @throws NotFoundException
369 public function move(Request $request, string $bookSlug, string $pageSlug)
371 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
372 $this->checkOwnablePermission('page-update', $page);
373 $this->checkOwnablePermission('page-delete', $page);
375 $entitySelection = $request->get('entity_selection', null);
376 if ($entitySelection === null || $entitySelection === '') {
377 return redirect($page->getUrl());
381 $parent = $this->pageRepo->move($page, $entitySelection);
382 } catch (Exception $exception) {
383 if ($exception instanceof PermissionsException) {
384 $this->showPermissionError();
387 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
388 return redirect()->back();
391 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
392 return redirect($page->getUrl());
396 * Show the view to copy a page.
397 * @throws NotFoundException
399 public function showCopy(string $bookSlug, string $pageSlug)
401 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
402 $this->checkOwnablePermission('page-view', $page);
403 session()->flashInput(['name' => $page->name]);
404 return view('pages.copy', [
405 'book' => $page->book,
412 * Create a copy of a page within the requested target destination.
413 * @throws NotFoundException
416 public function copy(Request $request, string $bookSlug, string $pageSlug)
418 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
419 $this->checkOwnablePermission('page-view', $page);
421 $entitySelection = $request->get('entity_selection', null) ?? null;
422 $newName = $request->get('name', null);
425 $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
426 } catch (Exception $exception) {
427 if ($exception instanceof PermissionsException) {
428 $this->showPermissionError();
431 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
432 return redirect()->back();
435 $this->showSuccessNotification(trans('entities.pages_copy_success'));
436 return redirect($pageCopy->getUrl());
440 * Show the Permissions view.
441 * @throws NotFoundException
443 public function showPermissions(string $bookSlug, string $pageSlug)
445 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
446 $this->checkOwnablePermission('restrictions-manage', $page);
447 return view('pages.permissions', [
453 * Set the permissions for this page.
454 * @throws NotFoundException
457 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
459 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
460 $this->checkOwnablePermission('restrictions-manage', $page);
462 $permissionsUpdater->updateFromPermissionsForm($page, $request);
464 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
465 return redirect($page->getUrl());