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