]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Added missing colon
[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 BookStack\Services\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     protected $exportService;
17
18     /**
19      * ChapterController constructor.
20      * @param EntityRepo $entityRepo
21      * @param UserRepo $userRepo
22      * @param ExportService $exportService
23      */
24     public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
25     {
26         $this->entityRepo = $entityRepo;
27         $this->userRepo = $userRepo;
28         $this->exportService = $exportService;
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->entityRepo->getBySlug('book', $bookSlug);
40         $this->checkOwnablePermission('chapter-create', $book);
41         $this->setPageTitle(trans('entities.chapters_create'));
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->entityRepo->getBySlug('book', $bookSlug);
58         $this->checkOwnablePermission('chapter-create', $book);
59
60         $input = $request->all();
61         $input['priority'] = $this->entityRepo->getNewBookPriority($book);
62         $chapter = $this->entityRepo->createFromInput('chapter', $input, $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         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
76         $this->checkOwnablePermission('chapter-view', $chapter);
77         $sidebarTree = $this->entityRepo->getBookChildren($chapter->book);
78         Views::add($chapter);
79         $this->setPageTitle($chapter->getShortName());
80         $pages = $this->entityRepo->getChapterChildren($chapter);
81         return view('chapters/show', [
82             'book' => $chapter->book,
83             'chapter' => $chapter,
84             'current' => $chapter,
85             'sidebarTree' => $sidebarTree,
86             'pages' => $pages
87         ]);
88     }
89
90     /**
91      * Show the form for editing the specified chapter.
92      * @param $bookSlug
93      * @param $chapterSlug
94      * @return Response
95      */
96     public function edit($bookSlug, $chapterSlug)
97     {
98         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
99         $this->checkOwnablePermission('chapter-update', $chapter);
100         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
101         return view('chapters/edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
102     }
103
104     /**
105      * Update the specified chapter in storage.
106      * @param  Request $request
107      * @param          $bookSlug
108      * @param          $chapterSlug
109      * @return Response
110      */
111     public function update(Request $request, $bookSlug, $chapterSlug)
112     {
113         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
114         $this->checkOwnablePermission('chapter-update', $chapter);
115         if ($chapter->name !== $request->get('name')) {
116             $chapter->slug = $this->entityRepo->findSuitableSlug('chapter', $request->get('name'), $chapter->id, $chapter->book->id);
117         }
118         $chapter->fill($request->all());
119         $chapter->updated_by = user()->id;
120         $chapter->save();
121         Activity::add($chapter, 'chapter_update', $chapter->book->id);
122         return redirect($chapter->getUrl());
123     }
124
125     /**
126      * Shows the page to confirm deletion of this chapter.
127      * @param $bookSlug
128      * @param $chapterSlug
129      * @return \Illuminate\View\View
130      */
131     public function showDelete($bookSlug, $chapterSlug)
132     {
133         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
134         $this->checkOwnablePermission('chapter-delete', $chapter);
135         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
136         return view('chapters/delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
137     }
138
139     /**
140      * Remove the specified chapter from storage.
141      * @param $bookSlug
142      * @param $chapterSlug
143      * @return Response
144      */
145     public function destroy($bookSlug, $chapterSlug)
146     {
147         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
148         $book = $chapter->book;
149         $this->checkOwnablePermission('chapter-delete', $chapter);
150         Activity::addMessage('chapter_delete', $book->id, $chapter->name);
151         $this->entityRepo->destroyChapter($chapter);
152         return redirect($book->getUrl());
153     }
154
155     /**
156      * Show the page for moving a chapter.
157      * @param $bookSlug
158      * @param $chapterSlug
159      * @return mixed
160      * @throws \BookStack\Exceptions\NotFoundException
161      */
162     public function showMove($bookSlug, $chapterSlug) {
163         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
164         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
165         $this->checkOwnablePermission('chapter-update', $chapter);
166         return view('chapters/move', [
167             'chapter' => $chapter,
168             'book' => $chapter->book
169         ]);
170     }
171
172     /**
173      * Perform the move action for a chapter.
174      * @param $bookSlug
175      * @param $chapterSlug
176      * @param Request $request
177      * @return mixed
178      * @throws \BookStack\Exceptions\NotFoundException
179      */
180     public function move($bookSlug, $chapterSlug, Request $request) {
181         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
182         $this->checkOwnablePermission('chapter-update', $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      */
217     public function showRestrict($bookSlug, $chapterSlug)
218     {
219         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
220         $this->checkOwnablePermission('restrictions-manage', $chapter);
221         $roles = $this->userRepo->getRestrictableRoles();
222         return view('chapters/restrictions', [
223             'chapter' => $chapter,
224             'roles' => $roles
225         ]);
226     }
227
228     /**
229      * Set the restrictions for this chapter.
230      * @param $bookSlug
231      * @param $chapterSlug
232      * @param Request $request
233      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
234      */
235     public function restrict($bookSlug, $chapterSlug, Request $request)
236     {
237         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
238         $this->checkOwnablePermission('restrictions-manage', $chapter);
239         $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
240         session()->flash('success', trans('entities.chapters_permissions_success'));
241         return redirect($chapter->getUrl());
242     }
243
244     /**
245      * Exports a chapter to pdf .
246      * @param string $bookSlug
247      * @param string $chapterSlug
248      * @return \Illuminate\Http\Response
249      */
250     public function exportPdf($bookSlug, $chapterSlug)
251     {
252         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
253         $pdfContent = $this->exportService->chapterToPdf($chapter);
254         return response()->make($pdfContent, 200, [
255             'Content-Type'        => 'application/octet-stream',
256             'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.pdf'
257         ]);
258     }
259
260     /**
261      * Export a chapter to a self-contained HTML file.
262      * @param string $bookSlug
263      * @param string $chapterSlug
264      * @return \Illuminate\Http\Response
265      */
266     public function exportHtml($bookSlug, $chapterSlug)
267     {
268         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
269         $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
270         return response()->make($containedHtml, 200, [
271             'Content-Type'        => 'application/octet-stream',
272             'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.html'
273         ]);
274     }
275
276     /**
277      * Export a chapter to a simple plaintext .txt file.
278      * @param string $bookSlug
279      * @param string $chapterSlug
280      * @return \Illuminate\Http\Response
281      */
282     public function exportPlainText($bookSlug, $chapterSlug)
283     {
284         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
285         $containedHtml = $this->exportService->chapterToPlainText($chapter);
286         return response()->make($containedHtml, 200, [
287             'Content-Type'        => 'application/octet-stream',
288             'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.txt'
289         ]);
290     }
291 }