]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterController.php
Merge pull request #4390 from BookStackApp/content_notifications
[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\Repos\ChapterRepo;
9 use BookStack\Entities\Tools\BookContents;
10 use BookStack\Entities\Tools\Cloner;
11 use BookStack\Entities\Tools\HierarchyTransformer;
12 use BookStack\Entities\Tools\NextPreviousContentLocator;
13 use BookStack\Exceptions\MoveOperationException;
14 use BookStack\Exceptions\NotFoundException;
15 use BookStack\Exceptions\PermissionsException;
16 use BookStack\Http\Controller;
17 use BookStack\References\ReferenceFetcher;
18 use Illuminate\Http\Request;
19 use Illuminate\Validation\ValidationException;
20 use Throwable;
21
22 class ChapterController extends Controller
23 {
24     protected ChapterRepo $chapterRepo;
25     protected ReferenceFetcher $referenceFetcher;
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             'watchOptions'   => new UserEntityWatchOptions(user(), $chapter),
86             'pages'          => $pages,
87             'next'           => $nextPreviousLocator->getNext(),
88             'previous'       => $nextPreviousLocator->getPrevious(),
89             'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
90         ]);
91     }
92
93     /**
94      * Show the form for editing the specified chapter.
95      */
96     public function edit(string $bookSlug, string $chapterSlug)
97     {
98         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
99         $this->checkOwnablePermission('chapter-update', $chapter);
100
101         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
102
103         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
104     }
105
106     /**
107      * Update the specified chapter in storage.
108      *
109      * @throws NotFoundException
110      */
111     public function update(Request $request, string $bookSlug, string $chapterSlug)
112     {
113         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
114         $this->checkOwnablePermission('chapter-update', $chapter);
115
116         $this->chapterRepo->update($chapter, $request->all());
117
118         return redirect($chapter->getUrl());
119     }
120
121     /**
122      * Shows the page to confirm deletion of this chapter.
123      *
124      * @throws NotFoundException
125      */
126     public function showDelete(string $bookSlug, string $chapterSlug)
127     {
128         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
129         $this->checkOwnablePermission('chapter-delete', $chapter);
130
131         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
132
133         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
134     }
135
136     /**
137      * Remove the specified chapter from storage.
138      *
139      * @throws NotFoundException
140      * @throws Throwable
141      */
142     public function destroy(string $bookSlug, string $chapterSlug)
143     {
144         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
145         $this->checkOwnablePermission('chapter-delete', $chapter);
146
147         $this->chapterRepo->destroy($chapter);
148
149         return redirect($chapter->book->getUrl());
150     }
151
152     /**
153      * Show the page for moving a chapter.
154      *
155      * @throws NotFoundException
156      */
157     public function showMove(string $bookSlug, string $chapterSlug)
158     {
159         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
160         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
161         $this->checkOwnablePermission('chapter-update', $chapter);
162         $this->checkOwnablePermission('chapter-delete', $chapter);
163
164         return view('chapters.move', [
165             'chapter' => $chapter,
166             'book'    => $chapter->book,
167         ]);
168     }
169
170     /**
171      * Perform the move action for a chapter.
172      *
173      * @throws NotFoundException
174      */
175     public function move(Request $request, string $bookSlug, string $chapterSlug)
176     {
177         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
178         $this->checkOwnablePermission('chapter-update', $chapter);
179         $this->checkOwnablePermission('chapter-delete', $chapter);
180
181         $entitySelection = $request->get('entity_selection', null);
182         if ($entitySelection === null || $entitySelection === '') {
183             return redirect($chapter->getUrl());
184         }
185
186         try {
187             $newBook = $this->chapterRepo->move($chapter, $entitySelection);
188         } catch (PermissionsException $exception) {
189             $this->showPermissionError();
190         } catch (MoveOperationException $exception) {
191             $this->showErrorNotification(trans('errors.selected_book_not_found'));
192
193             return redirect()->back();
194         }
195
196         return redirect($chapter->getUrl());
197     }
198
199     /**
200      * Show the view to copy a chapter.
201      *
202      * @throws NotFoundException
203      */
204     public function showCopy(string $bookSlug, string $chapterSlug)
205     {
206         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
207         $this->checkOwnablePermission('chapter-view', $chapter);
208
209         session()->flashInput(['name' => $chapter->name]);
210
211         return view('chapters.copy', [
212             'book'    => $chapter->book,
213             'chapter' => $chapter,
214         ]);
215     }
216
217     /**
218      * Create a copy of a chapter within the requested target destination.
219      *
220      * @throws NotFoundException
221      * @throws Throwable
222      */
223     public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
224     {
225         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
226         $this->checkOwnablePermission('chapter-view', $chapter);
227
228         $entitySelection = $request->get('entity_selection') ?: null;
229         $newParentBook = $entitySelection ? $this->chapterRepo->findParentByIdentifier($entitySelection) : $chapter->getParent();
230
231         if (is_null($newParentBook)) {
232             $this->showErrorNotification(trans('errors.selected_book_not_found'));
233
234             return redirect()->back();
235         }
236
237         $this->checkOwnablePermission('chapter-create', $newParentBook);
238
239         $newName = $request->get('name') ?: $chapter->name;
240         $chapterCopy = $cloner->cloneChapter($chapter, $newParentBook, $newName);
241         $this->showSuccessNotification(trans('entities.chapters_copy_success'));
242
243         return redirect($chapterCopy->getUrl());
244     }
245
246     /**
247      * Convert the chapter to a book.
248      */
249     public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
250     {
251         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
252         $this->checkOwnablePermission('chapter-update', $chapter);
253         $this->checkOwnablePermission('chapter-delete', $chapter);
254         $this->checkPermission('book-create-all');
255
256         $book = $transformer->transformChapterToBook($chapter);
257
258         return redirect($book->getUrl());
259     }
260 }