1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Actions\View;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Tools\BookContents;
6 use BookStack\Entities\Repos\ChapterRepo;
7 use BookStack\Entities\Tools\PermissionsUpdater;
8 use BookStack\Exceptions\MoveOperationException;
9 use BookStack\Exceptions\NotFoundException;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
15 class ChapterController extends Controller
18 protected $chapterRepo;
21 * ChapterController constructor.
23 public function __construct(ChapterRepo $chapterRepo)
25 $this->chapterRepo = $chapterRepo;
29 * Show the form for creating a new chapter.
31 public function create(string $bookSlug)
33 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
34 $this->checkOwnablePermission('chapter-create', $book);
36 $this->setPageTitle(trans('entities.chapters_create'));
37 return view('chapters.create', ['book' => $book, 'current' => $book]);
41 * Store a newly created chapter in storage.
42 * @throws ValidationException
44 public function store(Request $request, string $bookSlug)
46 $this->validate($request, [
47 'name' => 'required|string|max:255'
50 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
51 $this->checkOwnablePermission('chapter-create', $book);
53 $chapter = $this->chapterRepo->create($request->all(), $book);
55 return redirect($chapter->getUrl());
59 * Display the specified chapter.
61 public function show(string $bookSlug, string $chapterSlug)
63 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
64 $this->checkOwnablePermission('chapter-view', $chapter);
66 $sidebarTree = (new BookContents($chapter->book))->getTree();
67 $pages = $chapter->getVisiblePages();
68 View::incrementFor($chapter);
70 $this->setPageTitle($chapter->getShortName());
71 return view('chapters.show', [
72 'book' => $chapter->book,
73 'chapter' => $chapter,
74 'current' => $chapter,
75 'sidebarTree' => $sidebarTree,
81 * Show the form for editing the specified chapter.
83 public function edit(string $bookSlug, string $chapterSlug)
85 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
86 $this->checkOwnablePermission('chapter-update', $chapter);
88 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
89 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
93 * Update the specified chapter in storage.
94 * @throws NotFoundException
96 public function update(Request $request, string $bookSlug, string $chapterSlug)
98 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
99 $this->checkOwnablePermission('chapter-update', $chapter);
101 $this->chapterRepo->update($chapter, $request->all());
103 return redirect($chapter->getUrl());
107 * Shows the page to confirm deletion of this chapter.
108 * @throws NotFoundException
110 public function showDelete(string $bookSlug, string $chapterSlug)
112 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
113 $this->checkOwnablePermission('chapter-delete', $chapter);
115 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
116 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
120 * Remove the specified chapter from storage.
121 * @throws NotFoundException
124 public function destroy(string $bookSlug, string $chapterSlug)
126 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
127 $this->checkOwnablePermission('chapter-delete', $chapter);
129 $this->chapterRepo->destroy($chapter);
131 return redirect($chapter->book->getUrl());
135 * Show the page for moving a chapter.
136 * @throws NotFoundException
138 public function showMove(string $bookSlug, string $chapterSlug)
140 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
141 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
142 $this->checkOwnablePermission('chapter-update', $chapter);
143 $this->checkOwnablePermission('chapter-delete', $chapter);
145 return view('chapters.move', [
146 'chapter' => $chapter,
147 'book' => $chapter->book
152 * Perform the move action for a chapter.
153 * @throws NotFoundException
155 public function move(Request $request, string $bookSlug, string $chapterSlug)
157 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
158 $this->checkOwnablePermission('chapter-update', $chapter);
159 $this->checkOwnablePermission('chapter-delete', $chapter);
161 $entitySelection = $request->get('entity_selection', null);
162 if ($entitySelection === null || $entitySelection === '') {
163 return redirect($chapter->getUrl());
167 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
168 } catch (MoveOperationException $exception) {
169 $this->showErrorNotification(trans('errors.selected_book_not_found'));
170 return redirect()->back();
173 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
174 return redirect($chapter->getUrl());
178 * Show the Restrictions view.
179 * @throws NotFoundException
181 public function showPermissions(string $bookSlug, string $chapterSlug)
183 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
184 $this->checkOwnablePermission('restrictions-manage', $chapter);
186 return view('chapters.permissions', [
187 'chapter' => $chapter,
192 * Set the restrictions for this chapter.
193 * @throws NotFoundException
195 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
197 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
198 $this->checkOwnablePermission('restrictions-manage', $chapter);
200 $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
202 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
203 return redirect($chapter->getUrl());