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
110 * @throws \BookStack\Exceptions\NotFoundException
112 public function update(Request $request, $bookSlug, $chapterSlug)
114 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
115 $this->checkOwnablePermission('chapter-update', $chapter);
117 $this->entityRepo->updateFromInput('chapter', $chapter, $request->all());
118 Activity::add($chapter, 'chapter_update', $chapter->book->id);
119 return redirect($chapter->getUrl());
123 * Shows the page to confirm deletion of this chapter.
125 * @param $chapterSlug
126 * @return \Illuminate\View\View
128 public function showDelete($bookSlug, $chapterSlug)
130 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
131 $this->checkOwnablePermission('chapter-delete', $chapter);
132 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
133 return view('chapters/delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
137 * Remove the specified chapter from storage.
139 * @param $chapterSlug
142 public function destroy($bookSlug, $chapterSlug)
144 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
145 $book = $chapter->book;
146 $this->checkOwnablePermission('chapter-delete', $chapter);
147 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
148 $this->entityRepo->destroyChapter($chapter);
149 return redirect($book->getUrl());
153 * Show the page for moving a chapter.
155 * @param $chapterSlug
157 * @throws \BookStack\Exceptions\NotFoundException
159 public function showMove($bookSlug, $chapterSlug)
161 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
162 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
163 $this->checkOwnablePermission('chapter-update', $chapter);
164 return view('chapters/move', [
165 'chapter' => $chapter,
166 'book' => $chapter->book
171 * Perform the move action for a chapter.
173 * @param $chapterSlug
174 * @param Request $request
176 * @throws \BookStack\Exceptions\NotFoundException
178 public function move($bookSlug, $chapterSlug, Request $request)
180 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
181 $this->checkOwnablePermission('chapter-update', $chapter);
183 $entitySelection = $request->get('entity_selection', null);
184 if ($entitySelection === null || $entitySelection === '') {
185 return redirect($chapter->getUrl());
188 $stringExploded = explode(':', $entitySelection);
189 $entityType = $stringExploded[0];
190 $entityId = intval($stringExploded[1]);
194 if ($entityType == 'book') {
195 $parent = $this->entityRepo->getById('book', $entityId);
198 if ($parent === false || $parent === null) {
199 session()->flash('error', trans('errors.selected_book_not_found'));
200 return redirect()->back();
203 $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
204 Activity::add($chapter, 'chapter_move', $chapter->book->id);
205 session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
207 return redirect($chapter->getUrl());
211 * Show the Restrictions view.
213 * @param $chapterSlug
214 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
216 public function showRestrict($bookSlug, $chapterSlug)
218 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
219 $this->checkOwnablePermission('restrictions-manage', $chapter);
220 $roles = $this->userRepo->getRestrictableRoles();
221 return view('chapters/restrictions', [
222 'chapter' => $chapter,
228 * Set the restrictions for this chapter.
230 * @param $chapterSlug
231 * @param Request $request
232 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
234 public function restrict($bookSlug, $chapterSlug, Request $request)
236 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
237 $this->checkOwnablePermission('restrictions-manage', $chapter);
238 $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
239 session()->flash('success', trans('entities.chapters_permissions_success'));
240 return redirect($chapter->getUrl());
244 * Exports a chapter to pdf .
245 * @param string $bookSlug
246 * @param string $chapterSlug
247 * @return \Illuminate\Http\Response
249 public function exportPdf($bookSlug, $chapterSlug)
251 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
252 $pdfContent = $this->exportService->chapterToPdf($chapter);
253 return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
257 * Export a chapter to a self-contained HTML file.
258 * @param string $bookSlug
259 * @param string $chapterSlug
260 * @return \Illuminate\Http\Response
262 public function exportHtml($bookSlug, $chapterSlug)
264 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
265 $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
266 return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
270 * Export a chapter to a simple plaintext .txt file.
271 * @param string $bookSlug
272 * @param string $chapterSlug
273 * @return \Illuminate\Http\Response
275 public function exportPlainText($bookSlug, $chapterSlug)
277 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
278 $chapterText = $this->exportService->chapterToPlainText($chapter);
279 return $this->downloadResponse($chapterText, $chapterSlug . '.txt');