diff --git a/app/Http/Controllers/Bo/CodesEANController.php b/app/Http/Controllers/Bo/CodesEANController.php index 9cb79e9..11c14cd 100644 --- a/app/Http/Controllers/Bo/CodesEANController.php +++ b/app/Http/Controllers/Bo/CodesEANController.php @@ -3,47 +3,157 @@ namespace App\Http\Controllers\Bo; use App\Http\Controllers\Controller; +use App\Http\Requests\Bo\CodesEan\StoreCodeEanRequest; +use App\Http\Requests\Bo\CodesEan\UpdateCodeEanRequest; use App\Models\CodeEan; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Str; class CodesEANController extends Controller { - public function index() + /** + * Display a listing of the resource. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Contracts\View\View + */ + public function index(Request $request): \Illuminate\Contracts\View\View { - $ean = CodeEan::all(); - return view('bo.codes-ean.index', compact('ean')); - } + /** @var \Illuminate\Database\Eloquent\Builder $codeEanModels */ + $codeEanModels = CodeEan::query(); - public function saveDatas(Request $request) - { - $codes = $request->codes; - - for ($i = 0; $i < count($codes); $i++) { - $codeToInsert = !empty($codes[$i]["code"]) && !empty($codes[$i]["product"]); - if (isset($codes[$i]["id"]) && $codes[$i]["id"] !== null && $codeToInsert) { - $c = CodeEan::findOrFail($codes[$i]["id"]); - $c->code = $codes[$i]["code"]; - $c->product = $codes[$i]["product"]; - $c->save(); - } else { - if ($codeToInsert) { - $c = CodeEan::updateOrCreate(['code' => $codes[$i]["code"], 'product' => $codes[$i]["product"]]); - $codes[$i]["id"] = $c->id; - } else { - unset($codes[$i]); - $i--; - } - } + /** @var string $search Search field */ + $search = $request->search; + if ($search) { + $this->searchQuery( + $codeEanModels, + $search, + null, + 'code', + 'product' + ); } + $searchFields = \sprintf( + '%s, %s', + Str::ucfirst(__('validation.attributes.code')), + Str::ucfirst(__('validation.attributes.product')) + ); - return $codes; + /** Sort columns with a query */ + $this->sortQuery($codeEanModels); + + /** Custom pagination */ + $codeEanModels = $this->customPaginate($codeEanModels, $request->pagination); + + return view('bo.pages.codes-ean.index', compact('codeEanModels', 'search', 'searchFields')); } - public function destroy(Request $request) + /** + * Show the form for creating a new resource. + * + * @param \App\Models\CodeEan $codeEan + * @return \Illuminate\Contracts\View\View + */ + public function create(CodeEan $codeEan): \Illuminate\Contracts\View\View { - $code = CodeEan::findOrFail($request->id); - $code->delete(); + return view('bo.pages.codes-ean.create', [ + 'codeEanModel' => $codeEan, + ]); + } - return "Suppression terminée !"; + /** + * Store a newly created resource in storage. + * + * @param \App\Http\Requests\Bo\CodesEan\StoreCodeEanRequest $request + * @return \Illuminate\Http\RedirectResponse + */ + public function store(StoreCodeEanRequest $request): \Illuminate\Http\RedirectResponse + { + return DB::transaction(function () use ($request) { + $codeEan = new CodeEan(); + $codeEan->fill($request->validated()); + if ($codeEan->saveOrFail()) { + return redirect() + ->route('bo.code-eans.edit', $codeEan) + ->with('success', trans( + 'crud.messages.has_been_created', + ['model' => \trans_choice('models.classes.codes-ean', 1)] + )); + } + return redirect() + ->back() + ->with('error', trans( + 'crud.messages.cannot_be_created', + ['model' => \trans_choice('models.classes.codes-ean', 1)] + )); + }); + } + + /** + * Show the form for editing the specified resource. + * + * @param \App\Models\CodeEan $codeEan + * @return \Illuminate\Contracts\View\View + */ + public function edit(CodeEan $codeEan): \Illuminate\Contracts\View\View + { + return view('bo.pages.codes-ean.edit', [ + 'codeEanModel' => $codeEan, + ]); + } + + /** + * Update the specified resource in storage. + * + * @param \App\Http\Requests\Bo\CodesEan\UpdateCodeEanRequest $request + * @param \App\Models\CodeEan $codeEan + * @return \Illuminate\Http\RedirectResponse + */ + public function update(UpdateCodeEanRequest $request, CodeEan $codeEan): \Illuminate\Http\RedirectResponse + { + return DB::transaction(function () use ($request, $codeEan) { + $codeEan->fill($request->validated()); + if ($codeEan->saveOrFail()) { + return redirect() + ->route('bo.code-eans.edit', $codeEan) + ->with('success', trans( + 'crud.messages.has_been_updated', + ['model' => trans_choice('models.classes.codes-ean', 1)] + )); + } + return redirect() + ->back() + ->with('error', trans( + 'crud.messages.cannot_be_updated', + ['model' => trans_choice('models.classes.codes-ean', 1)] + )); + }); + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\CodeEan $codeEan + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy(CodeEan $codeEan): \Illuminate\Http\RedirectResponse + { + return DB::transaction(function () use ($codeEan) { + if ($codeEan->deleteOrFail()) { + return redirect() + ->route('bo.code-eans.index') + ->with('success', __( + 'crud.messages.has_been_deleted', + ['model' => trans_choice('models.classes.codes-ean', 1)] + )); + } + return redirect() + ->back() + ->with('error', __( + ':crud.messages.cannot_be_deleted', + ['model' => trans_choice('models.classes.codes-ean', 1)] + )); + }); } } diff --git a/app/Http/Requests/Bo/CodesEan/StoreCodeEanRequest.php b/app/Http/Requests/Bo/CodesEan/StoreCodeEanRequest.php new file mode 100644 index 0000000..c34e88d --- /dev/null +++ b/app/Http/Requests/Bo/CodesEan/StoreCodeEanRequest.php @@ -0,0 +1,49 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'code' => [ + 'required', + 'numeric', + 'unique:code_eans,code', + 'regex:/^[0-9]{7}$|^[0-9]{8}$|^[0-9]{13}$/', + ], + 'product' => 'required|string|min:3|max:255', + ]; + } + + /** + * Get custom attributes for validator errors. + * + * @return array + */ + public function attributes(): array + { + return [ + 'code' => trans('Code EAN'), + 'product' => trans('Produit'), + ]; + } +} diff --git a/app/Http/Requests/Bo/CodesEan/UpdateCodeEanRequest.php b/app/Http/Requests/Bo/CodesEan/UpdateCodeEanRequest.php new file mode 100644 index 0000000..7ef29d4 --- /dev/null +++ b/app/Http/Requests/Bo/CodesEan/UpdateCodeEanRequest.php @@ -0,0 +1,23 @@ + [ + 'required', + 'numeric', + 'unique:code_eans,code,' . $this->route('code_ean')->id . ',id', + 'regex:/^[0-9]{7}$|^[0-9]{8}$|^[0-9]{13}$/', + ], + ]); + } +} diff --git a/database/migrations/2020_10_20_171937_create_code_eans.php b/database/migrations/2020_10_20_171937_create_code_eans.php index 56c2cb6..0890e95 100644 --- a/database/migrations/2020_10_20_171937_create_code_eans.php +++ b/database/migrations/2020_10_20_171937_create_code_eans.php @@ -15,7 +15,7 @@ class CreateCodeEans extends Migration { Schema::create('code_eans', function (Blueprint $table) { $table->id(); - $table->string('code'); + $table->unsignedBigInteger('code'); $table->string('product'); $table->unique('code'); }); diff --git a/resources/lang/fr/models.php b/resources/lang/fr/models.php index a901cd0..3a8e1ce 100644 --- a/resources/lang/fr/models.php +++ b/resources/lang/fr/models.php @@ -3,6 +3,7 @@ return [ 'classes' => [ 'bannedips' => 'IP bannies', + 'codes-ean' => 'Code EAN|Codes EAN', 'contents' => 'Contenus', 'faq-questions' => 'Question pour la FAQ|Questions pour la FAQ', 'faq-themes' => 'Thème pour la FAQ|Thèmes pour la FAQ', diff --git a/resources/lang/fr/validation.php b/resources/lang/fr/validation.php index a971c8f..fca1a97 100644 --- a/resources/lang/fr/validation.php +++ b/resources/lang/fr/validation.php @@ -3,15 +3,15 @@ return [ /** - *-------------------------------------------------------------------------- - * Validation Language Lines - *-------------------------------------------------------------------------- - * - * The following language lines contain the default error messages used by - * the validator class. Some of these rules have multiple versions such - * as the size rules. Feel free to tweak each of these messages. - * - */ + *-------------------------------------------------------------------------- + * Validation Language Lines + *-------------------------------------------------------------------------- + * + * The following language lines contain the default error messages used by + * the validator class. Some of these rules have multiple versions such + * as the size rules. Feel free to tweak each of these messages. + * + */ 'accepted' => 'Le champ :attribute doit être accepté.', 'active_url' => 'Le champ :attribute n\'est pas une URL valide.', @@ -125,6 +125,7 @@ return [ 'url' => 'Le format de l\'URL de :attribute n\'est pas valide.', 'uuid' => 'Le champ :attribute doit être un UUID valide', 'ip' => 'Le champ :attribute doit être une adresse IP valide', + 'ean' => 'Le champ :attribute doit être un code EAN valide (7, 8 ou 13 chiffres)', 'custom' => [ 'attribute-name' => [ 'rule-name' => 'custom-message', @@ -133,15 +134,15 @@ return [ /** - *-------------------------------------------------------------------------- - * Custom Validation Attributes - *-------------------------------------------------------------------------- - * - * The following language lines are used to swap attribute place-holders - * with something more reader friendly such as E-Mail Address instead - * of "email". This simply helps us make messages a little cleaner. - * - */ + *-------------------------------------------------------------------------- + * Custom Validation Attributes + *-------------------------------------------------------------------------- + * + * The following language lines are used to swap attribute place-holders + * with something more reader friendly such as E-Mail Address instead + * of "email". This simply helps us make messages a little cleaner. + * + */ 'attributes' => [ 'name' => 'nom', @@ -176,6 +177,8 @@ return [ 'birth_date' => 'date de naissance', 'ip' => 'IP', + 'code' => 'code', + 'product' => 'produit', 'value' => 'valeur', 'question' => 'question', 'answer' => 'réponse', diff --git a/resources/views/bo/codes-ean/index.blade.php b/resources/views/bo/codes-ean/index.blade.php deleted file mode 100644 index 013a6bf..0000000 --- a/resources/views/bo/codes-ean/index.blade.php +++ /dev/null @@ -1,6 +0,0 @@ -@extends('bo.layout') - -@section('content') - @php $data = ['dbdatas' => $ean]; @endphp -
-@endsection diff --git a/resources/views/bo/layouts/nav-menu.blade.php b/resources/views/bo/layouts/nav-menu.blade.php index 2806890..587f6b0 100644 --- a/resources/views/bo/layouts/nav-menu.blade.php +++ b/resources/views/bo/layouts/nav-menu.blade.php @@ -41,11 +41,11 @@ @endsuperadmin