1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Repos\EntityRepo;
5 use BookStack\Repos\UserRepo;
6 use BookStack\Services\ExportService;
7 use Illuminate\Http\Request;
8 use Illuminate\Http\Response;
11 class ChapterController extends Controller
15 protected $entityRepo;
16 protected $exportService;
19 * ChapterController constructor.
20 * @param EntityRepo $entityRepo
21 * @param UserRepo $userRepo
22 * @param ExportService $exportService
24 public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
26 $this->entityRepo = $entityRepo;
27 $this->userRepo = $userRepo;
28 $this->exportService = $exportService;
29 parent::__construct();
33 * Show the form for creating a new chapter.
37 public function create($bookSlug)
39 $book = $this->entityRepo->getBySlug('book', $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->entityRepo->getBySlug('book', $bookSlug);
58 $this->checkOwnablePermission('chapter-create', $book);
60 $input = $request->all();
61 $input['priority'] = $this->entityRepo->getNewBookPriority($book);
62 $chapter = $this->entityRepo->createFromInput('chapter', $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 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
76 $this->checkOwnablePermission('chapter-view', $chapter);
77 $sidebarTree = $this->entityRepo->getBookChildren($chapter->book);
79 $this->setPageTitle($chapter->getShortName());
80 $pages = $this->entityRepo->getChapterChildren($chapter);
81 return view('chapters/show', [
82 'book' => $chapter->book,
83 'chapter' => $chapter,
84 'current' => $chapter,
85 'sidebarTree' => $sidebarTree,
91 * Show the form for editing the specified chapter.
96 public function edit($bookSlug, $chapterSlug)
98 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
99 $this->checkOwnablePermission('chapter-update', $chapter);
100 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
101 return view('chapters/edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
105 * Update the specified chapter in storage.
106 * @param Request $request
108 * @param $chapterSlug
111 public function update(Request $request, $bookSlug, $chapterSlug)
113 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
114 $this->checkOwnablePermission('chapter-update', $chapter);
115 if ($chapter->name !== $request->get('name')) {
116 $chapter->slug = $this->entityRepo->findSuitableSlug('chapter', $request->get('name'), $chapter->id, $chapter->book->id);
118 $chapter->fill($request->all());
119 $chapter->updated_by = user()->id;
121 Activity::add($chapter, 'chapter_update', $chapter->book->id);
122 return redirect($chapter->getUrl());
126 * Shows the page to confirm deletion of this chapter.
128 * @param $chapterSlug
129 * @return \Illuminate\View\View
131 public function showDelete($bookSlug, $chapterSlug)
133 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
134 $this->checkOwnablePermission('chapter-delete', $chapter);
135 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
136 return view('chapters/delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
140 * Remove the specified chapter from storage.
142 * @param $chapterSlug
145 public function destroy($bookSlug, $chapterSlug)
147 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
148 $book = $chapter->book;
149 $this->checkOwnablePermission('chapter-delete', $chapter);
150 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
151 $this->entityRepo->destroyChapter($chapter);
152 return redirect($book->getUrl());
156 * Show the page for moving a chapter.
158 * @param $chapterSlug
160 * @throws \BookStack\Exceptions\NotFoundException
162 public function showMove($bookSlug, $chapterSlug) {
163 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
164 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
165 $this->checkOwnablePermission('chapter-update', $chapter);
166 return view('chapters/move', [
167 'chapter' => $chapter,
168 'book' => $chapter->book
173 * Perform the move action for a chapter.
175 * @param $chapterSlug
176 * @param Request $request
178 * @throws \BookStack\Exceptions\NotFoundException
180 public function move($bookSlug, $chapterSlug, Request $request) {
181 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
182 $this->checkOwnablePermission('chapter-update', $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
217 public function showRestrict($bookSlug, $chapterSlug)
219 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
220 $this->checkOwnablePermission('restrictions-manage', $chapter);
221 $roles = $this->userRepo->getRestrictableRoles();
222 return view('chapters/restrictions', [
223 'chapter' => $chapter,
229 * Set the restrictions for this chapter.
231 * @param $chapterSlug
232 * @param Request $request
233 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
235 public function restrict($bookSlug, $chapterSlug, Request $request)
237 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
238 $this->checkOwnablePermission('restrictions-manage', $chapter);
239 $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
240 session()->flash('success', trans('entities.chapters_permissions_success'));
241 return redirect($chapter->getUrl());
245 * Exports a chapter to pdf .
246 * @param string $bookSlug
247 * @param string $chapterSlug
248 * @return \Illuminate\Http\Response
250 public function exportPdf($bookSlug, $chapterSlug)
252 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
253 $pdfContent = $this->exportService->chapterToPdf($chapter);
254 return response()->make($pdfContent, 200, [
255 'Content-Type' => 'application/octet-stream',
256 'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.pdf'
261 * Export a chapter to a self-contained HTML file.
262 * @param string $bookSlug
263 * @param string $chapterSlug
264 * @return \Illuminate\Http\Response
266 public function exportHtml($bookSlug, $chapterSlug)
268 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
269 $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
270 return response()->make($containedHtml, 200, [
271 'Content-Type' => 'application/octet-stream',
272 'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.html'
277 * Export a chapter to a simple plaintext .txt file.
278 * @param string $bookSlug
279 * @param string $chapterSlug
280 * @return \Illuminate\Http\Response
282 public function exportPlainText($bookSlug, $chapterSlug)
284 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
285 $containedHtml = $this->exportService->chapterToPlainText($chapter);
286 return response()->make($containedHtml, 200, [
287 'Content-Type' => 'application/octet-stream',
288 'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.txt'