diff --git a/app/Http/Controllers/Admin/CategoryController.php b/app/Http/Controllers/Admin/CategoryController.php new file mode 100644 index 0000000..6459bd1 --- /dev/null +++ b/app/Http/Controllers/Admin/CategoryController.php @@ -0,0 +1,122 @@ + + ['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(Category::class, CategorySaveRequest::class); + } + + /** + * @param $category Category + * @param $request CategorySaveRequest + * @return Category + */ + public function save($category, $request) + { + + $category->name = $request->input('name'); + $category->subtitle = $request->input('subtitle'); + $category->description = $request->input('description'); + $category->parent_id = $request->input('parent_id'); + $category->slug = $this->getSlug($category); + if ($request->has('image')){ + $category->image = $this->storeFile('image',$category, 'categories'); + } + if ($request->has('bg')){ + $category->bg = $this->storeFile('bg',$category, 'categories'); + } + $category->save(); + return $category; + + } + + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + $cats = Category::all(); + return view($this->formView,compact('cats')); + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Category $item) + { + // + $cats = Category::all(); + return view($this->formView, compact('item','cats')); + } + + 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; + + default: + $msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]); + } + + return $this->do_bulk($msg, $action, $ids); + } + + public function destroy(Category $item) + { + return parent::delete($item); + } + + + public function update(Request $request, Category $item) + { + return $this->bringUp($request, $item); + } + + +} diff --git a/app/Http/Requests/CategorySaveRequest.php b/app/Http/Requests/CategorySaveRequest.php new file mode 100644 index 0000000..dfdeff4 --- /dev/null +++ b/app/Http/Requests/CategorySaveRequest.php @@ -0,0 +1,33 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return [ + 'name' => ['required', 'string', 'min:2', 'max:128'], + 'subtitle' => ['nullable', 'string',], + 'image' => ['nullable', 'file', 'mimes:jpg,svg,png'], + 'bg' => ['nullable', 'file', 'mimes:jpg,svg,png'], + 'description' => ['nullable', 'string',], + 'parent_id' => ['nullable', 'exists:categories,id'], + ]; + } +} diff --git a/app/Models/Category.php b/app/Models/Category.php index 18bafab..c531ad0 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -4,10 +4,14 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; +use Spatie\Translatable\HasTranslations; class Category extends Model { - use HasFactory; + use HasFactory, HasTranslations,SoftDeletes; + + public $translatable = ['name', 'subtitle', 'description']; public function imgUrl() { @@ -15,14 +19,31 @@ class Category extends Model return asset('/assets/upload/logo.svg'); } - return \Storage::url('category/' . $this->image); + return \Storage::url('categories/' . $this->image); } + public function bgUrl() { if ($this->bg == null) { return asset('/assets/upload/logo.svg'); } - return \Storage::url('category/' . $this->bg); + return \Storage::url('categories/' . $this->bg); + } + + public function parent() + { + return $this->belongsTo(Category::class, 'parent_id'); + } + + public function children() + { + return $this->hasMany(Category::class, 'parent_id'); + } + + public function getRouteKeyName() + { + return 'slug'; } + } diff --git a/database/migrations/2024_05_07_125838_create_categories_table.php b/database/migrations/2024_05_07_125838_create_categories_table.php index b062d45..83a84bf 100644 --- a/database/migrations/2024_05_07_125838_create_categories_table.php +++ b/database/migrations/2024_05_07_125838_create_categories_table.php @@ -15,9 +15,11 @@ return new class extends Migration $table->id(); $table->text('name'); $table->string('slug',128)->unique(); + $table->text('subtitle')->nullable(); $table->text('description')->nullable(); $table->integer('sort')->default(0); - $table->string('image')->nullable()->default(null); + $table->string('image',2048)->nullable()->default(null); + $table->string('bg',2048)->nullable()->default(null); $table->unsignedInteger('parent_id')->nullable()->default(null)->index(); $table->softDeletes(); $table->timestamps(); diff --git a/resources/views/admin/categories/category-form.blade.php b/resources/views/admin/categories/category-form.blade.php new file mode 100644 index 0000000..f224b6b --- /dev/null +++ b/resources/views/admin/categories/category-form.blade.php @@ -0,0 +1,142 @@ +@extends('admin.templates.panel-form-template') +@section('title') + @if(isset($item)) + {{__("Edit category")}} [{{$item->name}}] + @else + {{__("Add new category")}} + @endif - +@endsection +@section('form') + +
+
+ + @include('components.err') +
+

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

+
    +
  • + {{__("You can leave the slug empty; it will be generated automatically.")}} +
  • +
+
+ + @if(isset($item)) +
+

+ + {{__('Feature image')}} +

+ {{$item->name}} + +
+
+

+ + {{__('Background image')}} +

+ {{$item->name}} + +
+ @endif + +
+
+
+ +

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

+ +
+ +
+
+ + +
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+
+ + +
+
+ +
+ + +
+
+ + +
+
+
+ +
+
+
+@endsection diff --git a/resources/views/admin/categories/category-list.blade.php b/resources/views/admin/categories/category-list.blade.php new file mode 100644 index 0000000..51fe02c --- /dev/null +++ b/resources/views/admin/categories/category-list.blade.php @@ -0,0 +1,15 @@ +@extends('admin.templates.panel-list-template') + +@section('list-title') + + {{__("Categories list")}} +@endsection +@section('title') + {{__("Categories list")}} - +@endsection +@section('filter') + {{-- Other filters --}} +@endsection +@section('bulk') + {{-- --}} +@endsection diff --git a/resources/views/admin/groups/group-form.blade.php b/resources/views/admin/groups/group-form.blade.php index 3019cdf..5649199 100644 --- a/resources/views/admin/groups/group-form.blade.php +++ b/resources/views/admin/groups/group-form.blade.php @@ -55,82 +55,87 @@ @endif -
-
- - -
-
- - -
-
-
-
diff --git a/resources/views/components/panel-side-navbar.blade.php b/resources/views/components/panel-side-navbar.blade.php index 8fc8047..69863d9 100644 --- a/resources/views/components/panel-side-navbar.blade.php +++ b/resources/views/components/panel-side-navbar.blade.php @@ -46,7 +46,7 @@
  • - + {{__('Product categories')}} diff --git a/routes/web.php b/routes/web.php index 74604bc..bfeb01c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -52,6 +52,19 @@ Route::prefix(config('app.panel.prefix'))->name('admin.')->group( Route::post('bulk', [\App\Http\Controllers\Admin\GroupController::class, "bulk"])->name('bulk'); Route::get('trashed', [\App\Http\Controllers\Admin\GroupController::class, "trashed"])->name('trashed'); }); + Route::prefix('categories')->name('category.')->group( + function () { + Route::get('', [\App\Http\Controllers\Admin\CategoryController::class, 'index'])->name('index'); + Route::get('create', [\App\Http\Controllers\Admin\CategoryController::class, 'create'])->name('create'); + Route::post('store', [\App\Http\Controllers\Admin\CategoryController::class, 'store'])->name('store'); + Route::get('edit/{item}', [\App\Http\Controllers\Admin\CategoryController::class, 'edit'])->name('edit'); + Route::post('update/{item}', [\App\Http\Controllers\Admin\CategoryController::class, 'update'])->name('update'); + Route::get('delete/{item}', [\App\Http\Controllers\Admin\CategoryController::class, 'destroy'])->name('destroy'); + Route::get('restore/{item}', [\App\Http\Controllers\Admin\CategoryController::class, 'restore'])->name('restore'); + Route::post('bulk', [\App\Http\Controllers\Admin\CategoryController::class, "bulk"])->name('bulk'); + Route::get('trashed', [\App\Http\Controllers\Admin\CategoryController::class, "trashed"])->name('trashed'); + }); + Route::prefix('posts')->name('post.')->group( function () { Route::get('', [\App\Http\Controllers\Admin\PostController::class, 'index'])->name('index');