From 2325536a267dfe6236f8c2c369ce852e53601640 Mon Sep 17 00:00:00 2001 From: A1Gard Date: Wed, 10 Jul 2024 10:36:37 +0330 Subject: [PATCH] added cities and states controller --- app/Http/Controllers/Admin/CityController.php | 127 ++++++++++++++++++ .../Controllers/Admin/StateController.php | 125 +++++++++++++++++ app/Http/Requests/CitySaveRequest.php | 32 +++++ app/Http/Requests/StateSaveRequest.php | 32 +++++ app/Models/City.php | 4 + resources/js/app.js | 2 + resources/js/components/latlng.vue | 113 ++++++++++++++++ resources/js/panel/general-events.js | 7 - .../views/admin/cities/city-form.blade.php | 106 +++++++++++++++ .../views/admin/cities/city-list.blade.php | 15 +++ .../views/admin/states/state-form.blade.php | 93 +++++++++++++ .../views/admin/states/state-list.blade.php | 15 +++ .../templates/panel-list-template.blade.php | 5 + .../components/panel-side-navbar.blade.php | 26 +++- routes/web.php | 26 ++++ 15 files changed, 714 insertions(+), 14 deletions(-) create mode 100644 app/Http/Controllers/Admin/CityController.php create mode 100644 app/Http/Controllers/Admin/StateController.php create mode 100644 app/Http/Requests/CitySaveRequest.php create mode 100644 app/Http/Requests/StateSaveRequest.php create mode 100644 resources/js/components/latlng.vue create mode 100644 resources/views/admin/cities/city-form.blade.php create mode 100644 resources/views/admin/cities/city-list.blade.php create mode 100644 resources/views/admin/states/state-form.blade.php create mode 100644 resources/views/admin/states/state-list.blade.php diff --git a/app/Http/Controllers/Admin/CityController.php b/app/Http/Controllers/Admin/CityController.php new file mode 100644 index 0000000..4292e20 --- /dev/null +++ b/app/Http/Controllers/Admin/CityController.php @@ -0,0 +1,127 @@ + + ['title' => "Edit", 'class' => 'btn-outline-primary', 'icon' => 'ri-edit-2-line'], + 'show' => + ['title' => "Detail", 'class' => 'btn-outline-light', 'icon' => 'ri-eye-line'], + 'destroy' => + ['title' => "Remove", 'class' => 'btn-outline-danger delete-confirm', 'icon' => 'ri-close-line'], + ]; + + + public function __construct() + { + parent::__construct(City::class, CitySaveRequest::class); + } + + /** + * @param $city City + * @param $request CitySaveRequest + * @return City + */ + public function save($city, $request) + { + + $city->name = $request->name; + $city->state_id = $request->state_id; + $city->lat = $request->lat; + $city->lng = $request->lng; + $city->save(); + return $city; + + } + + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + $states = State::orderBy('name')->get(['id', 'name']); + return view($this->formView,compact('states')); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(City $item) + { + // + $states = State::orderBy('name')->get(['id', 'name']); + return view($this->formView, compact('item','states')); + } + + public function bulk(Request $request) + { + +// dd($request->all()); + $data = explode('.', $request->input('action')); + $action = $data[0]; + $ids = $request->input('id'); + switch ($action) { + case 'delete': + $msg = __(':COUNT items deleted successfully', ['COUNT' => count($ids)]); + $this->_MODEL_::destroy($ids); + break; + /**restore*/ + case 'restore': + $msg = __(':COUNT items restored successfully', ['COUNT' => count($ids)]); + foreach ($ids as $id) { + $this->_MODEL_::withTrashed()->find($id)->restore(); + } + break; + /*restore**/ + default: + $msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]); + } + + return $this->do_bulk($msg, $action, $ids); + } + + public function destroy(City $item) + { + return parent::delete($item); + } + + + public function update(Request $request, City $item) + { + return $this->bringUp($request, $item); + } + + /**restore*/ + public function restore($item) + { + return parent::restoreing(City::withTrashed()->where('id', $item)->first()); + } + /*restore**/ +} diff --git a/app/Http/Controllers/Admin/StateController.php b/app/Http/Controllers/Admin/StateController.php new file mode 100644 index 0000000..c05109f --- /dev/null +++ b/app/Http/Controllers/Admin/StateController.php @@ -0,0 +1,125 @@ + + ['title' => "Edit", 'class' => 'btn-outline-primary', 'icon' => 'ri-edit-2-line'], + 'show' => + ['title' => "Detail", 'class' => 'btn-outline-light', 'icon' => 'ri-eye-line'], + 'destroy' => + ['title' => "Remove", 'class' => 'btn-outline-danger delete-confirm', 'icon' => 'ri-close-line'], + ]; + + + public function __construct() + { + parent::__construct(State::class, StateSaveRequest::class); + } + + /** + * @param $state State + * @param $request StateSaveRequest + * @return State + */ + public function save($state, $request) + { + + + $state->name = $request->name; + $state->country = $request->country; + $state->lat = $request->lat; + $state->lng = $request->lng; + $state->save(); + return $state; + + } + + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + return view($this->formView); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(State $item) + { + // + return view($this->formView, compact('item')); + } + + public function bulk(Request $request) + { + +// dd($request->all()); + $data = explode('.', $request->input('action')); + $action = $data[0]; + $ids = $request->input('id'); + switch ($action) { + case 'delete': + $msg = __(':COUNT items deleted successfully', ['COUNT' => count($ids)]); + $this->_MODEL_::destroy($ids); + break; + /**restore*/ + case 'restore': + $msg = __(':COUNT items restored successfully', ['COUNT' => count($ids)]); + foreach ($ids as $id) { + $this->_MODEL_::withTrashed()->find($id)->restore(); + } + break; + /*restore**/ + default: + $msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]); + } + + return $this->do_bulk($msg, $action, $ids); + } + + public function destroy(State $item) + { + return parent::delete($item); + } + + + public function update(Request $request, State $item) + { + return $this->bringUp($request, $item); + } + + /**restore*/ + public function restore($item) + { + return parent::restoreing(State::withTrashed()->where('id', $item)->first()); + } + /*restore**/ +} diff --git a/app/Http/Requests/CitySaveRequest.php b/app/Http/Requests/CitySaveRequest.php new file mode 100644 index 0000000..e4a2d97 --- /dev/null +++ b/app/Http/Requests/CitySaveRequest.php @@ -0,0 +1,32 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + // + 'name' => ['required','string','min:2'] , + 'state_id' => ['required','exists:states,id'] , + 'lat' => ['required','numeric','between:-90,90'], + 'lng' => ['required','numeric','between:-180,180'], + ]; + } +} diff --git a/app/Http/Requests/StateSaveRequest.php b/app/Http/Requests/StateSaveRequest.php new file mode 100644 index 0000000..1b19b1b --- /dev/null +++ b/app/Http/Requests/StateSaveRequest.php @@ -0,0 +1,32 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + // + 'name' => ['required','string','min:2'] , + 'country' => ['required','string','min:2'], + 'lat' => ['required','numeric','between:-90,90'], + 'lng' => ['required','numeric','between:-180,180'], + ]; + } +} diff --git a/app/Models/City.php b/app/Models/City.php index b2b3aee..3cec84c 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -12,4 +12,8 @@ class City extends Model use HasTranslations,SoftDeletes; public $translatable = ['name']; + + public function state(){ + return $this->belongsTo(State::class); + } } diff --git a/resources/js/app.js b/resources/js/app.js index c6b371b..be24d01 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -93,6 +93,8 @@ app.component('gfxer', Gfxer); import AreaDesginer from "./components/AreaDesginer.vue"; app.component('area-designer', AreaDesginer); +import Latlng from "./components/latlng.vue"; +app.component('lat-lng', Latlng); /** * The following block of code may be used to automatically register your diff --git a/resources/js/components/latlng.vue b/resources/js/components/latlng.vue new file mode 100644 index 0000000..2bd5ba1 --- /dev/null +++ b/resources/js/components/latlng.vue @@ -0,0 +1,113 @@ + + + + + diff --git a/resources/js/panel/general-events.js b/resources/js/panel/general-events.js index 426c969..3f0a4aa 100644 --- a/resources/js/panel/general-events.js +++ b/resources/js/panel/general-events.js @@ -17,13 +17,6 @@ window.findUrl = function (name, item = null) { document.addEventListener('DOMContentLoaded', () => { - document.addEventListener('click', function (e) { - if (e.target.classList.contains('delete-confirm')) { - if (!confirm('Are you sure you want to delete this item?')) { // WIP Need to translate - e.preventDefault(); - } - } - }); document.querySelectorAll('.delete-confirm')?.forEach(function (el) { el.addEventListener('click', function (e) { diff --git a/resources/views/admin/cities/city-form.blade.php b/resources/views/admin/cities/city-form.blade.php new file mode 100644 index 0000000..cecb0f7 --- /dev/null +++ b/resources/views/admin/cities/city-form.blade.php @@ -0,0 +1,106 @@ +@extends('admin.templates.panel-form-template') +@section('title') + @if(isset($item)) + {{__("Edit city")}} [{{$item->name}}] + @else + {{__("Add new city")}} + @endif - +@endsection +@section('form') + +
+
+ + @include('components.err') +
+

+ + {{__("Tips")}} +

+
    +
  • + {{__("Recommends")}} +
  • +
+
+
+

+ + {{__("Change latitude and longitude")}} +

+
+ +
+
+ +
+
+
+ +

+ @if(isset($item)) + {{__("Edit city")}} [{{$item->name}}] + @else + {{__("Add new city")}} + @endif +

+ +
+
+
+ + +
+
+
+
+ + state_id??null)}}' + :close-on-Select="true"> +
+
+
+
+ + +
+
+
+
+ + +
+
+
+ + +
+
+ +
+
+
+@endsection diff --git a/resources/views/admin/cities/city-list.blade.php b/resources/views/admin/cities/city-list.blade.php new file mode 100644 index 0000000..f53749c --- /dev/null +++ b/resources/views/admin/cities/city-list.blade.php @@ -0,0 +1,15 @@ +@extends('admin.templates.panel-list-template') + +@section('list-title') + + {{__("Cities list")}} +@endsection +@section('title') + {{__("Cities list")}} - +@endsection +@section('filter') + {{-- Other filters --}} +@endsection +@section('bulk') + {{-- --}} +@endsection diff --git a/resources/views/admin/states/state-form.blade.php b/resources/views/admin/states/state-form.blade.php new file mode 100644 index 0000000..d8869ed --- /dev/null +++ b/resources/views/admin/states/state-form.blade.php @@ -0,0 +1,93 @@ +@extends('admin.templates.panel-form-template') +@section('title') + @if(isset($item)) + {{__("Edit state")}} [{{$item->name}}] + @else + {{__("Add new state")}} + @endif - +@endsection +@section('form') + +
+
+ + @include('components.err') +
+

