]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterController.php
Added Default Templates for Chapters
[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     public function __construct(
26         protected ChapterRepo $chapterRepo,
27         protected ReferenceFetcher $referenceFetcher
28     ) {
29     }
30
31     /**
32      * Show the form for creating a new chapter.
33      */
34     public function create(string $bookSlug)
35     {
36         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
37         $this->checkOwnablePermission('chapter-create', $book);
38
39         $this->setPageTitle(trans('entities.chapters_create'));
40
41         return view('chapters.create', ['book' => $book, 'current' => $book]);
42     }
43
44     /**
45      * Store a newly created chapter in storage.
46      *
47      * @throws ValidationException
48      */
49     public function store(Request $request, string $bookSlug)
50     {
51         $validated = $this->validate($request, [
52             'name'                => ['required', 'string', 'max:255'],
53             'description_html'    => ['string', 'max:2000'],
54             'tags'                => ['array'],
55             'default_template_id' => ['nullable', 'integer'],
56         ]);
57
58         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
59         $this->checkOwnablePermission('chapter-create', $book);
60
61         $chapter = $this->chapterRepo->create($validated, $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->getReferenceCountToEntity($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         $validated = $this->validate($request, [
115             'name'                => ['required', 'string', 'max:255'],
116             'description_html'    => ['string', 'max:2000'],
117             'tags'                => ['array'],
118             'default_template_id' => ['nullable', 'integer'],
119         ]);
120
121         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
122         $this->checkOwnablePermission('chapter-update', $chapter);
123
124         $this->chapterRepo->update($chapter, $validated);
125
126         return redirect($chapter->getUrl());
127     }
128
129     /**
130      * Shows the page to confirm deletion of this chapter.
131      *
132      * @throws NotFoundException
133      */
134     public function showDelete(string $bookSlug, string $chapterSlug)
135     {
136         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
137         $this->checkOwnablePermission('chapter-delete', $chapter);
138
139         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
140
141         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
142     }
143
144     /**
145      * Remove the specified chapter from storage.
146      *
147      * @throws NotFoundException
148      * @throws Throwable
149      */
150     public function destroy(string $bookSlug, string $chapterSlug)
151     {
152         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
153         $this->checkOwnablePermission('chapter-delete', $chapter);
154
155         $this->chapterRepo->destroy($chapter);
156
157         return redirect($chapter->book->getUrl());
158     }
159
160     /**
161      * Show the page for moving a chapter.
162      *
163      * @throws NotFoundException
164      */
165     public function showMove(string $bookSlug, string $chapterSlug)
166     {
167         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
168         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
169         $this->checkOwnablePermission('chapter-update', $chapter);
170         $this->checkOwnablePermission('chapter-delete', $chapter);
171
172         return view('chapters.move', [
173             'chapter' => $chapter,
174             'book'    => $chapter->book,
175         ]);
176     }
177
178     /**
179      * Perform the move action for a chapter.
180      *
181      * @throws NotFoundException|NotifyException
182      */
183     public function move(Request $request, string $bookSlug, string $chapterSlug)
184     {
185         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
186         $this->checkOwnablePermission('chapter-update', $chapter);
187         $this->checkOwnablePermission('chapter-delete', $chapter);
188
189         $entitySelection = $request->get('entity_selection', null);
190         if ($entitySelection === null || $entitySelection === '') {
191             return redirect($chapter->getUrl());
192         }
193
194         try {
195             $this->chapterRepo->move($chapter, $entitySelection);
196         } catch (PermissionsException $exception) {
197             $this->showPermissionError();
198         } catch (MoveOperationException $exception) {
199             $this->showErrorNotification(trans('errors.selected_book_not_found'));
200
201             return redirect($chapter->getUrl('/move'));
202         }
203
204         return redirect($chapter->getUrl());
205     }
206
207     /**
208      * Show the view to copy a chapter.
209      *
210      * @throws NotFoundException
211      */
212     public function showCopy(string $bookSlug, string $chapterSlug)
213     {
214         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
215         $this->checkOwnablePermission('chapter-view', $chapter);
216
217         session()->flashInput(['name' => $chapter->name]);
218
219         return view('chapters.copy', [
220             'book'    => $chapter->book,
221             'chapter' => $chapter,
222         ]);
223     }
224
225     /**
226      * Create a copy of a chapter within the requested target destination.
227      *
228      * @throws NotFoundException
229      * @throws Throwable
230      */
231     public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
232     {
233         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
234         $this->checkOwnablePermission('chapter-view', $chapter);
235
236         $entitySelection = $request->get('entity_selection') ?: null;
237         $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
238
239         if (is_null($newParentBook)) {
240             $this->showErrorNotification(trans('errors.selected_book_not_found'));
241
242             return redirect($chapter->getUrl('/copy'));
243         }
244
245         $this->checkOwnablePermission('chapter-create', $newParentBook);
246
247         $newName = $request->get('name') ?: $chapter->name;
248         $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
249         $this->showSuccessNotification(trans('entities.chapters_copy_success'));
250
251         return redirect($chapterCopy->getUrl());
252     }
253
254     /**
255      * Convert the chapter to a book.
256      */
257     public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
258     {
259         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
260         $this->checkOwnablePermission('chapter-update', $chapter);
261         $this->checkOwnablePermission('chapter-delete', $chapter);
262         $this->checkPermission('book-create-all');
263
264         $book = $transformer->transformChapterToBook($chapter);
265
266         return redirect($book->getUrl());
267     }
268 }