]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
Merge branch 'v21.04.x'
[bookstack] / app / Http / Controllers / PageController.php
1 <?php namespace BookStack\Http\Controllers;
2
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;
12 use Exception;
13 use Illuminate\Http\Request;
14 use Illuminate\Validation\ValidationException;
15 use Throwable;
16 use Views;
17
18 class PageController extends Controller
19 {
20
21     protected $pageRepo;
22
23     /**
24      * PageController constructor.
25      */
26     public function __construct(PageRepo $pageRepo)
27     {
28         $this->pageRepo = $pageRepo;
29     }
30
31     /**
32      * Show the form for creating a new page.
33      * @throws Throwable
34      */
35     public function create(string $bookSlug, string $chapterSlug = null)
36     {
37         $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
38         $this->checkOwnablePermission('page-create', $parent);
39
40         // Redirect to draft edit screen if signed in
41         if ($this->isSignedIn()) {
42             $draft = $this->pageRepo->getNewDraftPage($parent);
43             return redirect($draft->getUrl());
44         }
45
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]);
49     }
50
51     /**
52      * Create a new page as a guest user.
53      * @throws ValidationException
54      */
55     public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
56     {
57         $this->validate($request, [
58             'name' => 'required|string|max:255'
59         ]);
60
61         $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
62         $this->checkOwnablePermission('page-create', $parent);
63
64         $page = $this->pageRepo->getNewDraftPage($parent);
65         $this->pageRepo->publishDraft($page, [
66             'name' => $request->get('name'),
67             'html' => ''
68         ]);
69
70         return redirect($page->getUrl('/edit'));
71     }
72
73     /**
74      * Show form to continue editing a draft page.
75      * @throws NotFoundException
76      */
77     public function editDraft(string $bookSlug, int $pageId)
78     {
79         $draft = $this->pageRepo->getById($pageId);
80         $this->checkOwnablePermission('page-create', $draft->getParent());
81         $this->setPageTitle(trans('entities.pages_edit_draft'));
82
83         $draftsEnabled = $this->isSignedIn();
84         $templates = $this->pageRepo->getTemplates(10);
85
86         return view('pages.edit', [
87             'page' => $draft,
88             'book' => $draft->book,
89             'isDraft' => true,
90             'draftsEnabled' => $draftsEnabled,
91             'templates' => $templates,
92         ]);
93     }
94
95     /**
96      * Store a new page by changing a draft into a page.
97      * @throws NotFoundException
98      * @throws ValidationException
99      */
100     public function store(Request $request, string $bookSlug, int $pageId)
101     {
102         $this->validate($request, [
103             'name' => 'required|string|max:255'
104         ]);
105         $draftPage = $this->pageRepo->getById($pageId);
106         $this->checkOwnablePermission('page-create', $draftPage->getParent());
107
108         $page = $this->pageRepo->publishDraft($draftPage, $request->all());
109
110         return redirect($page->getUrl());
111     }
112
113     /**
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
117      */
118     public function show(string $bookSlug, string $pageSlug)
119     {
120         try {
121             $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
122         } catch (NotFoundException $e) {
123             $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
124
125             if ($page === null) {
126                 throw $e;
127             }
128
129             return redirect($page->getUrl());
130         }
131
132         $this->checkOwnablePermission('page-view', $page);
133
134         $pageContent = (new PageContent($page));
135         $page->html = $pageContent->render();
136         $sidebarTree = (new BookContents($page->book))->getTree();
137         $pageNav = $pageContent->getNavigation($page->html);
138
139         // Check if page comments are enabled
140         $commentsEnabled = !setting('app-disable-comments');
141         if ($commentsEnabled) {
142             $page->load(['comments.createdBy']);
143         }
144
145         View::incrementFor($page);
146         $this->setPageTitle($page->getShortName());
147         return view('pages.show', [
148             'page' => $page,
149             'book' => $page->book,
150             'current' => $page,
151             'sidebarTree' => $sidebarTree,
152             'commentsEnabled' => $commentsEnabled,
153             'pageNav' => $pageNav
154         ]);
155     }
156
157     /**
158      * Get page from an ajax request.
159      * @throws NotFoundException
160      */
161     public function getPageAjax(int $pageId)
162     {
163         $page = $this->pageRepo->getById($pageId);
164         $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
165         $page->addHidden(['book']);
166         return response()->json($page);
167     }
168
169     /**
170      * Show the form for editing the specified page.
171      * @throws NotFoundException
172      */
173     public function edit(string $bookSlug, string $pageSlug)
174     {
175         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
176         $this->checkOwnablePermission('page-update', $page);
177
178         $page->isDraft = false;
179         $editActivity = new PageEditActivity($page);
180
181         // Check for active editing
182         $warnings = [];
183         if ($editActivity->hasActiveEditing()) {
184             $warnings[] = $editActivity->activeEditingMessage();
185         }
186
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);
193         }
194
195         if (count($warnings) > 0) {
196             $this->showWarningNotification(implode("\n", $warnings));
197         }
198
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', [
203             'page' => $page,
204             'book' => $page->book,
205             'current' => $page,
206             'draftsEnabled' => $draftsEnabled,
207             'templates' => $templates,
208         ]);
209     }
210
211     /**
212      * Update the specified page in storage.
213      * @throws ValidationException
214      * @throws NotFoundException
215      */
216     public function update(Request $request, string $bookSlug, string $pageSlug)
217     {
218         $this->validate($request, [
219             'name' => 'required|string|max:255'
220         ]);
221         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
222         $this->checkOwnablePermission('page-update', $page);
223
224         $this->pageRepo->update($page, $request->all());
225
226         return redirect($page->getUrl());
227     }
228
229     /**
230      * Save a draft update as a revision.
231      * @throws NotFoundException
232      */
233     public function saveDraft(Request $request, int $pageId)
234     {
235         $page = $this->pageRepo->getById($pageId);
236         $this->checkOwnablePermission('page-update', $page);
237
238         if (!$this->isSignedIn()) {
239             return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
240         }
241
242         $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
243
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
249         ]);
250     }
251
252     /**
253      * Redirect from a special link url which uses the page id rather than the name.
254      * @throws NotFoundException
255      */
256     public function redirectFromLink(int $pageId)
257     {
258         $page = $this->pageRepo->getById($pageId);
259         return redirect($page->getUrl());
260     }
261
262     /**
263      * Show the deletion page for the specified page.
264      * @throws NotFoundException
265      */
266     public function showDelete(string $bookSlug, string $pageSlug)
267     {
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,
273             'page' => $page,
274             'current' => $page
275         ]);
276     }
277
278     /**
279      * Show the deletion page for the specified page.
280      * @throws NotFoundException
281      */
282     public function showDeleteDraft(string $bookSlug, int $pageId)
283     {
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,
289             'page' => $page,
290             'current' => $page
291         ]);
292     }
293
294     /**
295      * Remove the specified page from storage.
296      * @throws NotFoundException
297      * @throws Throwable
298      */
299     public function destroy(string $bookSlug, string $pageSlug)
300     {
301         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
302         $this->checkOwnablePermission('page-delete', $page);
303         $parent = $page->getParent();
304
305         $this->pageRepo->destroy($page);
306
307         return redirect($parent->getUrl());
308     }
309
310     /**
311      * Remove the specified draft page from storage.
312      * @throws NotFoundException
313      * @throws Throwable
314      */
315     public function destroyDraft(string $bookSlug, int $pageId)
316     {
317         $page = $this->pageRepo->getById($pageId);
318         $book = $page->book;
319         $chapter = $page->chapter;
320         $this->checkOwnablePermission('page-update', $page);
321
322         $this->pageRepo->destroy($page);
323
324         $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
325
326         if ($chapter && userCan('view', $chapter)) {
327             return redirect($chapter->getUrl());
328         }
329         return redirect($book->getUrl());
330     }
331
332     /**
333      * Show a listing of recently created pages.
334      */
335     public function showRecentlyUpdated()
336     {
337         $pages = Page::visible()->orderBy('updated_at', 'desc')
338             ->paginate(20)
339             ->setPath(url('/pages/recently-updated'));
340
341         return view('common.detailed-listing-paginated', [
342             'title' => trans('entities.recently_updated_pages'),
343             'entities' => $pages
344         ]);
345     }
346
347     /**
348      * Show the view to choose a new parent to move a page into.
349      * @throws NotFoundException
350      */
351     public function showMove(string $bookSlug, string $pageSlug)
352     {
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,
358             'page' => $page
359         ]);
360     }
361
362     /**
363      * Does the action of moving the location of a page.
364      * @throws NotFoundException
365      * @throws Throwable
366      */
367     public function move(Request $request, string $bookSlug, string $pageSlug)
368     {
369         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
370         $this->checkOwnablePermission('page-update', $page);
371         $this->checkOwnablePermission('page-delete', $page);
372
373         $entitySelection = $request->get('entity_selection', null);
374         if ($entitySelection === null || $entitySelection === '') {
375             return redirect($page->getUrl());
376         }
377
378         try {
379             $parent = $this->pageRepo->move($page, $entitySelection);
380         } catch (Exception $exception) {
381             if ($exception instanceof  PermissionsException) {
382                 $this->showPermissionError();
383             }
384
385             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
386             return redirect()->back();
387         }
388
389         $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
390         return redirect($page->getUrl());
391     }
392
393     /**
394      * Show the view to copy a page.
395      * @throws NotFoundException
396      */
397     public function showCopy(string $bookSlug, string $pageSlug)
398     {
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,
404             'page' => $page
405         ]);
406     }
407
408
409     /**
410      * Create a copy of a page within the requested target destination.
411      * @throws NotFoundException
412      * @throws Throwable
413      */
414     public function copy(Request $request, string $bookSlug, string $pageSlug)
415     {
416         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
417         $this->checkOwnablePermission('page-view', $page);
418
419         $entitySelection = $request->get('entity_selection', null) ?? null;
420         $newName = $request->get('name', null);
421
422         try {
423             $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
424         } catch (Exception $exception) {
425             if ($exception instanceof  PermissionsException) {
426                 $this->showPermissionError();
427             }
428
429             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
430             return redirect()->back();
431         }
432
433         $this->showSuccessNotification(trans('entities.pages_copy_success'));
434         return redirect($pageCopy->getUrl());
435     }
436
437     /**
438      * Show the Permissions view.
439      * @throws NotFoundException
440      */
441     public function showPermissions(string $bookSlug, string $pageSlug)
442     {
443         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
444         $this->checkOwnablePermission('restrictions-manage', $page);
445         return view('pages.permissions', [
446             'page'  => $page,
447         ]);
448     }
449
450     /**
451      * Set the permissions for this page.
452      * @throws NotFoundException
453      * @throws Throwable
454      */
455     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
456     {
457         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
458         $this->checkOwnablePermission('restrictions-manage', $page);
459
460         $permissionsUpdater->updateFromPermissionsForm($page, $request);
461
462         $this->showSuccessNotification(trans('entities.pages_permissions_success'));
463         return redirect($page->getUrl());
464     }
465 }