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