1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Repos\EntityRepo;
6 use Illuminate\Http\Request;
7 use Illuminate\Http\Response;
10 class ChapterController extends Controller
14 protected $entityRepo;
17 * ChapterController constructor.
18 * @param EntityRepo $entityRepo
19 * @param UserRepo $userRepo
21 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo)
23 $this->entityRepo = $entityRepo;
24 $this->userRepo = $userRepo;
25 parent::__construct();
29 * Show the form for creating a new chapter.
33 public function create($bookSlug)
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]);
42 * Store a newly created chapter in storage.
43 * @param Request $request
44 * @param string $bookSlug
46 * @throws \BookStack\Exceptions\NotFoundException
47 * @throws \Illuminate\Validation\ValidationException
49 public function store(Request $request, string $bookSlug)
51 $this->validate($request, [
52 'name' => 'required|string|max:255'
55 $book = $this->entityRepo->getEntityBySlug('book', $bookSlug);
56 $this->checkOwnablePermission('chapter-create', $book);
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());
66 * Display the specified chapter.
71 public function show($bookSlug, $chapterSlug)
73 $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
74 $this->checkOwnablePermission('chapter-view', $chapter);
75 $sidebarTree = $this->entityRepo->getBookChildren($chapter->book);
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,
89 * Show the form for editing the specified chapter.
94 public function edit($bookSlug, $chapterSlug)
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]);
103 * Update the specified chapter in storage.
104 * @param Request $request
105 * @param string $bookSlug
106 * @param string $chapterSlug
108 * @throws \BookStack\Exceptions\NotFoundException
110 public function update(Request $request, string $bookSlug, string $chapterSlug)
112 $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
113 $this->checkOwnablePermission('chapter-update', $chapter);
115 $this->entityRepo->updateFromInput($chapter, $request->all());
116 Activity::add($chapter, 'chapter_update', $chapter->book->id);
117 return redirect($chapter->getUrl());
121 * Shows the page to confirm deletion of this chapter.
123 * @param $chapterSlug
124 * @return \Illuminate\View\View
126 public function showDelete($bookSlug, $chapterSlug)
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]);
135 * Remove the specified chapter from storage.
137 * @param $chapterSlug
140 public function destroy($bookSlug, $chapterSlug)
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());
151 * Show the page for moving a chapter.
153 * @param $chapterSlug
155 * @throws \BookStack\Exceptions\NotFoundException
157 public function showMove($bookSlug, $chapterSlug)
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
170 * Perform the move action for a chapter.
171 * @param Request $request
172 * @param string $bookSlug
173 * @param string $chapterSlug
175 * @throws \BookStack\Exceptions\NotFoundException
177 public function move(Request $request, string $bookSlug, string $chapterSlug)
179 $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug);
180 $this->checkOwnablePermission('chapter-update', $chapter);
181 $this->checkOwnablePermission('chapter-delete', $chapter);
183 $entitySelection = $request->get('entity_selection', null);
184 if ($entitySelection === null || $entitySelection === '') {
185 return redirect($chapter->getUrl());
188 $stringExploded = explode(':', $entitySelection);
189 $entityType = $stringExploded[0];
190 $entityId = intval($stringExploded[1]);
194 if ($entityType == 'book') {
195 $parent = $this->entityRepo->getById('book', $entityId);
198 if ($parent === false || $parent === null) {
199 $this->showErrorNotification( trans('errors.selected_book_not_found'));
200 return redirect()->back();
203 $this->entityRepo->changeBook($chapter, $parent->id);
204 $chapter->rebuildPermissions();
206 Activity::add($chapter, 'chapter_move', $chapter->book->id);
207 $this->showSuccessNotification( trans('entities.chapter_move_success', ['bookName' => $parent->name]));
209 return redirect($chapter->getUrl());
213 * Show the Restrictions view.
215 * @param $chapterSlug
216 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
217 * @throws \BookStack\Exceptions\NotFoundException
219 public function showPermissions($bookSlug, $chapterSlug)
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,
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
239 public function permissions(Request $request, string $bookSlug, string $chapterSlug)
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());