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