1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Repos\UserRepo;
5 use Illuminate\Http\Request;
6 use BookStack\Http\Requests;
7 use BookStack\Repos\BookRepo;
8 use BookStack\Repos\ChapterRepo;
11 class ChapterController extends Controller
15 protected $chapterRepo;
19 * ChapterController constructor.
20 * @param BookRepo $bookRepo
21 * @param ChapterRepo $chapterRepo
22 * @param UserRepo $userRepo
24 public function __construct(BookRepo $bookRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
26 $this->bookRepo = $bookRepo;
27 $this->chapterRepo = $chapterRepo;
28 $this->userRepo = $userRepo;
29 parent::__construct();
33 * Show the form for creating a new chapter.
37 public function create($bookSlug)
39 $book = $this->bookRepo->getBySlug($bookSlug);
40 $this->checkOwnablePermission('chapter-create', $book);
41 $this->setPageTitle('Create New Chapter');
42 return view('chapters/create', ['book' => $book, 'current' => $book]);
46 * Store a newly created chapter in storage.
48 * @param Request $request
51 public function store($bookSlug, Request $request)
53 $this->validate($request, [
54 'name' => 'required|string|max:255'
57 $book = $this->bookRepo->getBySlug($bookSlug);
58 $this->checkOwnablePermission('chapter-create', $book);
60 $chapter = $this->chapterRepo->newFromInput($request->all());
61 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
62 $chapter->priority = $this->bookRepo->getNewPriority($book);
63 $chapter->created_by = auth()->user()->id;
64 $chapter->updated_by = auth()->user()->id;
65 $book->chapters()->save($chapter);
66 Activity::add($chapter, 'chapter_create', $book->id);
67 return redirect($chapter->getUrl());
71 * Display the specified chapter.
76 public function show($bookSlug, $chapterSlug)
78 $book = $this->bookRepo->getBySlug($bookSlug);
79 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
80 $sidebarTree = $this->bookRepo->getChildren($book);
82 $this->setPageTitle($chapter->getShortName());
83 $pages = $this->chapterRepo->getChildren($chapter);
84 return view('chapters/show', [
86 'chapter' => $chapter,
87 'current' => $chapter,
88 'sidebarTree' => $sidebarTree,
94 * Show the form for editing the specified chapter.
99 public function edit($bookSlug, $chapterSlug)
101 $book = $this->bookRepo->getBySlug($bookSlug);
102 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
103 $this->checkOwnablePermission('chapter-update', $chapter);
104 $this->setPageTitle('Edit Chapter' . $chapter->getShortName());
105 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
109 * Update the specified chapter in storage.
110 * @param Request $request
112 * @param $chapterSlug
115 public function update(Request $request, $bookSlug, $chapterSlug)
117 $book = $this->bookRepo->getBySlug($bookSlug);
118 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
119 $this->checkOwnablePermission('chapter-update', $chapter);
120 $chapter->fill($request->all());
121 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
122 $chapter->updated_by = auth()->user()->id;
124 Activity::add($chapter, 'chapter_update', $book->id);
125 return redirect($chapter->getUrl());
129 * Shows the page to confirm deletion of this chapter.
131 * @param $chapterSlug
132 * @return \Illuminate\View\View
134 public function showDelete($bookSlug, $chapterSlug)
136 $book = $this->bookRepo->getBySlug($bookSlug);
137 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
138 $this->checkOwnablePermission('chapter-delete', $chapter);
139 $this->setPageTitle('Delete Chapter' . $chapter->getShortName());
140 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
144 * Remove the specified chapter from storage.
146 * @param $chapterSlug
149 public function destroy($bookSlug, $chapterSlug)
151 $book = $this->bookRepo->getBySlug($bookSlug);
152 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
153 $this->checkOwnablePermission('chapter-delete', $chapter);
154 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
155 $this->chapterRepo->destroy($chapter);
156 return redirect($book->getUrl());
160 * Show the Restrictions view.
162 * @param $chapterSlug
163 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
165 public function showRestrict($bookSlug, $chapterSlug)
167 $book = $this->bookRepo->getBySlug($bookSlug);
168 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
169 $this->checkOwnablePermission('restrictions-manage', $chapter);
170 $roles = $this->userRepo->getRestrictableRoles();
171 return view('chapters/restrictions', [
172 'chapter' => $chapter,
178 * Set the restrictions for this chapter.
180 * @param $chapterSlug
181 * @param Request $request
182 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
184 public function restrict($bookSlug, $chapterSlug, Request $request)
186 $book = $this->bookRepo->getBySlug($bookSlug);
187 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
188 $this->checkOwnablePermission('restrictions-manage', $chapter);
189 $this->chapterRepo->updateRestrictionsFromRequest($request, $chapter);
190 session()->flash('success', 'Page Restrictions Updated');
191 return redirect($chapter->getUrl());