3 namespace Oxbow\Http\Controllers;
6 use Illuminate\Http\Request;
8 use Illuminate\Support\Facades\Auth;
9 use Oxbow\Http\Requests;
10 use Oxbow\Http\Controllers\Controller;
11 use Oxbow\Repos\BookRepo;
12 use Oxbow\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;
33 * Show the form for creating a new chapter.
38 public function create($bookSlug)
40 $book = $this->bookRepo->getBySlug($bookSlug);
41 return view('chapters/create', ['book' => $book, 'current' => $book]);
45 * Store a newly created chapter in storage.
48 * @param Request $request
51 public function store($bookSlug, Request $request)
53 $this->validate($request, [
54 'name' => 'required|string|max:255'
57 $book = $this->bookRepo->getBySlug($bookSlug);
58 $chapter = $this->chapterRepo->newFromInput($request->all());
59 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
60 $chapter->priority = $this->bookRepo->getNewPriority($book);
61 $chapter->created_by = Auth::user()->id;
62 $chapter->updated_by = Auth::user()->id;
63 $book->chapters()->save($chapter);
64 Activity::add($chapter, 'chapter_create', $book->id);
65 return redirect($book->getUrl());
69 * Display the specified chapter.
75 public function show($bookSlug, $chapterSlug)
77 $book = $this->bookRepo->getBySlug($bookSlug);
78 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
79 return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
83 * Show the form for editing the specified chapter.
89 public function edit($bookSlug, $chapterSlug)
91 $book = $this->bookRepo->getBySlug($bookSlug);
92 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
93 return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
97 * Update the specified chapter in storage.
99 * @param Request $request
101 * @param $chapterSlug
104 public function update(Request $request, $bookSlug, $chapterSlug)
106 $book = $this->bookRepo->getBySlug($bookSlug);
107 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
108 $chapter->fill($request->all());
109 $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
110 $chapter->updated_by = Auth::user()->id;
112 Activity::add($chapter, 'chapter_update', $book->id);
113 return redirect($chapter->getUrl());
117 * Shows the page to confirm deletion of this chapter.
119 * @param $chapterSlug
120 * @return \Illuminate\View\View
122 public function showDelete($bookSlug, $chapterSlug)
124 $book = $this->bookRepo->getBySlug($bookSlug);
125 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
126 return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
130 * Remove the specified chapter from storage.
133 * @param $chapterSlug
136 public function destroy($bookSlug, $chapterSlug)
138 $book = $this->bookRepo->getBySlug($bookSlug);
139 $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
140 if(count($chapter->pages) > 0) {
141 foreach($chapter->pages as $page) {
142 $page->chapter_id = 0;
146 Activity::removeEntity($chapter);
147 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
149 return redirect($book->getUrl());