]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterController.php
Opensearch: Fixed XML declaration when php short tags enabled
[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->findVisibleBySlugOrFail($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->findVisibleBySlugOrFail($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->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
79         $this->checkOwnablePermission('chapter-view', $chapter);
80
81         $sidebarTree = (new BookContents($chapter->book))->getTree();
82         $pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)->get();
83
84         $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
85         View::incrementFor($chapter);
86
87         $this->setPageTitle($chapter->getShortName());
88
89         return view('chapters.show', [
90             'book'           => $chapter->book,
91             'chapter'        => $chapter,
92             'current'        => $chapter,
93             'sidebarTree'    => $sidebarTree,
94             'watchOptions'   => new UserEntityWatchOptions(user(), $chapter),
95             'pages'          => $pages,
96             'next'           => $nextPreviousLocator->getNext(),
97             'previous'       => $nextPreviousLocator->getPrevious(),
98             'referenceCount' => $this->referenceFetcher->getReferenceCountToEntity($chapter),
99         ]);
100     }
101
102     /**
103      * Show the form for editing the specified chapter.
104      */
105     public function edit(string $bookSlug, string $chapterSlug)
106     {
107         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
108         $this->checkOwnablePermission('chapter-update', $chapter);
109
110         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
111
112         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
113     }
114
115     /**
116      * Update the specified chapter in storage.
117      *
118      * @throws NotFoundException
119      */
120     public function update(Request $request, string $bookSlug, string $chapterSlug)
121     {
122         $validated = $this->validate($request, [
123             'name'                => ['required', 'string', 'max:255'],
124             'description_html'    => ['string', 'max:2000'],
125             'tags'                => ['array'],
126             'default_template_id' => ['nullable', 'integer'],
127         ]);
128
129         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
130         $this->checkOwnablePermission('chapter-update', $chapter);
131
132         $this->chapterRepo->update($chapter, $validated);
133
134         return redirect($chapter->getUrl());
135     }
136
137     /**
138      * Shows the page to confirm deletion of this chapter.
139      *
140      * @throws NotFoundException
141      */
142     public function showDelete(string $bookSlug, string $chapterSlug)
143     {
144         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
145         $this->checkOwnablePermission('chapter-delete', $chapter);
146
147         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
148
149         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
150     }
151
152     /**
153      * Remove the specified chapter from storage.
154      *
155      * @throws NotFoundException
156      * @throws Throwable
157      */
158     public function destroy(string $bookSlug, string $chapterSlug)
159     {
160         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
161         $this->checkOwnablePermission('chapter-delete', $chapter);
162
163         $this->chapterRepo->destroy($chapter);
164
165         return redirect($chapter->book->getUrl());
166     }
167
168     /**
169      * Show the page for moving a chapter.
170      *
171      * @throws NotFoundException
172      */
173     public function showMove(string $bookSlug, string $chapterSlug)
174     {
175         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
176         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
177         $this->checkOwnablePermission('chapter-update', $chapter);
178         $this->checkOwnablePermission('chapter-delete', $chapter);
179
180         return view('chapters.move', [
181             'chapter' => $chapter,
182             'book'    => $chapter->book,
183         ]);
184     }
185
186     /**
187      * Perform the move action for a chapter.
188      *
189      * @throws NotFoundException|NotifyException
190      */
191     public function move(Request $request, string $bookSlug, string $chapterSlug)
192     {
193         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
194         $this->checkOwnablePermission('chapter-update', $chapter);
195         $this->checkOwnablePermission('chapter-delete', $chapter);
196
197         $entitySelection = $request->get('entity_selection', null);
198         if ($entitySelection === null || $entitySelection === '') {
199             return redirect($chapter->getUrl());
200         }
201
202         try {
203             $this->chapterRepo->move($chapter, $entitySelection);
204         } catch (PermissionsException $exception) {
205             $this->showPermissionError();
206         } catch (MoveOperationException $exception) {
207             $this->showErrorNotification(trans('errors.selected_book_not_found'));
208
209             return redirect($chapter->getUrl('/move'));
210         }
211
212         return redirect($chapter->getUrl());
213     }
214
215     /**
216      * Show the view to copy a chapter.
217      *
218      * @throws NotFoundException
219      */
220     public function showCopy(string $bookSlug, string $chapterSlug)
221     {
222         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
223         $this->checkOwnablePermission('chapter-view', $chapter);
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         $this->checkOwnablePermission('chapter-view', $chapter);
243
244         $entitySelection = $request->get('entity_selection') ?: null;
245         $newParentBook = $entitySelection ? $this->entityQueries->findVisibleByStringIdentifier($entitySelection) : $chapter->getParent();
246
247         if (!$newParentBook instanceof Book) {
248             $this->showErrorNotification(trans('errors.selected_book_not_found'));
249
250             return redirect($chapter->getUrl('/copy'));
251         }
252
253         $this->checkOwnablePermission('chapter-create', $newParentBook);
254
255         $newName = $request->get('name') ?: $chapter->name;
256         $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
257         $this->showSuccessNotification(trans('entities.chapters_copy_success'));
258
259         return redirect($chapterCopy->getUrl());
260     }
261
262     /**
263      * Convert the chapter to a book.
264      */
265     public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
266     {
267         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
268         $this->checkOwnablePermission('chapter-update', $chapter);
269         $this->checkOwnablePermission('chapter-delete', $chapter);
270         $this->checkPermission('book-create-all');
271
272         $book = $transformer->transformChapterToBook($chapter);
273
274         return redirect($book->getUrl());
275     }
276 }