1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Tools\BookContents;
5 use BookStack\Entities\Repos\ChapterRepo;
6 use BookStack\Entities\Tools\PermissionsUpdater;
7 use BookStack\Exceptions\MoveOperationException;
8 use BookStack\Exceptions\NotFoundException;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
14 class ChapterController extends Controller
17 protected $chapterRepo;
20 * ChapterController constructor.
22 public function __construct(ChapterRepo $chapterRepo)
24 $this->chapterRepo = $chapterRepo;
28 * Show the form for creating a new chapter.
30 public function create(string $bookSlug)
32 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
33 $this->checkOwnablePermission('chapter-create', $book);
35 $this->setPageTitle(trans('entities.chapters_create'));
36 return view('chapters.create', ['book' => $book, 'current' => $book]);
40 * Store a newly created chapter in storage.
41 * @throws ValidationException
43 public function store(Request $request, string $bookSlug)
45 $this->validate($request, [
46 'name' => 'required|string|max:255'
49 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
50 $this->checkOwnablePermission('chapter-create', $book);
52 $chapter = $this->chapterRepo->create($request->all(), $book);
54 return redirect($chapter->getUrl());
58 * Display the specified chapter.
60 public function show(string $bookSlug, string $chapterSlug)
62 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
63 $this->checkOwnablePermission('chapter-view', $chapter);
65 $sidebarTree = (new BookContents($chapter->book))->getTree();
66 $pages = $chapter->getVisiblePages();
69 $this->setPageTitle($chapter->getShortName());
70 return view('chapters.show', [
71 'book' => $chapter->book,
72 'chapter' => $chapter,
73 'current' => $chapter,
74 'sidebarTree' => $sidebarTree,
80 * Show the form for editing the specified chapter.
82 public function edit(string $bookSlug, string $chapterSlug)
84 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
85 $this->checkOwnablePermission('chapter-update', $chapter);
87 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
88 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
92 * Update the specified chapter in storage.
93 * @throws NotFoundException
95 public function update(Request $request, string $bookSlug, string $chapterSlug)
97 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
98 $this->checkOwnablePermission('chapter-update', $chapter);
100 $this->chapterRepo->update($chapter, $request->all());
102 return redirect($chapter->getUrl());
106 * Shows the page to confirm deletion of this chapter.
107 * @throws NotFoundException
109 public function showDelete(string $bookSlug, string $chapterSlug)
111 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
112 $this->checkOwnablePermission('chapter-delete', $chapter);
114 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
115 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
119 * Remove the specified chapter from storage.
120 * @throws NotFoundException
123 public function destroy(string $bookSlug, string $chapterSlug)
125 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
126 $this->checkOwnablePermission('chapter-delete', $chapter);
128 $this->chapterRepo->destroy($chapter);
130 return redirect($chapter->book->getUrl());
134 * Show the page for moving a chapter.
135 * @throws NotFoundException
137 public function showMove(string $bookSlug, string $chapterSlug)
139 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
140 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
141 $this->checkOwnablePermission('chapter-update', $chapter);
142 $this->checkOwnablePermission('chapter-delete', $chapter);
144 return view('chapters.move', [
145 'chapter' => $chapter,
146 'book' => $chapter->book
151 * Perform the move action for a chapter.
152 * @throws NotFoundException
154 public function move(Request $request, string $bookSlug, string $chapterSlug)
156 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
157 $this->checkOwnablePermission('chapter-update', $chapter);
158 $this->checkOwnablePermission('chapter-delete', $chapter);
160 $entitySelection = $request->get('entity_selection', null);
161 if ($entitySelection === null || $entitySelection === '') {
162 return redirect($chapter->getUrl());
166 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
167 } catch (MoveOperationException $exception) {
168 $this->showErrorNotification(trans('errors.selected_book_not_found'));
169 return redirect()->back();
172 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
173 return redirect($chapter->getUrl());
177 * Show the Restrictions view.
178 * @throws NotFoundException
180 public function showPermissions(string $bookSlug, string $chapterSlug)
182 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
183 $this->checkOwnablePermission('restrictions-manage', $chapter);
185 return view('chapters.permissions', [
186 'chapter' => $chapter,
191 * Set the restrictions for this chapter.
192 * @throws NotFoundException
194 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
196 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
197 $this->checkOwnablePermission('restrictions-manage', $chapter);
199 $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
201 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
202 return redirect($chapter->getUrl());