]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Show bookshelves that a book belongs to on a book view
[bookstack] / app / Http / Controllers / ChapterController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Repos\EntityRepo;
6 use Illuminate\Http\Request;
7 use Illuminate\Http\Response;
8 use Views;
9
10 class ChapterController extends Controller
11 {
12
13     protected $userRepo;
14     protected $entityRepo;
15
16     /**
17      * ChapterController constructor.
18      * @param EntityRepo $entityRepo
19      * @param UserRepo $userRepo
20      */
21     public function __construct(EntityRepo $entityRepo, UserRepo $userRepo)
22     {
23         $this->entityRepo = $entityRepo;
24         $this->userRepo = $userRepo;
25         parent::__construct();
26     }
27
28     /**
29      * Show the form for creating a new chapter.
30      * @param $bookSlug
31      * @return Response
32      */
33     public function create($bookSlug)
34     {
35         $book = $this->entityRepo->getEntityBySlug('book', $bookSlug);
36         $this->checkOwnablePermission('chapter-create', $book);
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      * @param Request $request
44      * @param string $bookSlug
45      * @return Response
46      * @throws \BookStack\Exceptions\NotFoundException
47      * @throws \Illuminate\Validation\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 = $this->entityRepo->getEntityBySlug('book', $bookSlug);
56         $this->checkOwnablePermission('chapter-create', $book);
57
58         $input = $request->all();
59         $input['priority'] = $this->entityRepo->getNewBookPriority($book);
60         $chapter = $this->entityRepo->createFromInput('chapter', $input, $book);
61         Activity::add($chapter, 'chapter_create', $book->id);
62         return redirect($chapter->getUrl());
63     }
64
65     /**
66      * Display the specified chapter.
67      * @param $bookSlug
68      * @param $chapterSlug
69      * @return Response
70      */
71     public function show($bookSlug, $chapterSlug)
72     {
73         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
74         $this->checkOwnablePermission('chapter-view', $chapter);
75         $sidebarTree = $this->entityRepo->getBookChildren($chapter->book);
76         Views::add($chapter);
77         $this->setPageTitle($chapter->getShortName());
78         $pages = $this->entityRepo->getChapterChildren($chapter);
79         return view('chapters.show', [
80             'book' => $chapter->book,
81             'chapter' => $chapter,
82             'current' => $chapter,
83             'sidebarTree' => $sidebarTree,
84             'pages' => $pages
85         ]);
86     }
87
88     /**
89      * Show the form for editing the specified chapter.
90      * @param $bookSlug
91      * @param $chapterSlug
92      * @return Response
93      */
94     public function edit($bookSlug, $chapterSlug)
95     {
96         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
97         $this->checkOwnablePermission('chapter-update', $chapter);
98         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
99         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
100     }
101
102     /**
103      * Update the specified chapter in storage.
104      * @param Request $request
105      * @param string $bookSlug
106      * @param string $chapterSlug
107      * @return Response
108      * @throws \BookStack\Exceptions\NotFoundException
109      */
110     public function update(Request $request, string $bookSlug, string $chapterSlug)
111     {
112         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
113         $this->checkOwnablePermission('chapter-update', $chapter);
114
115         $this->entityRepo->updateFromInput($chapter, $request->all());
116         Activity::add($chapter, 'chapter_update', $chapter->book->id);
117         return redirect($chapter->getUrl());
118     }
119
120     /**
121      * Shows the page to confirm deletion of this chapter.
122      * @param $bookSlug
123      * @param $chapterSlug
124      * @return \Illuminate\View\View
125      */
126     public function showDelete($bookSlug, $chapterSlug)
127     {
128         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
129         $this->checkOwnablePermission('chapter-delete', $chapter);
130         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
131         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
132     }
133
134     /**
135      * Remove the specified chapter from storage.
136      * @param $bookSlug
137      * @param $chapterSlug
138      * @return Response
139      */
140     public function destroy($bookSlug, $chapterSlug)
141     {
142         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
143         $book = $chapter->book;
144         $this->checkOwnablePermission('chapter-delete', $chapter);
145         Activity::addMessage('chapter_delete', $chapter->name, $book->id);
146         $this->entityRepo->destroyChapter($chapter);
147         return redirect($book->getUrl());
148     }
149
150     /**
151      * Show the page for moving a chapter.
152      * @param $bookSlug
153      * @param $chapterSlug
154      * @return mixed
155      * @throws \BookStack\Exceptions\NotFoundException
156      */
157     public function showMove($bookSlug, $chapterSlug)
158     {
159         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
160         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
161         $this->checkOwnablePermission('chapter-update', $chapter);
162         $this->checkOwnablePermission('chapter-delete', $chapter);
163         return view('chapters.move', [
164             'chapter' => $chapter,
165             'book' => $chapter->book
166         ]);
167     }
168
169     /**
170      * Perform the move action for a chapter.
171      * @param Request $request
172      * @param string $bookSlug
173      * @param string $chapterSlug
174      * @return mixed
175      * @throws \BookStack\Exceptions\NotFoundException
176      */
177     public function move(Request $request, string $bookSlug, string $chapterSlug)
178     {
179         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
180         $this->checkOwnablePermission('chapter-update', $chapter);
181         $this->checkOwnablePermission('chapter-delete', $chapter);
182
183         $entitySelection = $request->get('entity_selection', null);
184         if ($entitySelection === null || $entitySelection === '') {
185             return redirect($chapter->getUrl());
186         }
187
188         $stringExploded = explode(':', $entitySelection);
189         $entityType = $stringExploded[0];
190         $entityId = intval($stringExploded[1]);
191
192         $parent = false;
193
194         if ($entityType == 'book') {
195             $parent = $this->entityRepo->getById('book', $entityId);
196         }
197
198         if ($parent === false || $parent === null) {
199             $this->showErrorNotification( trans('errors.selected_book_not_found'));
200             return redirect()->back();
201         }
202
203         $this->entityRepo->changeBook($chapter, $parent->id);
204         $chapter->rebuildPermissions();
205
206         Activity::add($chapter, 'chapter_move', $chapter->book->id);
207         $this->showSuccessNotification( trans('entities.chapter_move_success', ['bookName' => $parent->name]));
208
209         return redirect($chapter->getUrl());
210     }
211
212     /**
213      * Show the Restrictions view.
214      * @param $bookSlug
215      * @param $chapterSlug
216      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
217      * @throws \BookStack\Exceptions\NotFoundException
218      */
219     public function showPermissions($bookSlug, $chapterSlug)
220     {
221         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
222         $this->checkOwnablePermission('restrictions-manage', $chapter);
223         $roles = $this->userRepo->getRestrictableRoles();
224         return view('chapters.permissions', [
225             'chapter' => $chapter,
226             'roles' => $roles
227         ]);
228     }
229
230     /**
231      * Set the restrictions for this chapter.
232      * @param Request $request
233      * @param string $bookSlug
234      * @param string $chapterSlug
235      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
236      * @throws \BookStack\Exceptions\NotFoundException
237      * @throws \Throwable
238      */
239     public function permissions(Request $request, string $bookSlug, string $chapterSlug)
240     {
241         $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
242         $this->checkOwnablePermission('restrictions-manage', $chapter);
243         $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
244         $this->showSuccessNotification( trans('entities.chapters_permissions_success'));
245         return redirect($chapter->getUrl());
246     }
247 }