From 3bf9b68503e16db9bcdb1bff1ff6c7530a5aae6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 29 Jul 2018 19:11:55 +0200 Subject: [PATCH] table deleting --- app/Http/Controllers/TableController.php | 16 ++++++++ app/Http/Kernel.php | 2 +- app/Models/Concerns/Reportable.php | 3 +- app/Models/Table.php | 4 ++ app/View/WidgetFactory.php | 4 +- resources/assets/sass/_helpers.scss | 1 - .../bootstrap-customizations/_border.scss | 4 ++ resources/views/table/conf.blade.php | 37 ++++++++++++++++++- routes/web.php | 1 + 9 files changed, 67 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/TableController.php b/app/Http/Controllers/TableController.php index 43718b4..9de980e 100644 --- a/app/Http/Controllers/TableController.php +++ b/app/Http/Controllers/TableController.php @@ -43,6 +43,22 @@ class TableController extends Controller ]); } + public function delete(Request $request, User $user, string $table) + { + /** @var Table $tableModel */ + $tableModel = $user->tables()->where('name', $table)->first(); + if ($tableModel === null) abort(404, "No such table."); + $this->authorize('delete', $tableModel); + + if ($request->get('table-name', null) !== $table) { + return $this->backWithErrors(['table-name' => 'Fill table name']); + } + + $tableModel->delete(); + + return redirect(route('profile.view', $user->name)); + } + /** * SHow a form for creating a new table * diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 5053af9..68b67a4 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -28,7 +28,7 @@ class Kernel extends HttpKernel */ protected $middlewareGroups = [ 'web' => [ - 'throttle:60,15', // try to prevent people refresh-spamming the server to game table visit counts + 'throttle:120,15', // try to prevent people refresh-spamming the server to game table visit counts \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, diff --git a/app/Models/Concerns/Reportable.php b/app/Models/Concerns/Reportable.php index 57eb7ba..9770825 100644 --- a/app/Models/Concerns/Reportable.php +++ b/app/Models/Concerns/Reportable.php @@ -14,7 +14,8 @@ trait Reportable { public static function bootReportable() { - static::deleting(function(Reportable $self) { + static::deleting(function($self) { + /** @var Reportable $self */ $self->reportsOf()->delete(); }); } diff --git a/app/Models/Table.php b/app/Models/Table.php index c78c232..454d121 100644 --- a/app/Models/Table.php +++ b/app/Models/Table.php @@ -131,6 +131,10 @@ class Table extends BaseModel return route('table.conf', ['user' => $this->cachedOwner()->name, 'table' => $this->name]); } + if ($name == 'deleteRoute') { + return route('table.delete', ['user' => $this->cachedOwner()->name, 'table' => $this->name]); + } + return parent::__get($name); } diff --git a/app/View/WidgetFactory.php b/app/View/WidgetFactory.php index ce6f298..1be780a 100644 --- a/app/View/WidgetFactory.php +++ b/app/View/WidgetFactory.php @@ -28,9 +28,11 @@ class WidgetFactory public function par($text, $extraClasses='', $escape=true) { + if (false === strpos($extraClasses, 'mb-')) $extraClasses .= ' mb-2'; + return "
". - "

fieldCols offset-md-$this->labelCols mb-2 ".e($extraClasses)."\">". + "

fieldCols offset-md-$this->labelCols ".e($extraClasses)."\">". ($escape ? e($text) : $text) . "

". "
"; diff --git a/resources/assets/sass/_helpers.scss b/resources/assets/sass/_helpers.scss index e563220..42e4cff 100644 --- a/resources/assets/sass/_helpers.scss +++ b/resources/assets/sass/_helpers.scss @@ -16,4 +16,3 @@ .box-shadow { box-shadow: 0 2px 3px rgba(black, .3); } - diff --git a/resources/assets/sass/bootstrap-customizations/_border.scss b/resources/assets/sass/bootstrap-customizations/_border.scss index 7d36cc3..67bb404 100644 --- a/resources/assets/sass/bootstrap-customizations/_border.scss +++ b/resources/assets/sass/bootstrap-customizations/_border.scss @@ -11,3 +11,7 @@ border-color: $value !important; } } + +.border-2 { + border-width: 2px !important; +} diff --git a/resources/views/table/conf.blade.php b/resources/views/table/conf.blade.php index 775b728..032de80 100644 --- a/resources/views/table/conf.blade.php +++ b/resources/views/table/conf.blade.php @@ -25,7 +25,7 @@
+ aria-label="Change Table Metadata"> @csrf
@@ -63,4 +63,39 @@
+ + +
+ + @csrf +
+ @php(Widget::setLayout(0, 12)) + + {!! Widget::header(2, 'Delete Table') !!} + + {!! Widget::par( + 'Table deletion cannot be undone. However, data from the table may + remain accessible if any users forked it.', 'mb-0') !!} + +
+ + {!! Widget::par( + 'Confirm table removal by typing its name:') !!} + +
+
+
+ + + +
+
+
+
+
@endsection diff --git a/routes/web.php b/routes/web.php index 0bb7d7b..f834649 100644 --- a/routes/web.php +++ b/routes/web.php @@ -56,6 +56,7 @@ Route::group(['middleware' => 'auth'], 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'); }); // Table resource - located at the end to work as a fallback