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)
164 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
165 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
166 $this->checkOwnablePermission('chapter-update', $chapter);
167 return view('chapters/move', [
168 'chapter' => $chapter,
169 'book' => $chapter->book
174 * Perform the move action for a chapter.
176 * @param $chapterSlug
177 * @param Request $request
179 * @throws \BookStack\Exceptions\NotFoundException
181 public function move($bookSlug, $chapterSlug, Request $request)
183 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
184 $this->checkOwnablePermission('chapter-update', $chapter);
186 $entitySelection = $request->get('entity_selection', null);
187 if ($entitySelection === null || $entitySelection === '') {
188 return redirect($chapter->getUrl());
191 $stringExploded = explode(':', $entitySelection);
192 $entityType = $stringExploded[0];
193 $entityId = intval($stringExploded[1]);
197 if ($entityType == 'book') {
198 $parent = $this->entityRepo->getById('book', $entityId);
201 if ($parent === false || $parent === null) {
202 session()->flash('error', trans('errors.selected_book_not_found'));
203 return redirect()->back();
206 $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
207 Activity::add($chapter, 'chapter_move', $chapter->book->id);
208 session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
210 return redirect($chapter->getUrl());
214 * Show the Restrictions view.
216 * @param $chapterSlug
217 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
219 public function showRestrict($bookSlug, $chapterSlug)
221 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
222 $this->checkOwnablePermission('restrictions-manage', $chapter);
223 $roles = $this->userRepo->getRestrictableRoles();
224 return view('chapters/restrictions', [
225 'chapter' => $chapter,
231 * Set the restrictions for this chapter.
233 * @param $chapterSlug
234 * @param Request $request
235 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
237 public function restrict($bookSlug, $chapterSlug, Request $request)
239 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
240 $this->checkOwnablePermission('restrictions-manage', $chapter);
241 $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
242 session()->flash('success', trans('entities.chapters_permissions_success'));
243 return redirect($chapter->getUrl());
247 * Exports a chapter to pdf .
248 * @param string $bookSlug
249 * @param string $chapterSlug
250 * @return \Illuminate\Http\Response
252 public function exportPdf($bookSlug, $chapterSlug)
254 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
255 $pdfContent = $this->exportService->chapterToPdf($chapter);
256 return response()->make($pdfContent, 200, [
257 'Content-Type' => 'application/octet-stream',
258 'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.pdf'
263 * Export a chapter to a self-contained HTML file.
264 * @param string $bookSlug
265 * @param string $chapterSlug
266 * @return \Illuminate\Http\Response
268 public function exportHtml($bookSlug, $chapterSlug)
270 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
271 $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
272 return response()->make($containedHtml, 200, [
273 'Content-Type' => 'application/octet-stream',
274 'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.html'
279 * Export a chapter to a simple plaintext .txt file.
280 * @param string $bookSlug
281 * @param string $chapterSlug
282 * @return \Illuminate\Http\Response
284 public function exportPlainText($bookSlug, $chapterSlug)
286 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
287 $containedHtml = $this->exportService->chapterToPlainText($chapter);
288 return response()->make($containedHtml, 200, [
289 'Content-Type' => 'application/octet-stream',
290 'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.txt'