]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Added chapter move actions. Closes #86
[bookstack] / app / Http / Controllers / ChapterController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Repos\UserRepo;
5 use Illuminate\Http\Request;
6 use BookStack\Http\Requests;
7 use BookStack\Repos\BookRepo;
8 use BookStack\Repos\ChapterRepo;
9 use Views;
10
11 class ChapterController extends Controller
12 {
13
14     protected $bookRepo;
15     protected $chapterRepo;
16     protected $userRepo;
17
18     /**
19      * ChapterController constructor.
20      * @param BookRepo $bookRepo
21      * @param ChapterRepo $chapterRepo
22      * @param UserRepo $userRepo
23      */
24     public function __construct(BookRepo $bookRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
25     {
26         $this->bookRepo = $bookRepo;
27         $this->chapterRepo = $chapterRepo;
28         $this->userRepo = $userRepo;
29         parent::__construct();
30     }
31
32     /**
33      * Show the form for creating a new chapter.
34      * @param $bookSlug
35      * @return Response
36      */
37     public function create($bookSlug)
38     {
39         $book = $this->bookRepo->getBySlug($bookSlug);
40         $this->checkOwnablePermission('chapter-create', $book);
41         $this->setPageTitle('Create New Chapter');
42         return view('chapters/create', ['book' => $book, 'current' => $book]);
43     }
44
45     /**
46      * Store a newly created chapter in storage.
47      * @param          $bookSlug
48      * @param  Request $request
49      * @return Response
50      */
51     public function store($bookSlug, Request $request)
52     {
53         $this->validate($request, [
54             'name' => 'required|string|max:255'
55         ]);
56
57         $book = $this->bookRepo->getBySlug($bookSlug);
58         $this->checkOwnablePermission('chapter-create', $book);
59
60         $input = $request->all();
61         $input['priority'] = $this->bookRepo->getNewPriority($book);
62         $chapter = $this->chapterRepo->createFromInput($request->all(), $book);
63         Activity::add($chapter, 'chapter_create', $book->id);
64         return redirect($chapter->getUrl());
65     }
66
67     /**
68      * Display the specified chapter.
69      * @param $bookSlug
70      * @param $chapterSlug
71      * @return Response
72      */
73     public function show($bookSlug, $chapterSlug)
74     {
75         $book = $this->bookRepo->getBySlug($bookSlug);
76         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
77         $this->checkOwnablePermission('chapter-view', $chapter);
78         $sidebarTree = $this->bookRepo->getChildren($book);
79         Views::add($chapter);
80         $this->setPageTitle($chapter->getShortName());
81         $pages = $this->chapterRepo->getChildren($chapter);
82         return view('chapters/show', [
83             'book' => $book,
84             'chapter' => $chapter,
85             'current' => $chapter,
86             'sidebarTree' => $sidebarTree,
87             'pages' => $pages
88         ]);
89     }
90
91     /**
92      * Show the form for editing the specified chapter.
93      * @param $bookSlug
94      * @param $chapterSlug
95      * @return Response
96      */
97     public function edit($bookSlug, $chapterSlug)
98     {
99         $book = $this->bookRepo->getBySlug($bookSlug);
100         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
101         $this->checkOwnablePermission('chapter-update', $chapter);
102         $this->setPageTitle('Edit Chapter' . $chapter->getShortName());
103         return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
104     }
105
106     /**
107      * Update the specified chapter in storage.
108      * @param  Request $request
109      * @param          $bookSlug
110      * @param          $chapterSlug
111      * @return Response
112      */
113     public function update(Request $request, $bookSlug, $chapterSlug)
114     {
115         $book = $this->bookRepo->getBySlug($bookSlug);
116         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
117         $this->checkOwnablePermission('chapter-update', $chapter);
118         $chapter->fill($request->all());
119         $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
120         $chapter->updated_by = auth()->user()->id;
121         $chapter->save();
122         Activity::add($chapter, 'chapter_update', $book->id);
123         return redirect($chapter->getUrl());
124     }
125
126     /**
127      * Shows the page to confirm deletion of this chapter.
128      * @param $bookSlug
129      * @param $chapterSlug
130      * @return \Illuminate\View\View
131      */
132     public function showDelete($bookSlug, $chapterSlug)
133     {
134         $book = $this->bookRepo->getBySlug($bookSlug);
135         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
136         $this->checkOwnablePermission('chapter-delete', $chapter);
137         $this->setPageTitle('Delete Chapter' . $chapter->getShortName());
138         return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
139     }
140
141     /**
142      * Remove the specified chapter from storage.
143      * @param $bookSlug
144      * @param $chapterSlug
145      * @return Response
146      */
147     public function destroy($bookSlug, $chapterSlug)
148     {
149         $book = $this->bookRepo->getBySlug($bookSlug);
150         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
151         $this->checkOwnablePermission('chapter-delete', $chapter);
152         Activity::addMessage('chapter_delete', $book->id, $chapter->name);
153         $this->chapterRepo->destroy($chapter);
154         return redirect($book->getUrl());
155     }
156
157     /**
158      * Show the page for moving a chapter.
159      * @param $bookSlug
160      * @param $chapterSlug
161      * @return mixed
162      * @throws \BookStack\Exceptions\NotFoundException
163      */
164     public function showMove($bookSlug, $chapterSlug) {
165         $book = $this->bookRepo->getBySlug($bookSlug);
166         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
167         $this->checkOwnablePermission('chapter-update', $chapter);
168         return view('chapters/move', [
169             'chapter' => $chapter,
170             'book' => $book
171         ]);
172     }
173
174     public function move($bookSlug, $chapterSlug, Request $request) {
175         $book = $this->bookRepo->getBySlug($bookSlug);
176         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
177         $this->checkOwnablePermission('chapter-update', $chapter);
178
179         $entitySelection = $request->get('entity_selection', null);
180         if ($entitySelection === null || $entitySelection === '') {
181             return redirect($chapter->getUrl());
182         }
183
184         $stringExploded = explode(':', $entitySelection);
185         $entityType = $stringExploded[0];
186         $entityId = intval($stringExploded[1]);
187
188         $parent = false;
189
190         if ($entityType == 'book') {
191             $parent = $this->bookRepo->getById($entityId);
192         }
193
194         if ($parent === false || $parent === null) {
195             session()->flash('The selected Book was not found');
196             return redirect()->back();
197         }
198
199         $this->chapterRepo->changeBook($parent->id, $chapter);
200         Activity::add($chapter, 'chapter_move', $chapter->book->id);
201         session()->flash('success', sprintf('Chapter moved to "%s"', $parent->name));
202
203         return redirect($chapter->getUrl());
204     }
205
206     /**
207      * Show the Restrictions view.
208      * @param $bookSlug
209      * @param $chapterSlug
210      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
211      */
212     public function showRestrict($bookSlug, $chapterSlug)
213     {
214         $book = $this->bookRepo->getBySlug($bookSlug);
215         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
216         $this->checkOwnablePermission('restrictions-manage', $chapter);
217         $roles = $this->userRepo->getRestrictableRoles();
218         return view('chapters/restrictions', [
219             'chapter' => $chapter,
220             'roles' => $roles
221         ]);
222     }
223
224     /**
225      * Set the restrictions for this chapter.
226      * @param $bookSlug
227      * @param $chapterSlug
228      * @param Request $request
229      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
230      */
231     public function restrict($bookSlug, $chapterSlug, Request $request)
232     {
233         $book = $this->bookRepo->getBySlug($bookSlug);
234         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
235         $this->checkOwnablePermission('restrictions-manage', $chapter);
236         $this->chapterRepo->updateEntityPermissionsFromRequest($request, $chapter);
237         session()->flash('success', 'Chapter Restrictions Updated');
238         return redirect($chapter->getUrl());
239     }
240 }