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