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