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