Prevent revision encoding issues

This commit is contained in:
Dan Brown 2015-10-18 19:40:33 +01:00
parent 8ea75b0fdf
commit 3fd2613ab7
3 changed files with 45 additions and 24 deletions

View File

@ -196,13 +196,19 @@ class PageController extends Controller
return view('pages/revision', ['page' => $page, 'book' => $book]);
}
/**
* Restores a page using the content of the specified revision.
* @param $bookSlug
* @param $pageSlug
* @param $revisionId
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function restoreRevision($bookSlug, $pageSlug, $revisionId)
{
$this->checkPermission('page-update');
$book = $this->bookRepo->getBySlug($bookSlug);
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
$revision = $this->pageRepo->getRevisionById($revisionId);
$page = $this->pageRepo->updatePage($page, $book->id, $revision->toArray());
$page = $this->pageRepo->restoreRevision($page, $book, $revisionId);
Activity::add($page, 'page_restore', $book->id);
return redirect($page->getUrl());
}

View File

@ -82,7 +82,6 @@ class PageRepo
$page->updated_by = auth()->user()->id;
$book->pages()->save($page);
$this->saveRevision($page);
return $page;
}
@ -202,13 +201,37 @@ class PageRepo
*/
public function updatePage(Page $page, $book_id, $input)
{
// Save a revision before updating
if ($page->html !== $input['html'] || $page->name !== $input['name']) {
$this->saveRevision($page);
}
// Update with new details
$page->fill($input);
$page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id);
$page->html = $this->formatHtml($input['html']);
$page->text = strip_tags($page->html);
$page->updated_by = Auth::user()->id;
$page->updated_by = auth()->user()->id;
$page->save();
return $page;
}
/**
* Restores a revision's content back into a page.
* @param Page $page
* @param Book $book
* @param int $revisionId
* @return Page
*/
public function restoreRevision(Page $page, Book $book, $revisionId)
{
$this->saveRevision($page);
$revision = $this->getRevisionById($revisionId);
$page->fill($revision->toArray());
$page->slug = $this->findSuitableSlug($page->name, $book->id, $page->id);
$page->text = strip_tags($page->html);
$page->updated_by = auth()->user()->id;
$page->save();
return $page;
}
@ -217,15 +240,12 @@ class PageRepo
* @param Page $page
* @return $this
*/
public function saveRevision(Page $page)
private function saveRevision(Page $page)
{
$lastRevision = $this->getLastRevision($page);
if ($lastRevision && ($lastRevision->html === $page->html && $lastRevision->name === $page->name)) {
return $page;
}
$revision = $this->pageRevision->fill($page->toArray());
$revision->page_id = $page->id;
$revision->created_by = Auth::user()->id;
$revision->created_by = auth()->user()->id;
$revision->created_at = $page->updated_at;
$revision->save();
// Clear old revisions
if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
@ -235,17 +255,6 @@ class PageRepo
return $revision;
}
/**
* Gets the most recent revision for a page.
* @param Page $page
* @return mixed
*/
public function getLastRevision(Page $page)
{
return $this->pageRevision->where('page_id', '=', $page->id)
->orderBy('created_at', 'desc')->first();
}
/**
* Gets a single revision via it's id.
* @param $id
@ -266,12 +275,17 @@ class PageRepo
public function doesSlugExist($slug, $bookId, $currentId = false)
{
$query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
if ($currentId) {
$query = $query->where('id', '!=', $currentId);
}
if ($currentId) $query = $query->where('id', '!=', $currentId);
return $query->count() > 0;
}
/**
* Sets the book id for the specified page.
* Changes the book id of any relations to the page that store the book id.
* @param int $bookId
* @param Page $page
* @return Page
*/
public function setBookId($bookId, Page $page)
{
$page->book_id = $bookId;

View File

@ -6,6 +6,7 @@
<!-- Meta-->
<meta name="viewport" content="width=device-width">
<meta name="token" content="{{ csrf_token() }}">
<meta charset="utf-8">
<!-- Styles and Fonts -->
<link rel="stylesheet" href="/css/styles.css">