add revision reverting option
This commit is contained in:
@@ -31,6 +31,25 @@ class TableController extends Controller
|
||||
return $tableModel;
|
||||
}
|
||||
|
||||
public function revertTo(Request $request, User $user, string $table)
|
||||
{
|
||||
$input = $this->validate($request, [
|
||||
'rev' => 'int'
|
||||
]);
|
||||
|
||||
$tableModel = $this->resolveTable($request, $user, $table);
|
||||
$this->authorize('edit', $tableModel);
|
||||
|
||||
$revisionNum = (int)$input->rev;
|
||||
$revision = $tableModel->revisions()->orderBy('created_at')->skip($revisionNum - 1)->first();
|
||||
if ($revision === null) abort(404, "No such revision");
|
||||
|
||||
$tableModel->revision()->associate($revision);
|
||||
$tableModel->save();
|
||||
|
||||
return redirect($tableModel->revisionsRoute);
|
||||
}
|
||||
|
||||
public function view(Request $request, User $user, string $table)
|
||||
{
|
||||
$input = $this->validate($request, [
|
||||
@@ -72,7 +91,6 @@ class TableController extends Controller
|
||||
|
||||
public function delete(Request $request, User $user, string $table)
|
||||
{
|
||||
/** @var Table $tableModel */
|
||||
$tableModel = $this->resolveTable($request, $user, $table);
|
||||
$this->authorize('delete', $tableModel);
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\Reportable;
|
||||
use function GuzzleHttp\Psr7\build_query;
|
||||
use http\QueryString;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
@@ -153,6 +155,28 @@ class Table extends BaseModel
|
||||
return parent::__get($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get route to some action (so we dont have to add xxRoute for everything)
|
||||
*
|
||||
* @param $action
|
||||
* @param array $getargs
|
||||
* @return string
|
||||
*/
|
||||
public function actionRoute($action, $getargs = [])
|
||||
{
|
||||
$user = $this->cachedOwner()->name;
|
||||
$table = $this->name;
|
||||
|
||||
$base = "/@$user/$table";
|
||||
if ($action != 'view') $base .= "/$action";
|
||||
|
||||
if ($getargs) {
|
||||
return $base . '?' . build_query($getargs);
|
||||
} else {
|
||||
return $base;
|
||||
}
|
||||
}
|
||||
|
||||
public function getDraftRoute($tab=null)
|
||||
{
|
||||
return route('table.draft', [
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
{{--
|
||||
args: $table
|
||||
--}}
|
||||
@php
|
||||
/** @var \App\Models\Table $table */
|
||||
@endphp
|
||||
|
||||
<div class="row">
|
||||
{{-- Description field --}}
|
||||
@@ -68,7 +71,7 @@
|
||||
<th class="text-right pr-2">Revisions</th>
|
||||
<td>
|
||||
{{ $table->revisions_count }}
|
||||
<span class="text-muted ml-1">(<a class="link-no-color" href="{{ $table->revisionsRoute }}">see changes</a>)</span>
|
||||
<span class="text-muted ml-1">(<a class="link-no-color" href="{{ $table->actionRoute('revisions') }}">see changes</a>)</span>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
@@ -24,19 +24,20 @@
|
||||
<h2>Table Revisions</h2>
|
||||
|
||||
@php
|
||||
$revIds = $revisions->pluck('id');
|
||||
$revIds = $revisions->pluck('id')->reverse()->values();
|
||||
@endphp
|
||||
|
||||
@foreach($revisions as $i => $revision)
|
||||
@php
|
||||
$num = count($revisions) - $i;
|
||||
$isCurrent = $revision->id == $table->revision_id;
|
||||
@endphp
|
||||
<div class="x-revision rounded border box-shadow mb-3 p-2" id="revision-{{$revision->id}}">
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
#{{$num}}
|
||||
– {{ $revision->created_at->format('Y-m-d, G:i:s') }}
|
||||
@if($i == 0)
|
||||
@if($isCurrent)
|
||||
<span class="text-muted ml-1">(current)</span>
|
||||
@else
|
||||
<span class="text-muted ml-1">
|
||||
@@ -47,7 +48,7 @@
|
||||
<div class="col-md-4 text-right">
|
||||
@if($revision->ancestor_id)
|
||||
@if ($revIds->contains($revision->ancestor_id))
|
||||
<span class="x-revision-link text-muted" data-id="{{$revision->ancestor_id}}">Show parent</span>
|
||||
<span class="x-revision-link text-muted" data-id="{{$revision->ancestor_id}}">Based on #{{ 1+$revIds->search($revision->ancestor_id) }}</span>
|
||||
@else
|
||||
<span class="text-muted">Forked from elsewhere</span>
|
||||
@endif
|
||||
@@ -56,36 +57,17 @@
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-12" style="font-size:140%;">
|
||||
<div class="col-md-10" style="font-size:140%;">
|
||||
{{ $revision->note }}
|
||||
</div>
|
||||
<div class="col-md-2 text-right">
|
||||
@if(!$isCurrent && user()->ownsTable($table))
|
||||
<a href="{{$table->actionRoute('revert-to', ['rev' => $num])}}" class="btn btn-outline-secondary btn-sm">Set as current</a>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@endsection
|
||||
|
||||
@push('scripts')
|
||||
<style>
|
||||
.x-revision.highlight {
|
||||
background: #a5d5ff;
|
||||
border-color: #a5d5ff;
|
||||
}
|
||||
|
||||
.x-revision-link {
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
ready(function() {
|
||||
$('.x-revision-link').on('mouseenter', function () {
|
||||
$('#revision-'+$(this).data('id')).addClass('highlight');
|
||||
}).on('mouseleave', function () {
|
||||
$('#revision-'+$(this).data('id')).removeClass('highlight');
|
||||
})
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
|
||||
@@ -59,6 +59,7 @@ Route::group(['middleware' => ['auth', 'activated']], function () {
|
||||
Route::get('@{user}/{table}/settings', 'TableController@settings')->name('table.conf');
|
||||
Route::post('@{user}/{table}/settings', 'TableController@storeSettings')->name('table.storeConf');
|
||||
Route::post('@{user}/{table}/delete', 'TableController@delete')->name('table.delete');
|
||||
Route::get('@{user}/{table}/revert-to', 'TableController@revertTo')->name('table.revertTo');
|
||||
|
||||
Route::post('@{user}/{table}/draft/update', 'TableEditController@draftUpdate')->name('table.draft-update');
|
||||
Route::post('@{user}/{table}/draft/submit', 'TableEditController@draftSubmit')->name('table.draft-submit');
|
||||
|
||||
Reference in New Issue
Block a user