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.
47 * @param Request $request
48 * @param string $bookSlug
50 * @throws \BookStack\Exceptions\NotFoundException
51 * @throws \Illuminate\Validation\ValidationException
53 public function store(Request $request, string $bookSlug)
55 $this->validate($request, [
56 'name' => 'required|string|max:255'
59 $book = $this->entityRepo->getBySlug('book', $bookSlug);
60 $this->checkOwnablePermission('chapter-create', $book);
62 $input = $request->all();
63 $input['priority'] = $this->entityRepo->getNewBookPriority($book);
64 $chapter = $this->entityRepo->createFromInput('chapter', $input, $book);
65 Activity::add($chapter, 'chapter_create', $book->id);
66 return redirect($chapter->getUrl());
70 * Display the specified chapter.
75 public function show($bookSlug, $chapterSlug)
77 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
78 $this->checkOwnablePermission('chapter-view', $chapter);
79 $sidebarTree = $this->entityRepo->getBookChildren($chapter->book);
81 $this->setPageTitle($chapter->getShortName());
82 $pages = $this->entityRepo->getChapterChildren($chapter);
83 return view('chapters.show', [
84 'book' => $chapter->book,
85 'chapter' => $chapter,
86 'current' => $chapter,
87 'sidebarTree' => $sidebarTree,
93 * Show the form for editing the specified chapter.
98 public function edit($bookSlug, $chapterSlug)
100 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
101 $this->checkOwnablePermission('chapter-update', $chapter);
102 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
103 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
107 * Update the specified chapter in storage.
108 * @param Request $request
109 * @param string $bookSlug
110 * @param string $chapterSlug
112 * @throws \BookStack\Exceptions\NotFoundException
114 public function update(Request $request, string $bookSlug, string $chapterSlug)
116 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
117 $this->checkOwnablePermission('chapter-update', $chapter);
119 $this->entityRepo->updateFromInput('chapter', $chapter, $request->all());
120 Activity::add($chapter, 'chapter_update', $chapter->book->id);
121 return redirect($chapter->getUrl());
125 * Shows the page to confirm deletion of this chapter.
127 * @param $chapterSlug
128 * @return \Illuminate\View\View
130 public function showDelete($bookSlug, $chapterSlug)
132 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
133 $this->checkOwnablePermission('chapter-delete', $chapter);
134 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
135 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
139 * Remove the specified chapter from storage.
141 * @param $chapterSlug
144 public function destroy($bookSlug, $chapterSlug)
146 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
147 $book = $chapter->book;
148 $this->checkOwnablePermission('chapter-delete', $chapter);
149 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
150 $this->entityRepo->destroyChapter($chapter);
151 return redirect($book->getUrl());
155 * Show the page for moving a chapter.
157 * @param $chapterSlug
159 * @throws \BookStack\Exceptions\NotFoundException
161 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 $this->checkOwnablePermission('chapter-delete', $chapter);
167 return view('chapters.move', [
168 'chapter' => $chapter,
169 'book' => $chapter->book
174 * Perform the move action for a chapter.
175 * @param Request $request
176 * @param string $bookSlug
177 * @param string $chapterSlug
179 * @throws \BookStack\Exceptions\NotFoundException
181 public function move(Request $request, string $bookSlug, string $chapterSlug)
183 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
184 $this->checkOwnablePermission('chapter-update', $chapter);
185 $this->checkOwnablePermission('chapter-delete', $chapter);
187 $entitySelection = $request->get('entity_selection', null);
188 if ($entitySelection === null || $entitySelection === '') {
189 return redirect($chapter->getUrl());
192 $stringExploded = explode(':', $entitySelection);
193 $entityType = $stringExploded[0];
194 $entityId = intval($stringExploded[1]);
198 if ($entityType == 'book') {
199 $parent = $this->entityRepo->getById('book', $entityId);
202 if ($parent === false || $parent === null) {
203 session()->flash('error', trans('errors.selected_book_not_found'));
204 return redirect()->back();
207 $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
208 Activity::add($chapter, 'chapter_move', $chapter->book->id);
209 session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
211 return redirect($chapter->getUrl());
215 * Show the Restrictions view.
217 * @param $chapterSlug
218 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
219 * @throws \BookStack\Exceptions\NotFoundException
221 public function showPermissions($bookSlug, $chapterSlug)
223 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
224 $this->checkOwnablePermission('restrictions-manage', $chapter);
225 $roles = $this->userRepo->getRestrictableRoles();
226 return view('chapters.permissions', [
227 'chapter' => $chapter,
233 * Set the restrictions for this chapter.
234 * @param Request $request
235 * @param string $bookSlug
236 * @param string $chapterSlug
237 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
238 * @throws \BookStack\Exceptions\NotFoundException
241 public function permissions(Request $request, string $bookSlug, string $chapterSlug)
243 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
244 $this->checkOwnablePermission('restrictions-manage', $chapter);
245 $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
246 session()->flash('success', trans('entities.chapters_permissions_success'));
247 return redirect($chapter->getUrl());
251 * Exports a chapter to pdf .
252 * @param string $bookSlug
253 * @param string $chapterSlug
254 * @return \Illuminate\Http\Response
256 public function exportPdf($bookSlug, $chapterSlug)
258 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
259 $pdfContent = $this->exportService->chapterToPdf($chapter);
260 return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
264 * Export a chapter to a self-contained HTML file.
265 * @param string $bookSlug
266 * @param string $chapterSlug
267 * @return \Illuminate\Http\Response
269 public function exportHtml($bookSlug, $chapterSlug)
271 $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
272 $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
273 return $this->downloadResponse($containedHtml, $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 $chapterText = $this->exportService->chapterToPlainText($chapter);
286 return $this->downloadResponse($chapterText, $chapterSlug . '.txt');