1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Entities\Managers\BookContents;
5 use BookStack\Entities\Managers\PageContent;
6 use BookStack\Entities\Managers\PageEditActivity;
7 use BookStack\Entities\Page;
8 use BookStack\Entities\Repos\PageRepo;
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;
29 parent::__construct();
33 * Show the form for creating a new page.
36 public function create(string $bookSlug, string $chapterSlug = null)
38 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
39 $this->checkOwnablePermission('page-create', $parent);
41 // Redirect to draft edit screen if signed in
42 if ($this->isSignedIn()) {
43 $draft = $this->pageRepo->getNewDraftPage($parent);
44 return redirect($draft->getUrl());
47 // Otherwise show the edit view if they're a guest
48 $this->setPageTitle(trans('entities.pages_new'));
49 return view('pages.guest-create', ['parent' => $parent]);
53 * Create a new page as a guest user.
54 * @throws ValidationException
56 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
58 $this->validate($request, [
59 'name' => 'required|string|max:255'
62 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
63 $this->checkOwnablePermission('page-create', $parent);
65 $page = $this->pageRepo->getNewDraftPage($parent);
66 $this->pageRepo->publishDraft($page, [
67 'name' => $request->get('name'),
71 return redirect($page->getUrl('/edit'));
75 * Show form to continue editing a draft page.
76 * @throws NotFoundException
78 public function editDraft(string $bookSlug, int $pageId)
80 $draft = $this->pageRepo->getById($pageId);
81 $this->checkOwnablePermission('page-create', $draft->getParent());
82 $this->setPageTitle(trans('entities.pages_edit_draft'));
84 $draftsEnabled = $this->isSignedIn();
85 $templates = $this->pageRepo->getTemplates(10);
87 return view('pages.edit', [
89 'book' => $draft->book,
91 'draftsEnabled' => $draftsEnabled,
92 'templates' => $templates,
97 * Store a new page by changing a draft into a page.
98 * @throws NotFoundException
99 * @throws ValidationException
101 public function store(Request $request, string $bookSlug, int $pageId)
103 $this->validate($request, [
104 'name' => 'required|string|max:255'
106 $draftPage = $this->pageRepo->getById($pageId);
107 $this->checkOwnablePermission('page-create', $draftPage->getParent());
109 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
110 Activity::add($page, 'page_create', $draftPage->book->id);
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.
118 * @throws NotFoundException
120 public function show(string $bookSlug, string $pageSlug)
123 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
124 } catch (NotFoundException $e) {
125 $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
127 if ($page === null) {
131 return redirect($page->getUrl());
134 $this->checkOwnablePermission('page-view', $page);
136 $pageContent = (new PageContent($page));
137 $page->html = $pageContent->render();
138 $sidebarTree = (new BookContents($page->book))->getTree();
139 $pageNav = $pageContent->getNavigation($page->html);
141 // Check if page comments are enabled
142 $commentsEnabled = !setting('app-disable-comments');
143 if ($commentsEnabled) {
144 $page->load(['comments.createdBy']);
148 $this->setPageTitle($page->getShortName());
149 return view('pages.show', [
151 'book' => $page->book,
153 'sidebarTree' => $sidebarTree,
154 'commentsEnabled' => $commentsEnabled,
155 'pageNav' => $pageNav
160 * Get page from an ajax request.
161 * @throws NotFoundException
163 public function getPageAjax(int $pageId)
165 $page = $this->pageRepo->getById($pageId);
166 $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
167 $page->addHidden(['book']);
168 return response()->json($page);
172 * Show the form for editing the specified page.
173 * @throws NotFoundException
175 public function edit(string $bookSlug, string $pageSlug)
177 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
178 $this->checkOwnablePermission('page-update', $page);
180 $page->isDraft = false;
181 $editActivity = new PageEditActivity($page);
183 // Check for active editing
185 if ($editActivity->hasActiveEditing()) {
186 $warnings[] = $editActivity->activeEditingMessage();
189 // Check for a current draft version for this user
190 $userDraft = $this->pageRepo->getUserDraft($page);
191 if ($userDraft !== null) {
192 $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
193 $page->isDraft = true;
194 $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
197 if (count($warnings) > 0) {
198 $this->showWarningNotification(implode("\n", $warnings));
201 $templates = $this->pageRepo->getTemplates(10);
202 $draftsEnabled = $this->isSignedIn();
203 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
204 return view('pages.edit', [
206 'book' => $page->book,
208 'draftsEnabled' => $draftsEnabled,
209 'templates' => $templates,
214 * Update the specified page in storage.
215 * @throws ValidationException
216 * @throws NotFoundException
218 public function update(Request $request, string $bookSlug, string $pageSlug)
220 $this->validate($request, [
221 'name' => 'required|string|max:255'
223 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
224 $this->checkOwnablePermission('page-update', $page);
226 $this->pageRepo->update($page, $request->all());
227 Activity::add($page, 'page_update', $page->book->id);
229 return redirect($page->getUrl());
233 * Save a draft update as a revision.
234 * @throws NotFoundException
236 public function saveDraft(Request $request, int $pageId)
238 $page = $this->pageRepo->getById($pageId);
239 $this->checkOwnablePermission('page-update', $page);
241 if (!$this->isSignedIn()) {
242 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
245 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
247 $updateTime = $draft->updated_at->timestamp;
248 return response()->json([
249 'status' => 'success',
250 'message' => trans('entities.pages_edit_draft_save_at'),
251 'timestamp' => $updateTime
256 * Redirect from a special link url which uses the page id rather than the name.
257 * @throws NotFoundException
259 public function redirectFromLink(int $pageId)
261 $page = $this->pageRepo->getById($pageId);
262 return redirect($page->getUrl());
266 * Show the deletion page for the specified page.
267 * @throws NotFoundException
269 public function showDelete(string $bookSlug, string $pageSlug)
271 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
272 $this->checkOwnablePermission('page-delete', $page);
273 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
274 return view('pages.delete', [
275 'book' => $page->book,
282 * Show the deletion page for the specified page.
283 * @throws NotFoundException
285 public function showDeleteDraft(string $bookSlug, int $pageId)
287 $page = $this->pageRepo->getById($pageId);
288 $this->checkOwnablePermission('page-update', $page);
289 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
290 return view('pages.delete', [
291 'book' => $page->book,
298 * Remove the specified page from storage.
299 * @throws NotFoundException
301 * @throws NotifyException
303 public function destroy(string $bookSlug, string $pageSlug)
305 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
306 $this->checkOwnablePermission('page-delete', $page);
309 $parent = $page->chapter ?? $book;
310 $this->pageRepo->destroy($page);
311 Activity::add($page, 'page_delete', $page->book_id);
313 return redirect($parent->getUrl());
317 * Remove the specified draft page from storage.
318 * @throws NotFoundException
319 * @throws NotifyException
322 public function destroyDraft(string $bookSlug, int $pageId)
324 $page = $this->pageRepo->getById($pageId);
326 $chapter = $page->chapter;
327 $this->checkOwnablePermission('page-update', $page);
329 $this->pageRepo->destroy($page);
331 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
333 if ($chapter && userCan('view', $chapter)) {
334 return redirect($chapter->getUrl());
336 return redirect($book->getUrl());
340 * Show a listing of recently created pages.
342 public function showRecentlyUpdated()
344 $pages = Page::visible()->orderBy('updated_at', 'desc')
346 ->setPath(url('/pages/recently-updated'));
348 return view('pages.detailed-listing', [
349 'title' => trans('entities.recently_updated_pages'),
355 * Show the view to choose a new parent to move a page into.
356 * @throws NotFoundException
358 public function showMove(string $bookSlug, string $pageSlug)
360 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
361 $this->checkOwnablePermission('page-update', $page);
362 $this->checkOwnablePermission('page-delete', $page);
363 return view('pages.move', [
364 'book' => $page->book,
370 * Does the action of moving the location of a page.
371 * @throws NotFoundException
374 public function move(Request $request, string $bookSlug, string $pageSlug)
376 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
377 $this->checkOwnablePermission('page-update', $page);
378 $this->checkOwnablePermission('page-delete', $page);
380 $entitySelection = $request->get('entity_selection', null);
381 if ($entitySelection === null || $entitySelection === '') {
382 return redirect($page->getUrl());
386 $parent = $this->pageRepo->move($page, $entitySelection);
387 } catch (Exception $exception) {
388 if ($exception instanceof PermissionsException) {
389 $this->showPermissionError();
392 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
393 return redirect()->back();
396 Activity::add($page, 'page_move', $page->book->id);
397 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
398 return redirect($page->getUrl());
402 * Show the view to copy a page.
403 * @throws NotFoundException
405 public function showCopy(string $bookSlug, string $pageSlug)
407 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
408 $this->checkOwnablePermission('page-view', $page);
409 session()->flashInput(['name' => $page->name]);
410 return view('pages.copy', [
411 'book' => $page->book,
418 * Create a copy of a page within the requested target destination.
419 * @throws NotFoundException
422 public function copy(Request $request, string $bookSlug, string $pageSlug)
424 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
425 $this->checkOwnablePermission('page-view', $page);
427 $entitySelection = $request->get('entity_selection', null) ?? null;
428 $newName = $request->get('name', null);
431 $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
432 } catch (Exception $exception) {
433 if ($exception instanceof PermissionsException) {
434 $this->showPermissionError();
437 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
438 return redirect()->back();
441 Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
443 $this->showSuccessNotification(trans('entities.pages_copy_success'));
444 return redirect($pageCopy->getUrl());
448 * Show the Permissions view.
449 * @throws NotFoundException
451 public function showPermissions(string $bookSlug, string $pageSlug)
453 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
454 $this->checkOwnablePermission('restrictions-manage', $page);
455 return view('pages.permissions', [
461 * Set the permissions for this page.
462 * @throws NotFoundException
465 public function permissions(Request $request, string $bookSlug, string $pageSlug)
467 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
468 $this->checkOwnablePermission('restrictions-manage', $page);
470 $restricted = $request->get('restricted') === 'true';
471 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
472 $this->pageRepo->updatePermissions($page, $restricted, $permissions);
474 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
475 return redirect($page->getUrl());