From 42b9cbdba90cbbb19084470c2f569c697139ee44 Mon Sep 17 00:00:00 2001 From: Antoine Le Gonidec Date: Sat, 27 Jul 2024 18:08:45 +0200 Subject: [PATCH] Improve the sorting of e-mails when creating/editing a group --- .../Controllers/CustomersGroupsController.php | 41 ++++++++++++++++--- .../views/groups/partials/edit_form.blade.php | 4 +- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Http/Controllers/CustomersGroupsController.php b/Http/Controllers/CustomersGroupsController.php index 3f1c8aa..fce0325 100644 --- a/Http/Controllers/CustomersGroupsController.php +++ b/Http/Controllers/CustomersGroupsController.php @@ -51,19 +51,50 @@ class CustomersGroupsController extends Controller { // Get a list of Emails from the list of Customers. $emails = $customers ->pluck('emails') - ->flatten() - ->sortBy('email'); - // Group Emails by their first character. + ->flatten(); + + // Group Emails by the first character of the Customer name. // This is used to display them in collapsible groups. $emails = $emails->mapToGroups(function ($email) { - $first_character = substr($email->email, $offset = 0, $length = 1); + $name = $email->customer->last_name ? + $email->customer->last_name : + $email->customer->first_name; + $first_character = strtoupper(substr( + $name ? $name : '?', + $offset = 0, + $length = 1, + )); + // Group all Emails without a Customer name starting with a letter together. + if ( ! preg_match($pattern = '/[A-Z]/', $string = $first_character) ) + $first_character = '?'; return [ $first_character => $email ]; }); + // Sort the groups of Emails. + $emails = $emails->all(); + ksort($emails); + // Sort the Emails inside each group. + $emails_sorted = []; + foreach ( $emails as $first_character => $emails_group ) { + $emails_sorted[$first_character] = $emails_group->sortBy(function ($email) { + if ( $email->customer->last_name ) { + $sort_string = $email->customer->last_name .' '. $email->customer->first_name .' '. $email->email; + } else if ( $email->customer->first_name ) { + $sort_string = $email->customer->first_name .' '. $email->email; + } else { + $sort_string = $email->email; + } + // Do an case-insensitive sort. + $sort_string = strtolower($sort_string); + // Convert diacritics to ASCII characters. + $sort_string = iconv('UTF-8', 'ASCII//TRANSLIT', $sort_string); + return $sort_string; + }); + } return view('mmfcustomersgroups::groups/edit', [ 'mailboxes' => $mailboxes, 'customers' => $customers, - 'emails' => $emails, + 'emails' => $emails_sorted, 'group' => $group, ]); } diff --git a/Resources/views/groups/partials/edit_form.blade.php b/Resources/views/groups/partials/edit_form.blade.php index 08284f0..3c6ef53 100644 --- a/Resources/views/groups/partials/edit_form.blade.php +++ b/Resources/views/groups/partials/edit_form.blade.php @@ -108,7 +108,9 @@ checked="checked" @endif > - {{ $email->email }} + {{ $email->customer->last_name }} + {{ $email->customer->first_name }} + <{{ $email->email }}>