From 36494a79446d5c0ec7f8338e822b367347eb7fe8 Mon Sep 17 00:00:00 2001 From: Alexis Gousseau Date: Thu, 7 Aug 2025 16:07:58 +0200 Subject: [PATCH] feat: :sparkles: add brand fields (logo/domain/url/url_claim) --- .../Requests/Bo/Brands/StoreBrandRequest.php | 30 +++++- .../Requests/Bo/Brands/UpdateBrandRequest.php | 30 ++++++ app/Models/Brand.php | 15 ++- database/factories/BrandFactory.php | 4 +- ..._171943_add_unique_key_on_brands_table.php | 52 +++++++++++ resources/lang/en/validation.php | 4 + resources/lang/fr/validation.php | 4 + .../views/bo/pages/brands/create.blade.php | 2 +- .../views/bo/pages/brands/edit.blade.php | 2 +- .../views/bo/pages/brands/form.blade.php | 93 ++++++++++++++++++- .../partials/inputs/varchar-picture.blade.php | 25 ++--- 11 files changed, 238 insertions(+), 23 deletions(-) create mode 100644 database/migrations/2025_08_07_171943_add_unique_key_on_brands_table.php diff --git a/app/Http/Requests/Bo/Brands/StoreBrandRequest.php b/app/Http/Requests/Bo/Brands/StoreBrandRequest.php index 74af912..b1b81c0 100644 --- a/app/Http/Requests/Bo/Brands/StoreBrandRequest.php +++ b/app/Http/Requests/Bo/Brands/StoreBrandRequest.php @@ -3,9 +3,12 @@ namespace App\Http\Requests\Bo\Brands; use Illuminate\Foundation\Http\FormRequest; +use Uwil\LaravelCommon\Traits\Requests\HasPicture; class StoreBrandRequest extends FormRequest { + use HasPicture; + /** * Determine if the user is authorized to make this request. * @@ -16,6 +19,16 @@ class StoreBrandRequest extends FormRequest return auth('backend')->check(); } + /** + * Prepare the data for validation. + * + * @return void + */ + protected function prepareForValidation(): void + { + $this->mergePicture('logo'); + } + /** * Get the validation rules that apply to the request. * @@ -23,9 +36,16 @@ class StoreBrandRequest extends FormRequest */ public function rules(): array { - return [ - 'name' => 'required|string|min:5|max:255', + $rules = [ + 'name' => 'required|unique:brands,name|string|min:5|max:255', + 'url' => 'required|url|min:5|max:255', + 'url_claim' => 'required|url|min:5|max:255', + 'domain' => 'required|unique:brands,domain|string|min:5|max:255', ]; + return \array_merge( + $rules, + $this->pictureRules('logo', 550, 550, 550, 550), + ); } /** @@ -36,7 +56,11 @@ class StoreBrandRequest extends FormRequest public function attributes(): array { return [ - 'name' => trans('Nom de la marque'), + 'name' => trans('validation.attributes.name'), + 'logo' => trans('validation.custom.logo'), + 'url' => trans('validation.custom.url'), + 'url_claim' => trans('validation.custom.url_claim'), + 'domain' => trans('validation.custom.domain'), ]; } } diff --git a/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php b/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php index 976fef4..0871ed5 100644 --- a/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php +++ b/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php @@ -4,4 +4,34 @@ namespace App\Http\Requests\Bo\Brands; class UpdateBrandRequest extends StoreBrandRequest { + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + /** @var integer $brandModelKey */ + $brandModelKey = request()->route()->parameter('brand')->getKey(); + $rules = [ + 'name' => [ + 'required', + "unique:brands,name,$brandModelKey", + 'string', + 'min:5', + 'max:255', + ], + 'domain' => [ + 'required', + "unique:brands,domain,$brandModelKey", + 'string', + 'min:5', + 'max:255', + ], + ]; + return \array_merge( + parent::rules(), + $rules, + ); + } } diff --git a/app/Models/Brand.php b/app/Models/Brand.php index 5a41323..64267f7 100644 --- a/app/Models/Brand.php +++ b/app/Models/Brand.php @@ -7,6 +7,7 @@ use App\Traits\Models\ActivityLog; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; +use Uwil\LaravelCommon\Helpers\FileHelper; /** * @property integer $id Brand id. @@ -54,14 +55,22 @@ class Brand extends Model */ protected static function booted(): void { + static::creating(function (self $model) { + $model->logo = FileHelper::storeFile($model, $model->logo, true); + }); static::created(function () { self::resetCache(); }); - static::updated(function () { - self::resetCache(); + static::updating(function (self $model) { + $model->logo = FileHelper::storeFile($model, $model->logo, true); }); - static::deleted(function () { + static::updated(function (self $model) { self::resetCache(); + FileHelper::removeOldFile($model, 'logo'); + }); + static::deleted(function (self $model) { + self::resetCache(); + FileHelper::removeOldFile($model, 'logo'); }); } diff --git a/database/factories/BrandFactory.php b/database/factories/BrandFactory.php index b005079..f77185b 100644 --- a/database/factories/BrandFactory.php +++ b/database/factories/BrandFactory.php @@ -22,10 +22,10 @@ class BrandFactory extends Factory public function definition(): array { return [ - 'name' => $this->faker->company, + 'name' => $this->faker->unique()->company, 'url' => $this->faker->url, 'url_claim' => $this->faker->url, - 'domain' => $this->faker->domainName, + 'domain' => $this->faker->unique()->domainName, ]; } } diff --git a/database/migrations/2025_08_07_171943_add_unique_key_on_brands_table.php b/database/migrations/2025_08_07_171943_add_unique_key_on_brands_table.php new file mode 100644 index 0000000..0046b18 --- /dev/null +++ b/database/migrations/2025_08_07_171943_add_unique_key_on_brands_table.php @@ -0,0 +1,52 @@ +get()->map(function (Brand $brandModel) { + $brandModel->logo = FileHelper::storeFile( + $brandModel, + new \SplFileInfo( + \resource_path('../database/factories/assets/brands/default-logo-550x550.jpg') + ) + ); + $urlExample = "https://example-{$brandModel->getKey()}.com"; + $brandModel->url = $urlExample; + $brandModel->url_claim = $urlExample; + $brandModel->domain = parse_url($urlExample, PHP_URL_HOST); + $brandModel->saveOrFail(); + }); + + // Set unique keys. + Schema::table('brands', function (Blueprint $table) { + $table->unique('name', 'brands_name_unique'); + $table->unique('domain', 'brands_domain_unique'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('brands', function (Blueprint $table) { + $table->dropUnique('brands_name_unique'); + $table->dropUnique('brands_domain_unique'); + }); + } +}; diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index d9ca963..60a633d 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -233,5 +233,9 @@ return [ 'type' => 'type', 'status' => 'status', 'label' => 'label', + 'domain' => 'domain', + 'logo' => 'logo', + 'url' => 'url', + 'url_claim' => 'claim url', ], ]; diff --git a/resources/lang/fr/validation.php b/resources/lang/fr/validation.php index 5f9da36..5138f0c 100644 --- a/resources/lang/fr/validation.php +++ b/resources/lang/fr/validation.php @@ -233,5 +233,9 @@ return [ 'type' => 'type', 'status' => 'status', 'label' => 'label', + 'domain' => 'domaine', + 'logo' => 'logo', + 'url' => 'lien', + 'url_claim' => 'lien réclamation', ], ]; diff --git a/resources/views/bo/pages/brands/create.blade.php b/resources/views/bo/pages/brands/create.blade.php index 29f92cc..0b6cb2c 100644 --- a/resources/views/bo/pages/brands/create.blade.php +++ b/resources/views/bo/pages/brands/create.blade.php @@ -29,7 +29,7 @@
-
+ @csrf @include('bo.pages.brands.form')
diff --git a/resources/views/bo/pages/brands/edit.blade.php b/resources/views/bo/pages/brands/edit.blade.php index 2a20d0c..16f1457 100644 --- a/resources/views/bo/pages/brands/edit.blade.php +++ b/resources/views/bo/pages/brands/edit.blade.php @@ -29,7 +29,7 @@
- + @csrf @method('PUT') @include('bo.pages.brands.form') diff --git a/resources/views/bo/pages/brands/form.blade.php b/resources/views/bo/pages/brands/form.blade.php index 1200811..8233c38 100644 --- a/resources/views/bo/pages/brands/form.blade.php +++ b/resources/views/bo/pages/brands/form.blade.php @@ -2,22 +2,111 @@ {{-- TITLE --}}
- {{ __('Nom') }} + {{ str(__('validation.attributes.name'))->ucfirst() }}
@include('bo.partials.inputs.varchar', [ 'id' => 'brandName', - 'label' => __('Nom de la :model', ['model' => Str::singular(__('models.classes.brands'))]), + 'label' => __('Nom de la :model', ['model' => str(__('models.classes.brands'))->singular()]), 'hasWordCounter' => true, 'name' => 'name', 'required' => true, 'model' => $brandModel ?? null, + 'helper' => __('Doit décrire précisément la :model (3 caractères minimum, 255 maximum).', [ + 'model' => str(__('models.classes.brands'))->singular(), + ]), + ]) +
+
+
+
+ {{-- DOMAIN --}} +
+
+ {{ str(__('validation.custom.domain'))->ucfirst() }} +
+
+ @include('bo.partials.inputs.varchar', [ + 'id' => 'brandDomain', + 'label' => __(':field de la :model', [ + 'field' => str(__('validation.custom.domain'))->ucfirst(), + 'model' => str(__('models.classes.brands'))->singular(), + ]), + 'hasWordCounter' => true, + 'name' => 'domain', + 'required' => true, + 'model' => $brandModel ?? null, + 'placeholder' => parse_url('https://example.com/', PHP_URL_HOST), 'helper' => __('Doit décrire précisément le produit (3 caractères minimum, 255 maximum).'), ])
+ {{-- URL --}} +
+
+ {{ str(__('validation.custom.url'))->ucfirst() }} +
+
+ @include('bo.partials.inputs.varchar', [ + 'id' => 'brandUrl', + 'label' => __(':field de la :model', [ + 'field' => str(__('validation.custom.url'))->ucfirst(), + 'model' => str(__('models.classes.brands'))->singular(), + ]), + 'hasWordCounter' => true, + 'name' => 'url', + 'required' => true, + 'model' => $brandModel ?? null, + 'placeholder' => 'https://example.com/', + 'helper' => __('Doit décrire précisément le produit (3 caractères minimum, 255 maximum).'), + ]) +
+
+
+
+ {{-- URL_CLAIM --}} +
+
+ {{ str(__('validation.custom.url_claim'))->ucfirst() }} +
+
+ @include('bo.partials.inputs.varchar', [ + 'id' => 'brandUrlClaim', + 'label' => __(':field de la :model', [ + 'field' => str(__('validation.custom.url_claim'))->ucfirst(), + 'model' => str(__('models.classes.brands'))->singular(), + ]), + 'hasWordCounter' => true, + 'name' => 'url_claim', + 'required' => true, + 'model' => $brandModel ?? null, + 'placeholder' => 'https://example.com/', + 'helper' => __('Doit décrire précisément le produit (3 caractères minimum, 255 maximum).'), + ]) +
+
+
+
+
+
+ {{ str(__('validation.custom.url_claim'))->ucfirst() }} +
+
+ @include('bo.partials.inputs.varchar-picture', [ + 'id' => 'brandLogo', + 'name' => 'logo', + 'width' => 550, + 'height' => 550, + 'model' => $brandModel ?? null, + 'required' => true, + 'preview' => true, + ]) +
+
+
+
@push('scripts') diff --git a/resources/views/bo/partials/inputs/varchar-picture.blade.php b/resources/views/bo/partials/inputs/varchar-picture.blade.php index c41f82b..c9f3db6 100644 --- a/resources/views/bo/partials/inputs/varchar-picture.blade.php +++ b/resources/views/bo/partials/inputs/varchar-picture.blade.php @@ -15,17 +15,20 @@ --}} @php -$data = [ - 'id' => $id, - 'name' => $name, - 'helper' => $helper, - 'width' => $width, - 'height' => $height, - 'value' => $model->{$name} ?? $value ?? '', - 'preview' => $preview ?? false, - 'browseEvent' => $browseEvent ?? false, - 'required' => $required ?? false, -]; + $data = [ + 'id' => $id, + 'name' => $name, + 'helper' => __('crud.minicrud.dimensions_must_be', [ + 'widthpx' => "{$width}px", + 'heightpx' => "{$height}px", + ]), + 'width' => $width, + 'height' => $height, + 'value' => $model->{$name} ?? ($value ?? ''), + 'preview' => $preview ?? false, + 'browseEvent' => $browseEvent ?? false, + 'required' => $required ?? false, + ]; @endphp
@include('modules.input-error', ['inputName' => $name, 'helper' => null])