Add the ability to create a new Customers Group
This commit is contained in:
parent
7e749d7cf4
commit
1f03f4992a
6 changed files with 206 additions and 3 deletions
|
@ -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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,53 @@ class CustomersGroupsController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create() {
|
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() {
|
public function update() {
|
||||||
// TODO
|
// 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ Route::group(
|
||||||
function() {
|
function() {
|
||||||
Route::get('/groups/list', ['uses' => CustomersGroupsController::class . '@list', 'laroute' => true])->name('groups.list');
|
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::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');
|
Route::get('/groups/update', ['uses' => CustomersGroupsController::class . '@update', 'laroute' => true])->name('groups.update');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
18
Resources/views/groups/create.blade.php
Normal file
18
Resources/views/groups/create.blade.php
Normal 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
|
121
Resources/views/groups/partials/edit_form.blade.php
Normal file
121
Resources/views/groups/partials/edit_form.blade.php
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
<?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() }}
|
||||||
|
|
||||||
|
<!-- 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_id == null)
|
||||||
|
<option value="" disabled selected>---</option>
|
||||||
|
@endif
|
||||||
|
@foreach($mailboxes as $mailbox)
|
||||||
|
<option
|
||||||
|
value="{{ $mailbox->id }}"
|
||||||
|
@if($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>
|
Loading…
Reference in a new issue