1 <?php namespace BookStack\Http\Controllers;
4 use Illuminate\Http\Request;
5 use BookStack\Http\Requests;
6 use BookStack\Repos\BookRepo;
7 use BookStack\Repos\ChapterRepo;
10 class ChapterController extends Controller
14 protected $chapterRepo;
17 * ChapterController constructor.
21 public function __construct(BookRepo $bookRepo, ChapterRepo $chapterRepo)
23 $this->bookRepo = $bookRepo;
24 $this->chapterRepo = $chapterRepo;
25 parent::__construct();
29 * Show the form for creating a new chapter.
33 public function create($bookSlug)
35 $book = $this->bookRepo->getBySlug($bookSlug);
36 $this->checkOwnablePermission('chapter-create', $book);
37 $this->setPageTitle('Create New Chapter');
38 return view('chapters/create', ['book' => $book, 'current' => $book]);
42 * Store a newly created chapter in storage.
44 * @param Request $request
47 public function store($bookSlug, Request $request)
49 $this->validate($request, [
50 'name' => 'required|string|max:255'
53 $book = $this->bookRepo->getBySlug($bookSlug);
54 $this->checkOwnablePermission('chapter-create', $book);
56 $chapter = $this->chapterRepo->newFromInput($request->all());
57 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
58 $chapter->priority = $this->bookRepo->getNewPriority($book);
59 $chapter->created_by = auth()->user()->id;
60 $chapter->updated_by = auth()->user()->id;
61 $book->chapters()->save($chapter);
62 Activity::add($chapter, 'chapter_create', $book->id);
63 return redirect($chapter->getUrl());
67 * Display the specified chapter.
72 public function show($bookSlug, $chapterSlug)
74 $book = $this->bookRepo->getBySlug($bookSlug);
75 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
76 $sidebarTree = $this->bookRepo->getChildren($book);
78 $this->setPageTitle($chapter->getShortName());
79 return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter, 'sidebarTree' => $sidebarTree]);
83 * Show the form for editing the specified chapter.
88 public function edit($bookSlug, $chapterSlug)
90 $book = $this->bookRepo->getBySlug($bookSlug);
91 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
92 $this->checkOwnablePermission('chapter-update', $chapter);
93 $this->setPageTitle('Edit Chapter' . $chapter->getShortName());
94 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
98 * Update the specified chapter in storage.
99 * @param Request $request
101 * @param $chapterSlug
104 public function update(Request $request, $bookSlug, $chapterSlug)
106 $book = $this->bookRepo->getBySlug($bookSlug);
107 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
108 $this->checkOwnablePermission('chapter-update', $chapter);
109 $chapter->fill($request->all());
110 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
111 $chapter->updated_by = auth()->user()->id;
113 Activity::add($chapter, 'chapter_update', $book->id);
114 return redirect($chapter->getUrl());
118 * Shows the page to confirm deletion of this chapter.
120 * @param $chapterSlug
121 * @return \Illuminate\View\View
123 public function showDelete($bookSlug, $chapterSlug)
125 $book = $this->bookRepo->getBySlug($bookSlug);
126 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
127 $this->checkOwnablePermission('chapter-delete', $chapter);
128 $this->setPageTitle('Delete Chapter' . $chapter->getShortName());
129 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
133 * Remove the specified chapter from storage.
135 * @param $chapterSlug
138 public function destroy($bookSlug, $chapterSlug)
140 $book = $this->bookRepo->getBySlug($bookSlug);
141 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
142 $this->checkOwnablePermission('chapter-delete', $chapter);
143 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
144 $this->chapterRepo->destroy($chapter);
145 return redirect($book->getUrl());