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 $this->setPageTitle('Create New Chapter');
44 return view('chapters/create', ['book' => $book, 'current' => $book]);
48 * 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.
77 public function show($bookSlug, $chapterSlug)
79 $book = $this->bookRepo->getBySlug($bookSlug);
80 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
81 $sidebarTree = $this->bookRepo->getChildren($book);
83 $this->setPageTitle($chapter->getShortName());
84 return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter, 'sidebarTree' => $sidebarTree]);
88 * Show the form for editing the specified chapter.
93 public function edit($bookSlug, $chapterSlug)
95 $this->checkPermission('chapter-update');
96 $book = $this->bookRepo->getBySlug($bookSlug);
97 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
98 $this->setPageTitle('Edit Chapter' . $chapter->getShortName());
99 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
103 * Update the specified chapter in storage.
104 * @param Request $request
106 * @param $chapterSlug
109 public function update(Request $request, $bookSlug, $chapterSlug)
111 $this->checkPermission('chapter-update');
112 $book = $this->bookRepo->getBySlug($bookSlug);
113 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
114 $chapter->fill($request->all());
115 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
116 $chapter->updated_by = auth()->user()->id;
118 Activity::add($chapter, 'chapter_update', $book->id);
119 return redirect($chapter->getUrl());
123 * Shows the page to confirm deletion of this chapter.
125 * @param $chapterSlug
126 * @return \Illuminate\View\View
128 public function showDelete($bookSlug, $chapterSlug)
130 $this->checkPermission('chapter-delete');
131 $book = $this->bookRepo->getBySlug($bookSlug);
132 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
133 $this->setPageTitle('Delete Chapter' . $chapter->getShortName());
134 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
138 * Remove the specified chapter from storage.
140 * @param $chapterSlug
143 public function destroy($bookSlug, $chapterSlug)
145 $this->checkPermission('chapter-delete');
146 $book = $this->bookRepo->getBySlug($bookSlug);
147 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
148 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
149 $this->chapterRepo->destroy($chapter);
150 return redirect($book->getUrl());