]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterController.php
899569a6f7149008ba0a5df904e56fc5efe15e17
[bookstack] / app / Entities / Controllers / ChapterController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Activity\Models\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\Exceptions\MoveOperationException;
13 use BookStack\Exceptions\NotFoundException;
14 use BookStack\Exceptions\PermissionsException;
15 use BookStack\Http\Controllers\Controller;
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     public function __construct(ChapterRepo $chapterRepo, ReferenceFetcher $referenceFetcher)
27     {
28         $this->chapterRepo = $chapterRepo;
29         $this->referenceFetcher = $referenceFetcher;
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             'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
88         ]);
89     }
90
91     /**
92      * Show the form for editing the specified chapter.
93      */
94     public function edit(string $bookSlug, string $chapterSlug)
95     {
96         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
97         $this->checkOwnablePermission('chapter-update', $chapter);
98
99         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
100
101         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
102     }
103
104     /**
105      * Update the specified chapter in storage.
106      *
107      * @throws NotFoundException
108      */
109     public function update(Request $request, string $bookSlug, string $chapterSlug)
110     {
111         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
112         $this->checkOwnablePermission('chapter-update', $chapter);
113
114         $this->chapterRepo->update($chapter, $request->all());
115
116         return redirect($chapter->getUrl());
117     }
118
119     /**
120      * Shows the page to confirm deletion of this chapter.
121      *
122      * @throws NotFoundException
123      */
124     public function showDelete(string $bookSlug, string $chapterSlug)
125     {
126         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
127         $this->checkOwnablePermission('chapter-delete', $chapter);
128
129         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
130
131         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
132     }
133
134     /**
135      * Remove the specified chapter from storage.
136      *
137      * @throws NotFoundException
138      * @throws Throwable
139      */
140     public function destroy(string $bookSlug, string $chapterSlug)
141     {
142         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
143         $this->checkOwnablePermission('chapter-delete', $chapter);
144
145         $this->chapterRepo->destroy($chapter);
146
147         return redirect($chapter->book->getUrl());
148     }
149
150     /**
151      * Show the page for moving a chapter.
152      *
153      * @throws NotFoundException
154      */
155     public function showMove(string $bookSlug, string $chapterSlug)
156     {
157         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
158         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
159         $this->checkOwnablePermission('chapter-update', $chapter);
160         $this->checkOwnablePermission('chapter-delete', $chapter);
161
162         return view('chapters.move', [
163             'chapter' => $chapter,
164             'book'    => $chapter->book,
165         ]);
166     }
167
168     /**
169      * Perform the move action for a chapter.
170      *
171      * @throws NotFoundException
172      */
173     public function move(Request $request, string $bookSlug, string $chapterSlug)
174     {
175         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
176         $this->checkOwnablePermission('chapter-update', $chapter);
177         $this->checkOwnablePermission('chapter-delete', $chapter);
178
179         $entitySelection = $request->get('entity_selection', null);
180         if ($entitySelection === null || $entitySelection === '') {
181             return redirect($chapter->getUrl());
182         }
183
184         try {
185             $newBook = $this->chapterRepo->move($chapter, $entitySelection);
186         } catch (PermissionsException $exception) {
187             $this->showPermissionError();
188         } catch (MoveOperationException $exception) {
189             $this->showErrorNotification(trans('errors.selected_book_not_found'));
190
191             return redirect()->back();
192         }
193
194         $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
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 }