From 99386864356f67671f0d3ca8cf4ce6e6227f50e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 27 Jun 2022 16:05:32 +0200 Subject: [PATCH] Fix image input on models + Image Input evolution get original image size --- .editorconfig | 2 +- app/Http/Controllers/Bo/SliderController.php | 2 +- .../Requests/Bo/News/StoreNewsRequest.php | 9 +- .../Bo/Sliders/StoreSliderRequest.php | 9 +- .../{ => Controllers}/UpdateModelStatus.php | 2 +- app/Traits/MiniCrudModel.php | 103 ------------------ app/Traits/{ => Requests}/HasPicture.php | 100 +++++++++++++---- .../ts/components/bo/ImageInputComponent.vue | 60 ++++++++-- resources/views/back/sliders/form.blade.php | 2 +- 9 files changed, 145 insertions(+), 144 deletions(-) rename app/Traits/{ => Controllers}/UpdateModelStatus.php (93%) delete mode 100644 app/Traits/MiniCrudModel.php rename app/Traits/{ => Requests}/HasPicture.php (59%) diff --git a/.editorconfig b/.editorconfig index c6e4bd2..8b827c7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -14,7 +14,7 @@ trim_trailing_whitespace = false [*.{yml,yaml}] indent_size = 2 -[*.{vue}] +[*.vue] indent_size = 2 [docker-compose.yml] diff --git a/app/Http/Controllers/Bo/SliderController.php b/app/Http/Controllers/Bo/SliderController.php index 0f410f4..79ca0a5 100644 --- a/app/Http/Controllers/Bo/SliderController.php +++ b/app/Http/Controllers/Bo/SliderController.php @@ -8,7 +8,7 @@ use App\Http\Requests\Bo\ChangeOrderRequest; use App\Http\Requests\Bo\Sliders\StoreSliderRequest; use App\Http\Requests\Bo\Sliders\UpdateSliderRequest; use App\Models\Slider; -use App\Traits\UpdateModelStatus; +use App\Traits\Controllers\UpdateModelStatus; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; diff --git a/app/Http/Requests/Bo/News/StoreNewsRequest.php b/app/Http/Requests/Bo/News/StoreNewsRequest.php index f5d0586..ae6b28d 100644 --- a/app/Http/Requests/Bo/News/StoreNewsRequest.php +++ b/app/Http/Requests/Bo/News/StoreNewsRequest.php @@ -3,6 +3,7 @@ namespace App\Http\Requests\Bo\News; use App\Enums\PublishedStatus; +use App\Traits\Requests\HasPicture; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Str; use Kwaadpepper\Enum\Rules\EnumIsValidRule; @@ -12,6 +13,8 @@ use Kwaadpepper\Enum\Rules\EnumIsValidRule; */ class StoreNewsRequest extends FormRequest { + use HasPicture; + /** * Determine if the user is authorized to make this request. * @@ -32,6 +35,7 @@ class StoreNewsRequest extends FormRequest $this->merge([ 'slug' => Str::slug($this->title), ]); + $this->mergePicture(); } /** @@ -41,13 +45,12 @@ class StoreNewsRequest extends FormRequest */ public function rules() { - return [ + return \array_merge([ 'title' => 'required|min:6|max:255', 'slug' => 'required|max:255', 'seo_title' => 'required|min:45|max:70', 'seo_description' => 'required|min:50|max:160', 'content' => 'required|string|min:15', - 'picture' => 'sometimes|image|dimensions:width=300,height=300|mimes:png,jpg,jpeg,gif', 'picture_alt' => 'required_with:picture|nullable|string|min:6|max:255', 'picture_title' => 'required_with:picture|nullable|string|min:6|max:255', 'published' => ['required', new EnumIsValidRule(PublishedStatus::class)], @@ -55,7 +58,7 @@ class StoreNewsRequest extends FormRequest 'tags.*' => 'required|array', 'tags.*.id' => 'required|numeric|exists:tags,id|distinct', 'tags.*.name' => 'required|string', - ]; + ], $this->pictureRules(300, 300, 300, 300)); } /** diff --git a/app/Http/Requests/Bo/Sliders/StoreSliderRequest.php b/app/Http/Requests/Bo/Sliders/StoreSliderRequest.php index ab706f0..d3af302 100644 --- a/app/Http/Requests/Bo/Sliders/StoreSliderRequest.php +++ b/app/Http/Requests/Bo/Sliders/StoreSliderRequest.php @@ -3,11 +3,14 @@ namespace App\Http\Requests\Bo\Sliders; use App\Enums\Sliders\SliderPosition; +use App\Traits\Requests\HasPicture; use Illuminate\Foundation\Http\FormRequest; use Kwaadpepper\Enum\Rules\EnumIsValidRule; class StoreSliderRequest extends FormRequest { + use HasPicture; + /** * Determine if the user is authorized to make this request. * @@ -31,6 +34,7 @@ class StoreSliderRequest extends FormRequest 'publish_at' => $this->publish_at ? \str_replace('T', ' ', $this->publish_at) : null, 'unpublish_at' => $this->unpublish_at ? \str_replace('T', ' ', $this->unpublish_at) : null, ]); + $this->mergePicture(); } /** @@ -40,12 +44,11 @@ class StoreSliderRequest extends FormRequest */ public function rules() { - return [ + return \array_merge([ 'title' => 'required|min:3|max:255', 'title_color' => 'required|color_hex', 'title_color_bg' => 'required|color_hex', 'position' => ['required', new EnumIsValidRule(SliderPosition::class)], - 'picture' => 'required|image|dimensions:width=1400,height=630|mimes:png,jpg,jpeg,gif', 'link' => 'required|url', 'link_title' => 'required|min:3|max:255', 'link_color' => 'required|color_hex', @@ -54,7 +57,7 @@ class StoreSliderRequest extends FormRequest 'status' => 'required|boolean', 'publish_at' => 'nullable|date_format:Y-m-d H:i', 'unpublish_at' => 'nullable|date_format:Y-m-d H:i', - ]; + ], $this->pictureRules(1400, 630, 1400, 630)); } /** diff --git a/app/Traits/UpdateModelStatus.php b/app/Traits/Controllers/UpdateModelStatus.php similarity index 93% rename from app/Traits/UpdateModelStatus.php rename to app/Traits/Controllers/UpdateModelStatus.php index cc67e7e..a3a9842 100644 --- a/app/Traits/UpdateModelStatus.php +++ b/app/Traits/Controllers/UpdateModelStatus.php @@ -1,6 +1,6 @@ setData($attributes['data']); - } - } - - /** - * Set a specific prop - * - * @param array $data - * @return void - */ - private function setData(array $data): void - { - $attrs = $this->attributes; - if (isset($attrs['data'])) { - unset($attrs['data']); - } - $data = $this->serializeData($data); - $this->setRawAttributes(\array_merge_recursive( - $attrs, - ['data' => $data] - )); - } - - /** - * Get data attribute - * Cast json_decode to object. - * - * @return object|null - */ - protected function getDataAttribute(): ?object - { - return \json_decode($this->getAttributes()['data'] ?? '[]'); - } - - /** - * Get data attribute - * Cast json_decode to object. - * - * @param array $data - * @return void - */ - protected function setDataAttribute(array $data): void - { - $this->setData($data); - } - - /** - * Serialize properties - * - * @param array $data - * @return string - * @throws \ErrorException If input type is no handled. - * @throws \Illuminate\Validation\ValidationException If url is a bo url. - * @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber - * @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh - */ - private function serializeData(array $data): string - { - foreach ($data as $o => $input) { - switch ($input['inputType']->value ?? $input['inputType']['value']) { - case InputType::image()->value: - $data[$o]['value'] = ToolboxHelper::storeImage($this->getTable(), $input['value']); - break; - case InputType::boolean()->value: - case InputType::text()->value: - case InputType::number()->value: - case InputType::string()->value: - case InputType::videoVimeo()->value: - break; - case InputType::url()->value: - ToolboxHelper::assertNoInternalBoUrls('content', $input['value']); - break; - default: - throw new \ErrorException(sprintf( - 'Unknown input Type %s', - $input['inputType']->label ?? $input['inputType']['label'] - )); - } - }//end foreach - return json_encode($data); - } -} diff --git a/app/Traits/HasPicture.php b/app/Traits/Requests/HasPicture.php similarity index 59% rename from app/Traits/HasPicture.php rename to app/Traits/Requests/HasPicture.php index 91fdce2..f0d0d62 100644 --- a/app/Traits/HasPicture.php +++ b/app/Traits/Requests/HasPicture.php @@ -1,18 +1,58 @@ minWith = $minWith ?? $this->minWith; + $this->minHeight = $minHeight ?? $this->minHeight; + $this->maxWith = $maxWith ?? $this->maxWith; + $this->maxHeight = $maxHeight ?? $this->maxHeight; return [ 'picture' => [ 'required', @@ -26,10 +66,22 @@ trait HasPicture /** * Images rules * + * @param integer|null $minWith + * @param integer|null $minHeight + * @param integer|null $maxWith + * @param integer|null $maxHeight * @return array */ - public function pictureRules(): array - { + public function picturesRules( + ?int $minWith = null, + ?int $minHeight = null, + ?int $maxWith = null, + ?int $maxHeight = null + ): array { + $this->minWith = $minWith ?? $this->minWith; + $this->minHeight = $minHeight ?? $this->minHeight; + $this->maxWith = $maxWith ?? $this->maxWith; + $this->maxHeight = $maxHeight ?? $this->maxHeight; return [ 'pictures' => 'sometimes|array', 'pictureValidate' => 'required_with:pictures|array', @@ -57,25 +109,35 @@ trait HasPicture $fail(trans(":attribute n'existe pas dans le système de fichier")); return; } - if (!\in_array(File::mimeType($value), ['image/jpeg', 'image/jpg', 'image/png'])) { - $fail(trans(":attribute doit être une image de type 'image/jpeg,image/jpg,image/png'")); + if (!\in_array(File::mimeType($value), ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'])) { + $fail(trans(":attribute doit être une image de type 'image/jpeg,image/jpg,image/png,image/gif'")); } if (!($dimensions = \getimagesize($value)) or !isset($dimensions[0]) or !isset($dimensions[1])) { $fail(trans(":attribute impossible de valider les dimensions")); } else { - $minWith = 400; - $maxWith = 1800; - $minHeight = 300; - $maxHeight = 1800; - $width = intval($dimensions[0]); - $height = intval($dimensions[1]); - if (($width < $minWith) or ($width > $maxWith)) { - $fail(trans(":attribute la largeur doit être comprise entre $minWith et $maxWith")); + $width = intval($dimensions[0]); + $height = intval($dimensions[1]); + + $xMessage = trans( + ":attribute la largeur doit être comprise entre {$this->minWith}px et {$this->maxWith}px" + ); + if ($this->minWith === $this->maxWith) { + $xMessage = trans(":attribute la largeur doit être de {$this->minWith}px"); } - if (($height < $minHeight) or ($height > $maxHeight)) { - $fail(trans(":attribute la hauteur doit être comprise entre $minHeight et $maxHeight")); + $yMessage = trans( + ":attribute la hauteur doit être comprise entre {$this->minWith}px et {$this->maxWith}px" + ); + if ($this->minHeight === $this->maxHeight) { + $yMessage = trans(":attribute la hauteur doit être de {$this->minWith}px"); } - } + + if (($width < $this->minWith) or ($width > $this->maxWith)) { + $fail($xMessage); + } + if (($height < $this->minHeight) or ($height > $this->maxHeight)) { + $fail($yMessage); + } + } //end if } /** diff --git a/resources/ts/components/bo/ImageInputComponent.vue b/resources/ts/components/bo/ImageInputComponent.vue index 89082d2..4c2ea1e 100644 --- a/resources/ts/components/bo/ImageInputComponent.vue +++ b/resources/ts/components/bo/ImageInputComponent.vue @@ -2,9 +2,9 @@
- +