1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Tools\BookContents;
5 use BookStack\Entities\Repos\ChapterRepo;
6 use BookStack\Exceptions\MoveOperationException;
7 use BookStack\Exceptions\NotFoundException;
8 use Illuminate\Http\Request;
9 use Illuminate\Validation\ValidationException;
13 class ChapterController extends Controller
16 protected $chapterRepo;
19 * ChapterController constructor.
21 public function __construct(ChapterRepo $chapterRepo)
23 $this->chapterRepo = $chapterRepo;
27 * Show the form for creating a new chapter.
29 public function create(string $bookSlug)
31 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
32 $this->checkOwnablePermission('chapter-create', $book);
34 $this->setPageTitle(trans('entities.chapters_create'));
35 return view('chapters.create', ['book' => $book, 'current' => $book]);
39 * Store a newly created chapter in storage.
40 * @throws ValidationException
42 public function store(Request $request, string $bookSlug)
44 $this->validate($request, [
45 'name' => 'required|string|max:255'
48 $book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
49 $this->checkOwnablePermission('chapter-create', $book);
51 $chapter = $this->chapterRepo->create($request->all(), $book);
53 return redirect($chapter->getUrl());
57 * Display the specified chapter.
59 public function show(string $bookSlug, string $chapterSlug)
61 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
62 $this->checkOwnablePermission('chapter-view', $chapter);
64 $sidebarTree = (new BookContents($chapter->book))->getTree();
65 $pages = $chapter->getVisiblePages();
68 $this->setPageTitle($chapter->getShortName());
69 return view('chapters.show', [
70 'book' => $chapter->book,
71 'chapter' => $chapter,
72 'current' => $chapter,
73 'sidebarTree' => $sidebarTree,
79 * Show the form for editing the specified chapter.
81 public function edit(string $bookSlug, string $chapterSlug)
83 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
84 $this->checkOwnablePermission('chapter-update', $chapter);
86 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
87 return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
91 * Update the specified chapter in storage.
92 * @throws NotFoundException
94 public function update(Request $request, string $bookSlug, string $chapterSlug)
96 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
97 $this->checkOwnablePermission('chapter-update', $chapter);
99 $this->chapterRepo->update($chapter, $request->all());
101 return redirect($chapter->getUrl());
105 * Shows the page to confirm deletion of this chapter.
106 * @throws NotFoundException
108 public function showDelete(string $bookSlug, string $chapterSlug)
110 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
111 $this->checkOwnablePermission('chapter-delete', $chapter);
113 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
114 return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
118 * Remove the specified chapter from storage.
119 * @throws NotFoundException
122 public function destroy(string $bookSlug, string $chapterSlug)
124 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
125 $this->checkOwnablePermission('chapter-delete', $chapter);
127 $this->chapterRepo->destroy($chapter);
129 return redirect($chapter->book->getUrl());
133 * Show the page for moving a chapter.
134 * @throws NotFoundException
136 public function showMove(string $bookSlug, string $chapterSlug)
138 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
139 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
140 $this->checkOwnablePermission('chapter-update', $chapter);
141 $this->checkOwnablePermission('chapter-delete', $chapter);
143 return view('chapters.move', [
144 'chapter' => $chapter,
145 'book' => $chapter->book
150 * Perform the move action for a chapter.
151 * @throws NotFoundException
153 public function move(Request $request, string $bookSlug, string $chapterSlug)
155 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
156 $this->checkOwnablePermission('chapter-update', $chapter);
157 $this->checkOwnablePermission('chapter-delete', $chapter);
159 $entitySelection = $request->get('entity_selection', null);
160 if ($entitySelection === null || $entitySelection === '') {
161 return redirect($chapter->getUrl());
165 $newBook = $this->chapterRepo->move($chapter, $entitySelection);
166 } catch (MoveOperationException $exception) {
167 $this->showErrorNotification(trans('errors.selected_book_not_found'));
168 return redirect()->back();
171 $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
172 return redirect($chapter->getUrl());
176 * Show the Restrictions view.
177 * @throws NotFoundException
179 public function showPermissions(string $bookSlug, string $chapterSlug)
181 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
182 $this->checkOwnablePermission('restrictions-manage', $chapter);
184 return view('chapters.permissions', [
185 'chapter' => $chapter,
190 * Set the restrictions for this chapter.
191 * @throws NotFoundException
193 public function permissions(Request $request, string $bookSlug, string $chapterSlug)
195 $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
196 $this->checkOwnablePermission('restrictions-manage', $chapter);
198 $restricted = $request->get('restricted') === 'true';
199 $permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
200 $this->chapterRepo->updatePermissions($chapter, $restricted, $permissions);
202 $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
203 return redirect($chapter->getUrl());