From b16c0e9128594adacf98cda94e3234b3182dbf8e Mon Sep 17 00:00:00 2001 From: Olivier Legat Date: Wed, 21 Jul 2021 12:57:50 +0200 Subject: [PATCH] adding kwaadpepper enums adding register mode setting providing app db settings in config() validating requirement/optionnality of registration field upon defined register mode --- app/Enums/LogType.php | 70 +------------------ app/Enums/SettingRegisterMode.php | 33 +++++++++ .../Controllers/Bo/SettingsController.php | 7 +- app/Http/Controllers/CustomerController.php | 2 + app/Http/Requests/CustomerRegisterRequest.php | 20 ++++++ app/Providers/AppServiceProvider.php | 10 +++ composer.json | 2 +- composer.lock | 2 +- config/app.php | 2 + resources/views/bo/settings.blade.php | 19 +++++ 10 files changed, 95 insertions(+), 72 deletions(-) create mode 100644 app/Enums/SettingRegisterMode.php diff --git a/app/Enums/LogType.php b/app/Enums/LogType.php index 2c6fbc0..a2c8c4c 100644 --- a/app/Enums/LogType.php +++ b/app/Enums/LogType.php @@ -2,11 +2,7 @@ namespace App\Enums; -/** - * mini classe enum indépendante - * de la dépendance - * //use Kwaadpepper\Enum\BaseEnum; - */ +use Kwaadpepper\Enum\BaseEnum; /** * @method static self debug() @@ -16,68 +12,6 @@ namespace App\Enums; * @method static self error() * @method static self critical() */ -//final class LogType extends BaseEnum -class LogType +final class LogType extends BaseEnum { - - /** - * @var string|int - * @readonly - */ - protected $value; - - /** @readonly */ - protected $label; - - private static function makeElement($label, $value = null) { - if ($value === null) { - $value = $label; - } - $obj = new static(); - $obj->value = $value; - $obj->label = $label; - - return $obj; - } - - /** - * @param string $name - * - * @return int|string - * - */ - public function __get(string $name) - { - if ($name === 'label') { - return $this->label; - } - - if ($name === 'value') { - return $this->value; - } - } - - public static function debug() { - return self::makeElement("debug"); - } - - public static function log() { - return self::makeElement("log"); - } - - public static function warning() { - return self::makeElement("warning"); - } - - public static function info() { - return self::makeElement("info"); - } - - public static function error() { - return self::makeElement("error"); - } - - public static function critical() { - return self::makeElement("critical"); - } } diff --git a/app/Enums/SettingRegisterMode.php b/app/Enums/SettingRegisterMode.php new file mode 100644 index 0000000..86d26b3 --- /dev/null +++ b/app/Enums/SettingRegisterMode.php @@ -0,0 +1,33 @@ + 'public', + 'salaries' => 'salaries', + ]; + } + + protected static function labels(): array + { + return [ + 'public' => __('Public'), + 'salaries' => __('Salaries'), + ]; + } + + public function __toString(): string + { + return (string)$this->label; + } +} diff --git a/app/Http/Controllers/Bo/SettingsController.php b/app/Http/Controllers/Bo/SettingsController.php index 305c21e..a352aa8 100644 --- a/app/Http/Controllers/Bo/SettingsController.php +++ b/app/Http/Controllers/Bo/SettingsController.php @@ -35,10 +35,13 @@ class SettingsController extends Controller } for($i = 0; $i < count($settings); $i++) { - $settings[$i]->value = $request->input($settings[$i]->key) === null ? 0 : $request->input($settings[$i]->key); + if(is_array($request->input($settings[$i]->key))) { + $settings[$i]->value = implode(',', $request->input($settings[$i]->key)); + } else { + $settings[$i]->value = $request->input($settings[$i]->key) === null ? 0 : $request->input($settings[$i]->key); + } $settings[$i]->save(); } - return redirect()->route('bo.settings')->with('success', 'Modifications enregistrés !'); } diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index ef9fa71..aa30475 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -10,6 +10,7 @@ use App\Models\Customers; use App\Models\Magasin; use App\Models\Order; use App\Models\PasswordReset; +use App\Models\Setting; use Exception; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -27,6 +28,7 @@ class CustomerController extends Controller if (Auth::guard('customer')->check()) { return redirect()->route('home'); } + return view('auth.register'); } diff --git a/app/Http/Requests/CustomerRegisterRequest.php b/app/Http/Requests/CustomerRegisterRequest.php index d792c86..c2754d4 100644 --- a/app/Http/Requests/CustomerRegisterRequest.php +++ b/app/Http/Requests/CustomerRegisterRequest.php @@ -5,6 +5,7 @@ namespace App\Http\Requests; use Carbon\Carbon; use Illuminate\Foundation\Http\FormRequest; use App\Rules\Adult; +use Illuminate\Validation\Rule; class CustomerRegisterRequest extends FormRequest { @@ -25,7 +26,26 @@ class CustomerRegisterRequest extends FormRequest */ public function rules() { + if(config('bosettings.register_salaries') && config('bosettings.register_public')) { + $matriculeRule = 'nullable|exists:salaries,matricule'; + $codeRule = ['nullable', + Rule::exists('salaries', 'code') + ->where('matricule', data_get($this->all(), 'matricule')) + ]; + } elseif (config('bosettings.register_salaries') && !config('bosettings.register_public')) { + $matriculeRule = 'required|exists:salaries,matricule'; + $codeRule = ['required', + Rule::exists('salaries', 'code') + ->where('matricule', data_get($this->all(), 'matricule')) + ]; + } else { + $matriculeRule = 'sometimes'; + $codeRule = 'sometimes'; + } + return [ + 'matricule' => $matriculeRule, + 'code' => $codeRule, 'lastname' => 'required|string|max:255', 'firstname' => 'required|string|max:255', 'phone' => 'sometimes|max:255', diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 6af8087..d590788 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -39,6 +39,16 @@ class AppServiceProvider extends ServiceProvider setlocale(LC_TIME, strtoupper(config('app.locale')) . "_" . config('app.locale') . '.utf8'); } + //bo settings as config + config([ + 'bosettings' => Setting::all() + ->keyBy('key') // key every setting by its name + ->transform(function ($setting) { + return $setting->value; // return only the value + }) + ->toArray() // make it an array + ]); + //this is useful when setting up the app whene ranges table doesn't exist yet try { // on partage les ranges (gammes produit) diff --git a/composer.json b/composer.json index d8e6bd5..0e78142 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "fideloper/proxy": "^4.4", "fruitcake/laravel-cors": "^2.0", "guzzlehttp/guzzle": "^7.0.1", - "kwaadpepper/enum": "^0.1.3", + "kwaadpepper/enum": "^0.1.4", "laravel/framework": "^8.12", "laravel/tinker": "^2.5", "spatie/laravel-sitemap": "^5.8" diff --git a/composer.lock b/composer.lock index 1150287..4149116 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": "b28ee503421668121a44e5e711c9b199", + "content-hash": "87c02043e13235111043042aabbfc13f", "packages": [ { "name": "asm89/stack-cors", diff --git a/config/app.php b/config/app.php index 5a665aa..d834272 100644 --- a/config/app.php +++ b/config/app.php @@ -240,6 +240,8 @@ return [ 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, //Enums + 'LogType' => \App\Enums\LogType::class, + 'SettingRegisterMode' => \App\Enums\SettingRegisterMode::class, ], ]; diff --git a/resources/views/bo/settings.blade.php b/resources/views/bo/settings.blade.php index 458befe..331a3b1 100644 --- a/resources/views/bo/settings.blade.php +++ b/resources/views/bo/settings.blade.php @@ -66,5 +66,24 @@ Information lorsqu'un client valide une commande +
+ Mode d'inscription + @foreach(SettingRegisterMode::toArray() as $registermode) + @if ($loop->first) +
+ @endif + + @if($loop->last) +
+ @else +
+
+ @endif + @endforeach +
@endsection