1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Repos\UserRepo;
5 use Illuminate\Http\Request;
6 use BookStack\Http\Requests;
7 use BookStack\Repos\BookRepo;
8 use BookStack\Repos\ChapterRepo;
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('Create New Chapter');
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('Edit Chapter' . $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 $chapter->fill($request->all());
119 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
120 $chapter->updated_by = auth()->user()->id;
122 Activity::add($chapter, 'chapter_update', $book->id);
123 return redirect($chapter->getUrl());
127 * Shows the page to confirm deletion of this chapter.
129 * @param $chapterSlug
130 * @return \Illuminate\View\View
132 public function showDelete($bookSlug, $chapterSlug)
134 $book = $this->bookRepo->getBySlug($bookSlug);
135 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
136 $this->checkOwnablePermission('chapter-delete', $chapter);
137 $this->setPageTitle('Delete Chapter' . $chapter->getShortName());
138 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
142 * Remove the specified chapter from storage.
144 * @param $chapterSlug
147 public function destroy($bookSlug, $chapterSlug)
149 $book = $this->bookRepo->getBySlug($bookSlug);
150 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
151 $this->checkOwnablePermission('chapter-delete', $chapter);
152 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
153 $this->chapterRepo->destroy($chapter);
154 return redirect($book->getUrl());
158 * Show the page for moving a chapter.
160 * @param $chapterSlug
162 * @throws \BookStack\Exceptions\NotFoundException
164 public function showMove($bookSlug, $chapterSlug) {
165 $book = $this->bookRepo->getBySlug($bookSlug);
166 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
167 $this->checkOwnablePermission('chapter-update', $chapter);
168 return view('chapters/move', [
169 'chapter' => $chapter,
175 * Perform the move action for a chapter.
177 * @param $chapterSlug
178 * @param Request $request
180 * @throws \BookStack\Exceptions\NotFoundException
182 public function move($bookSlug, $chapterSlug, Request $request) {
183 $book = $this->bookRepo->getBySlug($bookSlug);
184 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
185 $this->checkOwnablePermission('chapter-update', $chapter);
187 $entitySelection = $request->get('entity_selection', null);
188 if ($entitySelection === null || $entitySelection === '') {
189 return redirect($chapter->getUrl());
192 $stringExploded = explode(':', $entitySelection);
193 $entityType = $stringExploded[0];
194 $entityId = intval($stringExploded[1]);
198 if ($entityType == 'book') {
199 $parent = $this->bookRepo->getById($entityId);
202 if ($parent === false || $parent === null) {
203 session()->flash('The selected Book was not found');
204 return redirect()->back();
207 $this->chapterRepo->changeBook($parent->id, $chapter);
208 Activity::add($chapter, 'chapter_move', $chapter->book->id);
209 session()->flash('success', sprintf('Chapter moved to "%s"', $parent->name));
211 return redirect($chapter->getUrl());
215 * Show the Restrictions view.
217 * @param $chapterSlug
218 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
220 public function showRestrict($bookSlug, $chapterSlug)
222 $book = $this->bookRepo->getBySlug($bookSlug);
223 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
224 $this->checkOwnablePermission('restrictions-manage', $chapter);
225 $roles = $this->userRepo->getRestrictableRoles();
226 return view('chapters/restrictions', [
227 'chapter' => $chapter,
233 * Set the restrictions for this chapter.
235 * @param $chapterSlug
236 * @param Request $request
237 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
239 public function restrict($bookSlug, $chapterSlug, Request $request)
241 $book = $this->bookRepo->getBySlug($bookSlug);
242 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
243 $this->checkOwnablePermission('restrictions-manage', $chapter);
244 $this->chapterRepo->updateEntityPermissionsFromRequest($request, $chapter);
245 session()->flash('success', 'Chapter Restrictions Updated');
246 return redirect($chapter->getUrl());