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