3 namespace BookStack\Http\Controllers;
6 use BookStack\Services\ExportService;
7 use Illuminate\Http\Request;
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;
17 class PageController extends Controller
22 protected $chapterRepo;
23 protected $exportService;
26 * PageController constructor.
27 * @param PageRepo $pageRepo
28 * @param BookRepo $bookRepo
29 * @param ChapterRepo $chapterRepo
30 * @param ExportService $exportService
32 public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService)
34 $this->pageRepo = $pageRepo;
35 $this->bookRepo = $bookRepo;
36 $this->chapterRepo = $chapterRepo;
37 $this->exportService = $exportService;
38 parent::__construct();
42 * Show the form for creating a new page.
45 * @param bool $chapterSlug
47 * @internal param bool $pageSlug
49 public function create($bookSlug, $chapterSlug = false)
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]);
59 * Store a newly created page in storage.
61 * @param Request $request
65 public function store(Request $request, $bookSlug)
67 $this->checkPermission('page-create');
68 $this->validate($request, [
69 'name' => 'required|string|max:255'
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);
77 $page = $this->pageRepo->saveNew($input, $book, $chapterId);
79 Activity::add($page, 'page_create', $book->id);
80 return redirect($page->getUrl());
84 * Display the specified page.
85 * If the page is not found via the slug the
86 * revisions are searched for a match.
92 public function show($bookSlug, $pageSlug)
94 $book = $this->bookRepo->getBySlug($bookSlug);
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());
104 $sidebarTree = $this->bookRepo->getChildren($book);
106 $this->setPageTitle($page->getShortName());
107 return view('pages/show', ['page' => $page, 'book' => $book, 'current' => $page, 'sidebarTree' => $sidebarTree]);
111 * Show the form for editing the specified page.
117 public function edit($bookSlug, $pageSlug)
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]);
127 * Update the specified page in storage.
129 * @param Request $request
134 public function update(Request $request, $bookSlug, $pageSlug)
136 $this->checkPermission('page-update');
137 $this->validate($request, [
138 'name' => 'required|string|max:255'
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());
148 * Redirect from a special link url which
149 * uses the page id rather than the name.
151 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
153 public function redirectFromLink($pageId)
155 $page = $this->pageRepo->getById($pageId);
156 return redirect($page->getUrl());
160 * Show the deletion page for the specified page.
163 * @return \Illuminate\View\View
165 public function showDelete($bookSlug, $pageSlug)
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]);
175 * Remove the specified page from storage.
180 * @internal param int $id
182 public function destroy($bookSlug, $pageSlug)
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());
193 * Shows the last revisions for this page.
196 * @return \Illuminate\View\View
198 public function showRevisions($bookSlug, $pageSlug)
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]);
207 * Shows a preview of a single revision
211 * @return \Illuminate\View\View
213 public function showRevision($bookSlug, $pageSlug, $revisionId)
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]);
224 * Restores a page using the content of the specified revision.
228 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
230 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
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());
241 * Exports a page to pdf format using barryvdh/laravel-dompdf wrapper.
242 * https://github.com/barryvdh/laravel-dompdf
245 * @return \Illuminate\Http\Response
247 public function exportPdf($bookSlug, $pageSlug)
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'
259 * Export a page to a self-contained HTML file.
262 * @return \Illuminate\Http\Response
264 public function exportHtml($bookSlug, $pageSlug)
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'
276 * Export a page to a simple plaintext .txt file.
279 * @return \Illuminate\Http\Response
281 public function exportPlainText($bookSlug, $pageSlug)
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'
293 * Show a listing of recently created pages
294 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
296 public function showRecentlyCreated()
298 $pages = $this->pageRepo->getRecentlyCreatedPaginated(20);
299 return view('pages/detailed-listing', [
300 'title' => 'Recently Created Pages',
306 * Show a listing of recently created pages
307 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
309 public function showRecentlyUpdated()
311 $pages = $this->pageRepo->getRecentlyUpdatedPaginated(20);
312 return view('pages/detailed-listing', [
313 'title' => 'Recently Updated Pages',