From d3217c067e642614c3f2176e963f41d77a1aed29 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sun, 6 Jun 2021 14:03:15 +0200 Subject: [PATCH] fix: fix name order selection and result (#5255) --- app/Helpers/FormHelper.php | 9 ++----- app/Http/Controllers/SettingsController.php | 15 +----------- app/Http/Requests/SettingsRequest.php | 26 +++++++++++++++++---- app/Models/User/User.php | 20 +++++++++++++++- app/Services/VCard/ImportVCard.php | 3 ++- resources/lang/en/settings.php | 2 +- resources/views/settings/index.blade.php | 4 ++-- 7 files changed, 48 insertions(+), 31 deletions(-) diff --git a/app/Helpers/FormHelper.php b/app/Helpers/FormHelper.php index b7f1f53431f..65ba72f5fb4 100644 --- a/app/Helpers/FormHelper.php +++ b/app/Helpers/FormHelper.php @@ -15,18 +15,13 @@ class FormHelper */ public static function getNameOrderForForms(User $user): string { - $nameOrder = ''; + $nameOrder = 'firstname'; switch ($user->name_order) { - case 'firstname_lastname': - case 'firstname_lastname_nickname': - case 'firstname_nickname_lastname': - case 'nickname': - $nameOrder = 'firstname'; - break; case 'lastname_firstname': case 'lastname_firstname_nickname': case 'lastname_nickname_firstname': + case 'nickname_lastname_firstname': $nameOrder = 'lastname'; break; } diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 0cae83ad3f3..316cfa230fc 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -48,19 +48,6 @@ public function __construct() */ public function index() { - // names order - $namesOrder = [ - 'firstname_lastname', - 'lastname_firstname', - 'firstname_lastname_nickname', - 'firstname_nickname_lastname', - 'lastname_firstname_nickname', - 'lastname_nickname_firstname', - 'nickname_firstname_lastname', - 'nickname_lastname_firstname', - 'nickname', - ]; - $meContact = null; $search = auth()->user()->first_name.' '. @@ -83,7 +70,7 @@ public function index() ->withAccountHasLimitations($accountHasLimitations) ->withMeContact($meContact ? new ContactResource($meContact) : null) ->withExistingContacts(ContactResource::collection($existingContacts)) - ->withNamesOrder($namesOrder) + ->withNamesOrder(User::NAMES_ORDER) ->withLocales(LocaleHelper::getLocaleList()->sortByCollator('name-orig')) ->withHours(DateHelper::getListOfHours()) ->withSelectedTimezone(TimezoneHelper::adjustEquivalentTimezone(DateHelper::getTimezone())) diff --git a/app/Http/Requests/SettingsRequest.php b/app/Http/Requests/SettingsRequest.php index 7ab06494ef3..20c9995f0bf 100644 --- a/app/Http/Requests/SettingsRequest.php +++ b/app/Http/Requests/SettingsRequest.php @@ -2,6 +2,9 @@ namespace App\Http\Requests; +use App\Models\User\User; +use Illuminate\Validation\Rule; + class SettingsRequest extends AuthorizedRequest { /** @@ -15,11 +18,24 @@ public function rules() 'first_name' => 'required|max:255', 'last_name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users,email,'.$this->id, - 'timezone' => '', - 'layout' => '', - 'temperature_scale' => '', - 'locale' => '', - 'currency_id' => '', + 'timezone' => 'required|string', + 'layout' => 'required|bool', + 'temperature_scale' => [ + 'required', + 'string', + Rule::in(['fahrenheit', 'celsius']), + ], + 'locale' => [ + 'required', + 'string', + Rule::In(config('lang-detector.languages')), + ], + 'currency_id' => 'required|int|exists:currencies,id', + 'name_order' => [ + 'required', + 'string', + Rule::In(User::NAMES_ORDER), + ], ]; } } diff --git a/app/Models/User/User.php b/app/Models/User/User.php index 37977e64579..1370ba4f180 100644 --- a/app/Models/User/User.php +++ b/app/Models/User/User.php @@ -3,6 +3,7 @@ namespace App\Models\User; use Carbon\Carbon; +use App\Helpers\FormHelper; use App\Jobs\SendVerifyEmail; use App\Models\Settings\Term; use App\Models\Account\Account; @@ -66,6 +67,23 @@ class User extends Authenticatable implements MustVerifyEmail, HasLocalePreferen 'admin' => 'boolean', ]; + /** + * Available names order. + * + * @var array + */ + public const NAMES_ORDER = [ + 'firstname_lastname', + 'lastname_firstname', + 'firstname_lastname_nickname', + 'firstname_nickname_lastname', + 'lastname_firstname_nickname', + 'lastname_nickname_firstname', + 'nickname_firstname_lastname', + 'nickname_lastname_firstname', + 'nickname', + ]; + /** * Get the account record associated with the user. * @@ -151,7 +169,7 @@ public function getNameAttribute(): string { $completeName = ''; - if ($this->name_order == 'firstname_lastname' || $this->name_order == 'firstname_lastname_nickname') { + if (FormHelper::getNameOrderForForms($this) === 'firstname') { $completeName = $this->first_name; if ($this->last_name !== '') { diff --git a/app/Services/VCard/ImportVCard.php b/app/Services/VCard/ImportVCard.php index 98cd1d1cfac..ef6b543bae7 100644 --- a/app/Services/VCard/ImportVCard.php +++ b/app/Services/VCard/ImportVCard.php @@ -8,6 +8,7 @@ use function Safe\substr; use Sabre\VObject\Reader; use App\Helpers\DateHelper; +use App\Helpers\FormHelper; use Illuminate\Support\Arr; use Illuminate\Support\Str; use App\Helpers\VCardHelper; @@ -626,7 +627,7 @@ private function importFromFN(Contact $contact, VCard $entry): void $user = User::where('account_id', $this->accountId) ->findOrFail($this->userId); - if ($user->name_order == 'firstname_lastname' || $user->name_order == 'firstname_lastname_nickname') { + if (FormHelper::getNameOrderForForms($user) === 'firstname') { $contact->first_name = $this->formatValue($fullnameParts[0]); if (count($fullnameParts) > 1) { $contact->last_name = $this->formatValue($fullnameParts[1]); diff --git a/resources/lang/en/settings.php b/resources/lang/en/settings.php index 684123ed104..8ac38f13230 100644 --- a/resources/lang/en/settings.php +++ b/resources/lang/en/settings.php @@ -50,7 +50,7 @@ 'name_order_lastname_firstname_nickname' => ' () – Doe John (Rambo)', 'name_order_lastname_nickname_firstname' => ' () – Doe (Rambo) John', 'name_order_nickname_firstname_lastname' => ' ( ) – Rambo (John Doe)', - 'name_order_nickname_firstname_lastname' => ' ( ) – Rambo (Doe John)', + 'name_order_nickname_lastname_firstname' => ' ( ) – Rambo (Doe John)', 'name_order_nickname' => ' – Rambo', 'currency' => 'Currency', 'name' => 'Your name: :name', diff --git a/resources/views/settings/index.blade.php b/resources/views/settings/index.blade.php index 7d9b4eea6e7..2f073774192 100644 --- a/resources/views/settings/index.blade.php +++ b/resources/views/settings/index.blade.php @@ -136,8 +136,8 @@