]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Added ability to configure email sender name
[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     {
164         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
165         $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
166         $this->checkOwnablePermission('chapter-update', $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 $bookSlug
176      * @param $chapterSlug
177      * @param Request $request
178      * @return mixed
179      * @throws \BookStack\Exceptions\NotFoundException
180      */
181     public function move($bookSlug, $chapterSlug, Request $request)
182     {
183         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
184         $this->checkOwnablePermission('chapter-update', $chapter);
185
186         $entitySelection = $request->get('entity_selection', null);
187         if ($entitySelection === null || $entitySelection === '') {
188             return redirect($chapter->getUrl());
189         }
190
191         $stringExploded = explode(':', $entitySelection);
192         $entityType = $stringExploded[0];
193         $entityId = intval($stringExploded[1]);
194
195         $parent = false;
196
197         if ($entityType == 'book') {
198             $parent = $this->entityRepo->getById('book', $entityId);
199         }
200
201         if ($parent === false || $parent === null) {
202             session()->flash('error', trans('errors.selected_book_not_found'));
203             return redirect()->back();
204         }
205
206         $this->entityRepo->changeBook('chapter', $parent->id, $chapter, true);
207         Activity::add($chapter, 'chapter_move', $chapter->book->id);
208         session()->flash('success', trans('entities.chapter_move_success', ['bookName' => $parent->name]));
209
210         return redirect($chapter->getUrl());
211     }
212
213     /**
214      * Show the Restrictions view.
215      * @param $bookSlug
216      * @param $chapterSlug
217      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
218      */
219     public function showRestrict($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/restrictions', [
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      */
237     public function restrict($bookSlug, $chapterSlug, Request $request)
238     {
239         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
240         $this->checkOwnablePermission('restrictions-manage', $chapter);
241         $this->entityRepo->updateEntityPermissionsFromRequest($request, $chapter);
242         session()->flash('success', trans('entities.chapters_permissions_success'));
243         return redirect($chapter->getUrl());
244     }
245
246     /**
247      * Exports a chapter to pdf .
248      * @param string $bookSlug
249      * @param string $chapterSlug
250      * @return \Illuminate\Http\Response
251      */
252     public function exportPdf($bookSlug, $chapterSlug)
253     {
254         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
255         $pdfContent = $this->exportService->chapterToPdf($chapter);
256         return response()->make($pdfContent, 200, [
257             'Content-Type'        => 'application/octet-stream',
258             'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.pdf'
259         ]);
260     }
261
262     /**
263      * Export a chapter to a self-contained HTML file.
264      * @param string $bookSlug
265      * @param string $chapterSlug
266      * @return \Illuminate\Http\Response
267      */
268     public function exportHtml($bookSlug, $chapterSlug)
269     {
270         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
271         $containedHtml = $this->exportService->chapterToContainedHtml($chapter);
272         return response()->make($containedHtml, 200, [
273             'Content-Type'        => 'application/octet-stream',
274             'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.html'
275         ]);
276     }
277
278     /**
279      * Export a chapter to a simple plaintext .txt file.
280      * @param string $bookSlug
281      * @param string $chapterSlug
282      * @return \Illuminate\Http\Response
283      */
284     public function exportPlainText($bookSlug, $chapterSlug)
285     {
286         $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
287         $containedHtml = $this->exportService->chapterToPlainText($chapter);
288         return response()->make($containedHtml, 200, [
289             'Content-Type'        => 'application/octet-stream',
290             'Content-Disposition' => 'attachment; filename="' . $chapterSlug . '.txt'
291         ]);
292     }
293 }