]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
Merge pull request #2735 from dopyrory3/table_column_fix
[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\Entities\Tools\PermissionsUpdater;
9 use BookStack\Exceptions\NotFoundException;
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      */
298     public function destroy(string $bookSlug, string $pageSlug)
299     {
300         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
301         $this->checkOwnablePermission('page-delete', $page);
302         $parent = $page->getParent();
303
304         $this->pageRepo->destroy($page);
305
306         return redirect($parent->getUrl());
307     }
308
309     /**
310      * Remove the specified draft page from storage.
311      * @throws NotFoundException
312      * @throws Throwable
313      */
314     public function destroyDraft(string $bookSlug, int $pageId)
315     {
316         $page = $this->pageRepo->getById($pageId);
317         $book = $page->book;
318         $chapter = $page->chapter;
319         $this->checkOwnablePermission('page-update', $page);
320
321         $this->pageRepo->destroy($page);
322
323         $this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
324
325         if ($chapter && userCan('view', $chapter)) {
326             return redirect($chapter->getUrl());
327         }
328         return redirect($book->getUrl());
329     }
330
331     /**
332      * Show a listing of recently created pages.
333      */
334     public function showRecentlyUpdated()
335     {
336         $pages = Page::visible()->orderBy('updated_at', 'desc')
337             ->paginate(20)
338             ->setPath(url('/pages/recently-updated'));
339
340         return view('pages.detailed-listing', [
341             'title' => trans('entities.recently_updated_pages'),
342             'pages' => $pages
343         ]);
344     }
345
346     /**
347      * Show the view to choose a new parent to move a page into.
348      * @throws NotFoundException
349      */
350     public function showMove(string $bookSlug, string $pageSlug)
351     {
352         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
353         $this->checkOwnablePermission('page-update', $page);
354         $this->checkOwnablePermission('page-delete', $page);
355         return view('pages.move', [
356             'book' => $page->book,
357             'page' => $page
358         ]);
359     }
360
361     /**
362      * Does the action of moving the location of a page.
363      * @throws NotFoundException
364      * @throws Throwable
365      */
366     public function move(Request $request, string $bookSlug, string $pageSlug)
367     {
368         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
369         $this->checkOwnablePermission('page-update', $page);
370         $this->checkOwnablePermission('page-delete', $page);
371
372         $entitySelection = $request->get('entity_selection', null);
373         if ($entitySelection === null || $entitySelection === '') {
374             return redirect($page->getUrl());
375         }
376
377         try {
378             $parent = $this->pageRepo->move($page, $entitySelection);
379         } catch (Exception $exception) {
380             if ($exception instanceof  PermissionsException) {
381                 $this->showPermissionError();
382             }
383
384             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
385             return redirect()->back();
386         }
387
388         $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
389         return redirect($page->getUrl());
390     }
391
392     /**
393      * Show the view to copy a page.
394      * @throws NotFoundException
395      */
396     public function showCopy(string $bookSlug, string $pageSlug)
397     {
398         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
399         $this->checkOwnablePermission('page-view', $page);
400         session()->flashInput(['name' => $page->name]);
401         return view('pages.copy', [
402             'book' => $page->book,
403             'page' => $page
404         ]);
405     }
406
407
408     /**
409      * Create a copy of a page within the requested target destination.
410      * @throws NotFoundException
411      * @throws Throwable
412      */
413     public function copy(Request $request, string $bookSlug, string $pageSlug)
414     {
415         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
416         $this->checkOwnablePermission('page-view', $page);
417
418         $entitySelection = $request->get('entity_selection', null) ?? null;
419         $newName = $request->get('name', null);
420
421         try {
422             $pageCopy = $this->pageRepo->copy($page, $entitySelection, $newName);
423         } catch (Exception $exception) {
424             if ($exception instanceof  PermissionsException) {
425                 $this->showPermissionError();
426             }
427
428             $this->showErrorNotification(trans('errors.selected_book_chapter_not_found'));
429             return redirect()->back();
430         }
431
432         $this->showSuccessNotification(trans('entities.pages_copy_success'));
433         return redirect($pageCopy->getUrl());
434     }
435
436     /**
437      * Show the Permissions view.
438      * @throws NotFoundException
439      */
440     public function showPermissions(string $bookSlug, string $pageSlug)
441     {
442         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
443         $this->checkOwnablePermission('restrictions-manage', $page);
444         return view('pages.permissions', [
445             'page'  => $page,
446         ]);
447     }
448
449     /**
450      * Set the permissions for this page.
451      * @throws NotFoundException
452      * @throws Throwable
453      */
454     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
455     {
456         $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
457         $this->checkOwnablePermission('restrictions-manage', $page);
458
459         $permissionsUpdater->updateFromPermissionsForm($page, $request);
460
461         $this->showSuccessNotification(trans('entities.pages_permissions_success'));
462         return redirect($page->getUrl());
463     }
464 }