1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Actions\View;
4 use BookStack\Entities\Tools\BookContents;
5 use BookStack\Entities\Tools\PageContent;
6 use BookStack\Entities\Tools\PageEditActivity;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Entities\Tools\PermissionsUpdater;
10 use BookStack\Exceptions\NotFoundException;
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']);
145 View::incrementFor($page);
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
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
315 public function destroyDraft(string $bookSlug, int $pageId)
317 $page = $this->pageRepo->getById($pageId);
319 $chapter = $page->chapter;
320 $this->checkOwnablePermission('page-update', $page);
322 $this->pageRepo->destroy($page);
324 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
326 if ($chapter && userCan('view', $chapter)) {
327 return redirect($chapter->getUrl());
329 return redirect($book->getUrl());
333 * Show a listing of recently created pages.
335 public function showRecentlyUpdated()
337 $pages = Page::visible()->orderBy('updated_at', 'desc')
339 ->setPath(url('/pages/recently-updated'));
341 return view('common.detailed-listing-paginated', [
342 'title' => trans('entities.recently_updated_pages'),
348 * Show the view to choose a new parent to move a page into.
349 * @throws NotFoundException
351 public function showMove(string $bookSlug, string $pageSlug)
353 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
354 $this->checkOwnablePermission('page-update', $page);
355 $this->checkOwnablePermission('page-delete', $page);
356 return view('pages.move', [
357 'book' => $page->book,
363 * Does the action of moving the location of a page.
364 * @throws NotFoundException
367 public function move(Request $request, string $bookSlug, string $pageSlug)
369 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
370 $this->checkOwnablePermission('page-update', $page);
371 $this->checkOwnablePermission('page-delete', $page);
373 $entitySelection = $request->get('entity_selection', null);
374 if ($entitySelection === null || $entitySelection === '') {
375 return redirect($page->getUrl());
379 $parent = $this->pageRepo->move($page, $entitySelection);
380 } catch (Exception $exception) {
381 if ($exception instanceof PermissionsException) {
382 $this->showPermissionError();
385 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
386 return redirect()->back();
389 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
390 return redirect($page->getUrl());
394 * Show the view to copy a page.
395 * @throws NotFoundException
397 public function showCopy(string $bookSlug, string $pageSlug)
399 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
400 $this->checkOwnablePermission('page-view', $page);
401 session()->flashInput(['name' => $page->name]);
402 return view('pages.copy', [
403 'book' => $page->book,
410 * Create a copy of a page within the requested target destination.
411 * @throws NotFoundException
414 public function copy(Request $request, string $bookSlug, string $pageSlug)
416 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
417 $this->checkOwnablePermission('page-view', $page);
419 $entitySelection = $request->get('entity_selection', null) ?? null;
420 $newName = $request->get('name', null);
423 $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
424 } catch (Exception $exception) {
425 if ($exception instanceof PermissionsException) {
426 $this->showPermissionError();
429 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
430 return redirect()->back();
433 $this->showSuccessNotification(trans('entities.pages_copy_success'));
434 return redirect($pageCopy->getUrl());
438 * Show the Permissions view.
439 * @throws NotFoundException
441 public function showPermissions(string $bookSlug, string $pageSlug)
443 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
444 $this->checkOwnablePermission('restrictions-manage', $page);
445 return view('pages.permissions', [
451 * Set the permissions for this page.
452 * @throws NotFoundException
455 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
457 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
458 $this->checkOwnablePermission('restrictions-manage', $page);
460 $permissionsUpdater->updateFromPermissionsForm($page, $request);
462 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
463 return redirect($page->getUrl());