+ + {{__("Tips")}} +

+
    +
  • + {{__("Recommends")}} +
  • +
+
+
+

+ + {{__("Change latitude and longitude")}} +

+
+ +
+
+ +
+
+
+ +

+ @if(isset($item)) + {{__("Edit state")}} [{{$item->name}}] + @else + {{__("Add new state")}} + @endif +

+ +
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+ + +
+
+
+
+@endsection diff --git a/resources/views/admin/states/state-list.blade.php b/resources/views/admin/states/state-list.blade.php new file mode 100644 index 0000000..3b06570 --- /dev/null +++ b/resources/views/admin/states/state-list.blade.php @@ -0,0 +1,15 @@ +@extends('admin.templates.panel-list-template') + +@section('list-title') + + {{__("States list")}} +@endsection +@section('title') + {{__("States list")}} - +@endsection +@section('filter') + {{-- Other filters --}} +@endsection +@section('bulk') + {{-- --}} +@endsection diff --git a/resources/views/admin/templates/panel-list-template.blade.php b/resources/views/admin/templates/panel-list-template.blade.php index 0deb64e..b505bc8 100644 --- a/resources/views/admin/templates/panel-list-template.blade.php +++ b/resources/views/admin/templates/panel-list-template.blade.php @@ -195,6 +195,11 @@ {{ $item->category?->name??'-' }} @break + @case('state_id') + + {{ $item->state?->name??'-' }} + + @break @case('product_id') {{ $item->product?->name??'-' }} diff --git a/resources/views/components/panel-side-navbar.blade.php b/resources/views/components/panel-side-navbar.blade.php index b6274b3..9573a55 100644 --- a/resources/views/components/panel-side-navbar.blade.php +++ b/resources/views/components/panel-side-navbar.blade.php @@ -184,10 +184,28 @@
  • name('admin.')->group( Route::post('bulk', [\App\Http\Controllers\Admin\CustomerController::class, "bulk"])->name('bulk'); Route::get('trashed', [\App\Http\Controllers\Admin\CustomerController::class, "trashed"])->name('trashed'); }); + Route::prefix('states')->name('state.')->group( + function () { + Route::get('', [\App\Http\Controllers\Admin\StateController::class, 'index'])->name('index'); + Route::get('create', [\App\Http\Controllers\Admin\StateController::class, 'create'])->name('create'); + Route::post('store', [\App\Http\Controllers\Admin\StateController::class, 'store'])->name('store'); + Route::get('show/{item}', [\App\Http\Controllers\Admin\StateController::class, 'show'])->name('show'); + Route::get('edit/{item}', [\App\Http\Controllers\Admin\StateController::class, 'edit'])->name('edit'); + Route::post('update/{item}', [\App\Http\Controllers\Admin\StateController::class, 'update'])->name('update'); + Route::get('delete/{item}', [\App\Http\Controllers\Admin\StateController::class, 'destroy'])->name('destroy'); + Route::get('restore/{item}', [\App\Http\Controllers\Admin\StateController::class, 'restore'])->name('restore'); + Route::post('bulk', [\App\Http\Controllers\Admin\StateController::class, "bulk"])->name('bulk'); + Route::get('trashed', [\App\Http\Controllers\Admin\StateController::class, "trashed"])->name('trashed'); + }); + Route::prefix('cities')->name('city.')->group( + function () { + Route::get('', [\App\Http\Controllers\Admin\CityController::class, 'index'])->name('index'); + Route::get('create', [\App\Http\Controllers\Admin\CityController::class, 'create'])->name('create'); + Route::post('store', [\App\Http\Controllers\Admin\CityController::class, 'store'])->name('store'); + Route::get('show/{item}', [\App\Http\Controllers\Admin\CityController::class, 'show'])->name('show'); + Route::get('edit/{item}', [\App\Http\Controllers\Admin\CityController::class, 'edit'])->name('edit'); + Route::post('update/{item}', [\App\Http\Controllers\Admin\CityController::class, 'update'])->name('update'); + Route::get('delete/{item}', [\App\Http\Controllers\Admin\CityController::class, 'destroy'])->name('destroy'); + Route::get('restore/{item}', [\App\Http\Controllers\Admin\CityController::class, 'restore'])->name('restore'); + Route::post('bulk', [\App\Http\Controllers\Admin\CityController::class, "bulk"])->name('bulk'); + Route::get('trashed', [\App\Http\Controllers\Admin\CityController::class, "trashed"])->name('trashed'); + }); Route::prefix('galleries')->name('gallery.')->group( function () { Route::get('', [\App\Http\Controllers\Admin\GalleryController::class, 'index'])->name('index');