]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Merge pull request #1066 from limkukhyun/codev-kuk-master
[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     protected $exportService;
17
18     /**
19      * ChapterController constructor.
20      * @param EntityRepo $entityRepo
21      * @param UserRepo $userRepo
22      * @param \BookStack\Entities\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      * @throws \BookStack\Exceptions\NotFoundException
111      */
112     public function update(Request $request, $bookSlug, $chapterSlug)
113     {
114         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
115         $this->checkOwnablePermission('chapter-update', $chapter);
116
117         $this->entityRepo->updateFromInput('chapter', $chapter, $request->all());
118         Activity::add($chapter, 'chapter_update', $chapter->book->id);
119         return redirect($chapter->getUrl());
120     }
121
122     /**
123      * Shows the page to confirm deletion of this chapter.
124      * @param $bookSlug
125      * @param $chapterSlug
126      * @return \Illuminate\View\View
127      */
128     public function showDelete($bookSlug, $chapterSlug)
129     {
130         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
131         $this->checkOwnablePermission('chapter-delete', $chapter);
132         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
133         return view('chapters/delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
134     }
135
136     /**
137      * Remove the specified chapter from storage.
138      * @param $bookSlug
139      * @param $chapterSlug
140      * @return Response
141      */
142     public function destroy($bookSlug, $chapterSlug)
143     {
144         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
145         $book = $chapter->book;
146         $this->checkOwnablePermission('chapter-delete', $chapter);
147         Activity::addMessage('chapter_delete', $book->id, $chapter->name);
148         $this->entityRepo->destroyChapter($chapter);
149         return redirect($book->getUrl());
150     }
151
152     /**
153      * Show the page for moving a chapter.
154      * @param $bookSlug
155      * @param $chapterSlug
156      * @return mixed
157      * @throws \BookStack\Exceptions\NotFoundException
158      */
159     public function showMove($bookSlug, $chapterSlug)
160     {
161         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
162         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
163         $this->checkOwnablePermission('chapter-update', $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 $bookSlug
173      * @param $chapterSlug
174      * @param Request $request
175      * @return mixed
176      * @throws \BookStack\Exceptions\NotFoundException
177      */
178     public function move($bookSlug, $chapterSlug, Request $request)
179     {
180         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
181         $this->checkOwnablePermission('chapter-update', $chapter);
182
183         $entitySelection = $request->get('entity_selection', null);
184         if ($entitySelection === null || $entitySelection === '') {
185             return redirect($chapter->getUrl());
186         }
187
188         $stringExploded = explode(':', $entitySelection);
189         $entityType = $stringExploded[0];
190         $entityId = intval($stringExploded[1]);
191
192         $parent = false;
193
194         if ($entityType == 'book') {
195             $parent = $this->entityRepo->getById('book', $entityId);
196         }
197
198         if ($parent === false || $parent === null) {
199             session()->flash('error', trans('errors.selected_book_not_found'));
200             return redirect()->back();
201         }
202
203         $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
204         Activity::add($chapter, 'chapter_move', $chapter->book->id);
205         session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
206
207         return redirect($chapter->getUrl());
208     }
209
210     /**
211      * Show the Restrictions view.
212      * @param $bookSlug
213      * @param $chapterSlug
214      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
215      */
216     public function showRestrict($bookSlug, $chapterSlug)
217     {
218         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
219         $this->checkOwnablePermission('restrictions-manage', $chapter);
220         $roles = $this->userRepo->getRestrictableRoles();
221         return view('chapters/restrictions', [
222             'chapter' => $chapter,
223             'roles' => $roles
224         ]);
225     }
226
227     /**
228      * Set the restrictions for this chapter.
229      * @param $bookSlug
230      * @param $chapterSlug
231      * @param Request $request
232      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
233      */
234     public function restrict($bookSlug, $chapterSlug, Request $request)
235     {
236         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
237         $this->checkOwnablePermission('restrictions-manage', $chapter);
238         $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
239         session()->flash('success', trans('entities.chapters_permissions_success'));
240         return redirect($chapter->getUrl());
241     }
242
243     /**
244      * Exports a chapter to pdf .
245      * @param string $bookSlug
246      * @param string $chapterSlug
247      * @return \Illuminate\Http\Response
248      */
249     public function exportPdf($bookSlug, $chapterSlug)
250     {
251         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
252         $pdfContent = $this->exportService->chapterToPdf($chapter);
253         return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
254     }
255
256     /**
257      * Export a chapter to a self-contained HTML file.
258      * @param string $bookSlug
259      * @param string $chapterSlug
260      * @return \Illuminate\Http\Response
261      */
262     public function exportHtml($bookSlug, $chapterSlug)
263     {
264         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
265         $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
266         return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
267     }
268
269     /**
270      * Export a chapter to a simple plaintext .txt file.
271      * @param string $bookSlug
272      * @param string $chapterSlug
273      * @return \Illuminate\Http\Response
274      */
275     public function exportPlainText($bookSlug, $chapterSlug)
276     {
277         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
278         $chapterText = $this->exportService->chapterToPlainText($chapter);
279         return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
280     }
281 }