]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
83d45e823ded11058973c8bd29d553cfce895cbe
[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 Request $request
48      * @param string $bookSlug
49      * @return Response
50      * @throws \BookStack\Exceptions\NotFoundException
51      * @throws \Illuminate\Validation\ValidationException
52      */
53     public function store(Request $request, string $bookSlug)
54     {
55         $this->validate($request, [
56             'name' => 'required|string|max:255'
57         ]);
58
59         $book = $this->entityRepo->getBySlug('book', $bookSlug);
60         $this->checkOwnablePermission('chapter-create', $book);
61
62         $input = $request->all();
63         $input['priority'] = $this->entityRepo->getNewBookPriority($book);
64         $chapter = $this->entityRepo->createFromInput('chapter', $input, $book);
65         Activity::add($chapter, 'chapter_create', $book->id);
66         return redirect($chapter->getUrl());
67     }
68
69     /**
70      * Display the specified chapter.
71      * @param $bookSlug
72      * @param $chapterSlug
73      * @return Response
74      */
75     public function show($bookSlug, $chapterSlug)
76     {
77         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
78         $this->checkOwnablePermission('chapter-view', $chapter);
79         $sidebarTree = $this->entityRepo->getBookChildren($chapter->book);
80         Views::add($chapter);
81         $this->setPageTitle($chapter->getShortName());
82         $pages = $this->entityRepo->getChapterChildren($chapter);
83         return view('chapters.show', [
84             'book' => $chapter->book,
85             'chapter' => $chapter,
86             'current' => $chapter,
87             'sidebarTree' => $sidebarTree,
88             'pages' => $pages
89         ]);
90     }
91
92     /**
93      * Show the form for editing the specified chapter.
94      * @param $bookSlug
95      * @param $chapterSlug
96      * @return Response
97      */
98     public function edit($bookSlug, $chapterSlug)
99     {
100         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
101         $this->checkOwnablePermission('chapter-update', $chapter);
102         $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
103         return view('chapters.edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
104     }
105
106     /**
107      * Update the specified chapter in storage.
108      * @param Request $request
109      * @param string $bookSlug
110      * @param string $chapterSlug
111      * @return Response
112      * @throws \BookStack\Exceptions\NotFoundException
113      */
114     public function update(Request $request, string $bookSlug, string $chapterSlug)
115     {
116         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
117         $this->checkOwnablePermission('chapter-update', $chapter);
118
119         $this->entityRepo->updateFromInput('chapter', $chapter, $request->all());
120         Activity::add($chapter, 'chapter_update', $chapter->book->id);
121         return redirect($chapter->getUrl());
122     }
123
124     /**
125      * Shows the page to confirm deletion of this chapter.
126      * @param $bookSlug
127      * @param $chapterSlug
128      * @return \Illuminate\View\View
129      */
130     public function showDelete($bookSlug, $chapterSlug)
131     {
132         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
133         $this->checkOwnablePermission('chapter-delete', $chapter);
134         $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
135         return view('chapters.delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
136     }
137
138     /**
139      * Remove the specified chapter from storage.
140      * @param $bookSlug
141      * @param $chapterSlug
142      * @return Response
143      */
144     public function destroy($bookSlug, $chapterSlug)
145     {
146         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
147         $book = $chapter->book;
148         $this->checkOwnablePermission('chapter-delete', $chapter);
149         Activity::addMessage('chapter_delete', $book->id, $chapter->name);
150         $this->entityRepo->destroyChapter($chapter);
151         return redirect($book->getUrl());
152     }
153
154     /**
155      * Show the page for moving a chapter.
156      * @param $bookSlug
157      * @param $chapterSlug
158      * @return mixed
159      * @throws \BookStack\Exceptions\NotFoundException
160      */
161     public function showMove($bookSlug, $chapterSlug)
162     {
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         $this->checkOwnablePermission('chapter-delete', $chapter);
167         return view('chapters.move', [
168             'chapter' => $chapter,
169             'book' => $chapter->book
170         ]);
171     }
172
173     /**
174      * Perform the move action for a chapter.
175      * @param Request $request
176      * @param string $bookSlug
177      * @param string $chapterSlug
178      * @return mixed
179      * @throws \BookStack\Exceptions\NotFoundException
180      */
181     public function move(Request $request, string $bookSlug, string $chapterSlug)
182     {
183         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
184         $this->checkOwnablePermission('chapter-update', $chapter);
185         $this->checkOwnablePermission('chapter-delete', $chapter);
186
187         $entitySelection = $request->get('entity_selection', null);
188         if ($entitySelection === null || $entitySelection === '') {
189             return redirect($chapter->getUrl());
190         }
191
192         $stringExploded = explode(':', $entitySelection);
193         $entityType = $stringExploded[0];
194         $entityId = intval($stringExploded[1]);
195
196         $parent = false;
197
198         if ($entityType == 'book') {
199             $parent = $this->entityRepo->getById('book', $entityId);
200         }
201
202         if ($parent === false || $parent === null) {
203             session()->flash('error', trans('errors.selected_book_not_found'));
204             return redirect()->back();
205         }
206
207         $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
208         Activity::add($chapter, 'chapter_move', $chapter->book->id);
209         session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
210
211         return redirect($chapter->getUrl());
212     }
213
214     /**
215      * Show the Restrictions view.
216      * @param $bookSlug
217      * @param $chapterSlug
218      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
219      * @throws \BookStack\Exceptions\NotFoundException
220      */
221     public function showPermissions($bookSlug, $chapterSlug)
222     {
223         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
224         $this->checkOwnablePermission('restrictions-manage', $chapter);
225         $roles = $this->userRepo->getRestrictableRoles();
226         return view('chapters.permissions', [
227             'chapter' => $chapter,
228             'roles' => $roles
229         ]);
230     }
231
232     /**
233      * Set the restrictions for this chapter.
234      * @param Request $request
235      * @param string $bookSlug
236      * @param string $chapterSlug
237      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
238      * @throws \BookStack\Exceptions\NotFoundException
239      * @throws \Throwable
240      */
241     public function permissions(Request $request, string $bookSlug, string $chapterSlug)
242     {
243         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
244         $this->checkOwnablePermission('restrictions-manage', $chapter);
245         $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
246         session()->flash('success', trans('entities.chapters_permissions_success'));
247         return redirect($chapter->getUrl());
248     }
249
250     /**
251      * Exports a chapter to pdf .
252      * @param string $bookSlug
253      * @param string $chapterSlug
254      * @return \Illuminate\Http\Response
255      */
256     public function exportPdf($bookSlug, $chapterSlug)
257     {
258         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
259         $pdfContent = $this->exportService->chapterToPdf($chapter);
260         return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
261     }
262
263     /**
264      * Export a chapter to a self-contained HTML file.
265      * @param string $bookSlug
266      * @param string $chapterSlug
267      * @return \Illuminate\Http\Response
268      */
269     public function exportHtml($bookSlug, $chapterSlug)
270     {
271         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
272         $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
273         return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
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         $chapterText = $this->exportService->chapterToPlainText($chapter);
286         return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
287     }
288 }