diff --git a/app/Http/Controllers/BrandController.php b/app/Http/Controllers/BrandController.php index 49c3e1e..d8b2f52 100644 --- a/app/Http/Controllers/BrandController.php +++ b/app/Http/Controllers/BrandController.php @@ -3,42 +3,125 @@ namespace App\Http\Controllers; use App\Http\Controllers\Controller; +use App\Http\Requests\StoreBrandRequest; +use App\Http\Requests\UpdateBrandRequest; use App\Models\Brand; -use Kwaadpepper\CrudPolicies\Http\Requests\CrudIndexRequest; -use Kwaadpepper\CrudPolicies\Traits\CrudController; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Str; /** * @codeCoverageIgnore */ class BrandController extends Controller { - use CrudController { - index as crudIndex; - } - - /** @var Brand */ - public static $modelClass = Brand::class; - /** - * The Bo app layout + * Display a listing of the resource. * - * @var string - */ - public static $layout = 'layouts.app'; - - /** - * Overload crud-policies index - * - * @param CrudIndexRequest $request + * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function index(CrudIndexRequest $request) + public function index(Request $request) { - /** @var \Illuminate\View\View */ - $view = $this->crudIndex($request); - if ($request->ajax()) { - return $view; + /** @var \Illuminate\Database\Eloquent\Builder $brands */ + $brands = Brand::query(); + + if ($request->search) { + $this->searchQuery( + $brands, + $request->search, + null, + 'name', + ); } - return view('brand.index', $view->getData()); + + $this->sortQuery($brands); + $searchFields = trans('Nom'); + + $brandModels = $brands->paginate(12); + + return view('brands.index', compact('brandModels', 'searchFields')); + } + + /** + * Show the form for creating a new resource. + * + * @param \App\Models\Brand $brands + * @return \Illuminate\Http\Response + */ + public function create(Brand $brands) + { + return view('brands.create', [ + 'brandModel' => $brands + ]); + } + + /** + * Store a newly created resource in storage. + * + * @param \App\Http\Requests\Brands\StoreBrandRequest $request + * @return \Illuminate\Http\Response + */ + public function store(StoreBrandRequest $request) + { + return DB::transaction(function () use ($request) { + $brand = new Brand(); + $brand->fill($request->validated()); + if ($brand->saveOrFail()) { + return redirect() + ->route('brands.edit', $brand) + ->with('success', trans( + ':model a été créé', + ['model' => Str::singular(trans('models.classes.brands'))] + )); + } + return redirect() + ->back() + ->with('error', trans( + ':model n\'a pas été créé', + ['model' => Str::singular(trans('models.classes.brands'))] + )); + }); + } + + /** + * Show the form for editing the specified resource. + * + * @param \App\Models\Brand $brands + * @return \Illuminate\Http\Response + */ + public function edit(Brand $brands) + { + return view('brands.edit', [ + 'brandModel' => $brands + ]); + } + + /** + * Update a spacific resource in storage. + * + * @param \App\Http\Requests\Brands\UpdateBrandRequest $request + * @return \Illuminate\Http\Response + */ + public function update(UpdateBrandRequest $request) + { + return DB::transaction(function () use ($request) { + $brand = new Brand(); + $brand->fill($request->validated()); + if ($brand->saveOrFail()) { + return redirect() + ->route('brands.edit', $brand) + ->with('success', trans( + ':model a été mis à jour', + ['model' => Str::singular(trans('models.classes.brands'))] + )); + } + return redirect() + ->back() + ->with('error', trans( + ':model n\'a pas été mis à jour', + ['model' => Str::singular(trans('models.classes.brands'))] + )); + }); } } diff --git a/app/Http/Requests/StoreBrandRequest.php b/app/Http/Requests/StoreBrandRequest.php new file mode 100644 index 0000000..289efbd --- /dev/null +++ b/app/Http/Requests/StoreBrandRequest.php @@ -0,0 +1,42 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + return [ + 'name' => 'required|string|min:3|max:255', + ]; + } + + /** + * Get custom attributes for validator errors. + * + * @return array + */ + public function attributes(): array + { + return [ + 'name' => trans('Nom de la marque'), + ]; + } +} diff --git a/app/Http/Requests/UpdateBrandRequest.php b/app/Http/Requests/UpdateBrandRequest.php new file mode 100644 index 0000000..bc5bc6a --- /dev/null +++ b/app/Http/Requests/UpdateBrandRequest.php @@ -0,0 +1,7 @@ +cacheFor = config('api.servicesDBCache') * 60; - self::$editableProperties = [ - 'name' => [ - 'label' => Str::ucfirst(trans('models.common.name')), - 'type' => CrudType::string(), - 'validate' => [ - 'min:5', - 'max:255' - ], - 'actions' => [ - CrudAction::viewAny(), - CrudAction::view(), - CrudAction::create(), - CrudAction::update() - ] - ] - ]; parent::__construct($attributes); } diff --git a/resources/views/brand/index.blade.php b/resources/views/brand/index.blade.php deleted file mode 100644 index 5d0906c..0000000 --- a/resources/views/brand/index.blade.php +++ /dev/null @@ -1,158 +0,0 @@ -@extends($viewLayout) - -@section('title', sprintf('%s - %s', transFb(sprintf('models.classes.%s', strtolower(Str::plural(class_basename($modelClass)))), Str::plural(class_basename($modelClass))), __('crud-policies::crud.viewAny'))) -@section('description', sprintf('%s %s', __('crud-policies::crud.viewAny'), trans(Str::plural(class_basename($modelClass))))) -@section('metaIndex', 'noindex,nofollow') - -@section('content') -
- @include('crud-policies::crud.modules.flashMessage') -
- @include('crud-policies::crud.modules.breadcrumb', ['action' => CrudAction::viewAny()]) - @include('crud-policies::crud.modules.actionbar.bar', ['action' => CrudAction::viewAny()]) -
-
-
-
-
-
- - - - @if(request()->brand) - - @endif - {{ __('crud-policies::crud.reset') }} - -
-
-
-
- - - - @foreach ($modelClass::getEditableProperties() as $colname => $prop) - @if(in_array(CrudAction::viewAny(), $prop['actions'])) - @switch($prop['type']) - @case(CrudType::password()) - {{-- Ignore --}} - @break - @default - - @endswitch - @endif - @endforeach - - - - - - - - @if(count($models)) - - {{-- INIT QUERIES for OPTIMIZING --}} - @foreach ($modelClass::getEditableProperties() as $fieldName => $prop) - @if($prop['type']->equals(CrudType::order())) - @php ${"$modelTable$fieldName"} = $modelClass::max($fieldName); @endphp - @endif - @if($prop['type']->equals(CrudType::belongsTo())) - @php try { $models->load(Str::singular((new $prop['belongsTo']())->getTable())); } catch(\Exception $e) {} @endphp - @endif - @if($prop['type']->equals(CrudType::belongsToMany())) - @php $models->load("$fieldName"); @endphp - @endif - @endforeach - - {{-- DISPLAY MODELS --}} - @foreach($models as $model) - - @foreach ($modelClass::getEditableProperties() as $fieldName => $prop) - @if(in_array(CrudAction::viewAny(), $prop['actions'])) - @switch($prop['type']) - @case(CrudType::belongsToMany() and $prop['belongsToMany'] === \App\Models\Project::class) - - @break - @case(CrudType::password()) - {{-- Ignore --}} - @break - @default - - @endswitch - @endif - @endforeach - - {{-- INFO COLS --}} - - - - - {{-- MODEL ACTIONS --}} - - - @endforeach - @else - - - - @endif - -
- {{ $prop['label'] }} - @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'desc' and session("crud.$modelTable.sort_col") === $colname)) -  ↓  - @endif - @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'asc' and session("crud.$modelTable.sort_col") === $colname)) -  ↑  - @endif - - {{ __('Nombre de comptes')}} - - {{ __('Nombre d\'optins')}} - - {{ __('Date d\'ajout en base')}} - - @if(session("crud.$modelTable.sort_way") and session("crud.$modelTable.sort_col")) - ⌫ - @endif -
- @foreach($model->{$fieldName} as $pSite) - {{ $pSite->{$prop['belongsToManyLabel']} }} - @endforeach - @include('crud-policies::crud.printfield', [ - 'action' => CrudAction::viewAny(), - 'field' => $model->{$fieldName}, - 'props' => $prop, - "$modelTable$fieldName" => ${"$modelTable$fieldName"} ?? null - ]){{ $model->getAccountsAttribute() }}{{ $model->getTotalOptinsAttribute() }}{{ $model->created_at->format('m/d/Y à H:i') }} -
- @can('view', $model) - {{ __('crud-policies::crud.view') }} - @endcan - @can('update', $model) - {{ __('crud-policies::crud.edit') }} - @endcan - @can('delete', $model) -
- @csrf - @method('DELETE') - -
- @endcan - @can('viewAny', \App\Models\Log::class) - {{ __('Journaux') }} - @endcan - @can('viewAny', \App\Models\File::class) - {{ __('Fichiers') }} - @endcan -
-
- {{ __('crud-policies::crud.nothingtoshow') }} -
- {{ $models->links() }} -
-
-@include('crud-policies::crud.modules.scriptConstants') -@endsection diff --git a/resources/views/brands/create.blade.php b/resources/views/brands/create.blade.php new file mode 100644 index 0000000..b0607c1 --- /dev/null +++ b/resources/views/brands/create.blade.php @@ -0,0 +1,50 @@ +@extends('layouts.app', ['brParam' => $brandModel]) + +@section('title', sprintf('%s - %s', __('models.classes.brands'), Str::ucfirst(__('création')))) +@section('description', __('Création d\'une :model', ['model' => __('models.classes.brands')])) +@section('metaIndex', 'noindex,nofollow') +@section('breadcrumb', request()->route()->getName()) + +@section('content') +
+
+
+
+ + + + @include('breadcrumbs.breadcrumb-body', ['brParam' => $brandModel]) +
+
+ +
+
+
+
+
+ @csrf + @include('brands.form') +
+
+ +
+
+
+
+
+@endsection + diff --git a/resources/views/brands/edit.blade.php b/resources/views/brands/edit.blade.php new file mode 100644 index 0000000..6980744 --- /dev/null +++ b/resources/views/brands/edit.blade.php @@ -0,0 +1,54 @@ +@extends('layouts.app', ['brParam' => $brandModel]) + +@section('title', sprintf('%s - %s', __('models.classes.brands'), Str::ucfirst(__('édition')))) +@section('description', __('Édition d\'une :model', ['model' => __('models.classes.brands')])) +@section('metaIndex', 'noindex,nofollow') +@section('breadcrumb', request()->route()->getName()) + +@section('content') +
+
+
+
+ + + + @include('breadcrumbs.breadcrumb-body', ['brParam' => $brandModel]) +
+
+ +
+
+
+
+
+ @csrf + @method('PUT') + @include('brands.form') +
+
+ +
+
+
+
+
+@endsection + diff --git a/resources/views/brands/form.blade.php b/resources/views/brands/form.blade.php new file mode 100644 index 0000000..ec61a19 --- /dev/null +++ b/resources/views/brands/form.blade.php @@ -0,0 +1,21 @@ +
+ {{-- TITLE --}} +
+
+ {{ __('Nom') }} +
+
+ @include('partials.inputs.varchar', [ + 'id' => 'brandName', + 'label' => __('Nom de la :model', ['model' => Str::singular(__('models.classes.brands'))]), + 'hasWordCounter' => true, + 'name' => 'name', + 'required' => true, + 'model' => $brandModel ?? null, + 'helper' => __('Doit décrire précisément le produit (3 caractères minimum, 255 maximum).'), + ]) +
+
+
+
+
diff --git a/resources/views/brands/index-list.blade.php b/resources/views/brands/index-list.blade.php new file mode 100644 index 0000000..53d732f --- /dev/null +++ b/resources/views/brands/index-list.blade.php @@ -0,0 +1,60 @@ +
+ + + @php + $rst = !is_null(request()->rst); + $routeName = request()->route()->getName(); + $noOrder = Session::get("$routeName.sort_col") !== 'status' and (Session::has("$routeName.sort_col") or Session::has("$routeName.sort_way")); + $cols = [ + 'name' => __('Nom'), + 'status' => __('Nombre de comptes'), + 'optin' => __('Nombre d\'optins'), + 'updated_at' => __('Date d\'ajout en base') + ]; + @endphp + @include('modules.table-col-sorter', ['cols' => $cols, 'mobileHide' => ['status'], 'ignore' => ['status', 'optin', 'updated_at']]) + + + @if($brandModels->count()) + @foreach ($brandModels as $brandModel) + + + + + @php $routeName = request()->route()->getName(); @endphp + @if(empty(request()->search) and empty($noOrder) and Session::get("$routeName.sort_col") === 'order') + + @endif + + + + @endforeach + @else + + + + @endif + +
{{ $brandModel->name }}{{ count(\App\Models\CustomerInfo::where('brand_id', 'LIKE', $brandModel->id)->get()) }}{{ count(\App\Models\CustomerInfo::where('brand_id', 'LIKE', $brandModel->id)->where('opt_in', true)->get()) }} + + + + {{ $brandModel->updated_at->formatLocalized('%d/%m/%Y à %H:%M') }} + + + + +
+ {{ __('Aucun résultat pour les :models', ['models' => __('models.classes.brands')]) }} +
+
diff --git a/resources/views/brands/index.blade.php b/resources/views/brands/index.blade.php new file mode 100644 index 0000000..0e7afcc --- /dev/null +++ b/resources/views/brands/index.blade.php @@ -0,0 +1,37 @@ +@extends('layouts.app') + +@section('title', sprintf('%s - %s', __('models.classes.brands'), Str::ucfirst(__('liste')))) +@section('description', __('Listage de toutes les :model', ['model' => __('models.classes.brands')])) +@section('metaIndex', 'noindex,nofollow') + +@section('content') +
+
+
+
+
+
+ @include('breadcrumbs.breadcrumb-body') +
+
+
+ + + +
+
+
+
+
+ @include('modules.search-bar') +
+
+ @include('brands.index-list') +
+
+ {!! $brandModels->links() !!} +
+
+
+
+@endsection diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index df4914b..7841cc4 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -10,10 +10,21 @@
@if(auth()->check()) + @include('layouts.backend.sidenav', ['modelsClasses' => [ + \App\Models\Brand::class, + \App\Models\Question::class, + \App\Models\Service::class, + \App\Models\SingleOptin::class + ]])
@else
@endif +
+
+ @include('modules.flash-messages') +
+
@yield('content')
diff --git a/resources/views/modules/flash-messages.blade.php b/resources/views/modules/flash-messages.blade.php index 6a499dc..b6da218 100644 --- a/resources/views/modules/flash-messages.blade.php +++ b/resources/views/modules/flash-messages.blade.php @@ -1,39 +1,36 @@ @if(!request()->routeIs('login')) @if ($message = Session::get('success', Session::get('bo.success'))) -