]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Expanded chapters interface and improved book/page deletion
[bookstack] / app / Http / Controllers / ChapterController.php
1 <?php
2
3 namespace Oxbow\Http\Controllers;
4
5 use Illuminate\Http\Request;
6
7 use Oxbow\Http\Requests;
8 use Oxbow\Http\Controllers\Controller;
9 use Oxbow\Repos\BookRepo;
10 use Oxbow\Repos\ChapterRepo;
11
12 class ChapterController extends Controller
13 {
14
15     protected $bookRepo;
16     protected $chapterRepo;
17
18     /**
19      * ChapterController constructor.
20      * @param $bookRepo
21      * @param $chapterRepo
22      */
23     public function __construct(BookRepo $bookRepo,ChapterRepo $chapterRepo)
24     {
25         $this->bookRepo = $bookRepo;
26         $this->chapterRepo = $chapterRepo;
27     }
28     
29
30     /**
31      * Show the form for creating a new chapter.
32      *
33      * @param $bookSlug
34      * @return Response
35      */
36     public function create($bookSlug)
37     {
38         $book = $this->bookRepo->getBySlug($bookSlug);
39         return view('chapters/create', ['book' => $book]);
40     }
41
42     /**
43      * Store a newly created chapter in storage.
44      *
45      * @param $bookSlug
46      * @param  Request $request
47      * @return Response
48      */
49     public function store($bookSlug, Request $request)
50     {
51         $this->validate($request, [
52             'name' => 'required|string|max:255'
53         ]);
54
55         $book = $this->bookRepo->getBySlug($bookSlug);
56         $chapter = $this->chapterRepo->newFromInput($request->all());
57         $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
58         $chapter->priority = $this->bookRepo->getNewPriority($book);
59         $book->chapters()->save($chapter);
60         return redirect($book->getUrl());
61     }
62
63     /**
64      * Display the specified chapter.
65      *
66      * @param $bookSlug
67      * @param $chapterSlug
68      * @return Response
69      */
70     public function show($bookSlug, $chapterSlug)
71     {
72         $book = $this->bookRepo->getBySlug($bookSlug);
73         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
74         return view('chapters/show', ['book' => $book, 'chapter' => $chapter]);
75     }
76
77     /**
78      * Show the form for editing the specified chapter.
79      *
80      * @param $bookSlug
81      * @param $chapterSlug
82      * @return Response
83      */
84     public function edit($bookSlug, $chapterSlug)
85     {
86         $book = $this->bookRepo->getBySlug($bookSlug);
87         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
88         return view('chapters/edit', ['book' => $book, 'chapter' => $chapter]);
89     }
90
91     /**
92      * Update the specified chapter in storage.
93      *
94      * @param  Request $request
95      * @param $bookSlug
96      * @param $chapterSlug
97      * @return Response
98      */
99     public function update(Request $request, $bookSlug, $chapterSlug)
100     {
101         $book = $this->bookRepo->getBySlug($bookSlug);
102         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
103         $chapter->fill($request->all());
104         $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
105         $chapter->save();
106         return redirect($chapter->getUrl());
107     }
108
109     /**
110      * Shows the page to confirm deletion of this chapter.
111      * @param $bookSlug
112      * @param $chapterSlug
113      * @return \Illuminate\View\View
114      */
115     public function showDelete($bookSlug, $chapterSlug)
116     {
117         $book = $this->bookRepo->getBySlug($bookSlug);
118         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
119         return view('chapters/delete', ['book' => $book, 'chapter' => $chapter]);
120     }
121
122     /**
123      * Remove the specified chapter from storage.
124      *
125      * @param $bookSlug
126      * @param $chapterSlug
127      * @return Response
128      */
129     public function destroy($bookSlug, $chapterSlug)
130     {
131         $book = $this->bookRepo->getBySlug($bookSlug);
132         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
133         if(count($chapter->pages) > 0) {
134             foreach($chapter->pages as $page) {
135                 $page->chapter_id = 0;
136                 $page->save();
137             }
138         }
139         $chapter->delete();
140         return redirect($book->getUrl());
141     }
142 }