diff --git a/app/Casts/HtmlColor.php b/app/Casts/HtmlColor.php new file mode 100644 index 0000000..8699a0f --- /dev/null +++ b/app/Casts/HtmlColor.php @@ -0,0 +1,55 @@ + $attributes + * @return mixed + * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed + * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed + */ + public function get(Model $model, string $key, mixed $value, array $attributes): mixed + { + // phpcs:enable + return '#' . $this->zeropad(dechex($value), 6); + } + + /** + * Prepare the given value for storage. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @param mixed|null $value + * @param array $attributes + * @return mixed + * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed + * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed + */ + public function set(Model $model, string $key, mixed $value, array $attributes): mixed + { + // phpcs:enable + return \intval(hexdec(ltrim($value, '#'))); + } + + /** + * Pad with zeros a numeric string + * + * @param string $num + * @param integer $lim + * @return string + */ + private function zeropad(string $num, int $lim): string + { + return (strlen($num) >= $lim) ? $num : $this->zeropad("0{$num}", $lim); + } +} diff --git a/app/Http/Controllers/Bo/BrandController.php b/app/Http/Controllers/Bo/BrandController.php new file mode 100644 index 0000000..da2a577 --- /dev/null +++ b/app/Http/Controllers/Bo/BrandController.php @@ -0,0 +1,149 @@ +authorizeResource(Brand::class); + } + + /** + * Display a listing of the resource. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Contracts\View\View + */ + public function index(Request $request): View + { + /** @var \Illuminate\Database\Eloquent\Builder $query */ + $query = Brand::query(); + + /** @var string $search Search field */ + $search = $request->input('search'); + if ($search) { + $this->searchQuery( + $query, + $search, + null, + 'name', + ); + } + $searchFields = \implode(', ', [ + trans('validation.attributes.name'), + ]); + + /** Sort columns with a query */ + $this->sortQuery($query); + + /** Custom pagination */ + $brandModelList = $this->paginate($query); + + return view('bo.pages.brands.index', compact('brandModelList', 'search', 'searchFields')); + } + + /** + * Show the form for creating a new resource. + * + * @return \Illuminate\Contracts\View\View + */ + public function create(): \Illuminate\Contracts\View\View + { + return view('bo.pages.brands.create'); + } + + /** + * Store a newly created resource in storage. + * + * @param \App\Http\Requests\Bo\Brands\StoreBrandRequest $request + * @return \Illuminate\Http\RedirectResponse + */ + public function store(StoreBrandRequest $request): RedirectResponse + { + return DB::transaction(function () use ($request) { + $brand = new Brand(); + $brand->fill($request->validated()); + + if ($brand->saveOrFail()) { + return redirect()->route('bo.brand.edit', $brand) + ->with('success', __('crud.messages.has_been_created', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1)) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_created', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1)) + ])); + }); + } + + /** + * Show the form for editing the specified resource. + * + * @param \App\Models\Brand $brand + * @return \Illuminate\Contracts\View\View + */ + public function edit(Brand $brand): View + { + return view('bo.pages.brands.edit', ['brandModel' => $brand]); + } + + /** + * Update the specified resource in storage. + * + * @param \App\Http\Requests\Bo\Brands\UpdateBrandRequest $request + * @param \App\Models\Brand $brand + * @return \Illuminate\Http\RedirectResponse + */ + public function update(UpdateBrandRequest $request, Brand $brand): RedirectResponse + { + return DB::transaction(function () use ($request, $brand) { + $brand->fill($request->validated()); + + if ($brand->saveOrFail()) { + return redirect()->route('bo.brands.edit', $brand) + ->with('success', __('crud.messages.has_been_updated', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1)) + ])); + } + return redirect()->route('bo.brands.edit', $brand) + ->with('error', __('crud.messages.cannot_be_updated', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1)) + ])); + }); + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\Brand $brand + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy(Brand $brand): RedirectResponse + { + if ($brand->deleteOrFail()) { + return redirect()->route('bo.brands.index') + ->with('success', __('crud.messages.has_been_deleted', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1)) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_deleted', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.brand', 1)) + ])); + } +} diff --git a/app/Http/Controllers/Bo/ProductController.php b/app/Http/Controllers/Bo/ProductController.php new file mode 100644 index 0000000..2a7bcfb --- /dev/null +++ b/app/Http/Controllers/Bo/ProductController.php @@ -0,0 +1,155 @@ +authorizeResource(Product::class); + } + + /** + * Display a listing of the resource. + * + * @param \Illuminate\Http\Client\Request $request + * @return \Illuminate\Contracts\View\View + */ + public function index(Request $request): View + { + /** @var \Illuminate\Database\Eloquent\Builder $query */ + $query = Product::query()->with('range'); + + /** @var string $search Search field */ + $search = $request->input('search'); + if ($search) { + $this->searchQuery( + $query, + $search, + null, + 'name', + ); + } + $searchFields = \implode(', ', [ + trans('validation.attributes.name'), + ]); + + /** Sort columns with a query */ + $this->sortQuery($query); + + /** Custom pagination */ + $productModelList = $this->paginate($query); + + return view('bo.pages.products.index', compact('productModelList', 'search', 'searchFields')); + } + + /** + * Show the form for creating a new resource. + * + * @return \Illuminate\Contracts\View\View + */ + public function create(): \Illuminate\Contracts\View\View + { + $rangeModelList = Range::all(); + return view('bo.pages.products.create', ['rangeModelList' => $rangeModelList]); + } + + /** + * Store a newly created resource in storage. + * + * @param \App\Http\Requests\Bo\Products\StoreProductRequest $request + * @return \Illuminate\Http\RedirectResponse + */ + public function store(StoreProductRequest $request): RedirectResponse + { + return DB::transaction(function () use ($request) { + $product = new Product(); + $product->fill($request->validated()); + + if ($product->saveOrFail()) { + return redirect()->route('bo.product.edit', $product) + ->with('success', __('crud.messages.has_been_created', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.product', 1)) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_created', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.product', 1)) + ])); + }); + } + + /** + * Show the form for editing the specified resource. + * + * @param \App\Models\Product $product + * @return \Illuminate\Contracts\View\View + */ + public function edit(Product $product): View + { + $rangeModelList = Range::all(); + return view('bo.pages.products.edit', [ + 'rangeModelList' => $rangeModelList, + 'productModel' => $product + ]); + } + + /** + * Update the specified resource in storage. + * + * @param \App\Http\Requests\Bo\Products\UpdateProductRequest $request + * @param \App\Models\Product $product + * @return \Illuminate\Http\RedirectResponse + */ + public function update(UpdateProductRequest $request, Product $product): RedirectResponse + { + return DB::transaction(function () use ($request, $product) { + $product->fill($request->validated()); + + if ($product->saveOrFail()) { + return redirect()->route('bo.products.edit', $product) + ->with('success', __('crud.messages.has_been_updated', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.product', 1)) + ])); + } + return redirect()->route('bo.products.edit', $product) + ->with('error', __('crud.messages.cannot_be_updated', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.product', 1)) + ])); + }); + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\Product $product + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy(Product $product): RedirectResponse + { + if ($product->deleteOrFail()) { + return redirect()->route('bo.products.index') + ->with('success', __('crud.messages.has_been_deleted', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.product', 1)) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_deleted', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.product', 1)) + ])); + } +} diff --git a/app/Http/Controllers/Bo/RangeController.php b/app/Http/Controllers/Bo/RangeController.php new file mode 100644 index 0000000..b4220ac --- /dev/null +++ b/app/Http/Controllers/Bo/RangeController.php @@ -0,0 +1,155 @@ +authorizeResource(Range::class); + } + + /** + * Display a listing of the resource. + * + * @param \Illuminate\Http\Client\Request $request + * @return \Illuminate\Contracts\View\View + */ + public function index(Request $request): View + { + /** @var \Illuminate\Database\Eloquent\Builder $query */ + $query = Range::query()->with('brand'); + + /** @var string $search Search field */ + $search = $request->input('search'); + if ($search) { + $this->searchQuery( + $query, + $search, + null, + 'name', + ); + } + $searchFields = \implode(', ', [ + trans('validation.attributes.name'), + ]); + + /** Sort columns with a query */ + $this->sortQuery($query); + + /** Custom pagination */ + $rangeModelList = $this->paginate($query); + + return view('bo.pages.ranges.index', compact('rangeModelList', 'search', 'searchFields')); + } + + /** + * Show the form for creating a new resource. + * + * @return \Illuminate\Contracts\View\View + */ + public function create(): \Illuminate\Contracts\View\View + { + $brandModelList = Brand::all(); + return view('bo.pages.ranges.create', ['brandModelList' => $brandModelList]); + } + + /** + * Store a newly created resource in storage. + * + * @param \App\Http\Requests\Bo\Ranges\StoreRangeRequest $request + * @return \Illuminate\Http\RedirectResponse + */ + public function store(StoreRangeRequest $request): RedirectResponse + { + return DB::transaction(function () use ($request) { + $range = new Range(); + $range->fill($request->validated()); + + if ($range->saveOrFail()) { + return redirect()->route('bo.range.edit', $range) + ->with('success', __('crud.messages.has_been_created', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.range', 1)) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_created', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.range', 1)) + ])); + }); + } + + /** + * Show the form for editing the specified resource. + * + * @param \App\Models\Range $range + * @return \Illuminate\Contracts\View\View + */ + public function edit(Range $range): View + { + $brandModelList = Brand::all(); + return view('bo.pages.ranges.edit', [ + 'brandModelList' => $brandModelList, + 'rangeModel' => $range + ]); + } + + /** + * Update the specified resource in storage. + * + * @param \App\Http\Requests\Bo\Ranges\UpdateRangeRequest $request + * @param \App\Models\Range $range + * @return \Illuminate\Http\RedirectResponse + */ + public function update(UpdateRangeRequest $request, Range $range): RedirectResponse + { + return DB::transaction(function () use ($request, $range) { + $range->fill($request->validated()); + + if ($range->saveOrFail()) { + return redirect()->route('bo.ranges.edit', $range) + ->with('success', __('crud.messages.has_been_updated', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.range', 1)) + ])); + } + return redirect()->route('bo.ranges.edit', $range) + ->with('error', __('crud.messages.cannot_be_updated', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.range', 1)) + ])); + }); + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\Range $range + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy(Range $range): RedirectResponse + { + if ($range->deleteOrFail()) { + return redirect()->route('bo.ranges.index') + ->with('success', __('crud.messages.has_been_deleted', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.range', 1)) + ])); + } + return redirect()->back() + ->with('error', __('crud.messages.cannot_be_deleted', [ + 'model' => Str::ucfirst(\trans_choice('models.classes.range', 1)) + ])); + } +} diff --git a/app/Http/Requests/Bo/Brands/StoreBrandRequest.php b/app/Http/Requests/Bo/Brands/StoreBrandRequest.php new file mode 100644 index 0000000..7c2cd42 --- /dev/null +++ b/app/Http/Requests/Bo/Brands/StoreBrandRequest.php @@ -0,0 +1,51 @@ +mergePicture('picture'); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + $rules = [ + 'name' => 'required|min:2|max:60', + 'color' => 'required|color_hex' + ]; + + return \array_merge( + $rules, + $this->pictureRules('picture', 500, 350, 800, 350, false) + ); + } +} diff --git a/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php b/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php new file mode 100644 index 0000000..48771ee --- /dev/null +++ b/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php @@ -0,0 +1,18 @@ +route('brand')); + } +} diff --git a/app/Http/Requests/Bo/Products/StoreProductRequest.php b/app/Http/Requests/Bo/Products/StoreProductRequest.php new file mode 100644 index 0000000..c5a1b27 --- /dev/null +++ b/app/Http/Requests/Bo/Products/StoreProductRequest.php @@ -0,0 +1,56 @@ +merge([ + 'description' => (string)Str::of($this->input('description', '')) + ]); + $this->mergePicture('picture'); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + $rules = [ + 'name' => 'required|min:2|max:130', + 'description' => 'present', + 'range_id' => 'required|exists:ranges,id' + ]; + + return \array_merge( + $rules, + $this->pictureRules('picture', 800, 800, 800, 800, false) + ); + } +} diff --git a/app/Http/Requests/Bo/Products/UpdateProductRequest.php b/app/Http/Requests/Bo/Products/UpdateProductRequest.php new file mode 100644 index 0000000..025c629 --- /dev/null +++ b/app/Http/Requests/Bo/Products/UpdateProductRequest.php @@ -0,0 +1,18 @@ +route('product')); + } +} diff --git a/app/Http/Requests/Bo/Ranges/StoreRangeRequest.php b/app/Http/Requests/Bo/Ranges/StoreRangeRequest.php new file mode 100644 index 0000000..1d09dd1 --- /dev/null +++ b/app/Http/Requests/Bo/Ranges/StoreRangeRequest.php @@ -0,0 +1,51 @@ +mergePicture('picture'); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + $rules = [ + 'name' => 'required|min:2|max:90', + 'brand_id' => 'required|exists:brands,id' + ]; + + return \array_merge( + $rules, + $this->pictureRules('picture', 500, 500, 800, 500, false) + ); + } +} diff --git a/app/Http/Requests/Bo/Ranges/UpdateRangeRequest.php b/app/Http/Requests/Bo/Ranges/UpdateRangeRequest.php new file mode 100644 index 0000000..87b2784 --- /dev/null +++ b/app/Http/Requests/Bo/Ranges/UpdateRangeRequest.php @@ -0,0 +1,18 @@ +route('range')); + } +} diff --git a/app/Lib/Helpers/ToolboxHelper.php b/app/Lib/Helpers/ToolboxHelper.php index 45595d8..29e25c6 100644 --- a/app/Lib/Helpers/ToolboxHelper.php +++ b/app/Lib/Helpers/ToolboxHelper.php @@ -471,19 +471,19 @@ class ToolboxHelper $height = intval($dimensions[1]); $xMessage = trans( - ":attribute la largeur doit être comprise entre {$sizes->minWith}px et {$sizes->maxWith}px" + ":attribute la largeur doit être comprise entre {$sizes->minWidth}px et {$sizes->maxWidth}px" ); - if ($sizes->minWith === $sizes->maxWith) { - $xMessage = trans(":attribute la largeur doit être de {$sizes->minWith}px"); + if ($sizes->minWidth === $sizes->maxWidth) { + $xMessage = trans(":attribute la largeur doit être de {$sizes->minWidth}px"); } $yMessage = trans( - ":attribute la hauteur doit être comprise entre {$sizes->minWith}px et {$sizes->maxWith}px" + ":attribute la hauteur doit être comprise entre {$sizes->minWidth}px et {$sizes->maxWidth}px" ); if ($sizes->minHeight === $sizes->maxHeight) { $yMessage = trans(":attribute la hauteur doit être de {$sizes->minHeight}px"); } - if (($width < $sizes->minWith) or ($width > $sizes->maxWith)) { + if (($width < $sizes->minWidth) or ($width > $sizes->maxWidth)) { $fail($xMessage); } if (($height < $sizes->minHeight) or ($height > $sizes->maxHeight)) { diff --git a/app/Models/Brand.php b/app/Models/Brand.php new file mode 100644 index 0000000..ae6e673 --- /dev/null +++ b/app/Models/Brand.php @@ -0,0 +1,105 @@ + $ranges Ranges Has-many relationship. + */ +class Brand extends Model +{ + use HasFactory; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'name', + 'color', + 'picture', + ]; + + /** + * The attributes that should be cast. + * + * @var array + */ + protected $casts = [ + 'color' => HtmlColor::class + ]; + + /** + * Perform any actions required after the model boots. + * + * @return void + */ + protected static function booted(): void + { + static::creating(function (self $model) { + // * ASSERTIONS + static::assertNameIsUnique($model->name); + + // * TRANSFORMATIONS + $model->slug = Str::slug($model->name); + $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); + }); + static::updating(function (self $model) { + // * ASSERTIONS + static::assertNameIsUnique($model->name, $model->id); + + // * TRANSFORMATIONS + $model->slug = Str::slug($model->name); + $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); + }); + static::updated(function (self $model) { + FileStorageHelper::removeOldFile($model, 'picture'); + }); + static::deleted(function (self $model) { + FileStorageHelper::removeFile($model, 'picture'); + }); + } + + // * RELATIONSHIPS + + /** + * Get all of the ranges for the Brand + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function ranges(): HasMany + { + return $this->hasMany(Range::class); + } + + // * METHODS + + /** + * Asserts using validation that the name is unique + * + * @param string|null $name + * @param integer|null $id + * @return void + * @throws \Illuminate\Validation\ValidationException If name already exists. + */ + private static function assertNameIsUnique(string|null $name, ?int $id = null) + { + $table = (new self())->getTable(); + ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id); + ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $id); + } +} diff --git a/app/Models/Product.php b/app/Models/Product.php new file mode 100644 index 0000000..72e439a --- /dev/null +++ b/app/Models/Product.php @@ -0,0 +1,96 @@ + + */ + protected $fillable = [ + 'name', + 'description', + 'picture', + 'range_id', + ]; + + /** + * Perform any actions required after the model boots. + * + * @return void + */ + protected static function booted(): void + { + static::creating(function (self $model) { + // * ASSERTIONS + static::assertNameIsUnique($model->name); + + // * TRANSFORMATIONS + $model->slug = Str::slug($model->name); + $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); + }); + static::updating(function (self $model) { + // * ASSERTIONS + static::assertNameIsUnique($model->name, $model->id); + + // * TRANSFORMATIONS + $model->slug = Str::slug($model->name); + $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); + }); + static::updated(function (self $model) { + FileStorageHelper::removeOldFile($model, 'picture'); + }); + static::deleted(function (self $model) { + FileStorageHelper::removeFile($model, 'picture'); + }); + } + + // * RELATIONSHIPS + + /** + * Get the range that owns the Product + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function range(): BelongsTo + { + return $this->belongsTo(Range::class); + } + + // * METHODS + + /** + * Asserts using validation that the name is unique + * + * @param string|null $name + * @param integer|null $id + * @return void + * @throws \Illuminate\Validation\ValidationException If name already exists. + */ + private static function assertNameIsUnique(string|null $name, ?int $id = null) + { + $table = (new self())->getTable(); + ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id); + ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $id); + } +} diff --git a/app/Models/Range.php b/app/Models/Range.php new file mode 100644 index 0000000..bdbeb90 --- /dev/null +++ b/app/Models/Range.php @@ -0,0 +1,106 @@ + $products Products Has-many. + */ +class Range extends Model +{ + use HasFactory; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'name', + 'picture', + 'brand_id', + ]; + + /** + * Perform any actions required after the model boots. + * + * @return void + */ + protected static function booted(): void + { + static::creating(function (self $model) { + // * ASSERTIONS + static::assertNameIsUnique($model->name); + + // * TRANSFORMATIONS + $model->slug = Str::slug($model->name); + $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); + }); + static::updating(function (self $model) { + // * ASSERTIONS + static::assertNameIsUnique($model->name, $model->id); + + // * TRANSFORMATIONS + $model->slug = Str::slug($model->name); + $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); + }); + static::updated(function (self $model) { + FileStorageHelper::removeOldFile($model, 'picture'); + }); + static::deleted(function (self $model) { + FileStorageHelper::removeFile($model, 'picture'); + }); + } + + // * RELATIONSHIPS + + /** + * Get the brand that owns the Product + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function brand(): BelongsTo + { + return $this->belongsTo(Brand::class); + } + + /** + * Get all of the products for the Brand + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function products(): HasMany + { + return $this->hasMany(Product::class); + } + + // * METHODS + + /** + * Asserts using validation that the name is unique + * + * @param string|null $name + * @param integer|null $id + * @return void + * @throws \Illuminate\Validation\ValidationException If name already exists. + */ + private static function assertNameIsUnique(string|null $name, ?int $id = null) + { + $table = (new self())->getTable(); + ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id); + ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $id); + } +} diff --git a/app/Policies/BrandPolicy.php b/app/Policies/BrandPolicy.php new file mode 100644 index 0000000..5082b73 --- /dev/null +++ b/app/Policies/BrandPolicy.php @@ -0,0 +1,103 @@ + $minWidth ?? $this->minWidth, + 'minHeight' => $minHeight ?? $this->minHeight, + 'maxWidth' => $maxWidth ?? $this->maxWidth, + 'maxHeight' => $maxHeight ?? $this->maxHeight + ]; + return [ + $name => [ + !$nullable ? 'required' : 'nullable', + (strlen($requiredWith)) ? 'required_with:' . $requiredWith : '', + (strlen($requiredWithout)) ? 'required_without:' . $requiredWithout : '', + ($size) ? 'max:' . $size : '', + function (string $attribute, $value, callable $fail) use ($dimensions) { + return ToolboxHelper::validatePicture($attribute, $value, $fail, $dimensions); + } + ] + ]; + } + + /** + * Images rules + * + * @param string $name + * @param integer|null $minWidth + * @param integer|null $minHeight + * @param integer|null $maxWidth + * @param integer|null $maxHeight + * @return array + */ + public function picturesRules( + string $name = 'pictures', + ?int $minWidth = null, + ?int $minHeight = null, + ?int $maxWidth = null, + ?int $maxHeight = null + ): array { + $sizes = (object)[ + 'minWidth' => $minWidth ?? $this->minWidth, + 'minHeight' => $minHeight ?? $this->minHeight, + 'maxWidth' => $maxWidth ?? $this->maxWidth, + 'maxHeight' => $maxHeight ?? $this->maxHeight + ]; + return [ + $name => 'sometimes|array', + "{$name}Validate" => "required_with:{$name}|array", + "{$name}Validate.*" => [ + 'required', + function (string $attribute, $value, callable $fail) use ($sizes) { + return $this->validatePicture($attribute, $value, $fail, $sizes); + } + ] + ]; + } + + /** + * Merges products pictures fields with request data + * + * @param string $name + * @return void + */ + protected function mergePicture(string $name = 'picture'): void + { + $picturePath = $this->{$name}; + if (is_string($picturePath)) { + $this->merge(["{$name}Validate" => []]); + // ! Empty picture. + if ($picturePath === 'false') { + // * Empty picture and exit + $this->merge([$name => []]); + return; + } + $decodedPath = \urldecode($picturePath); + $this->merge([ + "{$name}Validate" => array_merge( + $this->{"{$name}Validate"}, + [\public_path(ltrim($decodedPath, '/'))] + ) + ]); + $changedPicture = ltrim($decodedPath, '/'); + $this->merge([ + $name => $changedPicture + ]); + } //end if + } + + /** + * Merges products pictures fields with request data + * + * @param string $name + * @return void + */ + protected function mergePictures(string $name = 'pictures'): void + { + $pictures = $this->{$name}; + if (is_array($pictures)) { + $this->merge(["{$name}Validate" => []]); + foreach ($pictures as $key => $picturePath) { + // ! Empty picture. + if ($picturePath === 'false') { + // * Empty picture and exit + $this->merge([$name => []]); + return; + } + $decodedPath = \urldecode($picturePath); + $this->merge([ + "{$name}Validate" => array_merge( + $this->{"{$name}Validate"}, + [\public_path(ltrim($decodedPath, '/'))] + ) + ]); + $changedPicture = $this->{$name}; + $changedPicture[$key] = ltrim($decodedPath, '/'); + $this->merge([ + $name => $changedPicture + ]); + } + } //end if + } +} diff --git a/composer.json b/composer.json index 366c78e..d8b6427 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "php": "^8.2", "diglactic/laravel-breadcrumbs": "^8.0", "guzzlehttp/guzzle": "^7.2", + "kaishiyoku/laravel-color-validation": "^2.1", "laravel/framework": "^10.0", "laravel/tinker": "^2.8", "laravel/ui": "^4.2", diff --git a/composer.lock b/composer.lock index ffb664b..676ecb6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f6f8667757d8be0e48544e0bc4e250e5", + "content-hash": "be23935d916f62e36467d9c3fa5f5917", "packages": [ { "name": "brick/math", @@ -1338,6 +1338,69 @@ }, "time": "2021-10-08T21:21:46+00:00" }, + { + "name": "kaishiyoku/laravel-color-validation", + "version": "2.1.1", + "source": { + "type": "git", + "url": "https://github.com/Kaishiyoku/laravel-color-validation.git", + "reference": "4a77492335e37d18055ec9547332d224186d074b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Kaishiyoku/laravel-color-validation/zipball/4a77492335e37d18055ec9547332d224186d074b", + "reference": "4a77492335e37d18055ec9547332d224186d074b", + "shasum": "" + }, + "require": { + "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/validation": "^6.0|^7.0|^8.0|^9.0|^10.0", + "php": "^7.4|^8.0", + "spatie/regex": "^1.4|^3.1" + }, + "require-dev": { + "orchestra/testbench": "^6.0", + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Kaishiyoku\\Validation\\Color\\ServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Kaishiyoku\\": "src/Kaishiyoku" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kaishiyoku", + "email": "dev@andreas-wiedel.de", + "homepage": "https://andreas-wiedel.de", + "role": "Maintainer" + } + ], + "description": "Laravel color validation rules", + "homepage": "https://github.com/Kaishiyoku/laravel-color-validation", + "keywords": [ + "color", + "laravel", + "validation rules", + "validator" + ], + "support": { + "issues": "https://github.com/Kaishiyoku/laravel-color-validation/issues", + "source": "https://github.com/Kaishiyoku/laravel-color-validation/tree/2.1.1" + }, + "time": "2023-02-13T20:23:52+00:00" + }, { "name": "laravel/framework", "version": "v10.23.1", @@ -4247,6 +4310,69 @@ ], "time": "2023-09-05T11:02:34+00:00" }, + { + "name": "spatie/regex", + "version": "3.1.1", + "source": { + "type": "git", + "url": "https://github.com/spatie/regex.git", + "reference": "d543de2019a0068e7b80da0ba24f1c51c7469303" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/regex/zipball/d543de2019a0068e7b80da0ba24f1c51c7469303", + "reference": "d543de2019a0068e7b80da0ba24f1c51c7469303", + "shasum": "" + }, + "require": { + "php": "^8.0|^8.1" + }, + "require-dev": { + "phpunit/phpunit": "^9.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\Regex\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Sebastian De Deyne", + "email": "sebastian@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "A sane interface for php's built in preg_* functions", + "homepage": "https://github.com/spatie/regex", + "keywords": [ + "expression", + "expressions", + "regex", + "regular", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/regex/issues", + "source": "https://github.com/spatie/regex/tree/3.1.1" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + }, + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2021-11-30T21:13:59+00:00" + }, { "name": "symfony/console", "version": "v6.3.4", diff --git a/database/factories/BrandFactory.php b/database/factories/BrandFactory.php new file mode 100644 index 0000000..fec81f7 --- /dev/null +++ b/database/factories/BrandFactory.php @@ -0,0 +1,24 @@ + + */ +class BrandFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => 'product ' . fake()->unique()->safeColorName(), + 'color' => fake()->unique()->hexColor() + ]; + } +} diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php new file mode 100644 index 0000000..3cbfe30 --- /dev/null +++ b/database/factories/ProductFactory.php @@ -0,0 +1,24 @@ + + */ +class ProductFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => 'product' . fake()->unique()->words(3, true), + 'description' => 'TEXT' . fake()->realText() + ]; + } +} diff --git a/database/factories/RangeFactory.php b/database/factories/RangeFactory.php new file mode 100644 index 0000000..d1c2f3c --- /dev/null +++ b/database/factories/RangeFactory.php @@ -0,0 +1,23 @@ + + */ +class RangeFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => 'gamme ' . fake()->unique()->streetName(), + ]; + } +} diff --git a/database/factories/assets/brands/default-picture-600x350.jpg b/database/factories/assets/brands/default-picture-600x350.jpg new file mode 100644 index 0000000..7f8673b Binary files /dev/null and b/database/factories/assets/brands/default-picture-600x350.jpg differ diff --git a/database/factories/assets/products/default-picture-800x800.jpg b/database/factories/assets/products/default-picture-800x800.jpg new file mode 100644 index 0000000..13c9637 Binary files /dev/null and b/database/factories/assets/products/default-picture-800x800.jpg differ diff --git a/database/factories/assets/ranges/default-picture-600x500.jpg b/database/factories/assets/ranges/default-picture-600x500.jpg new file mode 100644 index 0000000..53d503a Binary files /dev/null and b/database/factories/assets/ranges/default-picture-600x500.jpg differ diff --git a/database/migrations/2024_01_05_105154_create_brands_table.php b/database/migrations/2024_01_05_105154_create_brands_table.php new file mode 100644 index 0000000..929fe40 --- /dev/null +++ b/database/migrations/2024_01_05_105154_create_brands_table.php @@ -0,0 +1,39 @@ +id(); + + $table->string('name')->comment('Nom de la marque'); + $table->string('slug')->comment('Slug de la marque'); + + $table->unsignedInteger('color')->comment('Couleur de la marque'); + + $table->string('picture')->comment('Logo de la marque'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::dropIfExists('brands'); + } +}; diff --git a/database/migrations/2024_01_05_114757_create_ranges_table.php b/database/migrations/2024_01_05_114757_create_ranges_table.php new file mode 100644 index 0000000..d59b334 --- /dev/null +++ b/database/migrations/2024_01_05_114757_create_ranges_table.php @@ -0,0 +1,40 @@ +id(); + + $table->string('name')->comment('Nom de la gamme'); + $table->string('slug')->comment('Slug de la gamme'); + + $table->string('picture')->comment('Logo de la marque'); + + $table->foreignId('brand_id'); + $table->foreign('brand_id')->references('id')->on('brands')->cascadeOnDelete(); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::dropIfExists('ranges'); + } +}; diff --git a/database/migrations/2024_01_05_114848_create_products_table.php b/database/migrations/2024_01_05_114848_create_products_table.php new file mode 100644 index 0000000..88ce849 --- /dev/null +++ b/database/migrations/2024_01_05_114848_create_products_table.php @@ -0,0 +1,42 @@ +id(); + + $table->string('name')->comment('Nom du produit'); + $table->string('slug')->comment('Slug du produit'); + + $table->text('description')->comment('Description du produit'); + + $table->string('picture')->comment('Logo du produit'); + + $table->foreignId('range_id'); + $table->foreign('range_id')->references('id')->on('ranges')->cascadeOnDelete(); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::dropIfExists('products'); + } +}; diff --git a/database/seeders/BrandSeeder.php b/database/seeders/BrandSeeder.php new file mode 100644 index 0000000..1965801 --- /dev/null +++ b/database/seeders/BrandSeeder.php @@ -0,0 +1,36 @@ +getTable())->truncate(); + Schema::enableForeignKeyConstraints(); + File::deleteDirectory(\storage_path('app/public/modelfiles/' . (new Brand())->getTable())); + + Brand::factory(fake()->randomDigitNot([0, 1, 2, 3]))->make()->each(function (Brand $brand) { + $brand->picture = FileStorageHelper::storeFile( + new Brand(), + new \SplFileInfo( + \resource_path('../database/factories/assets/brands/default-picture-600x350.jpg') + ) + ); + $brand->save(); + }); + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 805b951..9a9b2e7 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -15,7 +15,10 @@ class DatabaseSeeder extends Seeder { $this->call([ UserSeeder::class, - StaticPageSeeder::class + StaticPageSeeder::class, + BrandSeeder::class, + RangeSeeder::class, + ProductSeeder::class, ]); } } diff --git a/database/seeders/ProductSeeder.php b/database/seeders/ProductSeeder.php new file mode 100644 index 0000000..40071bc --- /dev/null +++ b/database/seeders/ProductSeeder.php @@ -0,0 +1,41 @@ +getTable())->truncate(); + Schema::enableForeignKeyConstraints(); + File::deleteDirectory(\storage_path('app/public/modelfiles/' . (new Product())->getTable())); + + Range::all()->each( + fn (Range $range) => + Product::factory(\random_int(6, 10))->make()->each(function (Product $product) use ($range) { + $product->picture = FileStorageHelper::storeFile( + new Product(), + new \SplFileInfo( + \resource_path('../database/factories/assets/products/default-picture-800x800.jpg') + ) + ); + $product->range()->associate($range); + $product->save(); + }) + ); + } +} diff --git a/database/seeders/RangeSeeder.php b/database/seeders/RangeSeeder.php new file mode 100644 index 0000000..1192387 --- /dev/null +++ b/database/seeders/RangeSeeder.php @@ -0,0 +1,42 @@ +getTable())->truncate(); + Schema::enableForeignKeyConstraints(); + File::deleteDirectory(\storage_path('app/public/modelfiles/' . (new Range())->getTable())); + + + Brand::all()->each( + fn (Brand $brand) => + Range::factory(\random_int(6, 16))->make()->each(function (Range $range) use ($brand) { + $range->picture = FileStorageHelper::storeFile( + new Range(), + new \SplFileInfo( + \resource_path('../database/factories/assets/ranges/default-picture-600x500.jpg') + ) + ); + $range->brand()->associate($brand); + $range->save(); + }) + ); + } +} diff --git a/resources/lang/fr/crud.php b/resources/lang/fr/crud.php index 92dfd9a..fd8cd62 100644 --- a/resources/lang/fr/crud.php +++ b/resources/lang/fr/crud.php @@ -87,5 +87,21 @@ return [ 'sort_delete' => 'Supprimer le tri', 'sort_arrow' => 'Ici, les flèches sont utilisées pour trier et non pour filtrer', 'right' => 'Vous n\'avez pas les droits', + ], + + 'meta' => [ + 'title' => [ + 'create' => '{0} Création d\'un :model|{1} Création d\'une :model', + 'edit' => '{0} Edition d\'un :model|{1} Edition d\'une :model', + 'show' => '{0} Visualisation d\'un :model|{1} Visualisation d\'une :model', + ], + // phpcs:disable Generic.Files.LineLength.TooLong + 'description' => [ + 'index' => '{0} Liste de tous les :model|{1} Liste de toutes les :model', + 'create' => '{0} Création d\'un tout nouveau :model|{1} Création d\'une toute nouvelle :model|{2} Création d\'un tout nouvel :model', + 'edit' => '{0} Edition d\'un :model déjà existant|{1} Edition d\'une :model déjà existante', + 'show' => '{0} Visualisation d\'un :model déjà existant|{1} Visualisation d\'une :model déjà existante', + ], + // phpcs:enable ] ]; diff --git a/resources/lang/fr/models.php b/resources/lang/fr/models.php index e0bf371..4050bff 100644 --- a/resources/lang/fr/models.php +++ b/resources/lang/fr/models.php @@ -4,6 +4,9 @@ return [ 'classes' => [ 'user' => 'utilisateur', 'activity_log' => 'journal d\'activité|journaux d\'activité', - 'static_page' => 'page statique|pages statiques' + 'static_page' => 'page statique|pages statiques', + 'brand' => 'marque|marques', + 'range' => 'gamme|gammes', + 'product' => 'produit|produits', ] ]; diff --git a/resources/lang/fr/validation.php b/resources/lang/fr/validation.php index 836251d..7e7a931 100644 --- a/resources/lang/fr/validation.php +++ b/resources/lang/fr/validation.php @@ -229,5 +229,6 @@ return [ 'static_page_type' => 'type de page', 'seo_title' => 'titre SEO', 'seo_description' => 'description SEO', + 'color' => 'couleur', ], ]; diff --git a/resources/views/bo/layouts/nav-list.blade.php b/resources/views/bo/layouts/nav-list.blade.php index 48016d1..b56e981 100644 --- a/resources/views/bo/layouts/nav-list.blade.php +++ b/resources/views/bo/layouts/nav-list.blade.php @@ -6,6 +6,30 @@ {{ __('Accueil') }} + @can('viewAny', \App\Models\Brand::class) + + @endcan + @can('viewAny', \App\Models\Range::class) + + @endcan + @can('viewAny', \App\Models\Product::class) + + @endcan {{-- @can('viewAny', \App\Models\StaticPage::class)