1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Repos\EntityRepo;
6 use BookStack\Entities\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 \BookStack\Entities\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 $this->checkOwnablePermission('chapter-delete', $chapter);
165 return view('chapters/move', [
166 'chapter' => $chapter,
167 'book' => $chapter->book
172 * Perform the move action for a chapter.
174 * @param $chapterSlug
175 * @param Request $request
177 * @throws \BookStack\Exceptions\NotFoundException
179 public function move($bookSlug, $chapterSlug, Request $request)
181 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
182 $this->checkOwnablePermission('chapter-update', $chapter);
183 $this->checkOwnablePermission('chapter-delete', $chapter);
185 $entitySelection = $request->get('entity_selection', null);
186 if ($entitySelection === null || $entitySelection === '') {
187 return redirect($chapter->getUrl());
190 $stringExploded = explode(':', $entitySelection);
191 $entityType = $stringExploded[0];
192 $entityId = intval($stringExploded[1]);
196 if ($entityType == 'book') {
197 $parent = $this->entityRepo->getById('book', $entityId);
200 if ($parent === false || $parent === null) {
201 session()->flash('error', trans('errors.selected_book_not_found'));
202 return redirect()->back();
205 $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
206 Activity::add($chapter, 'chapter_move', $chapter->book->id);
207 session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
209 return redirect($chapter->getUrl());
213 * Show the Restrictions view.
215 * @param $chapterSlug
216 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
218 public function showRestrict($bookSlug, $chapterSlug)
220 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
221 $this->checkOwnablePermission('restrictions-manage', $chapter);
222 $roles = $this->userRepo->getRestrictableRoles();
223 return view('chapters/restrictions', [
224 'chapter' => $chapter,
230 * Set the restrictions for this chapter.
232 * @param $chapterSlug
233 * @param Request $request
234 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
236 public function restrict($bookSlug, $chapterSlug, Request $request)
238 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
239 $this->checkOwnablePermission('restrictions-manage', $chapter);
240 $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
241 session()->flash('success', trans('entities.chapters_permissions_success'));
242 return redirect($chapter->getUrl());
246 * Exports a chapter to pdf .
247 * @param string $bookSlug
248 * @param string $chapterSlug
249 * @return \Illuminate\Http\Response
251 public function exportPdf($bookSlug, $chapterSlug)
253 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
254 $pdfContent = $this->exportService->chapterToPdf($chapter);
255 return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
259 * Export a chapter to a self-contained HTML file.
260 * @param string $bookSlug
261 * @param string $chapterSlug
262 * @return \Illuminate\Http\Response
264 public function exportHtml($bookSlug, $chapterSlug)
266 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
267 $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
268 return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
272 * Export a chapter to a simple plaintext .txt file.
273 * @param string $bookSlug
274 * @param string $chapterSlug
275 * @return \Illuminate\Http\Response
277 public function exportPlainText($bookSlug, $chapterSlug)
279 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
280 $chapterText = $this->exportService->chapterToPlainText($chapter);
281 return $this->downloadResponse($chapterText, $chapterSlug . '.txt');