Dan Brown

Prevent revision encoding issues

...@@ -196,13 +196,19 @@ class PageController extends Controller ...@@ -196,13 +196,19 @@ class PageController extends Controller
196 return view('pages/revision', ['page' => $page, 'book' => $book]); 196 return view('pages/revision', ['page' => $page, 'book' => $book]);
197 } 197 }
198 198
199 + /**
200 + * Restores a page using the content of the specified revision.
201 + * @param $bookSlug
202 + * @param $pageSlug
203 + * @param $revisionId
204 + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
205 + */
199 public function restoreRevision($bookSlug, $pageSlug, $revisionId) 206 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
200 { 207 {
201 $this->checkPermission('page-update'); 208 $this->checkPermission('page-update');
202 $book = $this->bookRepo->getBySlug($bookSlug); 209 $book = $this->bookRepo->getBySlug($bookSlug);
203 $page = $this->pageRepo->getBySlug($pageSlug, $book->id); 210 $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
204 - $revision = $this->pageRepo->getRevisionById($revisionId); 211 + $page = $this->pageRepo->restoreRevision($page, $book, $revisionId);
205 - $page = $this->pageRepo->updatePage($page, $book->id, $revision->toArray());
206 Activity::add($page, 'page_restore', $book->id); 212 Activity::add($page, 'page_restore', $book->id);
207 return redirect($page->getUrl()); 213 return redirect($page->getUrl());
208 } 214 }
......
...@@ -82,7 +82,6 @@ class PageRepo ...@@ -82,7 +82,6 @@ class PageRepo
82 $page->updated_by = auth()->user()->id; 82 $page->updated_by = auth()->user()->id;
83 83
84 $book->pages()->save($page); 84 $book->pages()->save($page);
85 - $this->saveRevision($page);
86 return $page; 85 return $page;
87 } 86 }
88 87
...@@ -202,13 +201,37 @@ class PageRepo ...@@ -202,13 +201,37 @@ class PageRepo
202 */ 201 */
203 public function updatePage(Page $page, $book_id, $input) 202 public function updatePage(Page $page, $book_id, $input)
204 { 203 {
204 + // Save a revision before updating
205 + if ($page->html !== $input['html'] || $page->name !== $input['name']) {
206 + $this->saveRevision($page);
207 + }
208 +
209 + // Update with new details
205 $page->fill($input); 210 $page->fill($input);
206 $page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id); 211 $page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id);
207 $page->html = $this->formatHtml($input['html']); 212 $page->html = $this->formatHtml($input['html']);
208 $page->text = strip_tags($page->html); 213 $page->text = strip_tags($page->html);
209 - $page->updated_by = Auth::user()->id; 214 + $page->updated_by = auth()->user()->id;
210 $page->save(); 215 $page->save();
216 + return $page;
217 + }
218 +
219 + /**
220 + * Restores a revision's content back into a page.
221 + * @param Page $page
222 + * @param Book $book
223 + * @param int $revisionId
224 + * @return Page
225 + */
226 + public function restoreRevision(Page $page, Book $book, $revisionId)
227 + {
211 $this->saveRevision($page); 228 $this->saveRevision($page);
229 + $revision = $this->getRevisionById($revisionId);
230 + $page->fill($revision->toArray());
231 + $page->slug = $this->findSuitableSlug($page->name, $book->id, $page->id);
232 + $page->text = strip_tags($page->html);
233 + $page->updated_by = auth()->user()->id;
234 + $page->save();
212 return $page; 235 return $page;
213 } 236 }
214 237
...@@ -217,15 +240,12 @@ class PageRepo ...@@ -217,15 +240,12 @@ class PageRepo
217 * @param Page $page 240 * @param Page $page
218 * @return $this 241 * @return $this
219 */ 242 */
220 - public function saveRevision(Page $page) 243 + private function saveRevision(Page $page)
221 { 244 {
222 - $lastRevision = $this->getLastRevision($page);
223 - if ($lastRevision && ($lastRevision->html === $page->html && $lastRevision->name === $page->name)) {
224 - return $page;
225 - }
226 $revision = $this->pageRevision->fill($page->toArray()); 245 $revision = $this->pageRevision->fill($page->toArray());
227 $revision->page_id = $page->id; 246 $revision->page_id = $page->id;
228 - $revision->created_by = Auth::user()->id; 247 + $revision->created_by = auth()->user()->id;
248 + $revision->created_at = $page->updated_at;
229 $revision->save(); 249 $revision->save();
230 // Clear old revisions 250 // Clear old revisions
231 if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) { 251 if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
...@@ -236,17 +256,6 @@ class PageRepo ...@@ -236,17 +256,6 @@ class PageRepo
236 } 256 }
237 257
238 /** 258 /**
239 - * Gets the most recent revision for a page.
240 - * @param Page $page
241 - * @return mixed
242 - */
243 - public function getLastRevision(Page $page)
244 - {
245 - return $this->pageRevision->where('page_id', '=', $page->id)
246 - ->orderBy('created_at', 'desc')->first();
247 - }
248 -
249 - /**
250 * Gets a single revision via it's id. 259 * Gets a single revision via it's id.
251 * @param $id 260 * @param $id
252 * @return mixed 261 * @return mixed
...@@ -266,12 +275,17 @@ class PageRepo ...@@ -266,12 +275,17 @@ class PageRepo
266 public function doesSlugExist($slug, $bookId, $currentId = false) 275 public function doesSlugExist($slug, $bookId, $currentId = false)
267 { 276 {
268 $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId); 277 $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
269 - if ($currentId) { 278 + if ($currentId) $query = $query->where('id', '!=', $currentId);
270 - $query = $query->where('id', '!=', $currentId);
271 - }
272 return $query->count() > 0; 279 return $query->count() > 0;
273 } 280 }
274 281
282 + /**
283 + * Sets the book id for the specified page.
284 + * Changes the book id of any relations to the page that store the book id.
285 + * @param int $bookId
286 + * @param Page $page
287 + * @return Page
288 + */
275 public function setBookId($bookId, Page $page) 289 public function setBookId($bookId, Page $page)
276 { 290 {
277 $page->book_id = $bookId; 291 $page->book_id = $bookId;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
6 <!-- Meta--> 6 <!-- Meta-->
7 <meta name="viewport" content="width=device-width"> 7 <meta name="viewport" content="width=device-width">
8 <meta name="token" content="{{ csrf_token() }}"> 8 <meta name="token" content="{{ csrf_token() }}">
9 + <meta charset="utf-8">
9 10
10 <!-- Styles and Fonts --> 11 <!-- Styles and Fonts -->
11 <link rel="stylesheet" href="/css/styles.css"> 12 <link rel="stylesheet" href="/css/styles.css">
......