diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index b66081928..b64290eaa 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -31,19 +31,8 @@ class PageController extends Controller $this->chapterRepo = $chapterRepo; } - /** - * Display a listing of the resource. - * - * @return Response - */ - public function index() - { - // - } - - /** - * Show the form for creating a new resource. + * Show the form for creating a new page. * * @param $bookSlug * @param bool $chapterSlug @@ -58,7 +47,7 @@ class PageController extends Controller } /** - * Store a newly created resource in storage. + * Store a newly created page in storage. * * @param Request $request * @param $bookSlug @@ -86,11 +75,12 @@ class PageController extends Controller $page->created_by = Auth::user()->id; $page->updated_by = Auth::user()->id; $page->save(); + $this->pageRepo->saveRevision($page); return redirect($page->getUrl()); } /** - * Display the specified resource. + * Display the specified page. * * @param $bookSlug * @param $pageSlug @@ -100,12 +90,11 @@ class PageController extends Controller { $book = $this->bookRepo->getBySlug($bookSlug); $page = $this->pageRepo->getBySlug($pageSlug, $book->id); - //dd($sidebarBookTree); return view('pages/show', ['page' => $page, 'book' => $book]); } /** - * Show the form for editing the specified resource. + * Show the form for editing the specified page. * * @param $bookSlug * @param $pageSlug @@ -119,7 +108,7 @@ class PageController extends Controller } /** - * Update the specified resource in storage. + * Update the specified page in storage. * * @param Request $request * @param $bookSlug @@ -130,11 +119,7 @@ class PageController extends Controller { $book = $this->bookRepo->getBySlug($bookSlug); $page = $this->pageRepo->getBySlug($pageSlug, $book->id); - $page->fill($request->all()); - $page->slug = $this->pageRepo->findSuitableSlug($page->name, $book->id, $page->id); - $page->text = strip_tags($page->html); - $page->updated_by = Auth::user()->id; - $page->save(); + $this->pageRepo->updatePage($page, $book->id, $request->all()); return redirect($page->getUrl()); } @@ -205,6 +190,12 @@ class PageController extends Controller return redirect($book->getUrl()); } + /** + * Show the deletion page for the specified page. + * @param $bookSlug + * @param $pageSlug + * @return \Illuminate\View\View + */ public function showDelete($bookSlug, $pageSlug) { $book = $this->bookRepo->getBySlug($bookSlug); @@ -213,7 +204,7 @@ class PageController extends Controller } /** - * Remove the specified resource from storage. + * Remove the specified page from storage. * * @param $bookSlug * @param $pageSlug @@ -227,4 +218,42 @@ class PageController extends Controller $page->delete(); return redirect($book->getUrl()); } + + /** + * Shows the last revisions for this page. + * @param $bookSlug + * @param $pageSlug + * @return \Illuminate\View\View + */ + public function showRevisions($bookSlug, $pageSlug) + { + $book = $this->bookRepo->getBySlug($bookSlug); + $page = $this->pageRepo->getBySlug($pageSlug, $book->id); + return view('pages/revisions', ['page' => $page, 'book' => $book]); + } + + /** + * Shows a preview of a single revision + * @param $bookSlug + * @param $pageSlug + * @param $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()); + return view('pages/revision', ['page' => $page, 'book' => $book]); + } + + public function restoreRevision($bookSlug, $pageSlug, $revisionId) + { + $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()); + return redirect($page->getUrl()); + } } diff --git a/app/Http/routes.php b/app/Http/routes.php index d1ea3e854..3065f7eb5 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -18,6 +18,7 @@ Route::group(['middleware' => 'auth'], function() { Route::group(['prefix' => 'books'], function() { + // Books Route::get('/', 'BookController@index'); Route::get('/create', 'BookController@create'); Route::post('/', 'BookController@store'); @@ -27,6 +28,7 @@ Route::group(['middleware' => 'auth'], function() { Route::get('/{slug}', 'BookController@show'); Route::get('/{slug}/delete', 'BookController@showDelete'); + // Pages Route::get('/{bookSlug}/page/create', 'PageController@create'); Route::post('/{bookSlug}/page', 'PageController@store'); Route::get('/{bookSlug}/sort', 'PageController@sortPages'); @@ -36,7 +38,12 @@ Route::group(['middleware' => 'auth'], function() { Route::get('/{bookSlug}/page/{pageSlug}/delete', 'PageController@showDelete'); Route::put('/{bookSlug}/page/{pageSlug}', 'PageController@update'); Route::delete('/{bookSlug}/page/{pageSlug}', 'PageController@destroy'); + //Revisions + Route::get('/{bookSlug}/page/{pageSlug}/revisions', 'PageController@showRevisions'); + Route::get('/{bookSlug}/page/{pageSlug}/revisions/{revId}', 'PageController@showRevision'); + Route::get('/{bookSlug}/page/{pageSlug}/revisions/{revId}/restore', 'PageController@restoreRevision'); + // Chapters Route::get('/{bookSlug}/chapter/{chapterSlug}/create-page', 'PageController@create'); Route::get('/{bookSlug}/chapter/create', 'ChapterController@create'); Route::post('/{bookSlug}/chapter/create', 'ChapterController@store'); diff --git a/app/Page.php b/app/Page.php index fd90b6d48..8ae744a8a 100644 --- a/app/Page.php +++ b/app/Page.php @@ -42,6 +42,11 @@ class Page extends Model return $this->belongsTo('Oxbow\User', 'updated_by'); } + public function revisions() + { + return $this->hasMany('Oxbow\PageRevision')->orderBy('created_at', 'desc'); + } + public function getUrl() { return '/books/' . $this->book->slug . '/page/' . $this->slug; diff --git a/app/PageRevision.php b/app/PageRevision.php new file mode 100644 index 000000000..baabe0f12 --- /dev/null +++ b/app/PageRevision.php @@ -0,0 +1,26 @@ +belongsTo('Oxbow\User', 'created_by'); + } + + public function page() + { + return $this->belongsTo('Oxbow\Page'); + } + + public function getUrl() + { + return $this->page->getUrl() . '/revisions/' . $this->id; + } + +} diff --git a/app/Repos/PageRepo.php b/app/Repos/PageRepo.php index 99be07d7a..52620961f 100644 --- a/app/Repos/PageRepo.php +++ b/app/Repos/PageRepo.php @@ -1,20 +1,25 @@ page = $page; + $this->pageRevision = $pageRevision; } public function idExists($id) @@ -64,6 +69,68 @@ class PageRepo return $query->get(); } + /** + * Updates a page with any fillable data and saves it into the database. + * @param Page $page + * @param $book_id + * @param $data + * @return Page + */ + public function updatePage(Page $page, $book_id, $data) + { + $page->fill($data); + $page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id); + $page->text = strip_tags($page->html); + $page->updated_by = Auth::user()->id; + $page->save(); + $this->saveRevision($page); + return $page; + } + + /** + * Saves a page revision into the system. + * @param Page $page + * @return $this + */ + public 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->save(); + // Clear old revisions + if($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) { + $this->pageRevision->where('page_id', '=', $page->id) + ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete(); + } + 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 + * @return mixed + */ + public function getRevisionById($id) + { + return $this->pageRevision->findOrFail($id); + } + /** * Checks if a slug exists within a book already. * @param $slug diff --git a/database/migrations/2015_08_09_093534_create_page_revisions_table.php b/database/migrations/2015_08_09_093534_create_page_revisions_table.php new file mode 100644 index 000000000..b55097f0b --- /dev/null +++ b/database/migrations/2015_08_09_093534_create_page_revisions_table.php @@ -0,0 +1,35 @@ +increments('id'); + $table->integer('page_id')->indexed(); + $table->string('name'); + $table->longText('html'); + $table->longText('text'); + $table->integer('created_by'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('page_revisions'); + } +} diff --git a/resources/views/pages/page-display.blade.php b/resources/views/pages/page-display.blade.php new file mode 100644 index 000000000..6eb623b39 --- /dev/null +++ b/resources/views/pages/page-display.blade.php @@ -0,0 +1,10 @@ +
Name | +Created By | +Revision Date | +Actions | +
---|---|---|---|
{{$revision->name}} | +{{$revision->createdBy->name}} | +{{$revision->created_at->format('jS F, Y H:i:s')}} ({{$revision->created_at->diffForHumans()}}) | ++ Preview + | + Restore + | +
This page has no revisions.
+ @endif + +Created {{$page->created_at->diffForHumans()}} @if($page->createdBy) by {{$page->createdBy->name}} @endif