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
217 * @throws \BookStack\Exceptions\NotFoundException
219 public function showPermissions($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.permissions', [
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
236 * @throws \BookStack\Exceptions\NotFoundException
239 public function permissions($bookSlug, $chapterSlug, Request $request)
241 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
242 $this->checkOwnablePermission('restrictions-manage', $chapter);
243 $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
244 session()->flash('success', trans('entities.chapters_permissions_success'));
245 return redirect($chapter->getUrl());
249 * Exports a chapter to pdf .
250 * @param string $bookSlug
251 * @param string $chapterSlug
252 * @return \Illuminate\Http\Response
254 public function exportPdf($bookSlug, $chapterSlug)
256 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
257 $pdfContent = $this->exportService->chapterToPdf($chapter);
258 return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
262 * Export a chapter to a self-contained HTML file.
263 * @param string $bookSlug
264 * @param string $chapterSlug
265 * @return \Illuminate\Http\Response
267 public function exportHtml($bookSlug, $chapterSlug)
269 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
270 $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
271 return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
275 * Export a chapter to a simple plaintext .txt file.
276 * @param string $bookSlug
277 * @param string $chapterSlug
278 * @return \Illuminate\Http\Response
280 public function exportPlainText($bookSlug, $chapterSlug)
282 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
283 $chapterText = $this->exportService->chapterToPlainText($chapter);
284 return $this->downloadResponse($chapterText, $chapterSlug . '.txt');