use BookStack\Exceptions\NotFoundException;
use BookStack\Repos\UserRepo;
use BookStack\Services\ExportService;
+use Carbon\Carbon;
use Illuminate\Http\Request;
use BookStack\Http\Requests;
use BookStack\Repos\BookRepo;
use BookStack\Repos\PageRepo;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Views;
+use GatherContent\Htmldiff\Htmldiff;
class PageController extends Controller
{
/**
* Show the form for creating a new page.
- * @param $bookSlug
- * @param bool $chapterSlug
+ * @param string $bookSlug
+ * @param string $chapterSlug
* @return Response
* @internal param bool $pageSlug
*/
- public function create($bookSlug, $chapterSlug = false)
+ public function create($bookSlug, $chapterSlug = null)
{
$book = $this->bookRepo->getBySlug($bookSlug);
$chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null;
$parent = $chapter ? $chapter : $book;
$this->checkOwnablePermission('page-create', $parent);
+
+ // Redirect to draft edit screen if signed in
+ if ($this->signedIn) {
+ $draft = $this->pageRepo->getDraftPage($book, $chapter);
+ return redirect($draft->getUrl());
+ }
+
+ // Otherwise show edit view
$this->setPageTitle('Create New Page');
+ return view('pages/guest-create', ['parent' => $parent]);
+ }
+
+ /**
+ * Create a new page as a guest user.
+ * @param Request $request
+ * @param string $bookSlug
+ * @param string|null $chapterSlug
+ * @return mixed
+ * @throws NotFoundException
+ */
+ public function createAsGuest(Request $request, $bookSlug, $chapterSlug = null)
+ {
+ $this->validate($request, [
+ 'name' => 'required|string|max:255'
+ ]);
+
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null;
+ $parent = $chapter ? $chapter : $book;
+ $this->checkOwnablePermission('page-create', $parent);
- $draft = $this->pageRepo->getDraftPage($book, $chapter);
- return redirect($draft->getUrl());
+ $page = $this->pageRepo->getDraftPage($book, $chapter);
+ $this->pageRepo->publishDraft($page, [
+ 'name' => $request->get('name'),
+ 'html' => ''
+ ]);
+ return redirect($page->getUrl('/edit'));
}
/**
* Show form to continue editing a draft page.
- * @param $bookSlug
- * @param $pageId
+ * @param string $bookSlug
+ * @param int $pageId
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function editDraft($bookSlug, $pageId)
{
$book = $this->bookRepo->getBySlug($bookSlug);
$draft = $this->pageRepo->getById($pageId, true);
- $this->checkOwnablePermission('page-create', $draft);
+ $this->checkOwnablePermission('page-create', $book);
$this->setPageTitle('Edit Page Draft');
- return view('pages/create', ['draft' => $draft, 'book' => $book]);
+ $draftsEnabled = $this->signedIn;
+ return view('pages/edit', [
+ 'page' => $draft,
+ 'book' => $book,
+ 'isDraft' => true,
+ 'draftsEnabled' => $draftsEnabled
+ ]);
}
/**
$draftPage = $this->pageRepo->getById($pageId, true);
- $chapterId = $draftPage->chapter_id;
+ $chapterId = intval($draftPage->chapter_id);
$parent = $chapterId !== 0 ? $this->chapterRepo->getById($chapterId) : $book;
$this->checkOwnablePermission('page-create', $parent);
* Display the specified page.
* If the page is not found via the slug the
* revisions are searched for a match.
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return Response
*/
public function show($bookSlug, $pageSlug)
return redirect($page->getUrl());
}
+ $this->checkOwnablePermission('page-view', $page);
+
$sidebarTree = $this->bookRepo->getChildren($book);
+ $pageNav = $this->pageRepo->getPageNav($page);
+
Views::add($page);
$this->setPageTitle($page->getShortName());
- return view('pages/show', ['page' => $page, 'book' => $book, 'current' => $page, 'sidebarTree' => $sidebarTree]);
+ return view('pages/show', ['page' => $page, 'book' => $book,
+ 'current' => $page, 'sidebarTree' => $sidebarTree, 'pageNav' => $pageNav]);
}
/**
* Get page from an ajax request.
- * @param $pageId
+ * @param int $pageId
* @return \Illuminate\Http\JsonResponse
*/
public function getPageAjax($pageId)
/**
* Show the form for editing the specified page.
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return Response
*/
public function edit($bookSlug, $pageSlug)
if (count($warnings) > 0) session()->flash('warning', implode("\n", $warnings));
- return view('pages/edit', ['page' => $page, 'book' => $book, 'current' => $page]);
+ $draftsEnabled = $this->signedIn;
+ return view('pages/edit', [
+ 'page' => $page,
+ 'book' => $book,
+ 'current' => $page,
+ 'draftsEnabled' => $draftsEnabled
+ ]);
}
/**
* Update the specified page in storage.
* @param Request $request
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return Response
*/
public function update(Request $request, $bookSlug, $pageSlug)
/**
* Save a draft update as a revision.
* @param Request $request
- * @param $pageId
+ * @param int $pageId
* @return \Illuminate\Http\JsonResponse
*/
public function saveDraft(Request $request, $pageId)
{
$page = $this->pageRepo->getById($pageId, true);
$this->checkOwnablePermission('page-update', $page);
+
+ if (!$this->signedIn) {
+ return response()->json([
+ 'status' => 'error',
+ 'message' => 'Guests cannot save drafts',
+ ], 500);
+ }
+
if ($page->draft) {
$draft = $this->pageRepo->updateDraftPage($page, $request->only(['name', 'html', 'markdown']));
} else {
$draft = $this->pageRepo->saveUpdateDraft($page, $request->only(['name', 'html', 'markdown']));
}
- $updateTime = $draft->updated_at->format('H:i');
- return response()->json(['status' => 'success', 'message' => 'Draft saved at ' . $updateTime]);
+
+ $updateTime = $draft->updated_at->timestamp;
+ $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset;
+ return response()->json([
+ 'status' => 'success',
+ 'message' => 'Draft saved at ',
+ 'timestamp' => $utcUpdateTimestamp
+ ]);
}
/**
* Redirect from a special link url which
* uses the page id rather than the name.
- * @param $pageId
+ * @param int $pageId
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function redirectFromLink($pageId)
/**
* Show the deletion page for the specified page.
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return \Illuminate\View\View
*/
public function showDelete($bookSlug, $pageSlug)
/**
* Show the deletion page for the specified page.
- * @param $bookSlug
- * @param $pageId
+ * @param string $bookSlug
+ * @param int $pageId
* @return \Illuminate\View\View
* @throws NotFoundException
*/
/**
* Remove the specified page from storage.
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return Response
* @internal param int $id
*/
/**
* Remove the specified draft page from storage.
- * @param $bookSlug
- * @param $pageId
+ * @param string $bookSlug
+ * @param int $pageId
* @return Response
* @throws NotFoundException
*/
/**
* Shows the last revisions for this page.
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return \Illuminate\View\View
*/
public function showRevisions($bookSlug, $pageSlug)
/**
* Shows a preview of a single revision
- * @param $bookSlug
- * @param $pageSlug
- * @param $revisionId
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @param int $revisionId
* @return \Illuminate\View\View
*/
public function showRevision($bookSlug, $pageSlug, $revisionId)
$book = $this->bookRepo->getBySlug($bookSlug);
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
$revision = $this->pageRepo->getRevisionById($revisionId);
+
$page->fill($revision->toArray());
$this->setPageTitle('Page Revision For ' . $page->getShortName());
- return view('pages/revision', ['page' => $page, 'book' => $book]);
+
+ return view('pages/revision', [
+ 'page' => $page,
+ 'book' => $book,
+ ]);
+ }
+
+ /**
+ * Shows the changes of a single revision
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @param int $revisionId
+ * @return \Illuminate\View\View
+ */
+ public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
+ {
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $revision = $this->pageRepo->getRevisionById($revisionId);
+
+ $prev = $revision->getPrevious();
+ $prevContent = ($prev === null) ? '' : $prev->html;
+ $diff = (new Htmldiff)->diff($prevContent, $revision->html);
+
+ $page->fill($revision->toArray());
+ $this->setPageTitle('Page Revision For ' . $page->getShortName());
+
+ return view('pages/revision', [
+ 'page' => $page,
+ 'book' => $book,
+ 'diff' => $diff,
+ ]);
}
/**
* Restores a page using the content of the specified revision.
- * @param $bookSlug
- * @param $pageSlug
- * @param $revisionId
+ * @param string $bookSlug
+ * @param string $pageSlug
+ * @param int $revisionId
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function restoreRevision($bookSlug, $pageSlug, $revisionId)
/**
* Exports a page to pdf format using barryvdh/laravel-dompdf wrapper.
* https://github.com/barryvdh/laravel-dompdf
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return \Illuminate\Http\Response
*/
public function exportPdf($bookSlug, $pageSlug)
/**
* Export a page to a self-contained HTML file.
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return \Illuminate\Http\Response
*/
public function exportHtml($bookSlug, $pageSlug)
/**
* Export a page to a simple plaintext .txt file.
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return \Illuminate\Http\Response
*/
public function exportPlainText($bookSlug, $pageSlug)
*/
public function showRecentlyCreated()
{
- $pages = $this->pageRepo->getRecentlyCreatedPaginated(20);
+ $pages = $this->pageRepo->getRecentlyCreatedPaginated(20)->setPath(baseUrl('/pages/recently-created'));
return view('pages/detailed-listing', [
'title' => 'Recently Created Pages',
'pages' => $pages
*/
public function showRecentlyUpdated()
{
- $pages = $this->pageRepo->getRecentlyUpdatedPaginated(20);
+ $pages = $this->pageRepo->getRecentlyUpdatedPaginated(20)->setPath(baseUrl('/pages/recently-updated'));
return view('pages/detailed-listing', [
'title' => 'Recently Updated Pages',
'pages' => $pages
/**
* Show the Restrictions view.
- * @param $bookSlug
- * @param $pageSlug
+ * @param string $bookSlug
+ * @param string $pageSlug
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showRestrict($bookSlug, $pageSlug)
}
/**
- * Set the restrictions for this page.
- * @param $bookSlug
- * @param $pageSlug
+ * 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)
+ {
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $this->checkOwnablePermission('page-update', $page);
+ return view('pages/move', [
+ 'book' => $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)
+ {
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
+ $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]);
+
+ $parent = false;
+
+ if ($entityType == 'chapter') {
+ $parent = $this->chapterRepo->getById($entityId);
+ } else if ($entityType == 'book') {
+ $parent = $this->bookRepo->getById($entityId);
+ }
+
+ if ($parent === false || $parent === null) {
+ session()->flash('The selected Book or Chapter was not found');
+ return redirect()->back();
+ }
+
+ $this->pageRepo->changePageParent($page, $parent);
+ Activity::add($page, 'page_move', $page->book->id);
+ session()->flash('success', sprintf('Page moved to "%s"', $parent->name));
+
+ return redirect($page->getUrl());
+ }
+
+ /**
+ * Set the permissions for this page.
+ * @param string $bookSlug
+ * @param string $pageSlug
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
$book = $this->bookRepo->getBySlug($bookSlug);
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
$this->checkOwnablePermission('restrictions-manage', $page);
- $this->pageRepo->updateRestrictionsFromRequest($request, $page);
- session()->flash('success', 'Page Restrictions Updated');
+ $this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
+ session()->flash('success', 'Page Permissions Updated');
return redirect($page->getUrl());
}