diff --git a/app/Http/Controllers/Bo/SeasonalSectionController.php b/app/Http/Controllers/Bo/SeasonalSectionController.php new file mode 100644 index 0000000..66d1240 --- /dev/null +++ b/app/Http/Controllers/Bo/SeasonalSectionController.php @@ -0,0 +1,167 @@ +authorizeResource(SeasonalSection::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 $seasonalSectionModels */ + $seasonalSectionModels = SeasonalSection::query(); + + /** @var string $search Search field */ + $search = $request->search; + if ($search) { + $this->searchQuery( + $seasonalSectionModels, + $search, + null, + 'title', + 'text', + ); + } + $searchFields = \implode(', ', [ + Str::ucfirst(trans('validation.attributes.title')), + trans('validation.attributes.text'), + ]); + + /** Sort columns with a query */ + $this->sortQuery($seasonalSectionModels); + + /** Custom pagination */ + $seasonalSectionModels = $this->paginate($seasonalSectionModels); + + return view('bo.pages.seasonal_sections.index', compact('seasonalSectionModels', 'search', 'searchFields')); + } + + + /** + * Show the form for creating a new resource. + * + * @param \App\Models\SeasonalSection $seasonalSection + * @return \Illuminate\Contracts\View\View + */ + public function create(SeasonalSection $seasonalSection): \Illuminate\Contracts\View\View + { + return view('bo.pages.seasonal_sections.create', ['seasonalSectionModel' => $seasonalSection]); + } + + /** + * Store a newly created resource in storage. + * + * @param \App\Http\Requests\Bo\SeasonalSections\StoreSeasonalSectionRequest $request + * @return \Illuminate\Http\RedirectResponse + */ + public function store(StoreSeasonalSectionRequest $request): \Illuminate\Http\RedirectResponse + { + return DB::transaction(function () use ($request) { + $seasonalSection = new SeasonalSection(); + $seasonalSection->fill($request->validated()); + + if ($seasonalSection->saveOrFail()) { + return redirect()->route('bo.seasonalsections.edit', $seasonalSection) + ->with('success', __('crud.messages.has_been_created', [ + 'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1)) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_created', [ + 'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1)) + ])); + }); + } + + /** + * Show the form for editing the specified resource. + * + * @param App\Models\SeasonalSection $seasonalSection + * @return \Illuminate\Contracts\View\View + */ + public function edit(SeasonalSection $seasonalSection): \Illuminate\Contracts\View\View + { + return view('bo.pages.seasonal_sections.edit', ['seasonalSectionModel' => $seasonalSection]); + } + + /** + * Update the specified resource in storage. + * + * @param \App\Http\Requests\Bo\SeasonalSections\UpdateSeasonalSectionRequest $request + * @param \App\Models\SeasonalSection $seasonalSection + * @return \Illuminate\Http\RedirectResponse + */ + public function update( + UpdateSeasonalSectionRequest $request, + SeasonalSection $seasonalSection + ): \Illuminate\Http\RedirectResponse { + return DB::transaction(function () use ($request, $seasonalSection) { + $seasonalSection->fill($request->validated()); + + if ($seasonalSection->saveOrFail()) { + return redirect()->route('bo.seasonalsections.edit', $seasonalSection) + ->with('success', __('crud.messages.has_been_updated', [ + 'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1)) + ])); + } + return redirect()->route('bo.seasonalsections.edit', $seasonalSection) + ->with('error', __('crud.messages.cannot_be_updated', [ + 'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1)) + ])); + }); + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\SeasonalSection $seasonalSection + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy(SeasonalSection $seasonalSection): \Illuminate\Http\RedirectResponse + { + if ($seasonalSection->deleteOrFail()) { + return redirect()->route('bo.seasonalsections.index') + ->with('success', __('crud.messages.has_been_deleted', [ + 'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1)) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_deleted', [ + 'model' => Str::ucfirst(trans_choice('models.classes.seasonal_section', 1)) + ])); + } + + /** + * Duplicate the specified resource. + * + * @param \App\Models\SeasonalSection $seasonalSection + * @return \Illuminate\Contracts\View\View + */ + public function duplicate(SeasonalSection $seasonalSection): \Illuminate\Contracts\View\View + { + return $this->create($seasonalSection->replicate()); + } +} diff --git a/app/Http/Requests/Bo/SeasonalSections/StoreSeasonalSectionRequest.php b/app/Http/Requests/Bo/SeasonalSections/StoreSeasonalSectionRequest.php new file mode 100644 index 0000000..7c46cba --- /dev/null +++ b/app/Http/Requests/Bo/SeasonalSections/StoreSeasonalSectionRequest.php @@ -0,0 +1,80 @@ +check(); + } + + /** + * Prepare the data for validation. + * + * @return void + */ + protected function prepareForValidation() + { + $this->merge([ + 'published' => $this->published ? true : false + ]); + $this->mergePicture('picture'); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + $rules = [ + 'title' => 'required|string|between:3,100', + 'text' => 'required|string|min:15', + 'picture_alt' => 'required|string|between:3,255', + 'picture_title' => 'required|string|between:3,255', + 'background' => ['required', new Enum(BackgroundEnum::class)], + 'published' => 'required|boolean', + 'publish_at' => 'nullable|date', + 'unpublish_at' => 'nullable|date|after:publish_at', + + ]; + return \array_merge( + $rules, + $this->pictureRules('picture', 362, 542, 362, 542, false), + ); + } + + /** + * Get custom attributes for validator errors. + * + * @return array + */ + public function attributes(): array + { + return [ + 'title' => trans('validation.attributes.title'), + 'text' => trans('validation.attributes.content'), + 'picture_alt' => trans('validation.custom.picture_alt'), + 'picture_title' => trans('validation.custom.picture_title'), + 'background' => trans('validation.custom.background'), + 'published' => trans('validation.custom.activated'), + 'publish_at' => trans('validation.custom.publish_at'), + 'unpublish_at' => trans('validation.custom.unpublish_at'), + + ]; + } +} diff --git a/app/Http/Requests/Bo/SeasonalSections/UpdateSeasonalSectionRequest.php b/app/Http/Requests/Bo/SeasonalSections/UpdateSeasonalSectionRequest.php new file mode 100644 index 0000000..a8fc285 --- /dev/null +++ b/app/Http/Requests/Bo/SeasonalSections/UpdateSeasonalSectionRequest.php @@ -0,0 +1,7 @@ + fake()->sentence(), - 'text' => fake()->text(), + 'text' => \clean(\fake()->randomHtml(6, 7)), 'picture_alt' => fake()->sentence(), 'picture_title' => fake()->sentence(), 'published' => false, diff --git a/database/seeders/SeasonalSectionSeeder.php b/database/seeders/SeasonalSectionSeeder.php index 09f0f73..6a89aff 100644 --- a/database/seeders/SeasonalSectionSeeder.php +++ b/database/seeders/SeasonalSectionSeeder.php @@ -20,15 +20,21 @@ class SeasonalSectionSeeder extends Seeder $publish_at = now()->subDays(rand(1, 10)); $unpublish_at = $publish_at->copy()->addDays(rand(10, 20)); foreach (BackgroundEnum::cases() as $i => $background) { - SeasonalSection::factory(1)->create([ - 'background' => $background, - 'published' => $i === array_key_first(BackgroundEnum::cases()) ? true : false, - 'publish_at' => $publish_at->subMonths(), - 'unpublish_at' => $unpublish_at->subMonths(), - 'picture' => FileStorageHelper::storeFile(new SeasonalSection(), new \SplFileInfo( - \resource_path('../database/factories/assets/seasonalsections/default-picture-362x542.png') - )), - ]); + SeasonalSection::factory(1)->make()->each( + function (SeasonalSection $seasonalSection) use ($background, $i, $publish_at, $unpublish_at) { + $seasonalSection->background = $background; + $seasonalSection->published = $i === array_key_first(BackgroundEnum::cases()) ? true : false; + $seasonalSection->publish_at = $publish_at->subMonths(); + $seasonalSection->unpublish_at = $unpublish_at->subMonths(); + $seasonalSection->picture = FileStorageHelper::storeFile( + new SeasonalSection(), + new \SplFileInfo( + \resource_path('../database/factories/assets/seasonalsections/default-picture-362x542.png') + ) + ); + $seasonalSection->save(); + } + ); } } } diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index 2a77847..f9ad2e7 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -237,6 +237,6 @@ return [ 'attribute_title' => 'title attribute', 'picture_productsheet' => 'productsheet picture', 'picture_thumbnail' => 'thumbnail', - + 'background' => 'background', ], ]; diff --git a/resources/lang/fr/models.php b/resources/lang/fr/models.php index fa7a7a4..9f3d9a9 100644 --- a/resources/lang/fr/models.php +++ b/resources/lang/fr/models.php @@ -2,9 +2,10 @@ return [ 'classes' => [ - 'user' => 'utilisateur', - 'activity_log' => 'journal d\'activité|journaux d\'activité', - 'static_page' => 'page statique|pages statiques', - 'range' => 'gamme', + 'user' => 'utilisateur', + 'activity_log' => 'journal d\'activité|journaux d\'activité', + 'static_page' => 'page statique|pages statiques', + 'range' => 'gamme', + 'seasonal_section' => 'section saisonnière|sections saisonnières', ] ]; diff --git a/resources/lang/fr/validation.php b/resources/lang/fr/validation.php index 9d94bff..979b969 100644 --- a/resources/lang/fr/validation.php +++ b/resources/lang/fr/validation.php @@ -237,6 +237,6 @@ return [ 'attribute_title' => 'attribut title', 'picture_productsheet' => 'image de la fiche produit', 'picture_thumbnail' => 'vignette', - + 'background' => 'fond', ], ]; diff --git a/resources/views/bo/layouts/nav-list.blade.php b/resources/views/bo/layouts/nav-list.blade.php index 77b8032..48b0834 100644 --- a/resources/views/bo/layouts/nav-list.blade.php +++ b/resources/views/bo/layouts/nav-list.blade.php @@ -24,6 +24,15 @@ @endcan + @can('viewAny', \App\Models\SeasonalSection::class) +
  • + + {{ Str::title(trans_choice('models.classes.seasonal_section', '*')) }} + +
  • + @endcan @canAny(['isConceptor', 'viewAny'], \App\Models\User::class)