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