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