From db4592c8e0f157a03d3ce633bb10b5ba241cf7e2 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Tue, 5 Dec 2023 16:05:47 +0100 Subject: [PATCH 1/3] Many fixes --- .../Requests/Bo/UpdateMiniCrudRequest.php | 4 +- app/Lib/Helpers/FileStorageHelper.php | 20 +- app/Lib/Helpers/ToolboxHelper.php | 12 +- app/Traits/Requests/.gitkeep | 0 app/Traits/Requests/HasPicture.php | 180 ++++++++++++ resources/lang/fr/crud.php | 2 +- .../SelectMultipleEnumComponent.vue | 276 ++++++++++++++++++ .../bo/pages/static_pages/form.blade.php | 2 +- .../inputs/pivot-belongsToMany.blade.php | 2 +- 9 files changed, 486 insertions(+), 12 deletions(-) delete mode 100644 app/Traits/Requests/.gitkeep create mode 100644 app/Traits/Requests/HasPicture.php create mode 100644 resources/ts/bo/components/SelectMultipleEnumComponent.vue diff --git a/app/Http/Requests/Bo/UpdateMiniCrudRequest.php b/app/Http/Requests/Bo/UpdateMiniCrudRequest.php index f845c96..40c2f4a 100644 --- a/app/Http/Requests/Bo/UpdateMiniCrudRequest.php +++ b/app/Http/Requests/Bo/UpdateMiniCrudRequest.php @@ -103,8 +103,8 @@ abstract class UpdateMiniCrudRequest extends FormRequest $sizes = (object)[ 'minHeight' => $dim['height'], 'maxHeight' => $dim['height'], - 'minWith' => $dim['width'], - 'maxWith' => $dim['width'], + 'minWidth' => $dim['width'], + 'maxWidth' => $dim['width'], ]; $rules = array_merge($rules, [ "data.$iptName.value" => [ diff --git a/app/Lib/Helpers/FileStorageHelper.php b/app/Lib/Helpers/FileStorageHelper.php index 45ccec9..937b8db 100644 --- a/app/Lib/Helpers/FileStorageHelper.php +++ b/app/Lib/Helpers/FileStorageHelper.php @@ -118,7 +118,7 @@ class FileStorageHelper ); return str_replace('/private/', '', $newPath); - }//end if + } //end if if (\is_object($file) and $file instanceof \SplFileInfo) { /** @var \SplFileInfo $file */ $filename = $filename ?: $file->getBaseName(); @@ -451,4 +451,22 @@ class FileStorageHelper $filename = trim($filename, '.-'); return $filename; } + + /** + * Remove all model files for the given Model. + * + * @param Model $model + * @return void + */ + public static function removeModelFiles(Model $model): void + { + $tableName = $model::query()->getModel()->getTable(); + + if (!empty(Storage::disk('public')->allFiles("modelfiles/{$tableName}"))) { + Storage::disk('public')->deleteDirectory("modelfiles/{$tableName}"); + } + if (!empty(Storage::disk('private')->allFiles("modelfiles/{$tableName}"))) { + Storage::disk('private')->deleteDirectory("modelfiles/{$tableName}"); + } + } } diff --git a/app/Lib/Helpers/ToolboxHelper.php b/app/Lib/Helpers/ToolboxHelper.php index de1c040..7629e8f 100644 --- a/app/Lib/Helpers/ToolboxHelper.php +++ b/app/Lib/Helpers/ToolboxHelper.php @@ -441,19 +441,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->minHeight}px et {$sizes->maxHeight}px" ); if ($sizes->minHeight === $sizes->maxHeight) { - $yMessage = trans(":attribute la hauteur doit être de {$sizes->minWith}px"); + $yMessage = trans(":attribute la hauteur doit être de {$sizes->minWidth}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/Traits/Requests/.gitkeep b/app/Traits/Requests/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/app/Traits/Requests/HasPicture.php b/app/Traits/Requests/HasPicture.php new file mode 100644 index 0000000..ec7c938 --- /dev/null +++ b/app/Traits/Requests/HasPicture.php @@ -0,0 +1,180 @@ + $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 : '', + ($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/resources/lang/fr/crud.php b/resources/lang/fr/crud.php index 72c5d72..f3dc47b 100644 --- a/resources/lang/fr/crud.php +++ b/resources/lang/fr/crud.php @@ -54,7 +54,7 @@ return [ 'minicrud' => [ 'visibility_label' => 'Visibilité', - 'visibility_helper' => 'Vérifier qu\'il est visible.', + 'visibility_helper' => 'Cocher pour rendre visible.', 'enum_select' => 'Sélectionnez :enum dans la liste.', 'associated_enum' => 'Associé :enum', 'dimensions_must_be' => 'Doit être :widthpx (largeur) par :heightpx (hauteur).', diff --git a/resources/ts/bo/components/SelectMultipleEnumComponent.vue b/resources/ts/bo/components/SelectMultipleEnumComponent.vue new file mode 100644 index 0000000..28ed00f --- /dev/null +++ b/resources/ts/bo/components/SelectMultipleEnumComponent.vue @@ -0,0 +1,276 @@ + + + + + diff --git a/resources/views/bo/pages/static_pages/form.blade.php b/resources/views/bo/pages/static_pages/form.blade.php index 9d4fb4f..ae2aae6 100644 --- a/resources/views/bo/pages/static_pages/form.blade.php +++ b/resources/views/bo/pages/static_pages/form.blade.php @@ -5,7 +5,7 @@ {{ Str::ucfirst(__('validation.custom.static_page_type')) }}
- @include('back::partials.inputs.tinyint-enum', [ + @include('bo.partials.inputs.tinyint-enum', [ 'id' => 'staticPageType', 'label' => __('Type de page'), 'name' => 'type', diff --git a/resources/views/bo/partials/inputs/pivot-belongsToMany.blade.php b/resources/views/bo/partials/inputs/pivot-belongsToMany.blade.php index 2f8adbe..6b62a7d 100644 --- a/resources/views/bo/partials/inputs/pivot-belongsToMany.blade.php +++ b/resources/views/bo/partials/inputs/pivot-belongsToMany.blade.php @@ -13,7 +13,7 @@ @php $data = [ 'name' => $name, - 'value' => Arr::wrap(old($name) ?? ($model->{$name} ?? [])), + 'value' => old($name, $model->{$name} ?? []) ?? [], 'placeholder' => $placeholder, 'itemsList' => $itemsList, 'itemLimit' => $itemLimit ?? null, From b56b0a6c993f40ff4d8fd8e82481edde0701d167 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Fri, 5 Jan 2024 15:56:21 +0100 Subject: [PATCH 2/3] Inverted size and nullable params in pictureRules method --- app/Traits/Requests/HasPicture.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Traits/Requests/HasPicture.php b/app/Traits/Requests/HasPicture.php index ec7c938..a79cce7 100644 --- a/app/Traits/Requests/HasPicture.php +++ b/app/Traits/Requests/HasPicture.php @@ -45,8 +45,8 @@ trait HasPicture * @param integer|null $minHeight * @param integer|null $maxWidth * @param integer|null $maxHeight - * @param integer|null $size * @param boolean $nullable + * @param integer|null $size * @param string|null $requiredWith * @return array */ @@ -56,8 +56,8 @@ trait HasPicture ?int $minHeight = null, ?int $maxWidth = null, ?int $maxHeight = null, - ?int $size = null, bool $nullable = false, + ?int $size = null, ?string $requiredWith = null, ): array { $dimensions = (object)[ From d8449fdc652884ecbe36811e9c07440de64251f9 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Wed, 31 Jan 2024 16:04:36 +0100 Subject: [PATCH 3/3] Fixed picture height validation message --- app/Lib/Helpers/ToolboxHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Lib/Helpers/ToolboxHelper.php b/app/Lib/Helpers/ToolboxHelper.php index 49ea073..17db496 100644 --- a/app/Lib/Helpers/ToolboxHelper.php +++ b/app/Lib/Helpers/ToolboxHelper.php @@ -483,7 +483,7 @@ class ToolboxHelper ":attribute la hauteur doit être comprise entre {$sizes->minHeight}px et {$sizes->maxHeight}px" ); if ($sizes->minHeight === $sizes->maxHeight) { - $yMessage = trans(":attribute la hauteur doit être de {$sizes->minWidth}px"); + $yMessage = trans(":attribute la hauteur doit être de {$sizes->minHeight}px"); } if (($width < $sizes->minWidth) or ($width > $sizes->maxWidth)) {