]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Split out export actions into own controllers
[bookstack] / app / Http / Controllers / ChapterController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Repos\EntityRepo;
6 use BookStack\Entities\ExportService;
7 use Illuminate\Http\Request;
8 use Illuminate\Http\Response;
9 use Views;
10
11 class ChapterController extends Controller
12 {
13
14     protected $userRepo;
15     protected $entityRepo;
16
17     /**
18      * ChapterController constructor.
19      * @param EntityRepo $entityRepo
20      * @param UserRepo $userRepo
21      */
22     public function __construct(EntityRepo $entityRepo, UserRepo $userRepo)
23     {
24         $this->entityRepo = $entityRepo;
25         $this->userRepo = $userRepo;
26         parent::__construct();
27     }
28
29     /**
30      * Show the form for creating a new chapter.
31      * @param $bookSlug
32      * @return Response
33      */
34     public function create($bookSlug)
35     {
36         $book = $this->entityRepo->getBySlug('book', $bookSlug);
37         $this->checkOwnablePermission('chapter-create', $book);
38         $this->setPageTitle(trans('entities.chapters_create'));
39         return view('chapters.create', ['book' => $book, 'current' => $book]);
40     }
41
42     /**
43      * Store a newly created chapter in storage.
44      * @param Request $request
45      * @param string $bookSlug
46      * @return Response
47      * @throws \BookStack\Exceptions\NotFoundException
48      * @throws \Illuminate\Validation\ValidationException
49      */
50     public function store(Request $request, string $bookSlug)
51     {
52         $this->validate($request, [
53             'name' => 'required|string|max:255'
54         ]);
55
56         $book = $this->entityRepo->getBySlug('book', $bookSlug);
57         $this->checkOwnablePermission('chapter-create', $book);
58
59         $input = $request->all();
60         $input['priority'] = $this->entityRepo->getNewBookPriority($book);
61         $chapter = $this->entityRepo->createFromInput('chapter', $input, $book);
62         Activity::add($chapter, 'chapter_create', $book->id);
63         return redirect($chapter->getUrl());
64     }
65
66     /**
67      * Display the specified chapter.
68      * @param $bookSlug
69      * @param $chapterSlug
70      * @return Response
71      */
72     public function show($bookSlug, $chapterSlug)
73     {
74         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
75         $this->checkOwnablePermission('chapter-view', $chapter);
76         $sidebarTree = $this->entityRepo->getBookChildren($chapter->book);
77         Views::add($chapter);
78         $this->setPageTitle($chapter->getShortName());
79         $pages = $this->entityRepo->getChapterChildren($chapter);
80         return view('chapters.show', [
81             'book' => $chapter->book,
82             'chapter' => $chapter,
83             'current' => $chapter,
84             'sidebarTree' => $sidebarTree,
85             'pages' => $pages
86         ]);
87     }
88
89     /**
90      * Show the form for editing the specified chapter.
91      * @param $bookSlug
92      * @param $chapterSlug
93      * @return Response
94      */
95     public function edit($bookSlug, $chapterSlug)
96     {
97         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
98         $this->checkOwnablePermission('chapter-update', $chapter);
99         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
100         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
101     }
102
103     /**
104      * Update the specified chapter in storage.
105      * @param Request $request
106      * @param string $bookSlug
107      * @param string $chapterSlug
108      * @return Response
109      * @throws \BookStack\Exceptions\NotFoundException
110      */
111     public function update(Request $request, string $bookSlug, string $chapterSlug)
112     {
113         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
114         $this->checkOwnablePermission('chapter-update', $chapter);
115
116         $this->entityRepo->updateFromInput('chapter', $chapter, $request->all());
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     {
160         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
161         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
162         $this->checkOwnablePermission('chapter-update', $chapter);
163         $this->checkOwnablePermission('chapter-delete', $chapter);
164         return view('chapters.move', [
165             'chapter' => $chapter,
166             'book' => $chapter->book
167         ]);
168     }
169
170     /**
171      * Perform the move action for a chapter.
172      * @param Request $request
173      * @param string $bookSlug
174      * @param string $chapterSlug
175      * @return mixed
176      * @throws \BookStack\Exceptions\NotFoundException
177      */
178     public function move(Request $request, string $bookSlug, string $chapterSlug)
179     {
180         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
181         $this->checkOwnablePermission('chapter-update', $chapter);
182         $this->checkOwnablePermission('chapter-delete', $chapter);
183
184         $entitySelection = $request->get('entity_selection', null);
185         if ($entitySelection === null || $entitySelection === '') {
186             return redirect($chapter->getUrl());
187         }
188
189         $stringExploded = explode(':', $entitySelection);
190         $entityType = $stringExploded[0];
191         $entityId = intval($stringExploded[1]);
192
193         $parent = false;
194
195         if ($entityType == 'book') {
196             $parent = $this->entityRepo->getById('book', $entityId);
197         }
198
199         if ($parent === false || $parent === null) {
200             session()->flash('error', trans('errors.selected_book_not_found'));
201             return redirect()->back();
202         }
203
204         $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
205         Activity::add($chapter, 'chapter_move', $chapter->book->id);
206         session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
207
208         return redirect($chapter->getUrl());
209     }
210
211     /**
212      * Show the Restrictions view.
213      * @param $bookSlug
214      * @param $chapterSlug
215      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
216      * @throws \BookStack\Exceptions\NotFoundException
217      */
218     public function showPermissions($bookSlug, $chapterSlug)
219     {
220         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
221         $this->checkOwnablePermission('restrictions-manage', $chapter);
222         $roles = $this->userRepo->getRestrictableRoles();
223         return view('chapters.permissions', [
224             'chapter' => $chapter,
225             'roles' => $roles
226         ]);
227     }
228
229     /**
230      * Set the restrictions for this chapter.
231      * @param Request $request
232      * @param string $bookSlug
233      * @param string $chapterSlug
234      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
235      * @throws \BookStack\Exceptions\NotFoundException
236      * @throws \Throwable
237      */
238     public function permissions(Request $request, string $bookSlug, string $chapterSlug)
239     {
240         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
241         $this->checkOwnablePermission('restrictions-manage', $chapter);
242         $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
243         session()->flash('success', trans('entities.chapters_permissions_success'));
244         return redirect($chapter->getUrl());
245     }
246 }