3 namespace Oxbow\Http\Controllers;
5 use Illuminate\Http\Request;
7 use Illuminate\Support\Facades\Auth;
8 use Oxbow\Http\Requests;
9 use Oxbow\Http\Controllers\Controller;
10 use Oxbow\Repos\BookRepo;
11 use Oxbow\Repos\ChapterRepo;
13 class ChapterController extends Controller
17 protected $chapterRepo;
20 * ChapterController constructor.
24 public function __construct(BookRepo $bookRepo,ChapterRepo $chapterRepo)
26 $this->bookRepo = $bookRepo;
27 $this->chapterRepo = $chapterRepo;
32 * Show the form for creating a new chapter.
37 public function create($bookSlug)
39 $book = $this->bookRepo->getBySlug($bookSlug);
40 return view('chapters/create', ['book' => $book]);
44 * Store a newly created chapter in storage.
47 * @param Request $request
50 public function store($bookSlug, Request $request)
52 $this->validate($request, [
53 'name' => 'required|string|max:255'
56 $book = $this->bookRepo->getBySlug($bookSlug);
57 $chapter = $this->chapterRepo->newFromInput($request->all());
58 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
59 $chapter->priority = $this->bookRepo->getNewPriority($book);
60 $chapter->created_by = Auth::user()->id;
61 $chapter->updated_by = Auth::user()->id;
62 $book->chapters()->save($chapter);
63 return redirect($book->getUrl());
67 * Display the specified chapter.
73 public function show($bookSlug, $chapterSlug)
75 $book = $this->bookRepo->getBySlug($bookSlug);
76 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
77 return view('chapters/show', ['book' => $book, 'chapter' => $chapter]);
81 * Show the form for editing the specified chapter.
87 public function edit($bookSlug, $chapterSlug)
89 $book = $this->bookRepo->getBySlug($bookSlug);
90 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
91 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter]);
95 * Update the specified chapter in storage.
97 * @param Request $request
102 public function update(Request $request, $bookSlug, $chapterSlug)
104 $book = $this->bookRepo->getBySlug($bookSlug);
105 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
106 $chapter->fill($request->all());
107 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
108 $chapter->updated_by = Auth::user()->id;
110 return redirect($chapter->getUrl());
114 * Shows the page to confirm deletion of this chapter.
116 * @param $chapterSlug
117 * @return \Illuminate\View\View
119 public function showDelete($bookSlug, $chapterSlug)
121 $book = $this->bookRepo->getBySlug($bookSlug);
122 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
123 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter]);
127 * Remove the specified chapter from storage.
130 * @param $chapterSlug
133 public function destroy($bookSlug, $chapterSlug)
135 $book = $this->bookRepo->getBySlug($bookSlug);
136 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
137 if(count($chapter->pages) > 0) {
138 foreach($chapter->pages as $page) {
139 $page->chapter_id = 0;
144 return redirect($book->getUrl());