diff --git a/app/Http/Controllers/Bo/TestimonialController.php b/app/Http/Controllers/Bo/TestimonialController.php new file mode 100644 index 0000000..80bbcbe --- /dev/null +++ b/app/Http/Controllers/Bo/TestimonialController.php @@ -0,0 +1,165 @@ +authorizeResource(Testimonial::class); + } + + /** + * 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 + { + /** @var \Illuminate\Database\Eloquent\Builder $testimonialModels */ + $testimonialModels = Testimonial::query(); + + /** @var string $search Search field */ + $search = $request->search; + if ($search) { + $this->searchQuery( + $testimonialModels, + $search, + null, + 'name', + 'job', + 'content', + 'type', + ); + } + $searchFields = \implode(', ', [ + Str::ucfirst(trans('validation.attributes.name')), + trans('validation.custom.job'), + trans('validation.attributes.content'), + trans('validation.custom.type'), + ]); + + /** Sort columns with a query */ + $this->sortQuery($testimonialModels); + + /** Custom pagination */ + $testimonialModels = $this->paginate($testimonialModels); + + return view('bo.pages.testimonials.index', compact('testimonialModels', 'search', 'searchFields')); + } + + + /** + * Show the form for creating a new resource. + * + * @param \App\Models\Testimonial $testimonial + * @return \Illuminate\Contracts\View\View + */ + public function create(Testimonial $testimonial): \Illuminate\Contracts\View\View + { + return view('bo.pages.testimonials.create', ['testimonialModel' => $testimonial]); + } + + /** + * Store a newly created resource in storage. + * + * @param \App\Http\Requests\Bo\Testimonials\StoreTestimonialRequest $request + * @return \Illuminate\Http\RedirectResponse + */ + public function store(StoreTestimonialRequest $request): \Illuminate\Http\RedirectResponse + { + return DB::transaction(function () use ($request) { + $testimonial = new Testimonial(); + + if ($testimonial->fill($request->validated())->saveOrFail()) { + return redirect()->route('bo.testimonials.edit', $testimonial) + ->with('success', __('crud.messages.has_been_created', [ + 'model' => Str::ucfirst(__('models.classes.testimonial')) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_created', [ + 'model' => Str::ucfirst(__('models.classes.testimonial')) + ])); + }); + } + + /** + * Show the form for editing the specified resource. + * + * @param App\Models\Testimonial $testimonial + * @return \Illuminate\Contracts\View\View + */ + public function edit(Testimonial $testimonial): \Illuminate\Contracts\View\View + { + return view('bo.pages.testimonials.edit', ['testimonialModel' => $testimonial]); + } + + /** + * Update the specified resource in storage. + * + * @param \App\Http\Requests\Bo\Testimonials\UpdateTestimonialRequest $request + * @param \App\Models\Testimonial $testimonial + * @return \Illuminate\Http\RedirectResponse + */ + public function update( + UpdateTestimonialRequest $request, + Testimonial $testimonial + ): \Illuminate\Http\RedirectResponse { + return DB::transaction(function () use ($request, $testimonial) { + if ($testimonial->fill($request->validated())->saveOrFail()) { + return redirect()->route('bo.testimonials.edit', $testimonial) + ->with('success', __('crud.messages.has_been_updated', [ + 'model' => Str::ucfirst(__('models.classes.testimonial')) + ])); + } + return redirect()->route('bo.testimonials.edit', $testimonial) + ->with('error', __('crud.messages.cannot_be_updated', [ + 'model' => Str::ucfirst(__('models.classes.testimonial')) + ])); + }); + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\Testimonial $testimonial + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy(Testimonial $testimonial): \Illuminate\Http\RedirectResponse + { + if ($testimonial->deleteOrFail()) { + return redirect()->route('bo.testimonials.index') + ->with('success', __('crud.messages.has_been_deleted', [ + 'model' => Str::ucfirst(__('models.classes.testimonial')) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_deleted', [ + 'model' => Str::ucfirst(__('models.classes.testimonial')) + ])); + } + + /** + * Duplicate the specified resource. + * + * @param \App\Models\Testimonial $testimonial + * @return \Illuminate\Contracts\View\View + */ + public function duplicate(Testimonial $testimonial): \Illuminate\Contracts\View\View + { + return $this->create($testimonial->replicate()); + } +} diff --git a/app/Http/Requests/Bo/Testimonials/StoreTestimonialRequest.php b/app/Http/Requests/Bo/Testimonials/StoreTestimonialRequest.php new file mode 100644 index 0000000..2f97eb2 --- /dev/null +++ b/app/Http/Requests/Bo/Testimonials/StoreTestimonialRequest.php @@ -0,0 +1,82 @@ +check(); + } + + /** + * Prepare the data for validation. + * + * @return void + */ + protected function prepareForValidation() + { + $this->mergePicture('picture'); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + $rules = [ + 'type' => [ + 'required', + new Enum(\App\Enums\Testimonials\TypeEnum::class) + ], + 'name' => 'required|string|between:3,255', + 'job' => 'required|string|between:3,255', + 'content' => 'required|string|min:3', + 'picture_alt' => 'required|string|between:3,255', + 'picture_title' => 'required|string|between:3,255', + ]; + return \array_merge( + $rules, + // FIXME: Mettre à jour les tailles. + $this->pictureRules('picture', 100, 100, 100, 100, null, false), + ); + } + + /** + * Get custom attributes for validator errors. + * + * @return array + */ + public function attributes(): array + { + return [ + 'type' => __('validation.custom.type'), + 'name' => __('validation.attributes.name'), + 'job' => __('validation.custom.job'), + 'content' => __('validation.attributes.content'), + 'picture_alt' => __(':field :inter:model', [ + 'field' => __('validation.custom.alt_attribute'), + 'inter' => __('validation.custom.of_the.vowel'), + 'model' => __('validation.custom.picture'), + ]), + 'picture_title' => __(':field :inter:model', [ + 'field' => __('validation.custom.title_attribute'), + 'inter' => __('validation.custom.of_the.vowel'), + 'model' => __('validation.custom.picture'), + ]), + ]; + } +} diff --git a/app/Http/Requests/Bo/Testimonials/UpdateTestimonialRequest.php b/app/Http/Requests/Bo/Testimonials/UpdateTestimonialRequest.php new file mode 100644 index 0000000..d622d90 --- /dev/null +++ b/app/Http/Requests/Bo/Testimonials/UpdateTestimonialRequest.php @@ -0,0 +1,7 @@ + 'slide', 'job_video' => 'vidéo métier|vidéos métiers', 'strip' => 'bandeau d\'information|bandeaux d\'information', + 'testimonial' => 'témoignage', ] ]; diff --git a/resources/lang/fr/validation.php b/resources/lang/fr/validation.php index 204895b..01103a3 100644 --- a/resources/lang/fr/validation.php +++ b/resources/lang/fr/validation.php @@ -271,5 +271,8 @@ return [ 'youtube_video' => 'vidéo YouTube', 'slider' => 'slider', 'place' => 'lieu', + 'job' => 'métier', + 'type' => 'type', + 'person' => 'personne', ], ]; diff --git a/resources/views/bo/layouts/nav-list.blade.php b/resources/views/bo/layouts/nav-list.blade.php index cc7891e..e08968e 100644 --- a/resources/views/bo/layouts/nav-list.blade.php +++ b/resources/views/bo/layouts/nav-list.blade.php @@ -93,6 +93,15 @@ @endcan + @can('viewAny', \App\Models\Testimonial::class) + + @endcan @can('viewAny', \App\Models\JobVideo::class)