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\NextPreviousContentLocator;
8 use BookStack\Entities\Tools\PermissionsUpdater;
9 use BookStack\Exceptions\MoveOperationException;
10 use BookStack\Exceptions\NotFoundException;
11 use Illuminate\Http\Request;
12 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 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
69 View::incrementFor($chapter);
71 $this->setPageTitle($chapter->getShortName());
72 return view('chapters.show', [
73 'book' => $chapter->book,
74 'chapter' => $chapter,
75 'current' => $chapter,
76 'sidebarTree' => $sidebarTree,
78 'next' => $nextPreviousLocator->getNext(),
79 'previous' => $nextPreviousLocator->getPrevious(),
84 * Show the form for editing the specified chapter.
86 public function edit(string $bookSlug, string $chapterSlug)
88 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
89 $this->checkOwnablePermission('chapter-update', $chapter);
91 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
92 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
96 * Update the specified chapter in storage.
97 * @throws NotFoundException
99 public function update(Request $request, string $bookSlug, string $chapterSlug)
101 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
102 $this->checkOwnablePermission('chapter-update', $chapter);
104 $this->chapterRepo->update($chapter, $request->all());
106 return redirect($chapter->getUrl());
110 * Shows the page to confirm deletion of this chapter.
111 * @throws NotFoundException
113 public function showDelete(string $bookSlug, string $chapterSlug)
115 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
116 $this->checkOwnablePermission('chapter-delete', $chapter);
118 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
119 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
123 * Remove the specified chapter from storage.
124 * @throws NotFoundException
127 public function destroy(string $bookSlug, string $chapterSlug)
129 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
130 $this->checkOwnablePermission('chapter-delete', $chapter);
132 $this->chapterRepo->destroy($chapter);
134 return redirect($chapter->book->getUrl());
138 * Show the page for moving a chapter.
139 * @throws NotFoundException
141 public function showMove(string $bookSlug, string $chapterSlug)
143 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
144 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
145 $this->checkOwnablePermission('chapter-update', $chapter);
146 $this->checkOwnablePermission('chapter-delete', $chapter);
148 return view('chapters.move', [
149 'chapter' => $chapter,
150 'book' => $chapter->book
155 * Perform the move action for a chapter.
156 * @throws NotFoundException
158 public function move(Request $request, string $bookSlug, string $chapterSlug)
160 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
161 $this->checkOwnablePermission('chapter-update', $chapter);
162 $this->checkOwnablePermission('chapter-delete', $chapter);
164 $entitySelection = $request->get('entity_selection', null);
165 if ($entitySelection === null || $entitySelection === '') {
166 return redirect($chapter->getUrl());
170 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
171 } catch (MoveOperationException $exception) {
172 $this->showErrorNotification(trans('errors.selected_book_not_found'));
173 return redirect()->back();
176 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
177 return redirect($chapter->getUrl());
181 * Show the Restrictions view.
182 * @throws NotFoundException
184 public function showPermissions(string $bookSlug, string $chapterSlug)
186 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
187 $this->checkOwnablePermission('restrictions-manage', $chapter);
189 return view('chapters.permissions', [
190 'chapter' => $chapter,
195 * Set the restrictions for this chapter.
196 * @throws NotFoundException
198 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
200 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
201 $this->checkOwnablePermission('restrictions-manage', $chapter);
203 $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
205 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
206 return redirect($chapter->getUrl());