2024-07-16 14:54:04 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Modules\MMFCustomersGroups\Http\Controllers;
|
|
|
|
|
2024-07-17 12:45:39 +00:00
|
|
|
use Eventy;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2024-07-16 14:54:04 +00:00
|
|
|
use Illuminate\Routing\Controller;
|
|
|
|
|
2024-07-17 12:45:39 +00:00
|
|
|
use App\Email;
|
|
|
|
|
|
|
|
use Modules\MMFCustomersGroups\Entities\Customer;
|
2024-07-16 15:16:41 +00:00
|
|
|
use Modules\MMFCustomersGroups\Entities\CustomersGroup;
|
|
|
|
|
2024-07-16 14:54:04 +00:00
|
|
|
class CustomersGroupsController extends Controller {
|
|
|
|
public function list() {
|
2024-07-16 15:16:41 +00:00
|
|
|
// 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() {
|
2024-07-17 14:21:51 +00:00
|
|
|
$group = new CustomersGroup();
|
|
|
|
return $this->update($group);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(CustomersGroup $group) {
|
2024-07-17 12:45:39 +00:00
|
|
|
// 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()
|
|
|
|
->sortBy('email');
|
|
|
|
|
2024-07-17 14:21:51 +00:00
|
|
|
return view('mmfcustomersgroups::groups/edit', [
|
2024-07-17 12:45:39 +00:00
|
|
|
'mailboxes' => $mailboxes,
|
|
|
|
'customers' => $customers,
|
|
|
|
'emails' => $emails,
|
|
|
|
'group' => $group,
|
|
|
|
]);
|
2024-07-16 15:16:41 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 12:45:39 +00:00
|
|
|
public function save(Request $request) {
|
2024-07-17 14:21:51 +00:00
|
|
|
// 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();
|
|
|
|
|
2024-07-17 12:45:39 +00:00
|
|
|
// 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');
|
2024-07-17 14:21:51 +00:00
|
|
|
$group->customers()->sync($customers_id);
|
2024-07-17 12:45:39 +00:00
|
|
|
|
|
|
|
return $this->list();
|
|
|
|
}
|
2024-07-16 14:54:04 +00:00
|
|
|
}
|