mirror of https://github.com/4xmen/xshop.git
added category controller
parent
16c68954ce
commit
342f1f91c4
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\XController;
|
||||
use App\Http\Requests\CategorySaveRequest;
|
||||
use App\Models\Access;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Helper;
|
||||
use function App\Helpers\hasCreateRoute;
|
||||
|
||||
class CategoryController extends XController
|
||||
{
|
||||
|
||||
// protected $_MODEL_ = Category::class;
|
||||
// protected $SAVE_REQUEST = CategorySaveRequest::class;
|
||||
|
||||
protected $cols = ['name','subtitle','parent_id'];
|
||||
protected $extra_cols = ['id','slug','image'];
|
||||
|
||||
protected $searchable = ['name','subtitle','description'];
|
||||
|
||||
|
||||
protected $listView = 'admin.categories.category-list';
|
||||
protected $formView = 'admin.categories.category-form';
|
||||
|
||||
|
||||
protected $buttons = [
|
||||
'edit' =>
|
||||
['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);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class CategorySaveRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|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'],
|
||||
];
|
||||
}
|
||||
}
|
@ -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')
|
||||
|
||||
<div class="row">
|
||||
<div class="col-lg-3">
|
||||
|
||||
@include('components.err')
|
||||
<div class="item-list mb-3">
|
||||
<h3 class="p-3">
|
||||
<i class="ri-message-3-line"></i>
|
||||
{{__("Tips")}}
|
||||
</h3>
|
||||
<ul>
|
||||
<li>
|
||||
{{__("You can leave the slug empty; it will be generated automatically.")}}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@if(isset($item))
|
||||
<div class="item-list mb-3">
|
||||
<h3 class="p-3">
|
||||
<i class="ri-image-2-line"></i>
|
||||
{{__('Feature image')}}
|
||||
</h3>
|
||||
<img src="{{$item->imgUrl()}}" alt="{{$item->name}}" class="img-fluid mb-4">
|
||||
|
||||
</div>
|
||||
<div class="item-list mb-3">
|
||||
<h3 class="p-3">
|
||||
<i class="ri-image-2-line"></i>
|
||||
{{__('Background image')}}
|
||||
</h3>
|
||||
<img src="{{$item->bgUrl()}}" alt="{{$item->name}}" class="img-fluid mb-4">
|
||||
|
||||
</div>
|
||||
@endif
|
||||
|
||||
</div>
|
||||
<div class="col-lg-9 ps-xl-1 ps-xxl-1">
|
||||
<div class="general-form ">
|
||||
|
||||
<h1>
|
||||
@if(isset($item))
|
||||
{{__("Edit category")}} [{{$item->name}}]
|
||||
@else
|
||||
{{__("Add new category")}}
|
||||
@endif
|
||||
</h1>
|
||||
|
||||
<div class="row">
|
||||
|
||||
<div class="form-group row">
|
||||
<div class="col-md-6 mt-3">
|
||||
<label for="name">
|
||||
{{__('Category name')}}
|
||||
</label>
|
||||
<input name="name" type="text" class="form-control @error('name') is-invalid @enderror"
|
||||
placeholder="{{__('Category name')}}" value="{{old('name',$item->name??null)}}"/>
|
||||
</div>
|
||||
<div class="col-md-6 mt-3">
|
||||
<label for="name">
|
||||
{{__('Category slug')}}
|
||||
</label>
|
||||
<input name="slug" type="text" class="form-control @error('slug') is-invalid @enderror"
|
||||
placeholder="{{__('Category slug')}}" value="{{old('slug',$item->slug??null)}}"/>
|
||||
</div>
|
||||
<div class="col-md-12 mt-3">
|
||||
<div class="form-group">
|
||||
<label for="subtitle">
|
||||
{{__('Subtitle')}}
|
||||
</label>
|
||||
<input name="subtitle" type="text"
|
||||
class="form-control @error('subtitle') is-invalid @enderror" id="subtitle"
|
||||
placeholder="{{__('Subtitle')}}"
|
||||
value="{{old('subtitle',$item->subtitle??null)}}"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4 mt-3">
|
||||
<label for="parent">
|
||||
{{__('Group Parent')}}
|
||||
</label>
|
||||
<select name="parent_id" id="parent"
|
||||
class="form-control @error('parent') is-invalid @enderror">
|
||||
<option value=""> {{__('No parent')}} </option>
|
||||
@foreach($cats as $cat )
|
||||
@if( !isset($item) || $cat->id != $item->id )
|
||||
<option value="{{ $cat->id }}"
|
||||
@if (old('parent',$item->parent_id??null) == $cat->id ) selected @endif >
|
||||
{{$cat->name}}
|
||||
</option>
|
||||
@endif
|
||||
@endforeach
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-4 mt-3">
|
||||
<div class="form-group">
|
||||
<label for="image">
|
||||
{{__('Feature image')}}
|
||||
</label>
|
||||
<input accept=".jpg,.png,.svg" name="image" type="file"
|
||||
class="form-control @error('image') is-invalid @enderror" id="image"
|
||||
placeholder="{{__('Feature image')}}"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4 mt-3">
|
||||
<div class="form-group">
|
||||
<label for="bg">
|
||||
{{__('Background image')}}
|
||||
</label>
|
||||
<input accept=".jpg,.png,.svg" name="bg" type="file"
|
||||
class="form-control @error('bg') is-invalid @enderror" id="bg"
|
||||
placeholder="{{__('Background image')}}"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-12 mt-3">
|
||||
<label for="description">
|
||||
{{__('Description')}}
|
||||
</label>
|
||||
<textarea rows="5" name="description"
|
||||
class="form-control @error('description') is-invalid @enderror"
|
||||
placeholder="{{__('Description')}}">{{old('description',$item->description??null)}}</textarea>
|
||||
</div>
|
||||
<div class="col-md-12">
|
||||
<label> </label>
|
||||
<input name="" type="submit" class="btn btn-primary mt-2" value="{{__('Save')}}"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
@ -0,0 +1,15 @@
|
||||
@extends('admin.templates.panel-list-template')
|
||||
|
||||
@section('list-title')
|
||||
<i class="ri-user-3-line"></i>
|
||||
{{__("Categories list")}}
|
||||
@endsection
|
||||
@section('title')
|
||||
{{__("Categories list")}} -
|
||||
@endsection
|
||||
@section('filter')
|
||||
{{-- Other filters --}}
|
||||
@endsection
|
||||
@section('bulk')
|
||||
{{-- <option value="-"> - </option> --}}
|
||||
@endsection
|
Loading…
Reference in New Issue