Add the ability to edit an existing Customers Group

This commit is contained in:
Antoine Le Gonidec 2024-07-17 16:21:51 +02:00
parent 1f03f4992a
commit 1a9d95bb04
Signed by: vv221
GPG key ID: 636B78F91CEB80D8
4 changed files with 48 additions and 21 deletions

View file

@ -33,6 +33,11 @@ class CustomersGroupsController extends Controller {
} }
public function create() { 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. // Get the list of Mailboxes the current User is allowed to access.
$user = auth()->user(); $user = auth()->user();
$mailboxes = $user->mailboxesCanView(); $mailboxes = $user->mailboxesCanView();
@ -49,9 +54,7 @@ class CustomersGroupsController extends Controller {
->flatten() ->flatten()
->sortBy('email'); ->sortBy('email');
$group = new CustomersGroup(); return view('mmfcustomersgroups::groups/edit', [
return view('mmfcustomersgroups::groups/create', [
'mailboxes' => $mailboxes, 'mailboxes' => $mailboxes,
'customers' => $customers, 'customers' => $customers,
'emails' => $emails, 'emails' => $emails,
@ -59,11 +62,18 @@ class CustomersGroupsController extends Controller {
]); ]);
} }
public function update() {
// TODO
}
public function save(Request $request) { 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. // Convert the list of Emails into a list of Customers.
foreach ( $request->emails as $email_id ) { foreach ( $request->emails as $email_id ) {
$email = Email::find($email_id); $email = Email::find($email_id);
@ -72,13 +82,7 @@ class CustomersGroupsController extends Controller {
// Drop duplicates from the list of Customers. // Drop duplicates from the list of Customers.
$customers = collect($customers)->unique(); $customers = collect($customers)->unique();
$customers_id = $customers->pluck('id'); $customers_id = $customers->pluck('id');
$group->customers()->sync($customers_id);
$group = new CustomersGroup();
$group->name = $request->name;
$group->mailbox_id = $request->mailbox;
// Save early to set the Group id, before setting the relationships.
$group->save();
$group->customers()->attach($customers_id);
return $this->list(); return $this->list();
} }

View file

@ -13,9 +13,26 @@ Route::group(
'namespace' => 'Modules\MMFCustomersGroups\Http\Controllers' 'namespace' => 'Modules\MMFCustomersGroups\Http\Controllers'
], ],
function() { function() {
Route::get('/groups/list', ['uses' => CustomersGroupsController::class . '@list', 'laroute' => true])->name('groups.list'); // TODO: Check if the "laroute" property is required.
Route::get('/groups/create', ['uses' => CustomersGroupsController::class . '@create', 'laroute' => true])->name('groups.create'); Route::get('/groups/list', [
Route::post('/groups/create', ['uses' => CustomersGroupsController::class . '@save', 'laroute' => true]); 'uses' => CustomersGroupsController::class . '@list',
Route::get('/groups/update', ['uses' => CustomersGroupsController::class . '@update', 'laroute' => true])->name('groups.update'); 'laroute' => true,
])->name('groups.list');
Route::get('/groups/create', [
'uses' => CustomersGroupsController::class . '@create',
'laroute' => true,
])->name('groups.create');
Route::post('/groups/create', [
'uses' => CustomersGroupsController::class . '@save',
'laroute' => true,
]);
Route::get('/groups/{group}/edit', [
'uses' => CustomersGroupsController::class . '@update',
'laroute' => true,
])->name('groups.update');
Route::post('/groups/{group}/edit', [
'uses' => CustomersGroupsController::class . '@save',
'laroute' => true,
]);
} }
); );

View file

@ -12,7 +12,13 @@
<div class="col-xs-12"> <div class="col-xs-12">
<form class="form-horizontal margin-top" method="POST" action="" enctype="multipart/form-data"> <form class="form-horizontal margin-top" method="POST" action="" enctype="multipart/form-data">
{{ csrf_field() }} {{ csrf_field() }}
<!-- Hidden field: The id of the Customers Group that is currently being edited -->
<input
id="customer_id"
type="hidden"
name="customer_id"
value="{{ $group->id }}"
/>
<!-- Mandatory field: The Mailbox this Customers Group is linked to --> <!-- Mandatory field: The Mailbox this Customers Group is linked to -->
<div class="form-group{{ $errors->has('mailbox') ? ' has-error' : '' }} margin-bottom-0"> <div class="form-group{{ $errors->has('mailbox') ? ' has-error' : '' }} margin-bottom-0">
<label <label
@ -27,13 +33,13 @@
<div> <div>
<div class="input-group input-group-flex input-sized-lg"> <div class="input-group input-group-flex input-sized-lg">
<select class="form-control" name="mailbox" required> <select class="form-control" name="mailbox" required>
@if($group->mailbox_id == null) @if($group->mailbox == null)
<option value="" disabled selected>---</option> <option value="" disabled selected>---</option>
@endif @endif
@foreach($mailboxes as $mailbox) @foreach($mailboxes as $mailbox)
<option <option
value="{{ $mailbox->id }}" value="{{ $mailbox->id }}"
@if($mailbox->id == $group->mailbox_id) @if($group->mailbox && $mailbox->id == $group->mailbox->id)
selected selected
@endif @endif
> >