1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Repos\UserRepo;
5 use Illuminate\Http\Request;
6 use BookStack\Repos\BookRepo;
7 use BookStack\Repos\ChapterRepo;
8 use Illuminate\Http\Response;
11 class ChapterController extends Controller
15 protected $chapterRepo;
19 * ChapterController constructor.
20 * @param BookRepo $bookRepo
21 * @param ChapterRepo $chapterRepo
22 * @param UserRepo $userRepo
24 public function __construct(BookRepo $bookRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
26 $this->bookRepo = $bookRepo;
27 $this->chapterRepo = $chapterRepo;
28 $this->userRepo = $userRepo;
29 parent::__construct();
33 * Show the form for creating a new chapter.
37 public function create($bookSlug)
39 $book = $this->bookRepo->getBySlug($bookSlug);
40 $this->checkOwnablePermission('chapter-create', $book);
41 $this->setPageTitle(trans('entities.chapters_create'));
42 return view('chapters/create', ['book' => $book, 'current' => $book]);
46 * Store a newly created chapter in storage.
48 * @param Request $request
51 public function store($bookSlug, Request $request)
53 $this->validate($request, [
54 'name' => 'required|string|max:255'
57 $book = $this->bookRepo->getBySlug($bookSlug);
58 $this->checkOwnablePermission('chapter-create', $book);
60 $input = $request->all();
61 $input['priority'] = $this->bookRepo->getNewPriority($book);
62 $chapter = $this->chapterRepo->createFromInput($input, $book);
63 Activity::add($chapter, 'chapter_create', $book->id);
64 return redirect($chapter->getUrl());
68 * Display the specified chapter.
73 public function show($bookSlug, $chapterSlug)
75 $book = $this->bookRepo->getBySlug($bookSlug);
76 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
77 $this->checkOwnablePermission('chapter-view', $chapter);
78 $sidebarTree = $this->bookRepo->getChildren($book);
80 $this->setPageTitle($chapter->getShortName());
81 $pages = $this->chapterRepo->getChildren($chapter);
82 return view('chapters/show', [
84 'chapter' => $chapter,
85 'current' => $chapter,
86 'sidebarTree' => $sidebarTree,
92 * Show the form for editing the specified chapter.
97 public function edit($bookSlug, $chapterSlug)
99 $book = $this->bookRepo->getBySlug($bookSlug);
100 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
101 $this->checkOwnablePermission('chapter-update', $chapter);
102 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
103 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
107 * Update the specified chapter in storage.
108 * @param Request $request
110 * @param $chapterSlug
113 public function update(Request $request, $bookSlug, $chapterSlug)
115 $book = $this->bookRepo->getBySlug($bookSlug);
116 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
117 $this->checkOwnablePermission('chapter-update', $chapter);
118 if ($chapter->name !== $request->get('name')) {
119 $chapter->slug = $this->chapterRepo->findSuitableSlug($request->get('name'), $book->id, $chapter->id);
121 $chapter->fill($request->all());
122 $chapter->updated_by = user()->id;
124 Activity::add($chapter, 'chapter_update', $book->id);
125 return redirect($chapter->getUrl());
129 * Shows the page to confirm deletion of this chapter.
131 * @param $chapterSlug
132 * @return \Illuminate\View\View
134 public function showDelete($bookSlug, $chapterSlug)
136 $book = $this->bookRepo->getBySlug($bookSlug);
137 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
138 $this->checkOwnablePermission('chapter-delete', $chapter);
139 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
140 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
144 * Remove the specified chapter from storage.
146 * @param $chapterSlug
149 public function destroy($bookSlug, $chapterSlug)
151 $book = $this->bookRepo->getBySlug($bookSlug);
152 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
153 $this->checkOwnablePermission('chapter-delete', $chapter);
154 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
155 $this->chapterRepo->destroy($chapter);
156 return redirect($book->getUrl());
160 * Show the page for moving a chapter.
162 * @param $chapterSlug
164 * @throws \BookStack\Exceptions\NotFoundException
166 public function showMove($bookSlug, $chapterSlug) {
167 $book = $this->bookRepo->getBySlug($bookSlug);
168 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
169 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
170 $this->checkOwnablePermission('chapter-update', $chapter);
171 return view('chapters/move', [
172 'chapter' => $chapter,
178 * Perform the move action for a chapter.
180 * @param $chapterSlug
181 * @param Request $request
183 * @throws \BookStack\Exceptions\NotFoundException
185 public function move($bookSlug, $chapterSlug, Request $request) {
186 $book = $this->bookRepo->getBySlug($bookSlug);
187 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
188 $this->checkOwnablePermission('chapter-update', $chapter);
190 $entitySelection = $request->get('entity_selection', null);
191 if ($entitySelection === null || $entitySelection === '') {
192 return redirect($chapter->getUrl());
195 $stringExploded = explode(':', $entitySelection);
196 $entityType = $stringExploded[0];
197 $entityId = intval($stringExploded[1]);
201 if ($entityType == 'book') {
202 $parent = $this->bookRepo->getById($entityId);
205 if ($parent === false || $parent === null) {
206 session()->flash('error', trans('errors.selected_book_not_found'));
207 return redirect()->back();
210 $this->chapterRepo->changeBook($parent->id, $chapter, true);
211 Activity::add($chapter, 'chapter_move', $chapter->book->id);
212 session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
214 return redirect($chapter->getUrl());
218 * Show the Restrictions view.
220 * @param $chapterSlug
221 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
223 public function showRestrict($bookSlug, $chapterSlug)
225 $book = $this->bookRepo->getBySlug($bookSlug);
226 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
227 $this->checkOwnablePermission('restrictions-manage', $chapter);
228 $roles = $this->userRepo->getRestrictableRoles();
229 return view('chapters/restrictions', [
230 'chapter' => $chapter,
236 * Set the restrictions for this chapter.
238 * @param $chapterSlug
239 * @param Request $request
240 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
242 public function restrict($bookSlug, $chapterSlug, Request $request)
244 $book = $this->bookRepo->getBySlug($bookSlug);
245 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
246 $this->checkOwnablePermission('restrictions-manage', $chapter);
247 $this->chapterRepo->updateEntityPermissionsFromRequest($request, $chapter);
248 session()->flash('success', trans('entities.chapters_permissions_success'));
249 return redirect($chapter->getUrl());