3 namespace BookStack\Http\Controllers;
6 use Illuminate\Http\Request;
8 use Illuminate\Support\Facades\Auth;
9 use BookStack\Http\Requests;
10 use BookStack\Http\Controllers\Controller;
11 use BookStack\Repos\BookRepo;
12 use BookStack\Repos\ChapterRepo;
15 class ChapterController extends Controller
19 protected $chapterRepo;
22 * ChapterController constructor.
26 public function __construct(BookRepo $bookRepo, ChapterRepo $chapterRepo)
28 $this->bookRepo = $bookRepo;
29 $this->chapterRepo = $chapterRepo;
30 parent::__construct();
35 * Show the form for creating a new chapter.
40 public function create($bookSlug)
42 $this->checkPermission('chapter-create');
43 $book = $this->bookRepo->getBySlug($bookSlug);
44 return view('chapters/create', ['book' => $book, 'current' => $book]);
48 * Store a newly created chapter in storage.
51 * @param Request $request
54 public function store($bookSlug, Request $request)
56 $this->checkPermission('chapter-create');
57 $this->validate($request, [
58 'name' => 'required|string|max:255'
61 $book = $this->bookRepo->getBySlug($bookSlug);
62 $chapter = $this->chapterRepo->newFromInput($request->all());
63 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
64 $chapter->priority = $this->bookRepo->getNewPriority($book);
65 $chapter->created_by = Auth::user()->id;
66 $chapter->updated_by = Auth::user()->id;
67 $book->chapters()->save($chapter);
68 Activity::add($chapter, 'chapter_create', $book->id);
69 return redirect($chapter->getUrl());
73 * Display the specified chapter.
79 public function show($bookSlug, $chapterSlug)
81 $book = $this->bookRepo->getBySlug($bookSlug);
82 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
84 return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
88 * Show the form for editing the specified chapter.
94 public function edit($bookSlug, $chapterSlug)
96 $this->checkPermission('chapter-update');
97 $book = $this->bookRepo->getBySlug($bookSlug);
98 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
99 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
103 * Update the specified chapter in storage.
105 * @param Request $request
107 * @param $chapterSlug
110 public function update(Request $request, $bookSlug, $chapterSlug)
112 $this->checkPermission('chapter-update');
113 $book = $this->bookRepo->getBySlug($bookSlug);
114 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
115 $chapter->fill($request->all());
116 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
117 $chapter->updated_by = Auth::user()->id;
119 Activity::add($chapter, 'chapter_update', $book->id);
120 return redirect($chapter->getUrl());
124 * Shows the page to confirm deletion of this chapter.
126 * @param $chapterSlug
127 * @return \Illuminate\View\View
129 public function showDelete($bookSlug, $chapterSlug)
131 $this->checkPermission('chapter-delete');
132 $book = $this->bookRepo->getBySlug($bookSlug);
133 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
134 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
138 * Remove the specified chapter from storage.
141 * @param $chapterSlug
144 public function destroy($bookSlug, $chapterSlug)
146 $this->checkPermission('chapter-delete');
147 $book = $this->bookRepo->getBySlug($bookSlug);
148 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
149 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
150 $this->chapterRepo->destroy($chapter);
151 return redirect($book->getUrl());