freescout-customers-groups/Http/Controllers/CustomersGroupsController.php

126 lines
3.9 KiB
PHP

<?php
/*
SPDX-License-Identifier: AGPL-3.0-only
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
*/
namespace Modules\MMFCustomersGroups\Http\Controllers;
use Eventy;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use App\Email;
use Modules\MMFCustomersGroups\Entities\Customer;
use Modules\MMFCustomersGroups\Entities\CustomersGroup;
class CustomersGroupsController extends Controller {
public function list() {
// Get the list of Mailboxes the current User is allowed to access.
$user = auth()->user();
$mailboxes = $user->mailboxesIdsCanView();
// Get the list of Customers Groups, filtered by Mailbox.
$groups = CustomersGroup
::whereIn('mailbox_id', $mailboxes)
->get();
return view('mmfcustomersgroups::groups/list', [
'groups' => $groups,
]);
}
public function create() {
$group = new CustomersGroup();
return $this->update($group);
}
public function update(CustomersGroup $group) {
// Get the list of Mailboxes the current User is allowed to access.
$user = auth()->user();
$mailboxes = $user->mailboxesCanView();
// Get the list of Customers that can be attached to a group.
$customers_query = Customer::with('emails');
// Set a hook so the query can be filtered by other modules.
$customers_query = Eventy::filter('mmfcustomergroups.groups.create.customers-query', $customers_query);
$customers = $customers_query->get();
// Get a list of Emails from the list of Customers.
$emails = $customers
->pluck('emails')
->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) {
$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_sorted,
'group' => $group,
]);
}
public function save(Request $request) {
// Get the Customer Group that should be updated,
// or create a new one.
$group = ( $request->customer_id ) ?
CustomersGroup::find($request->customer_id):
new CustomersGroup();
$group->name = $request->name;
$group->mailbox_id = $request->mailbox;
// Save early to set the Group id, before setting the relationships.
$group->save();
// Convert the list of Emails into a list of Customers.
foreach ( $request->emails as $email_id ) {
$email = Email::find($email_id);
$customers[] = $email->customer;
}
// Drop duplicates from the list of Customers.
$customers = collect($customers)->unique();
$customers_id = $customers->pluck('id');
$group->customers()->sync($customers_id);
return $this->list();
}
}