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.
39 public function create($bookSlug)
41 $this->checkPermission('chapter-create');
42 $book = $this->bookRepo->getBySlug($bookSlug);
43 return view('chapters/create', ['book' => $book, 'current' => $book]);
47 * Store a newly created chapter in storage.
49 * @param Request $request
52 public function store($bookSlug, Request $request)
54 $this->checkPermission('chapter-create');
55 $this->validate($request, [
56 'name' => 'required|string|max:255'
59 $book = $this->bookRepo->getBySlug($bookSlug);
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 return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter, 'sidebarTree' => $sidebarTree]);
86 * Show the form for editing the specified chapter.
91 public function edit($bookSlug, $chapterSlug)
93 $this->checkPermission('chapter-update');
94 $book = $this->bookRepo->getBySlug($bookSlug);
95 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
96 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
100 * Update the specified chapter in storage.
101 * @param Request $request
103 * @param $chapterSlug
106 public function update(Request $request, $bookSlug, $chapterSlug)
108 $this->checkPermission('chapter-update');
109 $book = $this->bookRepo->getBySlug($bookSlug);
110 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
111 $chapter->fill($request->all());
112 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
113 $chapter->updated_by = auth()->user()->id;
115 Activity::add($chapter, 'chapter_update', $book->id);
116 return redirect($chapter->getUrl());
120 * Shows the page to confirm deletion of this chapter.
122 * @param $chapterSlug
123 * @return \Illuminate\View\View
125 public function showDelete($bookSlug, $chapterSlug)
127 $this->checkPermission('chapter-delete');
128 $book = $this->bookRepo->getBySlug($bookSlug);
129 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
130 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
134 * Remove the specified chapter from storage.
136 * @param $chapterSlug
139 public function destroy($bookSlug, $chapterSlug)
141 $this->checkPermission('chapter-delete');
142 $book = $this->bookRepo->getBySlug($bookSlug);
143 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
144 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
145 $this->chapterRepo->destroy($chapter);
146 return redirect($book->getUrl());