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