]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Added force option for update-url command
[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\Exceptions\MoveOperationException;
13 use BookStack\Exceptions\NotFoundException;
14 use BookStack\Exceptions\PermissionsException;
15 use BookStack\References\ReferenceFetcher;
16 use Illuminate\Http\Request;
17 use Illuminate\Validation\ValidationException;
18 use Throwable;
19
20 class ChapterController extends Controller
21 {
22     protected ChapterRepo $chapterRepo;
23     protected ReferenceFetcher $referenceFetcher;
24
25     public function __construct(ChapterRepo $chapterRepo, ReferenceFetcher $referenceFetcher)
26     {
27         $this->chapterRepo = $chapterRepo;
28         $this->referenceFetcher = $referenceFetcher;
29     }
30
31     /**
32      * Show the form for creating a new chapter.
33      */
34     public function create(string $bookSlug)
35     {
36         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
37         $this->checkOwnablePermission('chapter-create', $book);
38
39         $this->setPageTitle(trans('entities.chapters_create'));
40
41         return view('chapters.create', ['book' => $book, 'current' => $book]);
42     }
43
44     /**
45      * Store a newly created chapter in storage.
46      *
47      * @throws ValidationException
48      */
49     public function store(Request $request, string $bookSlug)
50     {
51         $this->validate($request, [
52             'name' => ['required', 'string', 'max:255'],
53         ]);
54
55         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
56         $this->checkOwnablePermission('chapter-create', $book);
57
58         $chapter = $this->chapterRepo->create($request->all(), $book);
59
60         return redirect($chapter->getUrl());
61     }
62
63     /**
64      * Display the specified chapter.
65      */
66     public function show(string $bookSlug, string $chapterSlug)
67     {
68         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
69         $this->checkOwnablePermission('chapter-view', $chapter);
70
71         $sidebarTree = (new BookContents($chapter->book))->getTree();
72         $pages = $chapter->getVisiblePages();
73         $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
74         View::incrementFor($chapter);
75
76         $this->setPageTitle($chapter->getShortName());
77
78         return view('chapters.show', [
79             'book'           => $chapter->book,
80             'chapter'        => $chapter,
81             'current'        => $chapter,
82             'sidebarTree'    => $sidebarTree,
83             'pages'          => $pages,
84             'next'           => $nextPreviousLocator->getNext(),
85             'previous'       => $nextPreviousLocator->getPrevious(),
86             'referenceCount' => $this->referenceFetcher->getPageReferenceCountToEntity($chapter),
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      * Convert the chapter to a book.
247      */
248     public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
249     {
250         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
251         $this->checkOwnablePermission('chapter-update', $chapter);
252         $this->checkOwnablePermission('chapter-delete', $chapter);
253         $this->checkPermission('book-create-all');
254
255         $book = $transformer->transformChapterToBook($chapter);
256
257         return redirect($book->getUrl());
258     }
259 }