$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
}
/**
- * Store a newly created resource in storage.
+ * Store a newly created page in storage.
*
* @param Request $request
* @param $bookSlug
$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
{
$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
}
/**
- * Update the specified resource in storage.
+ * Update the specified page in storage.
*
* @param Request $request
* @param $bookSlug
{
$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());
}
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);
}
/**
- * Remove the specified resource from storage.
+ * Remove the specified page from storage.
*
* @param $bookSlug
* @param $pageSlug
$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());
+ }
}
Route::group(['prefix' => 'books'], function() {
+ // Books
Route::get('/', 'BookController@index');
Route::get('/create', 'BookController@create');
Route::post('/', 'BookController@store');
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');
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');
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;
--- /dev/null
+<?php
+
+namespace Oxbow;
+
+use Illuminate\Database\Eloquent\Model;
+
+class PageRevision extends Model
+{
+ protected $fillable = ['name', 'html', 'text'];
+
+ public function createdBy()
+ {
+ return $this->belongsTo('Oxbow\User', 'created_by');
+ }
+
+ public function page()
+ {
+ return $this->belongsTo('Oxbow\Page');
+ }
+
+ public function getUrl()
+ {
+ return $this->page->getUrl() . '/revisions/' . $this->id;
+ }
+
+}
<?php namespace Oxbow\Repos;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Oxbow\Page;
+use Oxbow\PageRevision;
class PageRepo
{
protected $page;
+ protected $pageRevision;
/**
* PageRepo constructor.
- * @param $page
+ * @param Page $page
+ * @param PageRevision $pageRevision
*/
- public function __construct(Page $page)
+ public function __construct(Page $page, PageRevision $pageRevision)
{
$this->page = $page;
+ $this->pageRevision = $pageRevision;
}
public function idExists($id)
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
--- /dev/null
+<?php
+
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreatePageRevisionsTable extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::create('page_revisions', function (Blueprint $table) {
+ $table->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');
+ }
+}
--- /dev/null
+<h1>{{$page->name}}</h1>
+@if(count($page->children) > 0)
+ <h4 class="text-muted">Sub-pages</h4>
+ <div class="page-list">
+ @foreach($page->children as $childPage)
+ <a href="{{ $childPage->getUrl() }}">{{ $childPage->name }}</a>
+ @endforeach
+ </div>
+@endif
+{!! $page->html !!}
\ No newline at end of file
--- /dev/null
+@extends('base')
+
+@section('content')
+
+ <div class="page-content">
+ @include('pages/page-display')
+ </div>
+
+@stop
--- /dev/null
+@extends('base')
+
+@section('content')
+
+ <div class="row faded-small">
+ <div class="col-md-6 faded">
+ <div class="breadcrumbs padded-horizontal">
+ <a href="{{$page->getUrl()}}" class="text-primary"><i class="zmdi zmdi-arrow-left"></i>Back to page</a>
+ </div>
+ </div>
+ <div class="col-md-6 faded">
+ </div>
+ </div>
+
+ <div class="page-content">
+ <h1>Page Revisions <span class="subheader">For "{{ $page->name }}"</span></h1>
+
+ @if(count($page->revisions) > 0)
+
+ <table class="table">
+ <tr>
+ <th>Name</th>
+ <th>Created By</th>
+ <th>Revision Date</th>
+ <th>Actions</th>
+ </tr>
+ @foreach($page->revisions as $revision)
+ <tr>
+ <td>{{$revision->name}}</td>
+ <td>{{$revision->createdBy->name}}</td>
+ <td><small>{{$revision->created_at->format('jS F, Y H:i:s')}} ({{$revision->created_at->diffForHumans()}})</small></td>
+ <td>
+ <a href="{{$revision->getUrl()}}" target="_blank">Preview</a>
+ <span class="text-muted"> | </span>
+ <a href="{{$revision->getUrl()}}/restore">Restore</a>
+ </td>
+ </tr>
+ @endforeach
+ </table>
+
+ @else
+ <p>This page has no revisions.</p>
+ @endif
+
+ </div>
+
+@stop
</div>
<div class="col-md-6 faded">
<div class="action-buttons">
+ <a href="{{$page->getUrl() . '/revisions'}}" class="text-primary"><i class="zmdi zmdi-replay"></i>Revisions</a>
<a href="{{$page->getUrl() . '/edit'}}" class="text-primary" ><i class="zmdi zmdi-edit"></i>Edit</a>
<a href="{{$page->getUrl() . '/delete'}}" class="text-neg"><i class="zmdi zmdi-delete"></i>Delete</a>
</div>
<div class="page-content">
- <h1>{{$page->name}}</h1>
- @if(count($page->children) > 0)
- <h4 class="text-muted">Sub-pages</h4>
- <div class="page-list">
- @foreach($page->children as $childPage)
- <a href="{{ $childPage->getUrl() }}">{{ $childPage->name }}</a>
- @endforeach
- </div>
- @endif
- {!! $page->html !!}
-
+ @include('pages/page-display')
<hr>
<p class="text-muted small">
Created {{$page->created_at->diffForHumans()}} @if($page->createdBy) by {{$page->createdBy->name}} @endif