+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $this->checkOwnablePermission('page-update', $page);
+ $page = $this->pageRepo->restorePageRevision($page, $page->book, $revisionId);
+ Activity::add($page, 'page_restore', $page->book->id);
+ return redirect($page->getUrl());
+ }
+
+
+ /**
+ * Deletes a revision using the id of the specified revision.
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @param int $revId
+ * @throws NotFoundException
+ * @throws BadRequestException
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+ */
+ public function destroyRevision($bookSlug, $pageSlug, $revId)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $this->checkOwnablePermission('page-delete', $page);
+
+ $revision = $page->revisions()->where('id', '=', $revId)->first();
+ if ($revision === null) {
+ throw new NotFoundException("Revision #{$revId} not found");
+ }
+
+ // Get the current revision for the page
+ $currentRevision = $page->getCurrentRevision();
+
+ // Check if its the latest revision, cannot delete latest revision.
+ if (intval($currentRevision->id) === intval($revId)) {
+ session()->flash('error', trans('entities.revision_cannot_delete_latest'));
+ return response()->view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page], 400);
+ }
+
+ $revision->delete();
+ session()->flash('success', trans('entities.revision_delete_success'));
+ return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
+ }
+
+ /**
+ * Exports a page to a PDF.
+ * https://github.com/barryvdh/laravel-dompdf
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @return \Illuminate\Http\Response
+ */
+ public function exportPdf($bookSlug, $pageSlug)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $page->html = $this->pageRepo->renderPage($page);
+ $pdfContent = $this->exportService->pageToPdf($page);
+ return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
+ }
+
+ /**
+ * Export a page to a self-contained HTML file.
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @return \Illuminate\Http\Response
+ */
+ public function exportHtml($bookSlug, $pageSlug)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $page->html = $this->pageRepo->renderPage($page);
+ $containedHtml = $this->exportService->pageToContainedHtml($page);
+ return $this->downloadResponse($containedHtml, $pageSlug . '.html');
+ }
+
+ /**
+ * Export a page to a simple plaintext .txt file.
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @return \Illuminate\Http\Response
+ */
+ public function exportPlainText($bookSlug, $pageSlug)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $pageText = $this->exportService->pageToPlainText($page);
+ return $this->downloadResponse($pageText, $pageSlug . '.txt');
+ }
+
+ /**
+ * Show a listing of recently created pages
+ * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+ */
+ public function showRecentlyCreated()
+ {
+ $pages = $this->pageRepo->getRecentlyCreatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-created'));
+ return view('pages/detailed-listing', [
+ 'title' => trans('entities.recently_created_pages'),
+ 'pages' => $pages
+ ]);
+ }
+
+ /**
+ * Show a listing of recently created pages
+ * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+ */
+ public function showRecentlyUpdated()
+ {
+ $pages = $this->pageRepo->getRecentlyUpdatedPaginated('page', 20)->setPath(baseUrl('/pages/recently-updated'));
+ return view('pages/detailed-listing', [
+ 'title' => trans('entities.recently_updated_pages'),
+ 'pages' => $pages
+ ]);
+ }
+
+ /**
+ * Show the Restrictions view.
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+ */
+ public function showRestrict($bookSlug, $pageSlug)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $this->checkOwnablePermission('restrictions-manage', $page);
+ $roles = $this->userRepo->getRestrictableRoles();
+ return view('pages/restrictions', [
+ 'page' => $page,
+ 'roles' => $roles
+ ]);
+ }
+
+ /**
+ * Show the view to choose a new parent to move a page into.
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @return mixed
+ * @throws NotFoundException
+ */
+ public function showMove($bookSlug, $pageSlug)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $this->checkOwnablePermission('page-update', $page);
+ return view('pages/move', [
+ 'book' => $page->book,
+ 'page' => $page
+ ]);
+ }
+
+ /**
+ * Does the action of moving the location of a page
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @param Request $request
+ * @return mixed
+ * @throws NotFoundException
+ */
+ public function move($bookSlug, $pageSlug, Request $request)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $this->checkOwnablePermission('page-update', $page);
+
+ $entitySelection = $request->get('entity_selection', null);
+ if ($entitySelection === null || $entitySelection === '') {
+ return redirect($page->getUrl());
+ }
+
+ $stringExploded = explode(':', $entitySelection);
+ $entityType = $stringExploded[0];
+ $entityId = intval($stringExploded[1]);
+
+
+ try {
+ $parent = $this->pageRepo->getById($entityType, $entityId);
+ } catch (\Exception $e) {
+ session()->flash(trans('entities.selected_book_chapter_not_found'));
+ return redirect()->back();
+ }
+
+ $this->checkOwnablePermission('page-create', $parent);
+
+ $this->pageRepo->changePageParent($page, $parent);
+ Activity::add($page, 'page_move', $page->book->id);
+ session()->flash('success', trans('entities.pages_move_success', ['parentName' => $parent->name]));
+
+ return redirect($page->getUrl());
+ }
+
+ /**
+ * Show the view to copy a page.
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @return mixed
+ * @throws NotFoundException
+ */
+ public function showCopy($bookSlug, $pageSlug)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $this->checkOwnablePermission('page-update', $page);
+ session()->flashInput(['name' => $page->name]);
+ return view('pages/copy', [
+ 'book' => $page->book,
+ 'page' => $page
+ ]);
+ }
+
+ /**
+ * Create a copy of a page within the requested target destination.
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @param Request $request
+ * @return mixed
+ * @throws NotFoundException
+ */
+ public function copy($bookSlug, $pageSlug, Request $request)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $this->checkOwnablePermission('page-update', $page);
+
+ $entitySelection = $request->get('entity_selection', null);
+ if ($entitySelection === null || $entitySelection === '') {
+ $parent = $page->chapter ? $page->chapter : $page->book;
+ } else {
+ $stringExploded = explode(':', $entitySelection);
+ $entityType = $stringExploded[0];
+ $entityId = intval($stringExploded[1]);
+
+ try {
+ $parent = $this->pageRepo->getById($entityType, $entityId);
+ } catch (\Exception $e) {
+ session()->flash(trans('entities.selected_book_chapter_not_found'));
+ return redirect()->back();
+ }
+ }
+
+ $this->checkOwnablePermission('page-create', $parent);
+
+ $pageCopy = $this->pageRepo->copyPage($page, $parent, $request->get('name', ''));
+
+ Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
+ session()->flash('success', trans('entities.pages_copy_success'));
+
+ return redirect($pageCopy->getUrl());
+ }
+
+ /**
+ * Set the permissions for this page.
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @param Request $request
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+ * @throws NotFoundException
+ */
+ public function restrict($bookSlug, $pageSlug, Request $request)
+ {
+ $page = $this->pageRepo->getPageBySlug($pageSlug, $bookSlug);
+ $this->checkOwnablePermission('restrictions-manage', $page);
+ $this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
+ session()->flash('success', trans('entities.pages_permissions_success'));