1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Repos\EntityRepo;
6 use BookStack\Entities\ExportService;
7 use Illuminate\Http\Request;
8 use Illuminate\Http\Response;
11 class ChapterController extends Controller
15 protected $entityRepo;
18 * ChapterController constructor.
19 * @param EntityRepo $entityRepo
20 * @param UserRepo $userRepo
22 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo)
24 $this->entityRepo = $entityRepo;
25 $this->userRepo = $userRepo;
26 parent::__construct();
30 * Show the form for creating a new chapter.
34 public function create($bookSlug)
36 $book = $this->entityRepo->getBySlug('book', $bookSlug);
37 $this->checkOwnablePermission('chapter-create', $book);
38 $this->setPageTitle(trans('entities.chapters_create'));
39 return view('chapters.create', ['book' => $book, 'current' => $book]);
43 * Store a newly created chapter in storage.
44 * @param Request $request
45 * @param string $bookSlug
47 * @throws \BookStack\Exceptions\NotFoundException
48 * @throws \Illuminate\Validation\ValidationException
50 public function store(Request $request, string $bookSlug)
52 $this->validate($request, [
53 'name' => 'required|string|max:255'
56 $book = $this->entityRepo->getBySlug('book', $bookSlug);
57 $this->checkOwnablePermission('chapter-create', $book);
59 $input = $request->all();
60 $input['priority'] = $this->entityRepo->getNewBookPriority($book);
61 $chapter = $this->entityRepo->createFromInput('chapter', $input, $book);
62 Activity::add($chapter, 'chapter_create', $book->id);
63 return redirect($chapter->getUrl());
67 * Display the specified chapter.
72 public function show($bookSlug, $chapterSlug)
74 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
75 $this->checkOwnablePermission('chapter-view', $chapter);
76 $sidebarTree = $this->entityRepo->getBookChildren($chapter->book);
78 $this->setPageTitle($chapter->getShortName());
79 $pages = $this->entityRepo->getChapterChildren($chapter);
80 return view('chapters.show', [
81 'book' => $chapter->book,
82 'chapter' => $chapter,
83 'current' => $chapter,
84 'sidebarTree' => $sidebarTree,
90 * Show the form for editing the specified chapter.
95 public function edit($bookSlug, $chapterSlug)
97 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
98 $this->checkOwnablePermission('chapter-update', $chapter);
99 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
100 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
104 * Update the specified chapter in storage.
105 * @param Request $request
106 * @param string $bookSlug
107 * @param string $chapterSlug
109 * @throws \BookStack\Exceptions\NotFoundException
111 public function update(Request $request, string $bookSlug, string $chapterSlug)
113 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
114 $this->checkOwnablePermission('chapter-update', $chapter);
116 $this->entityRepo->updateFromInput('chapter', $chapter, $request->all());
117 Activity::add($chapter, 'chapter_update', $chapter->book->id);
118 return redirect($chapter->getUrl());
122 * Shows the page to confirm deletion of this chapter.
124 * @param $chapterSlug
125 * @return \Illuminate\View\View
127 public function showDelete($bookSlug, $chapterSlug)
129 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
130 $this->checkOwnablePermission('chapter-delete', $chapter);
131 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
132 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
136 * Remove the specified chapter from storage.
138 * @param $chapterSlug
141 public function destroy($bookSlug, $chapterSlug)
143 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
144 $book = $chapter->book;
145 $this->checkOwnablePermission('chapter-delete', $chapter);
146 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
147 $this->entityRepo->destroyChapter($chapter);
148 return redirect($book->getUrl());
152 * Show the page for moving a chapter.
154 * @param $chapterSlug
156 * @throws \BookStack\Exceptions\NotFoundException
158 public function showMove($bookSlug, $chapterSlug)
160 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
161 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
162 $this->checkOwnablePermission('chapter-update', $chapter);
163 $this->checkOwnablePermission('chapter-delete', $chapter);
164 return view('chapters.move', [
165 'chapter' => $chapter,
166 'book' => $chapter->book
171 * Perform the move action for a chapter.
172 * @param Request $request
173 * @param string $bookSlug
174 * @param string $chapterSlug
176 * @throws \BookStack\Exceptions\NotFoundException
178 public function move(Request $request, string $bookSlug, string $chapterSlug)
180 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
181 $this->checkOwnablePermission('chapter-update', $chapter);
182 $this->checkOwnablePermission('chapter-delete', $chapter);
184 $entitySelection = $request->get('entity_selection', null);
185 if ($entitySelection === null || $entitySelection === '') {
186 return redirect($chapter->getUrl());
189 $stringExploded = explode(':', $entitySelection);
190 $entityType = $stringExploded[0];
191 $entityId = intval($stringExploded[1]);
195 if ($entityType == 'book') {
196 $parent = $this->entityRepo->getById('book', $entityId);
199 if ($parent === false || $parent === null) {
200 session()->flash('error', trans('errors.selected_book_not_found'));
201 return redirect()->back();
204 $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
205 Activity::add($chapter, 'chapter_move', $chapter->book->id);
206 session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
208 return redirect($chapter->getUrl());
212 * Show the Restrictions view.
214 * @param $chapterSlug
215 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
216 * @throws \BookStack\Exceptions\NotFoundException
218 public function showPermissions($bookSlug, $chapterSlug)
220 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
221 $this->checkOwnablePermission('restrictions-manage', $chapter);
222 $roles = $this->userRepo->getRestrictableRoles();
223 return view('chapters.permissions', [
224 'chapter' => $chapter,
230 * Set the restrictions for this chapter.
231 * @param Request $request
232 * @param string $bookSlug
233 * @param string $chapterSlug
234 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
235 * @throws \BookStack\Exceptions\NotFoundException
238 public function permissions(Request $request, string $bookSlug, string $chapterSlug)
240 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
241 $this->checkOwnablePermission('restrictions-manage', $chapter);
242 $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
243 session()->flash('success', trans('entities.chapters_permissions_success'));
244 return redirect($chapter->getUrl());