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);
83 $sidebarTree = $this->bookRepo->getChildren($book);
85 return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter, 'sidebarTree' => $sidebarTree]);
89 * Show the form for editing the specified chapter.
95 public function edit($bookSlug, $chapterSlug)
97 $this->checkPermission('chapter-update');
98 $book = $this->bookRepo->getBySlug($bookSlug);
99 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
100 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
104 * Update the specified chapter in storage.
106 * @param Request $request
108 * @param $chapterSlug
111 public function update(Request $request, $bookSlug, $chapterSlug)
113 $this->checkPermission('chapter-update');
114 $book = $this->bookRepo->getBySlug($bookSlug);
115 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
116 $chapter->fill($request->all());
117 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
118 $chapter->updated_by = Auth::user()->id;
120 Activity::add($chapter, 'chapter_update', $book->id);
121 return redirect($chapter->getUrl());
125 * Shows the page to confirm deletion of this chapter.
127 * @param $chapterSlug
128 * @return \Illuminate\View\View
130 public function showDelete($bookSlug, $chapterSlug)
132 $this->checkPermission('chapter-delete');
133 $book = $this->bookRepo->getBySlug($bookSlug);
134 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
135 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
139 * Remove the specified chapter from storage.
142 * @param $chapterSlug
145 public function destroy($bookSlug, $chapterSlug)
147 $this->checkPermission('chapter-delete');
148 $book = $this->bookRepo->getBySlug($bookSlug);
149 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
150 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
151 $this->chapterRepo->destroy($chapter);
152 return redirect($book->getUrl());