]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
Merge pull request #1646 from kostefun/patch-15
[bookstack] / app / Http / Controllers / PageController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Entities\Managers\BookContents;
5 use BookStack\Entities\Managers\PageContent;
6 use BookStack\Entities\Managers\PageEditActivity;
7 use BookStack\Entities\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Exceptions\NotifyException;
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         parent::__construct();
30     }
31
32     /**
33      * Show the form for creating a new page.
34      * @throws Throwable
35      */
36     public function create(string $bookSlug, string $chapterSlug = null)
37     {
38         $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
39         $this->checkOwnablePermission('page-create', $parent);
40
41         // Redirect to draft edit screen if signed in
42         if ($this->isSignedIn()) {
43             $draft = $this->pageRepo->getNewDraftPage($parent);
44             return redirect($draft->getUrl());
45         }
46
47         // Otherwise show the edit view if they're a guest
48         $this->setPageTitle(trans('entities.pages_new'));
49         return view('pages.guest-create', ['parent' => $parent]);
50     }
51
52     /**
53      * Create a new page as a guest user.
54      * @throws ValidationException
55      */
56     public function createAsGuest(Request $request, string $bookSlug, string $chapterSlug = null)
57     {
58         $this->validate($request, [
59             'name' => 'required|string|max:255'
60         ]);
61
62         $parent = $this->pageRepo->getParentFromSlugs($bookSlug, $chapterSlug);
63         $this->checkOwnablePermission('page-create', $parent);
64
65         $page = $this->pageRepo->getNewDraftPage($parent);
66         $this->pageRepo->publishDraft($page, [
67             'name' => $request->get('name'),
68             'html' => ''
69         ]);
70
71         return redirect($page->getUrl('/edit'));
72     }
73
74     /**
75      * Show form to continue editing a draft page.
76      * @throws NotFoundException
77      */
78     public function editDraft(string $bookSlug, int $pageId)
79     {
80         $draft = $this->pageRepo->getById($pageId);
81         $this->checkOwnablePermission('page-create', $draft->parent());
82         $this->setPageTitle(trans('entities.pages_edit_draft'));
83
84         $draftsEnabled = $this->isSignedIn();
85         $templates = $this->pageRepo->getTemplates(10);
86
87         return view('pages.edit', [
88             'page' => $draft,
89             'book' => $draft->book,
90             'isDraft' => true,
91             'draftsEnabled' => $draftsEnabled,
92             'templates' => $templates,
93         ]);
94     }
95
96     /**
97      * Store a new page by changing a draft into a page.
98      * @throws NotFoundException
99      * @throws ValidationException
100      */
101     public function store(Request $request, string $bookSlug, int $pageId)
102     {
103         $this->validate($request, [
104             'name' => 'required|string|max:255'
105         ]);
106         $draftPage = $this->pageRepo->getById($pageId);
107         $this->checkOwnablePermission('page-create', $draftPage->parent());
108
109         $page = $this->pageRepo->publishDraft($draftPage, $request->all());
110         Activity::add($page, 'page_create', $draftPage->book->id);
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         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         Activity::add($page, 'page_update', $page->book->id);
226
227         return redirect($page->getUrl());
228     }
229
230     /**
231      * Save a draft update as a revision.
232      * @throws NotFoundException
233      */
234     public function saveDraft(Request $request, int $pageId)
235     {
236         $page = $this->pageRepo->getById($pageId);
237         $this->checkOwnablePermission('page-update', $page);
238
239         if (!$this->isSignedIn()) {
240             return $this->jsonError(trans('errors.guests_cannot_save_drafts'), 500);
241         }
242
243         $draft = $this->pageRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
244
245         $updateTime = $draft->updated_at->timestamp;
246         return response()->json([
247             'status'    => 'success',
248             'message'   => trans('entities.pages_edit_draft_save_at'),
249             'timestamp' => $updateTime
250         ]);
251     }
252
253     /**
254      * Redirect from a special link url which uses the page id rather than the name.
255      * @throws NotFoundException
256      */
257     public function redirectFromLink(int $pageId)
258     {
259         $page = $this->pageRepo->getById($pageId);
260         return redirect($page->getUrl());
261     }
262
263     /**
264      * Show the deletion page for the specified page.
265      * @throws NotFoundException
266      */
267     public function showDelete(string $bookSlug, string $pageSlug)
268     {
269         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
270         $this->checkOwnablePermission('page-delete', $page);
271         $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
272         return view('pages.delete', [
273             'book' => $page->book,
274             'page' => $page,
275             'current' => $page
276         ]);
277     }
278
279     /**
280      * Show the deletion page for the specified page.
281      * @throws NotFoundException
282      */
283     public function showDeleteDraft(string $bookSlug, int $pageId)
284     {
285         $page = $this->pageRepo->getById($pageId);
286         $this->checkOwnablePermission('page-update', $page);
287         $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
288         return view('pages.delete', [
289             'book' => $page->book,
290             'page' => $page,
291             'current' => $page
292         ]);
293     }
294
295     /**
296      * Remove the specified page from storage.
297      * @throws NotFoundException
298      * @throws Throwable
299      * @throws NotifyException
300      */
301     public function destroy(string $bookSlug, string $pageSlug)
302     {
303         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
304         $this->checkOwnablePermission('page-delete', $page);
305
306         $book = $page->book;
307         $this->pageRepo->destroy($page);
308         Activity::addMessage('page_delete', $page->name, $book->id);
309
310         $this->showSuccessNotification(trans('entities.pages_delete_success'));
311         return redirect($book->getUrl());
312     }
313
314     /**
315      * Remove the specified draft page from storage.
316      * @throws NotFoundException
317      * @throws NotifyException
318      * @throws Throwable
319      */
320     public function destroyDraft(string $bookSlug, int $pageId)
321     {
322         $page = $this->pageRepo->getById($pageId);
323         $book = $page->book;
324         $chapter = $page->chapter;
325         $this->checkOwnablePermission('page-update', $page);
326
327         $this->pageRepo->destroy($page);
328
329         $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
330
331         if ($chapter && userCan('view', $chapter)) {
332             return redirect($chapter->getUrl());
333         }
334         return redirect($book->getUrl());
335     }
336
337     /**
338      * Show a listing of recently created pages.
339      */
340     public function showRecentlyUpdated()
341     {
342         $pages = Page::visible()->orderBy('updated_at', 'desc')
343             ->paginate(20)
344             ->setPath(url('/pages/recently-updated'));
345
346         return view('pages.detailed-listing', [
347             'title' => trans('entities.recently_updated_pages'),
348             'pages' => $pages
349         ]);
350     }
351
352     /**
353      * Show the view to choose a new parent to move a page into.
354      * @throws NotFoundException
355      */
356     public function showMove(string $bookSlug, string $pageSlug)
357     {
358         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
359         $this->checkOwnablePermission('page-update', $page);
360         $this->checkOwnablePermission('page-delete', $page);
361         return view('pages.move', [
362             'book' => $page->book,
363             'page' => $page
364         ]);
365     }
366
367     /**
368      * Does the action of moving the location of a page.
369      * @throws NotFoundException
370      * @throws Throwable
371      */
372     public function move(Request $request, string $bookSlug, string $pageSlug)
373     {
374         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
375         $this->checkOwnablePermission('page-update', $page);
376         $this->checkOwnablePermission('page-delete', $page);
377
378         $entitySelection = $request->get('entity_selection', null);
379         if ($entitySelection === null || $entitySelection === '') {
380             return redirect($page->getUrl());
381         }
382
383         try {
384             $parent = $this->pageRepo->move($page, $entitySelection);
385         } catch (Exception $exception) {
386             if ($exception instanceof  PermissionsException) {
387                 $this->showPermissionError();
388             }
389
390             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
391             return redirect()->back();
392         }
393
394         Activity::add($page, 'page_move', $page->book->id);
395         $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
396         return redirect($page->getUrl());
397     }
398
399     /**
400      * Show the view to copy a page.
401      * @throws NotFoundException
402      */
403     public function showCopy(string $bookSlug, string $pageSlug)
404     {
405         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
406         $this->checkOwnablePermission('page-view', $page);
407         session()->flashInput(['name' => $page->name]);
408         return view('pages.copy', [
409             'book' => $page->book,
410             'page' => $page
411         ]);
412     }
413
414
415     /**
416      * Create a copy of a page within the requested target destination.
417      * @throws NotFoundException
418      * @throws Throwable
419      */
420     public function copy(Request $request, string $bookSlug, string $pageSlug)
421     {
422         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
423         $this->checkOwnablePermission('page-view', $page);
424
425         $entitySelection = $request->get('entity_selection', null) ?? null;
426         $newName = $request->get('name', null);
427
428         try {
429             $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
430         } catch (Exception $exception) {
431             if ($exception instanceof  PermissionsException) {
432                 $this->showPermissionError();
433             }
434
435             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
436             return redirect()->back();
437         }
438
439         Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
440
441         $this->showSuccessNotification(trans('entities.pages_copy_success'));
442         return redirect($pageCopy->getUrl());
443     }
444
445     /**
446      * Show the Permissions view.
447      * @throws NotFoundException
448      */
449     public function showPermissions(string $bookSlug, string $pageSlug)
450     {
451         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
452         $this->checkOwnablePermission('restrictions-manage', $page);
453         return view('pages.permissions', [
454             'page'  => $page,
455         ]);
456     }
457
458     /**
459      * Set the permissions for this page.
460      * @throws NotFoundException
461      * @throws Throwable
462      */
463     public function permissions(Request $request, string $bookSlug, string $pageSlug)
464     {
465         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
466         $this->checkOwnablePermission('restrictions-manage', $page);
467
468         $restricted = $request->get('restricted') === 'true';
469         $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
470         $this->pageRepo->updatePermissions($page, $restricted, $permissions);
471
472         $this->showSuccessNotification(trans('entities.pages_permissions_success'));
473         return redirect($page->getUrl());
474     }
475 }