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;
14 class ChapterController extends Controller
18 protected $chapterRepo;
21 * ChapterController constructor.
25 public function __construct(BookRepo $bookRepo, ChapterRepo $chapterRepo)
27 $this->bookRepo = $bookRepo;
28 $this->chapterRepo = $chapterRepo;
29 parent::__construct();
34 * 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.
50 * @param Request $request
53 public function store($bookSlug, Request $request)
55 $this->checkPermission('chapter-create');
56 $this->validate($request, [
57 'name' => 'required|string|max:255'
60 $book = $this->bookRepo->getBySlug($bookSlug);
61 $chapter = $this->chapterRepo->newFromInput($request->all());
62 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
63 $chapter->priority = $this->bookRepo->getNewPriority($book);
64 $chapter->created_by = Auth::user()->id;
65 $chapter->updated_by = Auth::user()->id;
66 $book->chapters()->save($chapter);
67 Activity::add($chapter, 'chapter_create', $book->id);
68 return redirect($chapter->getUrl());
72 * Display the specified chapter.
78 public function show($bookSlug, $chapterSlug)
80 $book = $this->bookRepo->getBySlug($bookSlug);
81 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
82 return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
86 * Show the form for editing the specified chapter.
92 public function edit($bookSlug, $chapterSlug)
94 $this->checkPermission('chapter-update');
95 $book = $this->bookRepo->getBySlug($bookSlug);
96 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
97 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
101 * Update the specified chapter in storage.
103 * @param Request $request
105 * @param $chapterSlug
108 public function update(Request $request, $bookSlug, $chapterSlug)
110 $this->checkPermission('chapter-update');
111 $book = $this->bookRepo->getBySlug($bookSlug);
112 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
113 $chapter->fill($request->all());
114 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
115 $chapter->updated_by = Auth::user()->id;
117 Activity::add($chapter, 'chapter_update', $book->id);
118 return redirect($chapter->getUrl());
122 * Shows the page to confirm deletion of this chapter.
124 * @param $chapterSlug
125 * @return \Illuminate\View\View
127 public function showDelete($bookSlug, $chapterSlug)
129 $this->checkPermission('chapter-delete');
130 $book = $this->bookRepo->getBySlug($bookSlug);
131 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
132 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
136 * Remove the specified chapter from storage.
139 * @param $chapterSlug
142 public function destroy($bookSlug, $chapterSlug)
144 $this->checkPermission('chapter-delete');
145 $book = $this->bookRepo->getBySlug($bookSlug);
146 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
147 if (count($chapter->pages) > 0) {
148 foreach ($chapter->pages as $page) {
149 $page->chapter_id = 0;
153 Activity::removeEntity($chapter);
154 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
156 return redirect($book->getUrl());