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