1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Actions\ActivityType;
5 use BookStack\Entities\Managers\BookContents;
6 use BookStack\Entities\Managers\PageContent;
7 use BookStack\Entities\Managers\PageEditActivity;
8 use BookStack\Entities\Page;
9 use BookStack\Entities\Repos\PageRepo;
10 use BookStack\Exceptions\NotFoundException;
11 use BookStack\Exceptions\NotifyException;
12 use BookStack\Exceptions\PermissionsException;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
19 class PageController extends Controller
25 * PageController constructor.
27 public function __construct(PageRepo $pageRepo)
29 $this->pageRepo = $pageRepo;
30 parent::__construct();
34 * Show the form for creating a new page.
37 public function create(string $bookSlug, string $chapterSlug = null)
39 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
40 $this->checkOwnablePermission('page-create', $parent);
42 // Redirect to draft edit screen if signed in
43 if ($this->isSignedIn()) {
44 $draft = $this->pageRepo->getNewDraftPage($parent);
45 return redirect($draft->getUrl());
48 // Otherwise show the edit view if they're a guest
49 $this->setPageTitle(trans('entities.pages_new'));
50 return view('pages.guest-create', ['parent' => $parent]);
54 * Create a new page as a guest user.
55 * @throws ValidationException
57 public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
59 $this->validate($request, [
60 'name' => 'required|string|max:255'
63 $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
64 $this->checkOwnablePermission('page-create', $parent);
66 $page = $this->pageRepo->getNewDraftPage($parent);
67 $this->pageRepo->publishDraft($page, [
68 'name' => $request->get('name'),
72 return redirect($page->getUrl('/edit'));
76 * Show form to continue editing a draft page.
77 * @throws NotFoundException
79 public function editDraft(string $bookSlug, int $pageId)
81 $draft = $this->pageRepo->getById($pageId);
82 $this->checkOwnablePermission('page-create', $draft->getParent());
83 $this->setPageTitle(trans('entities.pages_edit_draft'));
85 $draftsEnabled = $this->isSignedIn();
86 $templates = $this->pageRepo->getTemplates(10);
88 return view('pages.edit', [
90 'book' => $draft->book,
92 'draftsEnabled' => $draftsEnabled,
93 'templates' => $templates,
98 * Store a new page by changing a draft into a page.
99 * @throws NotFoundException
100 * @throws ValidationException
102 public function store(Request $request, string $bookSlug, int $pageId)
104 $this->validate($request, [
105 'name' => 'required|string|max:255'
107 $draftPage = $this->pageRepo->getById($pageId);
108 $this->checkOwnablePermission('page-create', $draftPage->getParent());
110 $page = $this->pageRepo->publishDraft($draftPage, $request->all());
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());
228 return redirect($page->getUrl());
232 * Save a draft update as a revision.
233 * @throws NotFoundException
235 public function saveDraft(Request $request, int $pageId)
237 $page = $this->pageRepo->getById($pageId);
238 $this->checkOwnablePermission('page-update', $page);
240 if (!$this->isSignedIn()) {
241 return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
244 $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
246 $updateTime = $draft->updated_at->timestamp;
247 return response()->json([
248 'status' => 'success',
249 'message' => trans('entities.pages_edit_draft_save_at'),
250 'timestamp' => $updateTime
255 * Redirect from a special link url which uses the page id rather than the name.
256 * @throws NotFoundException
258 public function redirectFromLink(int $pageId)
260 $page = $this->pageRepo->getById($pageId);
261 return redirect($page->getUrl());
265 * Show the deletion page for the specified page.
266 * @throws NotFoundException
268 public function showDelete(string $bookSlug, string $pageSlug)
270 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
271 $this->checkOwnablePermission('page-delete', $page);
272 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
273 return view('pages.delete', [
274 'book' => $page->book,
281 * Show the deletion page for the specified page.
282 * @throws NotFoundException
284 public function showDeleteDraft(string $bookSlug, int $pageId)
286 $page = $this->pageRepo->getById($pageId);
287 $this->checkOwnablePermission('page-update', $page);
288 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
289 return view('pages.delete', [
290 'book' => $page->book,
297 * Remove the specified page from storage.
298 * @throws NotFoundException
300 * @throws NotifyException
302 public function destroy(string $bookSlug, string $pageSlug)
304 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
305 $this->checkOwnablePermission('page-delete', $page);
306 $parent = $page->getParent();
308 $this->pageRepo->destroy($page);
310 return redirect($parent->getUrl());
314 * Remove the specified draft page from storage.
315 * @throws NotFoundException
316 * @throws NotifyException
319 public function destroyDraft(string $bookSlug, int $pageId)
321 $page = $this->pageRepo->getById($pageId);
323 $chapter = $page->chapter;
324 $this->checkOwnablePermission('page-update', $page);
326 $this->pageRepo->destroy($page);
328 $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
330 if ($chapter && userCan('view', $chapter)) {
331 return redirect($chapter->getUrl());
333 return redirect($book->getUrl());
337 * Show a listing of recently created pages.
339 public function showRecentlyUpdated()
341 $pages = Page::visible()->orderBy('updated_at', 'desc')
343 ->setPath(url('/pages/recently-updated'));
345 return view('pages.detailed-listing', [
346 'title' => trans('entities.recently_updated_pages'),
352 * Show the view to choose a new parent to move a page into.
353 * @throws NotFoundException
355 public function showMove(string $bookSlug, string $pageSlug)
357 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
358 $this->checkOwnablePermission('page-update', $page);
359 $this->checkOwnablePermission('page-delete', $page);
360 return view('pages.move', [
361 'book' => $page->book,
367 * Does the action of moving the location of a page.
368 * @throws NotFoundException
371 public function move(Request $request, string $bookSlug, string $pageSlug)
373 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
374 $this->checkOwnablePermission('page-update', $page);
375 $this->checkOwnablePermission('page-delete', $page);
377 $entitySelection = $request->get('entity_selection', null);
378 if ($entitySelection === null || $entitySelection === '') {
379 return redirect($page->getUrl());
383 $parent = $this->pageRepo->move($page, $entitySelection);
384 } catch (Exception $exception) {
385 if ($exception instanceof PermissionsException) {
386 $this->showPermissionError();
389 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
390 return redirect()->back();
393 $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
394 return redirect($page->getUrl());
398 * Show the view to copy a page.
399 * @throws NotFoundException
401 public function showCopy(string $bookSlug, string $pageSlug)
403 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
404 $this->checkOwnablePermission('page-view', $page);
405 session()->flashInput(['name' => $page->name]);
406 return view('pages.copy', [
407 'book' => $page->book,
414 * Create a copy of a page within the requested target destination.
415 * @throws NotFoundException
418 public function copy(Request $request, string $bookSlug, string $pageSlug)
420 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
421 $this->checkOwnablePermission('page-view', $page);
423 $entitySelection = $request->get('entity_selection', null) ?? null;
424 $newName = $request->get('name', null);
427 $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
428 } catch (Exception $exception) {
429 if ($exception instanceof PermissionsException) {
430 $this->showPermissionError();
433 $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
434 return redirect()->back();
437 $this->showSuccessNotification(trans('entities.pages_copy_success'));
438 return redirect($pageCopy->getUrl());
442 * Show the Permissions view.
443 * @throws NotFoundException
445 public function showPermissions(string $bookSlug, string $pageSlug)
447 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
448 $this->checkOwnablePermission('restrictions-manage', $page);
449 return view('pages.permissions', [
455 * Set the permissions for this page.
456 * @throws NotFoundException
459 public function permissions(Request $request, string $bookSlug, string $pageSlug)
461 $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
462 $this->checkOwnablePermission('restrictions-manage', $page);
464 $restricted = $request->get('restricted') === 'true';
465 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
466 $this->pageRepo->updatePermissions($page, $restricted, $permissions);
468 $this->showSuccessNotification(trans('entities.pages_permissions_success'));
469 return redirect($page->getUrl());