]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
Closes #69. Implemented and tested memcached.
[bookstack] / app / Http / Controllers / PageController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use Activity;
6 use BookStack\Services\ExportService;
7 use Illuminate\Http\Request;
8
9 use Illuminate\Support\Facades\Auth;
10 use BookStack\Http\Requests;
11 use BookStack\Repos\BookRepo;
12 use BookStack\Repos\ChapterRepo;
13 use BookStack\Repos\PageRepo;
14 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15 use Views;
16
17 class PageController extends Controller
18 {
19
20     protected $pageRepo;
21     protected $bookRepo;
22     protected $chapterRepo;
23     protected $exportService;
24
25     /**
26      * PageController constructor.
27      * @param PageRepo      $pageRepo
28      * @param BookRepo      $bookRepo
29      * @param ChapterRepo   $chapterRepo
30      * @param ExportService $exportService
31      */
32     public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService)
33     {
34         $this->pageRepo = $pageRepo;
35         $this->bookRepo = $bookRepo;
36         $this->chapterRepo = $chapterRepo;
37         $this->exportService = $exportService;
38         parent::__construct();
39     }
40
41     /**
42      * Show the form for creating a new page.
43      *
44      * @param      $bookSlug
45      * @param bool $chapterSlug
46      * @return Response
47      * @internal param bool $pageSlug
48      */
49     public function create($bookSlug, $chapterSlug = false)
50     {
51         $this->checkPermission('page-create');
52         $book = $this->bookRepo->getBySlug($bookSlug);
53         $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : false;
54         $this->setPageTitle('Create New Page');
55         return view('pages/create', ['book' => $book, 'chapter' => $chapter]);
56     }
57
58     /**
59      * Store a newly created page in storage.
60      *
61      * @param  Request $request
62      * @param          $bookSlug
63      * @return Response
64      */
65     public function store(Request $request, $bookSlug)
66     {
67         $this->checkPermission('page-create');
68         $this->validate($request, [
69             'name'   => 'required|string|max:255'
70         ]);
71
72         $input = $request->all();
73         $book = $this->bookRepo->getBySlug($bookSlug);
74         $chapterId = ($request->has('chapter') && $this->chapterRepo->idExists($request->get('chapter'))) ? $request->get('chapter') : null;
75         $input['priority'] = $this->bookRepo->getNewPriority($book);
76
77         $page = $this->pageRepo->saveNew($input, $book, $chapterId);
78
79         Activity::add($page, 'page_create', $book->id);
80         return redirect($page->getUrl());
81     }
82
83     /**
84      * Display the specified page.
85      * If the page is not found via the slug the
86      * revisions are searched for a match.
87      *
88      * @param $bookSlug
89      * @param $pageSlug
90      * @return Response
91      */
92     public function show($bookSlug, $pageSlug)
93     {
94         $book = $this->bookRepo->getBySlug($bookSlug);
95
96         try {
97             $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
98         } catch (NotFoundHttpException $e) {
99             $page = $this->pageRepo->findPageUsingOldSlug($pageSlug, $bookSlug);
100             if ($page === null) abort(404);
101             return redirect($page->getUrl());
102         }
103
104         $sidebarTree = $this->bookRepo->getChildren($book);
105         Views::add($page);
106         $this->setPageTitle($page->getShortName());
107         return view('pages/show', ['page' => $page, 'book' => $book, 'current' => $page, 'sidebarTree' => $sidebarTree]);
108     }
109
110     /**
111      * Show the form for editing the specified page.
112      *
113      * @param $bookSlug
114      * @param $pageSlug
115      * @return Response
116      */
117     public function edit($bookSlug, $pageSlug)
118     {
119         $this->checkPermission('page-update');
120         $book = $this->bookRepo->getBySlug($bookSlug);
121         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
122         $this->setPageTitle('Editing Page ' . $page->getShortName());
123         return view('pages/edit', ['page' => $page, 'book' => $book, 'current' => $page]);
124     }
125
126     /**
127      * Update the specified page in storage.
128      *
129      * @param  Request $request
130      * @param          $bookSlug
131      * @param          $pageSlug
132      * @return Response
133      */
134     public function update(Request $request, $bookSlug, $pageSlug)
135     {
136         $this->checkPermission('page-update');
137         $this->validate($request, [
138             'name'   => 'required|string|max:255'
139         ]);
140         $book = $this->bookRepo->getBySlug($bookSlug);
141         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
142         $this->pageRepo->updatePage($page, $book->id, $request->all());
143         Activity::add($page, 'page_update', $book->id);
144         return redirect($page->getUrl());
145     }
146
147     /**
148      * Redirect from a special link url which
149      * uses the page id rather than the name.
150      * @param $pageId
151      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
152      */
153     public function redirectFromLink($pageId)
154     {
155         $page = $this->pageRepo->getById($pageId);
156         return redirect($page->getUrl());
157     }
158
159     /**
160      * Show the deletion page for the specified page.
161      * @param $bookSlug
162      * @param $pageSlug
163      * @return \Illuminate\View\View
164      */
165     public function showDelete($bookSlug, $pageSlug)
166     {
167         $this->checkPermission('page-delete');
168         $book = $this->bookRepo->getBySlug($bookSlug);
169         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
170         $this->setPageTitle('Delete Page ' . $page->getShortName());
171         return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
172     }
173
174     /**
175      * Remove the specified page from storage.
176      *
177      * @param $bookSlug
178      * @param $pageSlug
179      * @return Response
180      * @internal param int $id
181      */
182     public function destroy($bookSlug, $pageSlug)
183     {
184         $this->checkPermission('page-delete');
185         $book = $this->bookRepo->getBySlug($bookSlug);
186         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
187         Activity::addMessage('page_delete', $book->id, $page->name);
188         $this->pageRepo->destroy($page);
189         return redirect($book->getUrl());
190     }
191
192     /**
193      * Shows the last revisions for this page.
194      * @param $bookSlug
195      * @param $pageSlug
196      * @return \Illuminate\View\View
197      */
198     public function showRevisions($bookSlug, $pageSlug)
199     {
200         $book = $this->bookRepo->getBySlug($bookSlug);
201         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
202         $this->setPageTitle('Revisions For ' . $page->getShortName());
203         return view('pages/revisions', ['page' => $page, 'book' => $book, 'current' => $page]);
204     }
205
206     /**
207      * Shows a preview of a single revision
208      * @param $bookSlug
209      * @param $pageSlug
210      * @param $revisionId
211      * @return \Illuminate\View\View
212      */
213     public function showRevision($bookSlug, $pageSlug, $revisionId)
214     {
215         $book = $this->bookRepo->getBySlug($bookSlug);
216         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
217         $revision = $this->pageRepo->getRevisionById($revisionId);
218         $page->fill($revision->toArray());
219         $this->setPageTitle('Page Revision For ' . $page->getShortName());
220         return view('pages/revision', ['page' => $page, 'book' => $book]);
221     }
222
223     /**
224      * Restores a page using the content of the specified revision.
225      * @param $bookSlug
226      * @param $pageSlug
227      * @param $revisionId
228      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
229      */
230     public function restoreRevision($bookSlug, $pageSlug, $revisionId)
231     {
232         $this->checkPermission('page-update');
233         $book = $this->bookRepo->getBySlug($bookSlug);
234         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
235         $page = $this->pageRepo->restoreRevision($page, $book, $revisionId);
236         Activity::add($page, 'page_restore', $book->id);
237         return redirect($page->getUrl());
238     }
239
240     /**
241      * Exports a page to pdf format using barryvdh/laravel-dompdf wrapper.
242      * https://github.com/barryvdh/laravel-dompdf
243      * @param $bookSlug
244      * @param $pageSlug
245      * @return \Illuminate\Http\Response
246      */
247     public function exportPdf($bookSlug, $pageSlug)
248     {
249         $book = $this->bookRepo->getBySlug($bookSlug);
250         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
251         $pdfContent = $this->exportService->pageToPdf($page);
252         return response()->make($pdfContent, 200, [
253             'Content-Type' => 'application/octet-stream',
254             'Content-Disposition' => 'attachment; filename="'.$pageSlug.'.pdf'
255         ]);
256     }
257
258     /**
259      * Export a page to a self-contained HTML file.
260      * @param $bookSlug
261      * @param $pageSlug
262      * @return \Illuminate\Http\Response
263      */
264     public function exportHtml($bookSlug, $pageSlug)
265     {
266         $book = $this->bookRepo->getBySlug($bookSlug);
267         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
268         $containedHtml = $this->exportService->pageToContainedHtml($page);
269         return response()->make($containedHtml, 200, [
270             'Content-Type' => 'application/octet-stream',
271             'Content-Disposition' => 'attachment; filename="'.$pageSlug.'.html'
272         ]);
273     }
274
275     /**
276      * Export a page to a simple plaintext .txt file.
277      * @param $bookSlug
278      * @param $pageSlug
279      * @return \Illuminate\Http\Response
280      */
281     public function exportPlainText($bookSlug, $pageSlug)
282     {
283         $book = $this->bookRepo->getBySlug($bookSlug);
284         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
285         $containedHtml = $this->exportService->pageToPlainText($page);
286         return response()->make($containedHtml, 200, [
287             'Content-Type' => 'application/octet-stream',
288             'Content-Disposition' => 'attachment; filename="'.$pageSlug.'.txt'
289         ]);
290     }
291
292     /**
293      * Show a listing of recently created pages
294      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
295      */
296     public function showRecentlyCreated()
297     {
298         $pages = $this->pageRepo->getRecentlyCreatedPaginated(20);
299         return view('pages/detailed-listing', [
300             'title' => 'Recently Created Pages',
301             'pages' => $pages
302         ]);
303     }
304
305     /**
306      * Show a listing of recently created pages
307      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
308      */
309     public function showRecentlyUpdated()
310     {
311         $pages = $this->pageRepo->getRecentlyUpdatedPaginated(20);
312         return view('pages/detailed-listing', [
313             'title' => 'Recently Updated Pages',
314             'pages' => $pages
315         ]);
316     }
317
318 }