use BookStack\Repos\EntityRepo;
use BookStack\Repos\UserRepo;
use BookStack\Services\ExportService;
-use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Views;
* @param string $chapterSlug
* @return Response
* @internal param bool $pageSlug
+ * @throws NotFoundException
*/
public function create($bookSlug, $chapterSlug = null)
{
- $book = $this->entityRepo->getBySlug('book', $bookSlug);
- $chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
+ if ($chapterSlug !== null) {
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
+ $book = $chapter->book;
+ } else {
+ $chapter = null;
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
+ }
+
$parent = $chapter ? $chapter : $book;
$this->checkOwnablePermission('page-create', $parent);
return redirect($draft->getUrl());
}
- // Otherwise show edit view
+ // Otherwise show the edit view if they're a guest
$this->setPageTitle(trans('entities.pages_new'));
return view('pages/guest-create', ['parent' => $parent]);
}
'name' => 'required|string|max:255'
]);
- $book = $this->entityRepo->getBySlug('book', $bookSlug);
- $chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
+ if ($chapterSlug !== null) {
+ $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
+ $book = $chapter->book;
+ } else {
+ $chapter = null;
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
+ }
+
$parent = $chapter ? $chapter : $book;
$this->checkOwnablePermission('page-create', $parent);
public function editDraft($bookSlug, $pageId)
{
$draft = $this->entityRepo->getById('page', $pageId, true);
- $this->checkOwnablePermission('page-create', $draft->book);
+ $this->checkOwnablePermission('page-create', $draft->parent);
$this->setPageTitle(trans('entities.pages_edit_draft'));
$draftsEnabled = $this->signedIn;
]);
$input = $request->all();
- $book = $this->entityRepo->getBySlug('book', $bookSlug);
-
$draftPage = $this->entityRepo->getById('page', $pageId, true);
+ $book = $draftPage->book;
- $chapterId = intval($draftPage->chapter_id);
- $parent = $chapterId !== 0 ? $this->entityRepo->getById('chapter', $chapterId) : $book;
+ $parent = $draftPage->parent;
$this->checkOwnablePermission('page-create', $parent);
if ($parent->isA('chapter')) {
* @param string $bookSlug
* @param string $pageSlug
* @return Response
+ * @throws NotFoundException
*/
public function show($bookSlug, $pageSlug)
{
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
} catch (NotFoundException $e) {
$page = $this->entityRepo->getPageByOldSlug($pageSlug, $bookSlug);
- if ($page === null) abort(404);
+ if ($page === null) {
+ throw $e;
+ }
return redirect($page->getUrl());
}
-
$this->checkOwnablePermission('page-view', $page);
- $pageContent = $this->entityRepo->renderPage($page);
+ $page->html = $this->entityRepo->renderPage($page);
$sidebarTree = $this->entityRepo->getBookChildren($page->book);
- $pageNav = $this->entityRepo->getPageNav($page);
-
+ $pageNav = $this->entityRepo->getPageNav($page->html);
+
+ // check if the comment's are enabled
+ $commentsEnabled = !setting('app-disable-comments');
+ if ($commentsEnabled) {
+ $page->load(['comments.createdBy']);
+ }
+
Views::add($page);
$this->setPageTitle($page->getShortName());
return view('pages/show', [
'page' => $page,'book' => $page->book,
- 'current' => $page, 'sidebarTree' => $sidebarTree,
- 'pageNav' => $pageNav, 'pageContent' => $pageContent]);
+ 'current' => $page,
+ 'sidebarTree' => $sidebarTree,
+ 'commentsEnabled' => $commentsEnabled,
+ 'pageNav' => $pageNav
+ ]);
}
/**
$warnings [] = $this->entityRepo->getUserPageDraftMessage($draft);
}
- if (count($warnings) > 0) session()->flash('warning', implode("\n", $warnings));
+ if (count($warnings) > 0) {
+ session()->flash('warning', implode("\n", $warnings));
+ }
$draftsEnabled = $this->signedIn;
return view('pages/edit', [
$draft = $this->entityRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown']));
$updateTime = $draft->updated_at->timestamp;
- $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset;
return response()->json([
'status' => 'success',
'message' => trans('entities.pages_edit_draft_save_at'),
- 'timestamp' => $utcUpdateTimestamp
+ 'timestamp' => $updateTime
]);
}
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$book = $page->book;
$this->checkOwnablePermission('page-delete', $page);
+ $this->entityRepo->destroyPage($page);
+
Activity::addMessage('page_delete', $book->id, $page->name);
session()->flash('success', trans('entities.pages_delete_success'));
- $this->entityRepo->destroyPage($page);
return redirect($book->getUrl());
}
public function showRevision($bookSlug, $pageSlug, $revisionId)
{
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
- $revision = $this->entityRepo->getById('page_revision', $revisionId, false);
+ $revision = $page->revisions()->where('id', '=', $revisionId)->first();
+ if ($revision === null) {
+ abort(404);
+ }
$page->fill($revision->toArray());
- $this->setPageTitle(trans('entities.pages_revision_named', ['pageName'=>$page->getShortName()]));
-
+ $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
+
return view('pages/revision', [
'page' => $page,
'book' => $page->book,
+ 'revision' => $revision
]);
}
public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
{
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
- $revision = $this->entityRepo->getById('page_revision', $revisionId);
+ $revision = $page->revisions()->where('id', '=', $revisionId)->first();
+ if ($revision === null) {
+ abort(404);
+ }
$prev = $revision->getPrevious();
$prevContent = ($prev === null) ? '' : $prev->html;
'page' => $page,
'book' => $page->book,
'diff' => $diff,
+ 'revision' => $revision
]);
}
return redirect($page->getUrl());
}
+
/**
- * Exports a page to pdf format using barryvdh/laravel-dompdf wrapper.
+ * 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->entityRepo->getBySlug('page', $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
public function exportPdf($bookSlug, $pageSlug)
{
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
+ $page->html = $this->entityRepo->renderPage($page);
$pdfContent = $this->exportService->pageToPdf($page);
- return response()->make($pdfContent, 200, [
- 'Content-Type' => 'application/octet-stream',
- 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
- ]);
+ return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
}
/**
public function exportHtml($bookSlug, $pageSlug)
{
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
+ $page->html = $this->entityRepo->renderPage($page);
$containedHtml = $this->exportService->pageToContainedHtml($page);
- return response()->make($containedHtml, 200, [
- 'Content-Type' => 'application/octet-stream',
- 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.html'
- ]);
+ return $this->downloadResponse($containedHtml, $pageSlug . '.html');
}
/**
public function exportPlainText($bookSlug, $pageSlug)
{
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
- $containedHtml = $this->exportService->pageToPlainText($page);
- return response()->make($containedHtml, 200, [
- 'Content-Type' => 'application/octet-stream',
- 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.txt'
- ]);
+ $pageText = $this->exportService->pageToPlainText($page);
+ return $this->downloadResponse($pageText, $pageSlug . '.txt');
}
/**
return redirect()->back();
}
+ $this->checkOwnablePermission('page-create', $parent);
+
$this->entityRepo->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->entityRepo->getBySlug('page', $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->entityRepo->getBySlug('page', $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->entityRepo->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->entityRepo->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)
{
session()->flash('success', trans('entities.pages_permissions_success'));
return redirect($page->getUrl());
}
-
}