]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Apply fixes from StyleCI
[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\NextPreviousContentLocator;
10 use BookStack\Entities\Tools\PermissionsUpdater;
11 use BookStack\Exceptions\MoveOperationException;
12 use BookStack\Exceptions\NotFoundException;
13 use Illuminate\Http\Request;
14 use Illuminate\Validation\ValidationException;
15 use Throwable;
16
17 class ChapterController extends Controller
18 {
19     protected $chapterRepo;
20
21     /**
22      * ChapterController constructor.
23      */
24     public function __construct(ChapterRepo $chapterRepo)
25     {
26         $this->chapterRepo = $chapterRepo;
27     }
28
29     /**
30      * Show the form for creating a new chapter.
31      */
32     public function create(string $bookSlug)
33     {
34         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
35         $this->checkOwnablePermission('chapter-create', $book);
36
37         $this->setPageTitle(trans('entities.chapters_create'));
38
39         return view('chapters.create', ['book' => $book, 'current' => $book]);
40     }
41
42     /**
43      * Store a newly created chapter in storage.
44      *
45      * @throws ValidationException
46      */
47     public function store(Request $request, string $bookSlug)
48     {
49         $this->validate($request, [
50             'name' => 'required|string|max:255',
51         ]);
52
53         $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
54         $this->checkOwnablePermission('chapter-create', $book);
55
56         $chapter = $this->chapterRepo->create($request->all(), $book);
57
58         return redirect($chapter->getUrl());
59     }
60
61     /**
62      * Display the specified chapter.
63      */
64     public function show(string $bookSlug, string $chapterSlug)
65     {
66         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
67         $this->checkOwnablePermission('chapter-view', $chapter);
68
69         $sidebarTree = (new BookContents($chapter->book))->getTree();
70         $pages = $chapter->getVisiblePages();
71         $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
72         View::incrementFor($chapter);
73
74         $this->setPageTitle($chapter->getShortName());
75
76         return view('chapters.show', [
77             'book'        => $chapter->book,
78             'chapter'     => $chapter,
79             'current'     => $chapter,
80             'sidebarTree' => $sidebarTree,
81             'pages'       => $pages,
82             'next'        => $nextPreviousLocator->getNext(),
83             'previous'    => $nextPreviousLocator->getPrevious(),
84         ]);
85     }
86
87     /**
88      * Show the form for editing the specified chapter.
89      */
90     public function edit(string $bookSlug, string $chapterSlug)
91     {
92         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
93         $this->checkOwnablePermission('chapter-update', $chapter);
94
95         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
96
97         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
98     }
99
100     /**
101      * Update the specified chapter in storage.
102      *
103      * @throws NotFoundException
104      */
105     public function update(Request $request, string $bookSlug, string $chapterSlug)
106     {
107         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
108         $this->checkOwnablePermission('chapter-update', $chapter);
109
110         $this->chapterRepo->update($chapter, $request->all());
111
112         return redirect($chapter->getUrl());
113     }
114
115     /**
116      * Shows the page to confirm deletion of this chapter.
117      *
118      * @throws NotFoundException
119      */
120     public function showDelete(string $bookSlug, string $chapterSlug)
121     {
122         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
123         $this->checkOwnablePermission('chapter-delete', $chapter);
124
125         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
126
127         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
128     }
129
130     /**
131      * Remove the specified chapter from storage.
132      *
133      * @throws NotFoundException
134      * @throws Throwable
135      */
136     public function destroy(string $bookSlug, string $chapterSlug)
137     {
138         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
139         $this->checkOwnablePermission('chapter-delete', $chapter);
140
141         $this->chapterRepo->destroy($chapter);
142
143         return redirect($chapter->book->getUrl());
144     }
145
146     /**
147      * Show the page for moving a chapter.
148      *
149      * @throws NotFoundException
150      */
151     public function showMove(string $bookSlug, string $chapterSlug)
152     {
153         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
154         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
155         $this->checkOwnablePermission('chapter-update', $chapter);
156         $this->checkOwnablePermission('chapter-delete', $chapter);
157
158         return view('chapters.move', [
159             'chapter' => $chapter,
160             'book'    => $chapter->book,
161         ]);
162     }
163
164     /**
165      * Perform the move action for a chapter.
166      *
167      * @throws NotFoundException
168      */
169     public function move(Request $request, string $bookSlug, string $chapterSlug)
170     {
171         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
172         $this->checkOwnablePermission('chapter-update', $chapter);
173         $this->checkOwnablePermission('chapter-delete', $chapter);
174
175         $entitySelection = $request->get('entity_selection', null);
176         if ($entitySelection === null || $entitySelection === '') {
177             return redirect($chapter->getUrl());
178         }
179
180         try {
181             $newBook = $this->chapterRepo->move($chapter, $entitySelection);
182         } catch (MoveOperationException $exception) {
183             $this->showErrorNotification(trans('errors.selected_book_not_found'));
184
185             return redirect()->back();
186         }
187
188         $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
189
190         return redirect($chapter->getUrl());
191     }
192
193     /**
194      * Show the Restrictions view.
195      *
196      * @throws NotFoundException
197      */
198     public function showPermissions(string $bookSlug, string $chapterSlug)
199     {
200         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
201         $this->checkOwnablePermission('restrictions-manage', $chapter);
202
203         return view('chapters.permissions', [
204             'chapter' => $chapter,
205         ]);
206     }
207
208     /**
209      * Set the restrictions for this chapter.
210      *
211      * @throws NotFoundException
212      */
213     public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
214     {
215         $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
216         $this->checkOwnablePermission('restrictions-manage', $chapter);
217
218         $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
219
220         $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
221
222         return redirect($chapter->getUrl());
223     }
224 }