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