diff --git a/app/Http/Controllers/Contacts/IntroductionsController.php b/app/Http/Controllers/Contacts/IntroductionsController.php index 9cf396c9300..714115e2601 100644 --- a/app/Http/Controllers/Contacts/IntroductionsController.php +++ b/app/Http/Controllers/Contacts/IntroductionsController.php @@ -26,6 +26,7 @@ public function edit(Contact $contact) $contacts = $contact->siblingContacts() ->real() ->active() + ->orderByUserPreference() ->paginate(20); $introducer = $contact->getIntroducer(); diff --git a/app/Models/Contact/Contact.php b/app/Models/Contact/Contact.php index ea610b63774..48aeae8065d 100644 --- a/app/Models/Contact/Contact.php +++ b/app/Models/Contact/Contact.php @@ -638,6 +638,51 @@ public function scopeAddressBook($query, int $accountId = null, string $addressB return $query->where('address_book_id', $addressBook ? $addressBook->id : null); } + /** + * Get contacts ordered by user preferences. + * + * @param Builder $query + * @return Builder + */ + public function scopeOrderByUserPreference(Builder $query): Builder + { + switch (Auth::user()->name_order) { + case 'firstname_lastname': + $query = $query->orderBy('first_name') + ->orderBy('last_name'); + break; + case 'firstname_lastname_nickname': + $query = $query->orderBy('first_name') + ->orderBy('last_name') + ->orderBy('nickname'); + break; + case 'firstname_nickname_lastname': + $query = $query->orderBy('first_name') + ->orderBy('nickname') + ->orderBy('last_name'); + break; + case 'nickname': + $query = $query->orderBy('nickname'); + break; + case 'lastname_firstname': + $query = $query->orderBy('last_name') + ->orderby('first_name'); + break; + case 'lastname_firstname_nickname': + $query = $query->orderBy('last_name') + ->orderby('first_name') + ->orderby('nickname'); + break; + case 'lastname_nickname_firstname': + $query = $query->orderBy('last_name') + ->orderby('nickname') + ->orderby('first_name'); + break; + } + + return $query; + } + /** * Mutator first_name. * Get the first name of the contact.