]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterController.php
Merge branch 'development' into default-templates
[bookstack] / app / Entities / Controllers / ChapterController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Activity\Models\View;
6 use BookStack\Activity\Tools\UserEntityWatchOptions;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Repos\ChapterRepo;
9 use BookStack\Entities\Tools\BookContents;
10 use BookStack\Entities\Tools\Cloner;
11 use BookStack\Entities\Tools\HierarchyTransformer;
12 use BookStack\Entities\Tools\NextPreviousContentLocator;
13 use BookStack\Exceptions\MoveOperationException;
14 use BookStack\Exceptions\NotFoundException;
15 use BookStack\Exceptions\NotifyException;
16 use BookStack\Exceptions\PermissionsException;
17 use BookStack\Http\Controller;
18 use BookStack\References\ReferenceFetcher;
19 use Illuminate\Http\Request;
20 use Illuminate\Validation\ValidationException;
21 use Throwable;
22
23 class ChapterController extends Controller
24 {
25     protected ChapterRepo $chapterRepo;
26     protected ReferenceFetcher $referenceFetcher;
27
28     public function __construct(ChapterRepo $chapterRepo, ReferenceFetcher $referenceFetcher)
29     {
30         $this->chapterRepo = $chapterRepo;
31         $this->referenceFetcher = $referenceFetcher;
32     }
33
34     /**
35      * Show the form for creating a new chapter.
36      */
37     public function create(string $bookSlug)
38     {
39         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
40         $this->checkOwnablePermission('chapter-create', $book);
41
42         $this->setPageTitle(trans('entities.chapters_create'));
43
44         return view('chapters.create', ['book' => $book, 'current' => $book]);
45     }
46
47     /**
48      * Store a newly created chapter in storage.
49      *
50      * @throws ValidationException
51      */
52     public function store(Request $request, string $bookSlug)
53     {
54         $this->validate($request, [
55             'name' => ['required', 'string', 'max:255'],
56         ]);
57
58         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
59         $this->checkOwnablePermission('chapter-create', $book);
60
61         $chapter = $this->chapterRepo->create($request->all(), $book);
62
63         return redirect($chapter->getUrl());
64     }
65
66     /**
67      * Display the specified chapter.
68      */
69     public function show(string $bookSlug, string $chapterSlug)
70     {
71         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
72         $this->checkOwnablePermission('chapter-view', $chapter);
73
74         $sidebarTree = (new BookContents($chapter->book))->getTree();
75         $pages = $chapter->getVisiblePages();
76         $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
77         View::incrementFor($chapter);
78
79         $this->setPageTitle($chapter->getShortName());
80
81         return view('chapters.show', [
82             'book'           => $chapter->book,
83             'chapter'        => $chapter,
84             'current'        => $chapter,
85             'sidebarTree'    => $sidebarTree,
86             'watchOptions'   => new UserEntityWatchOptions(user(), $chapter),
87             'pages'          => $pages,
88             'next'           => $nextPreviousLocator->getNext(),
89             'previous'       => $nextPreviousLocator->getPrevious(),
90             'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
91         ]);
92     }
93
94     /**
95      * Show the form for editing the specified chapter.
96      */
97     public function edit(string $bookSlug, string $chapterSlug)
98     {
99         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
100         $this->checkOwnablePermission('chapter-update', $chapter);
101
102         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
103
104         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
105     }
106
107     /**
108      * Update the specified chapter in storage.
109      *
110      * @throws NotFoundException
111      */
112     public function update(Request $request, string $bookSlug, string $chapterSlug)
113     {
114         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
115         $this->checkOwnablePermission('chapter-update', $chapter);
116
117         $this->chapterRepo->update($chapter, $request->all());
118
119         return redirect($chapter->getUrl());
120     }
121
122     /**
123      * Shows the page to confirm deletion of this chapter.
124      *
125      * @throws NotFoundException
126      */
127     public function showDelete(string $bookSlug, string $chapterSlug)
128     {
129         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
130         $this->checkOwnablePermission('chapter-delete', $chapter);
131
132         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
133
134         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
135     }
136
137     /**
138      * Remove the specified chapter from storage.
139      *
140      * @throws NotFoundException
141      * @throws Throwable
142      */
143     public function destroy(string $bookSlug, string $chapterSlug)
144     {
145         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
146         $this->checkOwnablePermission('chapter-delete', $chapter);
147
148         $this->chapterRepo->destroy($chapter);
149
150         return redirect($chapter->book->getUrl());
151     }
152
153     /**
154      * Show the page for moving a chapter.
155      *
156      * @throws NotFoundException
157      */
158     public function showMove(string $bookSlug, string $chapterSlug)
159     {
160         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
161         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
162         $this->checkOwnablePermission('chapter-update', $chapter);
163         $this->checkOwnablePermission('chapter-delete', $chapter);
164
165         return view('chapters.move', [
166             'chapter' => $chapter,
167             'book'    => $chapter->book,
168         ]);
169     }
170
171     /**
172      * Perform the move action for a chapter.
173      *
174      * @throws NotFoundException|NotifyException
175      */
176     public function move(Request $request, string $bookSlug, string $chapterSlug)
177     {
178         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
179         $this->checkOwnablePermission('chapter-update', $chapter);
180         $this->checkOwnablePermission('chapter-delete', $chapter);
181
182         $entitySelection = $request->get('entity_selection', null);
183         if ($entitySelection === null || $entitySelection === '') {
184             return redirect($chapter->getUrl());
185         }
186
187         try {
188             $this->chapterRepo->move($chapter, $entitySelection);
189         } catch (PermissionsException $exception) {
190             $this->showPermissionError();
191         } catch (MoveOperationException $exception) {
192             $this->showErrorNotification(trans('errors.selected_book_not_found'));
193
194             return redirect($chapter->getUrl('/move'));
195         }
196
197         return redirect($chapter->getUrl());
198     }
199
200     /**
201      * Show the view to copy a chapter.
202      *
203      * @throws NotFoundException
204      */
205     public function showCopy(string $bookSlug, string $chapterSlug)
206     {
207         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
208         $this->checkOwnablePermission('chapter-view', $chapter);
209
210         session()->flashInput(['name' => $chapter->name]);
211
212         return view('chapters.copy', [
213             'book'    => $chapter->book,
214             'chapter' => $chapter,
215         ]);
216     }
217
218     /**
219      * Create a copy of a chapter within the requested target destination.
220      *
221      * @throws NotFoundException
222      * @throws Throwable
223      */
224     public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
225     {
226         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
227         $this->checkOwnablePermission('chapter-view', $chapter);
228
229         $entitySelection = $request->get('entity_selection') ?: null;
230         $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
231
232         if (is_null($newParentBook)) {
233             $this->showErrorNotification(trans('errors.selected_book_not_found'));
234
235             return redirect($chapter->getUrl('/copy'));
236         }
237
238         $this->checkOwnablePermission('chapter-create', $newParentBook);
239
240         $newName = $request->get('name') ?: $chapter->name;
241         $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
242         $this->showSuccessNotification(trans('entities.chapters_copy_success'));
243
244         return redirect($chapterCopy->getUrl());
245     }
246
247     /**
248      * Convert the chapter to a book.
249      */
250     public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
251     {
252         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
253         $this->checkOwnablePermission('chapter-update', $chapter);
254         $this->checkOwnablePermission('chapter-delete', $chapter);
255         $this->checkPermission('book-create-all');
256
257         $book = $transformer->transformChapterToBook($chapter);
258
259         return redirect($book->getUrl());
260     }
261 }