From 1f03f4992a8cad8b59a3ab8a76944654477f2abf Mon Sep 17 00:00:00 2001 From: Antoine Le Gonidec Date: Wed, 17 Jul 2024 14:45:39 +0200 Subject: [PATCH] Add the ability to create a new Customers Group --- Entities/Customer.php | 2 +- Entities/CustomersGroup.php | 16 ++- .../Controllers/CustomersGroupsController.php | 51 +++++++- Http/routes.php | 1 + Resources/views/groups/create.blade.php | 18 +++ .../views/groups/partials/edit_form.blade.php | 121 ++++++++++++++++++ 6 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 Resources/views/groups/create.blade.php create mode 100644 Resources/views/groups/partials/edit_form.blade.php diff --git a/Entities/Customer.php b/Entities/Customer.php index 1a16936..85cf919 100644 --- a/Entities/Customer.php +++ b/Entities/Customer.php @@ -13,6 +13,6 @@ class Customer extends BaseCustomer { * Get the groups this Customer belongs to. */ public function groups() { - return $this->belongsToMany(CustomersGroup::class); + return $this->belongsToMany(CustomersGroup::class, 'customers_group_customers'); } } diff --git a/Entities/CustomersGroup.php b/Entities/CustomersGroup.php index 75b70f4..c6bdf56 100644 --- a/Entities/CustomersGroup.php +++ b/Entities/CustomersGroup.php @@ -9,6 +9,8 @@ namespace Modules\MMFCustomersGroups\Entities; use Illuminate\Database\Eloquent\Model; class CustomersGroup extends Model { + public $timestamps = false; + /** * 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. */ 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; } } diff --git a/Http/Controllers/CustomersGroupsController.php b/Http/Controllers/CustomersGroupsController.php index d1a0e46..9195f42 100644 --- a/Http/Controllers/CustomersGroupsController.php +++ b/Http/Controllers/CustomersGroupsController.php @@ -6,8 +6,14 @@ 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 { @@ -27,10 +33,53 @@ class CustomersGroupsController extends Controller { } public function create() { - // 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'); + + $group = new CustomersGroup(); + + return view('mmfcustomersgroups::groups/create', [ + 'mailboxes' => $mailboxes, + 'customers' => $customers, + 'emails' => $emails, + 'group' => $group, + ]); } public function update() { // TODO } + + public function save(Request $request) { + // 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 = 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(); + } } diff --git a/Http/routes.php b/Http/routes.php index 6114f3b..9716617 100644 --- a/Http/routes.php +++ b/Http/routes.php @@ -15,6 +15,7 @@ Route::group( function() { Route::get('/groups/list', ['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/update', ['uses' => CustomersGroupsController::class . '@update', 'laroute' => true])->name('groups.update'); } ); diff --git a/Resources/views/groups/create.blade.php b/Resources/views/groups/create.blade.php new file mode 100644 index 0000000..7086ceb --- /dev/null +++ b/Resources/views/groups/create.blade.php @@ -0,0 +1,18 @@ + + */ +?> + +@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 diff --git a/Resources/views/groups/partials/edit_form.blade.php b/Resources/views/groups/partials/edit_form.blade.php new file mode 100644 index 0000000..47bc397 --- /dev/null +++ b/Resources/views/groups/partials/edit_form.blade.php @@ -0,0 +1,121 @@ + + */ +?> + +@include('partials/flash_messages') + +
+
+
+
+ {{ csrf_field() }} + + +
+ +
+
+
+
+
+ +
+
+
+
+ @include('partials/field_error', ['field'=>'mailbox']) +
+
+ +
+ +
+ + @include('partials/field_error', ['field'=>'name']) +
+
+ +
+ +
+
+ @foreach ( $emails as $email ) +
+
+ +
+
+ @endforeach +
+ {{-- @include('partials/field_error', ['field'=>'emails.*']) --}} +
+
+
+
+ +
+
+
+
+
+