Compare commits

...

2 commits

6 changed files with 238 additions and 8 deletions

View file

@ -13,6 +13,6 @@ class Customer extends BaseCustomer {
* Get the groups this Customer belongs to. * Get the groups this Customer belongs to.
*/ */
public function groups() { public function groups() {
return $this->belongsToMany(CustomersGroup::class); return $this->belongsToMany(CustomersGroup::class, 'customers_group_customers');
} }
} }

View file

@ -9,6 +9,8 @@ namespace Modules\MMFCustomersGroups\Entities;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class CustomersGroup extends Model { class CustomersGroup extends Model {
public $timestamps = false;
/** /**
* Get the Mailbox this group is linked to. * Get the Mailbox this group is linked to.
*/ */
@ -20,6 +22,18 @@ class CustomersGroup extends Model {
* Get the Customers that are included in this group. * Get the Customers that are included in this group.
*/ */
public function customers() { public function customers() {
return $this->belongsToMany(Customer::class); return $this->belongsToMany(Customer::class, 'customers_group_customers');
}
/**
* Get the list of Emails that are included in this group.
*/
public function emails() {
$customers = $this->customers;
// Get a list of Emails from the list of Customers.
$emails = $customers
->pluck('emails')
->flatten();
return $emails;
} }
} }

View file

@ -6,8 +6,14 @@
namespace Modules\MMFCustomersGroups\Http\Controllers; namespace Modules\MMFCustomersGroups\Http\Controllers;
use Eventy;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller; use Illuminate\Routing\Controller;
use App\Email;
use Modules\MMFCustomersGroups\Entities\Customer;
use Modules\MMFCustomersGroups\Entities\CustomersGroup; use Modules\MMFCustomersGroups\Entities\CustomersGroup;
class CustomersGroupsController extends Controller { class CustomersGroupsController extends Controller {
@ -27,10 +33,57 @@ class CustomersGroupsController extends Controller {
} }
public function create() { public function create() {
// TODO $group = new CustomersGroup();
return $this->update($group);
} }
public function update() { public function update(CustomersGroup $group) {
// TODO // 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');
return view('mmfcustomersgroups::groups/edit', [
'mailboxes' => $mailboxes,
'customers' => $customers,
'emails' => $emails,
'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();
} }
} }

View file

@ -13,8 +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::get('/groups/update', ['uses' => CustomersGroupsController::class . '@update', 'laroute' => true])->name('groups.update'); 'uses' => CustomersGroupsController::class . '@list',
'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

@ -0,0 +1,18 @@
<?php
/*
SPDX-License-Identifier: AGPL-3.0-only
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
*/
?>
@extends('layouts.app')
@section('title', __('Add Customers Group'))
@section('content')
@include('mmfcustomersgroups::groups/partials/edit_form', ['save_button_title' => __('Add')])
@endsection
@section('javascript')
@parent
@endsection

View file

@ -0,0 +1,127 @@
<?php
/*
SPDX-License-Identifier: AGPL-3.0-only
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
*/
?>
@include('partials/flash_messages')
<div class="container form-container">
<div class="row">
<div class="col-xs-12">
<form class="form-horizontal margin-top" method="POST" action="" enctype="multipart/form-data">
{{ 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 -->
<div class="form-group{{ $errors->has('mailbox') ? ' has-error' : '' }} margin-bottom-0">
<label
for="mailbox"
class="col-sm-2 control-label"
>
{{ __('Mailbox') }}
</label>
<div class="col-sm-6">
<div class="multi-container">
<div class="multi-item">
<div>
<div class="input-group input-group-flex input-sized-lg">
<select class="form-control" name="mailbox" required>
@if($group->mailbox == null)
<option value="" disabled selected>---</option>
@endif
@foreach($mailboxes as $mailbox)
<option
value="{{ $mailbox->id }}"
@if($group->mailbox && $mailbox->id == $group->mailbox->id)
selected
@endif
>
{{ $mailbox->name }}
</option>
@endforeach
</select>
</div>
</div>
</div>
</div>
@include('partials/field_error', ['field'=>'mailbox'])
</div>
</div>
<!-- Mandatory field: The name of this Customers Group -->
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label
for="name"
class="col-sm-2 control-label"
>
{{ __('Name') }}
</label>
<div class="col-sm-6">
<input
id="name"
type="text"
class="form-control input-sized-lg"
name="name"
value="{{ old('name', $group->name) }}"
maxlength="255"
/>
@include('partials/field_error', ['field'=>'name'])
</div>
</div>
<!-- Mandatory field: The list of Emails included in this Customer Group -->
<div class="form-group margin-bottom-0">
<label
for="emails"
class="col-sm-2 control-label"
>
{{ __('Emails') }}
</label>
<div class="col-sm-6">
<div class="multi-container">
@foreach ( $emails as $email )
<div class="control-group">
<div class="controls">
<label
class="control-label checkbox"
for="email-{{ $email->id }}"
style="text-align: left;"
>
<input
type="checkbox"
name="emails[]"
id="email-{{ $email->id }}"
value="{{ $email->id }}"
@if ( $group->emails()->contains($email) )
checked="checked"
@endif
>
{{ $email->email }}
</label>
</div>
</div>
@endforeach
</div>
{{-- @include('partials/field_error', ['field'=>'emails.*']) --}}
</div>
</div>
<div class="form-group">
<div class="col-sm-6 col-sm-offset-2">
<button type="submit" class="btn btn-primary">
@if (!empty($save_button_title))
{{ $save_button_title }}
@else
{{ __('Save Group') }}
@endif
</button>
</div>
</div>
</form>
</div>
</div>
</div>