]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
862ba3d7ffe3822169cd9b51ad6b060945f6db80
[bookstack] / app / Http / Controllers / PageController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
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;
13 use Exception;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
16 use Throwable;
17 use Views;
18
19 class PageController extends Controller
20 {
21
22     protected $pageRepo;
23
24     /**
25      * PageController constructor.
26      */
27     public function __construct(PageRepo $pageRepo)
28     {
29         $this->pageRepo = $pageRepo;
30         parent::__construct();
31     }
32
33     /**
34      * Show the form for creating a new page.
35      * @throws Throwable
36      */
37     public function create(string $bookSlug, string $chapterSlug = null)
38     {
39         $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
40         $this->checkOwnablePermission('page-create', $parent);
41
42         // Redirect to draft edit screen if signed in
43         if ($this->isSignedIn()) {
44             $draft = $this->pageRepo->getNewDraftPage($parent);
45             return redirect($draft->getUrl());
46         }
47
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]);
51     }
52
53     /**
54      * Create a new page as a guest user.
55      * @throws ValidationException
56      */
57     public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
58     {
59         $this->validate($request, [
60             'name' => 'required|string|max:255'
61         ]);
62
63         $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
64         $this->checkOwnablePermission('page-create', $parent);
65
66         $page = $this->pageRepo->getNewDraftPage($parent);
67         $this->pageRepo->publishDraft($page, [
68             'name' => $request->get('name'),
69             'html' => ''
70         ]);
71
72         return redirect($page->getUrl('/edit'));
73     }
74
75     /**
76      * Show form to continue editing a draft page.
77      * @throws NotFoundException
78      */
79     public function editDraft(string $bookSlug, int $pageId)
80     {
81         $draft = $this->pageRepo->getById($pageId);
82         $this->checkOwnablePermission('page-create', $draft->getParent());
83         $this->setPageTitle(trans('entities.pages_edit_draft'));
84
85         $draftsEnabled = $this->isSignedIn();
86         $templates = $this->pageRepo->getTemplates(10);
87
88         return view('pages.edit', [
89             'page' => $draft,
90             'book' => $draft->book,
91             'isDraft' => true,
92             'draftsEnabled' => $draftsEnabled,
93             'templates' => $templates,
94         ]);
95     }
96
97     /**
98      * Store a new page by changing a draft into a page.
99      * @throws NotFoundException
100      * @throws ValidationException
101      */
102     public function store(Request $request, string $bookSlug, int $pageId)
103     {
104         $this->validate($request, [
105             'name' => 'required|string|max:255'
106         ]);
107         $draftPage = $this->pageRepo->getById($pageId);
108         $this->checkOwnablePermission('page-create', $draftPage->getParent());
109
110         $page = $this->pageRepo->publishDraft($draftPage, $request->all());
111
112         return redirect($page->getUrl());
113     }
114
115     /**
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
119      */
120     public function show(string $bookSlug, string $pageSlug)
121     {
122         try {
123             $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
124         } catch (NotFoundException $e) {
125             $page = $this->pageRepo->getByOldSlug($bookSlug, $pageSlug);
126
127             if ($page === null) {
128                 throw $e;
129             }
130
131             return redirect($page->getUrl());
132         }
133
134         $this->checkOwnablePermission('page-view', $page);
135
136         $pageContent = (new PageContent($page));
137         $page->html = $pageContent->render();
138         $sidebarTree = (new BookContents($page->book))->getTree();
139         $pageNav = $pageContent->getNavigation($page->html);
140
141         // Check if page comments are enabled
142         $commentsEnabled = !setting('app-disable-comments');
143         if ($commentsEnabled) {
144             $page->load(['comments.createdBy']);
145         }
146
147         Views::add($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         ]);
157     }
158
159     /**
160      * Get page from an ajax request.
161      * @throws NotFoundException
162      */
163     public function getPageAjax(int $pageId)
164     {
165         $page = $this->pageRepo->getById($pageId);
166         $page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
167         $page->addHidden(['book']);
168         return response()->json($page);
169     }
170
171     /**
172      * Show the form for editing the specified page.
173      * @throws NotFoundException
174      */
175     public function edit(string $bookSlug, string $pageSlug)
176     {
177         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
178         $this->checkOwnablePermission('page-update', $page);
179
180         $page->isDraft = false;
181         $editActivity = new PageEditActivity($page);
182
183         // Check for active editing
184         $warnings = [];
185         if ($editActivity->hasActiveEditing()) {
186             $warnings[] = $editActivity->activeEditingMessage();
187         }
188
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);
195         }
196
197         if (count($warnings) > 0) {
198             $this->showWarningNotification(implode("\n", $warnings));
199         }
200
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', [
205             'page' => $page,
206             'book' => $page->book,
207             'current' => $page,
208             'draftsEnabled' => $draftsEnabled,
209             'templates' => $templates,
210         ]);
211     }
212
213     /**
214      * Update the specified page in storage.
215      * @throws ValidationException
216      * @throws NotFoundException
217      */
218     public function update(Request $request, string $bookSlug, string $pageSlug)
219     {
220         $this->validate($request, [
221             'name' => 'required|string|max:255'
222         ]);
223         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
224         $this->checkOwnablePermission('page-update', $page);
225
226         $this->pageRepo->update($page, $request->all());
227
228         return redirect($page->getUrl());
229     }
230
231     /**
232      * Save a draft update as a revision.
233      * @throws NotFoundException
234      */
235     public function saveDraft(Request $request, int $pageId)
236     {
237         $page = $this->pageRepo->getById($pageId);
238         $this->checkOwnablePermission('page-update', $page);
239
240         if (!$this->isSignedIn()) {
241             return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
242         }
243
244         $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
245
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
251         ]);
252     }
253
254     /**
255      * Redirect from a special link url which uses the page id rather than the name.
256      * @throws NotFoundException
257      */
258     public function redirectFromLink(int $pageId)
259     {
260         $page = $this->pageRepo->getById($pageId);
261         return redirect($page->getUrl());
262     }
263
264     /**
265      * Show the deletion page for the specified page.
266      * @throws NotFoundException
267      */
268     public function showDelete(string $bookSlug, string $pageSlug)
269     {
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,
275             'page' => $page,
276             'current' => $page
277         ]);
278     }
279
280     /**
281      * Show the deletion page for the specified page.
282      * @throws NotFoundException
283      */
284     public function showDeleteDraft(string $bookSlug, int $pageId)
285     {
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,
291             'page' => $page,
292             'current' => $page
293         ]);
294     }
295
296     /**
297      * Remove the specified page from storage.
298      * @throws NotFoundException
299      * @throws Throwable
300      * @throws NotifyException
301      */
302     public function destroy(string $bookSlug, string $pageSlug)
303     {
304         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
305         $this->checkOwnablePermission('page-delete', $page);
306         $parent = $page->getParent();
307
308         $this->pageRepo->destroy($page);
309
310         return redirect($parent->getUrl());
311     }
312
313     /**
314      * Remove the specified draft page from storage.
315      * @throws NotFoundException
316      * @throws NotifyException
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('pages.detailed-listing', [
346             'title' => trans('entities.recently_updated_pages'),
347             'pages' => $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, string $bookSlug, string $pageSlug)
460     {
461         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
462         $this->checkOwnablePermission('restrictions-manage', $page);
463
464         $restricted = $request->get('restricted') === 'true';
465         $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
466         $this->pageRepo->updatePermissions($page, $restricted, $permissions);
467
468         $this->showSuccessNotification(trans('entities.pages_permissions_success'));
469         return redirect($page->getUrl());
470     }
471 }