Improve the sorting of e-mails when creating/editing a group

This commit is contained in:
Antoine Le Gonidec 2024-07-27 18:08:45 +02:00
parent 680e6b22c0
commit 42b9cbdba9
Signed by: vv221
GPG key ID: 636B78F91CEB80D8
2 changed files with 39 additions and 6 deletions

View file

@ -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,
]);
}

View file

@ -108,7 +108,9 @@
checked="checked"
@endif
>
{{ $email->email }}
{{ $email->customer->last_name }}
{{ $email->customer->first_name }}
<{{ $email->email }}>
</label>
</div>
</div>