]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Merge branch 'patch-1' of git://github.com/XVilka/BookStack into XVilka-patch-1
[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         $this->checkOwnablePermission('chapter-delete', $chapter);
165         return view('chapters.move', [
166             'chapter' => $chapter,
167             'book' => $chapter->book
168         ]);
169     }
170
171     /**
172      * Perform the move action for a chapter.
173      * @param $bookSlug
174      * @param $chapterSlug
175      * @param Request $request
176      * @return mixed
177      * @throws \BookStack\Exceptions\NotFoundException
178      */
179     public function move($bookSlug, $chapterSlug, Request $request)
180     {
181         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
182         $this->checkOwnablePermission('chapter-update', $chapter);
183         $this->checkOwnablePermission('chapter-delete', $chapter);
184
185         $entitySelection = $request->get('entity_selection', null);
186         if ($entitySelection === null || $entitySelection === '') {
187             return redirect($chapter->getUrl());
188         }
189
190         $stringExploded = explode(':', $entitySelection);
191         $entityType = $stringExploded[0];
192         $entityId = intval($stringExploded[1]);
193
194         $parent = false;
195
196         if ($entityType == 'book') {
197             $parent = $this->entityRepo->getById('book', $entityId);
198         }
199
200         if ($parent === false || $parent === null) {
201             session()->flash('error', trans('errors.selected_book_not_found'));
202             return redirect()->back();
203         }
204
205         $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
206         Activity::add($chapter, 'chapter_move', $chapter->book->id);
207         session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
208
209         return redirect($chapter->getUrl());
210     }
211
212     /**
213      * Show the Restrictions view.
214      * @param $bookSlug
215      * @param $chapterSlug
216      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
217      * @throws \BookStack\Exceptions\NotFoundException
218      */
219     public function showPermissions($bookSlug, $chapterSlug)
220     {
221         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
222         $this->checkOwnablePermission('restrictions-manage', $chapter);
223         $roles = $this->userRepo->getRestrictableRoles();
224         return view('chapters.permissions', [
225             'chapter' => $chapter,
226             'roles' => $roles
227         ]);
228     }
229
230     /**
231      * Set the restrictions for this chapter.
232      * @param $bookSlug
233      * @param $chapterSlug
234      * @param Request $request
235      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
236      * @throws \BookStack\Exceptions\NotFoundException
237      * @throws \Throwable
238      */
239     public function permissions($bookSlug, $chapterSlug, Request $request)
240     {
241         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
242         $this->checkOwnablePermission('restrictions-manage', $chapter);
243         $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
244         session()->flash('success', trans('entities.chapters_permissions_success'));
245         return redirect($chapter->getUrl());
246     }
247
248     /**
249      * Exports a chapter to pdf .
250      * @param string $bookSlug
251      * @param string $chapterSlug
252      * @return \Illuminate\Http\Response
253      */
254     public function exportPdf($bookSlug, $chapterSlug)
255     {
256         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
257         $pdfContent = $this->exportService->chapterToPdf($chapter);
258         return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
259     }
260
261     /**
262      * Export a chapter to a self-contained HTML file.
263      * @param string $bookSlug
264      * @param string $chapterSlug
265      * @return \Illuminate\Http\Response
266      */
267     public function exportHtml($bookSlug, $chapterSlug)
268     {
269         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
270         $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
271         return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
272     }
273
274     /**
275      * Export a chapter to a simple plaintext .txt file.
276      * @param string $bookSlug
277      * @param string $chapterSlug
278      * @return \Illuminate\Http\Response
279      */
280     public function exportPlainText($bookSlug, $chapterSlug)
281     {
282         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
283         $chapterText = $this->exportService->chapterToPlainText($chapter);
284         return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
285     }
286 }