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;
16 class ChapterController extends Controller
19 protected $chapterRepo;
22 * ChapterController constructor.
24 public function __construct(ChapterRepo $chapterRepo)
26 $this->chapterRepo = $chapterRepo;
30 * Show the form for creating a new chapter.
32 public function create(string $bookSlug)
34 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
35 $this->checkOwnablePermission('chapter-create', $book);
37 $this->setPageTitle(trans('entities.chapters_create'));
38 return view('chapters.create', ['book' => $book, 'current' => $book]);
42 * Store a newly created chapter in storage.
43 * @throws ValidationException
45 public function store(Request $request, string $bookSlug)
47 $this->validate($request, [
48 'name' => 'required|string|max:255'
51 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
52 $this->checkOwnablePermission('chapter-create', $book);
54 $chapter = $this->chapterRepo->create($request->all(), $book);
56 return redirect($chapter->getUrl());
60 * Display the specified chapter.
62 public function show(string $bookSlug, string $chapterSlug)
64 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
65 $this->checkOwnablePermission('chapter-view', $chapter);
67 $sidebarTree = (new BookContents($chapter->book))->getTree();
68 $pages = $chapter->getVisiblePages();
69 $nextPreviousLocator = new NextPreviousContentLocator($chapter, $sidebarTree);
70 View::incrementFor($chapter);
72 $this->setPageTitle($chapter->getShortName());
73 return view('chapters.show', [
74 'book' => $chapter->book,
75 'chapter' => $chapter,
76 'current' => $chapter,
77 'sidebarTree' => $sidebarTree,
79 'next' => $nextPreviousLocator->getNext(),
80 'previous' => $nextPreviousLocator->getPrevious(),
85 * Show the form for editing the specified chapter.
87 public function edit(string $bookSlug, string $chapterSlug)
89 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
90 $this->checkOwnablePermission('chapter-update', $chapter);
92 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
93 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
97 * Update the specified chapter in storage.
98 * @throws NotFoundException
100 public function update(Request $request, string $bookSlug, string $chapterSlug)
102 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
103 $this->checkOwnablePermission('chapter-update', $chapter);
105 $this->chapterRepo->update($chapter, $request->all());
107 return redirect($chapter->getUrl());
111 * Shows the page to confirm deletion of this chapter.
112 * @throws NotFoundException
114 public function showDelete(string $bookSlug, string $chapterSlug)
116 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
117 $this->checkOwnablePermission('chapter-delete', $chapter);
119 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
120 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
124 * Remove the specified chapter from storage.
125 * @throws NotFoundException
128 public function destroy(string $bookSlug, string $chapterSlug)
130 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
131 $this->checkOwnablePermission('chapter-delete', $chapter);
133 $this->chapterRepo->destroy($chapter);
135 return redirect($chapter->book->getUrl());
139 * Show the page for moving a chapter.
140 * @throws NotFoundException
142 public function showMove(string $bookSlug, string $chapterSlug)
144 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
145 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
146 $this->checkOwnablePermission('chapter-update', $chapter);
147 $this->checkOwnablePermission('chapter-delete', $chapter);
149 return view('chapters.move', [
150 'chapter' => $chapter,
151 'book' => $chapter->book
156 * Perform the move action for a chapter.
157 * @throws NotFoundException
159 public function move(Request $request, string $bookSlug, string $chapterSlug)
161 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
162 $this->checkOwnablePermission('chapter-update', $chapter);
163 $this->checkOwnablePermission('chapter-delete', $chapter);
165 $entitySelection = $request->get('entity_selection', null);
166 if ($entitySelection === null || $entitySelection === '') {
167 return redirect($chapter->getUrl());
171 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
172 } catch (MoveOperationException $exception) {
173 $this->showErrorNotification(trans('errors.selected_book_not_found'));
174 return redirect()->back();
177 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
178 return redirect($chapter->getUrl());
182 * Show the Restrictions view.
183 * @throws NotFoundException
185 public function showPermissions(string $bookSlug, string $chapterSlug)
187 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
188 $this->checkOwnablePermission('restrictions-manage', $chapter);
190 return view('chapters.permissions', [
191 'chapter' => $chapter,
196 * Set the restrictions for this chapter.
197 * @throws NotFoundException
199 public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
201 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
202 $this->checkOwnablePermission('restrictions-manage', $chapter);
204 $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
206 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
207 return redirect($chapter->getUrl());