diff --git a/app/Http/Controllers/TableController.php b/app/Http/Controllers/TableController.php index a0dd03b..442bc37 100644 --- a/app/Http/Controllers/TableController.php +++ b/app/Http/Controllers/TableController.php @@ -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); diff --git a/app/Models/Table.php b/app/Models/Table.php index c0e83ea..9ae63d7 100644 --- a/app/Models/Table.php +++ b/app/Models/Table.php @@ -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', [ diff --git a/resources/views/table/_panel-metadata.blade.php b/resources/views/table/_panel-metadata.blade.php index e1f2f92..d2100a1 100644 --- a/resources/views/table/_panel-metadata.blade.php +++ b/resources/views/table/_panel-metadata.blade.php @@ -1,6 +1,9 @@ {{-- args: $table --}} +@php +/** @var \App\Models\Table $table */ +@endphp