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