]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
31ee4e970bd4952c9a9613863a723d33f7972560
[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\NextPreviousContentLocator;
6 use BookStack\Entities\Tools\PageContent;
7 use BookStack\Entities\Tools\PageEditActivity;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Repos\PageRepo;
10 use BookStack\Entities\Tools\PermissionsUpdater;
11 use BookStack\Exceptions\NotFoundException;
12 use BookStack\Exceptions\PermissionsException;
13 use Exception;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
16 use Throwable;
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         $nextPreviousLocator = new NextPreviousContentLocator($page, $sidebarTree);
146
147         View::incrementFor($page);
148         $this->setPageTitle($page->getShortName());
149         return view('pages.show', [
150             'page' => $page,
151             'book' => $page->book,
152             'current' => $page,
153             'sidebarTree' => $sidebarTree,
154             'commentsEnabled' => $commentsEnabled,
155             'pageNav' => $pageNav,
156             'next' => $nextPreviousLocator->getNext(),
157             'previous' => $nextPreviousLocator->getPrevious(),
158         ]);
159     }
160
161     /**
162      * Get page from an ajax request.
163      * @throws NotFoundException
164      */
165     public function getPageAjax(int $pageId)
166     {
167         $page = $this->pageRepo->getById($pageId);
168         $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
169         $page->addHidden(['book']);
170         return response()->json($page);
171     }
172
173     /**
174      * Show the form for editing the specified page.
175      * @throws NotFoundException
176      */
177     public function edit(string $bookSlug, string $pageSlug)
178     {
179         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
180         $this->checkOwnablePermission('page-update', $page);
181
182         $page->isDraft = false;
183         $editActivity = new PageEditActivity($page);
184
185         // Check for active editing
186         $warnings = [];
187         if ($editActivity->hasActiveEditing()) {
188             $warnings[] = $editActivity->activeEditingMessage();
189         }
190
191         // Check for a current draft version for this user
192         $userDraft = $this->pageRepo->getUserDraft($page);
193         if ($userDraft !== null) {
194             $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
195             $page->isDraft = true;
196             $warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
197         }
198
199         if (count($warnings) > 0) {
200             $this->showWarningNotification(implode("\n", $warnings));
201         }
202
203         $templates = $this->pageRepo->getTemplates(10);
204         $draftsEnabled = $this->isSignedIn();
205         $this->setPageTitle(trans('entities.pages_editing_named', ['pageName' => $page->getShortName()]));
206         return view('pages.edit', [
207             'page' => $page,
208             'book' => $page->book,
209             'current' => $page,
210             'draftsEnabled' => $draftsEnabled,
211             'templates' => $templates,
212         ]);
213     }
214
215     /**
216      * Update the specified page in storage.
217      * @throws ValidationException
218      * @throws NotFoundException
219      */
220     public function update(Request $request, string $bookSlug, string $pageSlug)
221     {
222         $this->validate($request, [
223             'name' => 'required|string|max:255'
224         ]);
225         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
226         $this->checkOwnablePermission('page-update', $page);
227
228         $this->pageRepo->update($page, $request->all());
229
230         return redirect($page->getUrl());
231     }
232
233     /**
234      * Save a draft update as a revision.
235      * @throws NotFoundException
236      */
237     public function saveDraft(Request $request, int $pageId)
238     {
239         $page = $this->pageRepo->getById($pageId);
240         $this->checkOwnablePermission('page-update', $page);
241
242         if (!$this->isSignedIn()) {
243             return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
244         }
245
246         $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
247
248         $updateTime = $draft->updated_at->timestamp;
249         return response()->json([
250             'status' => 'success',
251             'message' => trans('entities.pages_edit_draft_save_at'),
252             'timestamp' => $updateTime
253         ]);
254     }
255
256     /**
257      * Redirect from a special link url which uses the page id rather than the name.
258      * @throws NotFoundException
259      */
260     public function redirectFromLink(int $pageId)
261     {
262         $page = $this->pageRepo->getById($pageId);
263         return redirect($page->getUrl());
264     }
265
266     /**
267      * Show the deletion page for the specified page.
268      * @throws NotFoundException
269      */
270     public function showDelete(string $bookSlug, string $pageSlug)
271     {
272         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
273         $this->checkOwnablePermission('page-delete', $page);
274         $this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
275         return view('pages.delete', [
276             'book' => $page->book,
277             'page' => $page,
278             'current' => $page
279         ]);
280     }
281
282     /**
283      * Show the deletion page for the specified page.
284      * @throws NotFoundException
285      */
286     public function showDeleteDraft(string $bookSlug, int $pageId)
287     {
288         $page = $this->pageRepo->getById($pageId);
289         $this->checkOwnablePermission('page-update', $page);
290         $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
291         return view('pages.delete', [
292             'book' => $page->book,
293             'page' => $page,
294             'current' => $page
295         ]);
296     }
297
298     /**
299      * Remove the specified page from storage.
300      * @throws NotFoundException
301      * @throws Throwable
302      */
303     public function destroy(string $bookSlug, string $pageSlug)
304     {
305         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
306         $this->checkOwnablePermission('page-delete', $page);
307         $parent = $page->getParent();
308
309         $this->pageRepo->destroy($page);
310
311         return redirect($parent->getUrl());
312     }
313
314     /**
315      * Remove the specified draft page from storage.
316      * @throws NotFoundException
317      * @throws Throwable
318      */
319     public function destroyDraft(string $bookSlug, int $pageId)
320     {
321         $page = $this->pageRepo->getById($pageId);
322         $book = $page->book;
323         $chapter = $page->chapter;
324         $this->checkOwnablePermission('page-update', $page);
325
326         $this->pageRepo->destroy($page);
327
328         $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
329
330         if ($chapter && userCan('view', $chapter)) {
331             return redirect($chapter->getUrl());
332         }
333         return redirect($book->getUrl());
334     }
335
336     /**
337      * Show a listing of recently created pages.
338      */
339     public function showRecentlyUpdated()
340     {
341         $pages = Page::visible()->orderBy('updated_at', 'desc')
342             ->paginate(20)
343             ->setPath(url('/pages/recently-updated'));
344
345         return view('common.detailed-listing-paginated', [
346             'title' => trans('entities.recently_updated_pages'),
347             'entities' => $pages
348         ]);
349     }
350
351     /**
352      * Show the view to choose a new parent to move a page into.
353      * @throws NotFoundException
354      */
355     public function showMove(string $bookSlug, string $pageSlug)
356     {
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,
362             'page' => $page
363         ]);
364     }
365
366     /**
367      * Does the action of moving the location of a page.
368      * @throws NotFoundException
369      * @throws Throwable
370      */
371     public function move(Request $request, string $bookSlug, string $pageSlug)
372     {
373         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
374         $this->checkOwnablePermission('page-update', $page);
375         $this->checkOwnablePermission('page-delete', $page);
376
377         $entitySelection = $request->get('entity_selection', null);
378         if ($entitySelection === null || $entitySelection === '') {
379             return redirect($page->getUrl());
380         }
381
382         try {
383             $parent = $this->pageRepo->move($page, $entitySelection);
384         } catch (Exception $exception) {
385             if ($exception instanceof PermissionsException) {
386                 $this->showPermissionError();
387             }
388
389             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
390             return redirect()->back();
391         }
392
393         $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
394         return redirect($page->getUrl());
395     }
396
397     /**
398      * Show the view to copy a page.
399      * @throws NotFoundException
400      */
401     public function showCopy(string $bookSlug, string $pageSlug)
402     {
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,
408             'page' => $page
409         ]);
410     }
411
412
413     /**
414      * Create a copy of a page within the requested target destination.
415      * @throws NotFoundException
416      * @throws Throwable
417      */
418     public function copy(Request $request, string $bookSlug, string $pageSlug)
419     {
420         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
421         $this->checkOwnablePermission('page-view', $page);
422
423         $entitySelection = $request->get('entity_selection', null) ?? null;
424         $newName = $request->get('name', null);
425
426         try {
427             $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
428         } catch (Exception $exception) {
429             if ($exception instanceof PermissionsException) {
430                 $this->showPermissionError();
431             }
432
433             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
434             return redirect()->back();
435         }
436
437         $this->showSuccessNotification(trans('entities.pages_copy_success'));
438         return redirect($pageCopy->getUrl());
439     }
440
441     /**
442      * Show the Permissions view.
443      * @throws NotFoundException
444      */
445     public function showPermissions(string $bookSlug, string $pageSlug)
446     {
447         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
448         $this->checkOwnablePermission('restrictions-manage', $page);
449         return view('pages.permissions', [
450             'page' => $page,
451         ]);
452     }
453
454     /**
455      * Set the permissions for this page.
456      * @throws NotFoundException
457      * @throws Throwable
458      */
459     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
460     {
461         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
462         $this->checkOwnablePermission('restrictions-manage', $page);
463
464         $permissionsUpdater->updateFromPermissionsForm($page, $request);
465
466         $this->showSuccessNotification(trans('entities.pages_permissions_success'));
467         return redirect($page->getUrl());
468     }
469 }