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->parent());
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->parent());
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 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());
225 Activity::add($page, 'page_update', $page->book->id);
227 return redirect($page->getUrl());
231 * Save a draft update as a revision.
232 * @throws NotFoundException
234 public function saveDraft(Request $request, int $pageId)
236 $page = $this->pageRepo->getById($pageId);
237 $this->checkOwnablePermission('page-update', $page);
239 if (!$this->isSignedIn()) {
240 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
243 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
245 $updateTime = $draft->updated_at->timestamp;
246 return response()->json([
247 'status' => 'success',
248 'message' => trans('entities.pages_edit_draft_save_at'),
249 'timestamp' => $updateTime
254 * Redirect from a special link url which uses the page id rather than the name.
255 * @throws NotFoundException
257 public function redirectFromLink(int $pageId)
259 $page = $this->pageRepo->getById($pageId);
260 return redirect($page->getUrl());
264 * Show the deletion page for the specified page.
265 * @throws NotFoundException
267 public function showDelete(string $bookSlug, string $pageSlug)
269 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
270 $this->checkOwnablePermission('page-delete', $page);
271 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
272 return view('pages.delete', [
273 'book' => $page->book,
280 * Show the deletion page for the specified page.
281 * @throws NotFoundException
283 public function showDeleteDraft(string $bookSlug, int $pageId)
285 $page = $this->pageRepo->getById($pageId);
286 $this->checkOwnablePermission('page-update', $page);
287 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
288 return view('pages.delete', [
289 'book' => $page->book,
296 * Remove the specified page from storage.
297 * @throws NotFoundException
299 * @throws NotifyException
301 public function destroy(string $bookSlug, string $pageSlug)
303 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
304 $this->checkOwnablePermission('page-delete', $page);
307 $parent = $page->chapter ?? $book;
308 $this->pageRepo->destroy($page);
309 Activity::addMessage('page_delete', $page->name, $book->id);
311 $this->showSuccessNotification(trans('entities.pages_delete_success'));
312 return redirect($parent->getUrl());
316 * Remove the specified draft page from storage.
317 * @throws NotFoundException
318 * @throws NotifyException
321 public function destroyDraft(string $bookSlug, int $pageId)
323 $page = $this->pageRepo->getById($pageId);
325 $chapter = $page->chapter;
326 $this->checkOwnablePermission('page-update', $page);
328 $this->pageRepo->destroy($page);
330 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
332 if ($chapter && userCan('view', $chapter)) {
333 return redirect($chapter->getUrl());
335 return redirect($book->getUrl());
339 * Show a listing of recently created pages.
341 public function showRecentlyUpdated()
343 $pages = Page::visible()->orderBy('updated_at', 'desc')
345 ->setPath(url('/pages/recently-updated'));
347 return view('pages.detailed-listing', [
348 'title' => trans('entities.recently_updated_pages'),
354 * Show the view to choose a new parent to move a page into.
355 * @throws NotFoundException
357 public function showMove(string $bookSlug, string $pageSlug)
359 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
360 $this->checkOwnablePermission('page-update', $page);
361 $this->checkOwnablePermission('page-delete', $page);
362 return view('pages.move', [
363 'book' => $page->book,
369 * Does the action of moving the location of a page.
370 * @throws NotFoundException
373 public function move(Request $request, string $bookSlug, string $pageSlug)
375 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
376 $this->checkOwnablePermission('page-update', $page);
377 $this->checkOwnablePermission('page-delete', $page);
379 $entitySelection = $request->get('entity_selection', null);
380 if ($entitySelection === null || $entitySelection === '') {
381 return redirect($page->getUrl());
385 $parent = $this->pageRepo->move($page, $entitySelection);
386 } catch (Exception $exception) {
387 if ($exception instanceof PermissionsException) {
388 $this->showPermissionError();
391 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
392 return redirect()->back();
395 Activity::add($page, 'page_move', $page->book->id);
396 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
397 return redirect($page->getUrl());
401 * Show the view to copy a page.
402 * @throws NotFoundException
404 public function showCopy(string $bookSlug, string $pageSlug)
406 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
407 $this->checkOwnablePermission('page-view', $page);
408 session()->flashInput(['name' => $page->name]);
409 return view('pages.copy', [
410 'book' => $page->book,
417 * Create a copy of a page within the requested target destination.
418 * @throws NotFoundException
421 public function copy(Request $request, string $bookSlug, string $pageSlug)
423 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
424 $this->checkOwnablePermission('page-view', $page);
426 $entitySelection = $request->get('entity_selection', null) ?? null;
427 $newName = $request->get('name', null);
430 $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
431 } catch (Exception $exception) {
432 if ($exception instanceof PermissionsException) {
433 $this->showPermissionError();
436 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
437 return redirect()->back();
440 Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
442 $this->showSuccessNotification(trans('entities.pages_copy_success'));
443 return redirect($pageCopy->getUrl());
447 * Show the Permissions view.
448 * @throws NotFoundException
450 public function showPermissions(string $bookSlug, string $pageSlug)
452 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
453 $this->checkOwnablePermission('restrictions-manage', $page);
454 return view('pages.permissions', [
460 * Set the permissions for this page.
461 * @throws NotFoundException
464 public function permissions(Request $request, string $bookSlug, string $pageSlug)
466 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
467 $this->checkOwnablePermission('restrictions-manage', $page);
469 $restricted = $request->get('restricted') === 'true';
470 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
471 $this->pageRepo->updatePermissions($page, $restricted, $permissions);
473 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
474 return redirect($page->getUrl());