From b754fefe70973049a60802f585b2d51558e499af Mon Sep 17 00:00:00 2001 From: Antoine Le Gonidec Date: Fri, 5 Jul 2024 14:18:54 +0200 Subject: [PATCH] Link Customer creation to a Mailbox This only covers Customer creation going through the dedicated form. --- resources/views/create_customer.blade.php | 12 + .../customers/partials/edit_form.blade.php | 301 ++++++++++++++++++ routes/web.php | 2 + src/Customer.php | 21 ++ ...coutRestrictedCustomersServiceProvider.php | 1 + src/Http/Controllers/CrmController.php | 15 +- 6 files changed, 346 insertions(+), 6 deletions(-) create mode 100644 resources/views/create_customer.blade.php create mode 100644 resources/views/customers/partials/edit_form.blade.php diff --git a/resources/views/create_customer.blade.php b/resources/views/create_customer.blade.php new file mode 100644 index 0000000..5d08703 --- /dev/null +++ b/resources/views/create_customer.blade.php @@ -0,0 +1,12 @@ +@extends('layouts.app') + +@section('title', 'Add Customer (MMF)') + +@section('content') + @include('freescout-restricted-customers::customers/partials/edit_form', ['save_button_title' => __('Add')]) +@endsection + +@section('javascript') + @parent + multiInputInit(); +@endsection diff --git a/resources/views/customers/partials/edit_form.blade.php b/resources/views/customers/partials/edit_form.blade.php new file mode 100644 index 0000000..f402d00 --- /dev/null +++ b/resources/views/customers/partials/edit_form.blade.php @@ -0,0 +1,301 @@ +@include('partials/flash_messages') + +@action('customer.edit.before_form', $customer) + +
+
+
+
+ {{ csrf_field() }} + + +
+ +
+
+
+
+
+ +
+
+
+

{{ __('Add a mailbox') }}

+
+ @include('partials/field_error', ['field'=>'mailbox']) +
+
+ +
+ + +
+ + + @include('partials/field_error', ['field'=>'first_name']) +
+
+ +
+ + +
+ + + @include('partials/field_error', ['field'=>'last_name']) +
+
+ +
+ + +
+
+ @foreach (old('emails', $emails) as $i => $email) +
+
+ + +
+ + @include('partials/field_error', ['field'=>'emails.'.$i]) +
+ @endforeach +

{{ __('Add an email address') }}

+
+ + {{-- @include('partials/field_error', ['field'=>'emails.*']) --}} +
+
+ +
+ + +
+
+ @foreach ($customer->getPhones(true) as $i => $phone) + @if (!empty($phone['type']) && isset($phone['value'])) +
+
+
+ + + @include('partials/field_error', ['field'=>'company']) +
+
+ +
+ + +
+ + + @include('partials/field_error', ['field'=>'job_title']) +
+
+ +
+ + +
+
+ @foreach ($customer->getWebsites(true) as $website) +
+
+ + +
+
+ @endforeach +

{{ __('Add a website') }}

+
+ + @include('partials/field_error', ['field'=>'websites']) +
+
+ +
+ + +
+
+ @foreach ($customer->getSocialProfiles(true) as $i => $social_profile) + @if (isset($social_profile['type']) && isset($social_profile['value'])) +
+
+
+ + + +
+ +
+
+ @endif + @endforeach +

{{ __('Add a social profile') }}

+
+ + @include('partials/field_error', ['field'=>'social_profiles']) +
+
+ + {{-- +
+ + +
+ + + @include('partials/field_error', ['field'=>'age']) +
+
+ +
+ + +
+ + + @include('partials/field_error', ['field'=>'gender']) +
+
+ --}} +
+ + +
+ + + + + @include('partials/field_error', ['field'=>'country']) +
+
+ +
state)) && empty(old('city', $customer->city)) && empty(old('address', $customer->address)) && empty(old('zip', $customer->zip)))@else class="collapse in" @endif> + +
+ + +
+ + + @include('partials/field_error', ['field'=>'state']) +
+
+ +
+ + +
+ + + @include('partials/field_error', ['field'=>'city']) +
+
+ +
+ + +
+ + + @include('partials/field_error', ['field'=>'address']) +
+
+ +
+ + +
+ + + @include('partials/field_error', ['field'=>'zip']) +
+
+
+ +
+ + +
+ +

(JPG, GIF, PNG)

+ + @include('partials/field_error', ['field'=>'photo_url']) +
+
+ +
+ + +
+ + + @include('partials/field_error', ['field'=>'notes']) +
+
+ + @action('customer.edit.after_fields', $customer, $errors) + +
+
+ +
+
+ +
+
+
diff --git a/routes/web.php b/routes/web.php index 902879f..386c705 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,6 +18,8 @@ Route::get('/customers/ajax-search', ['uses' => CustomersController::class . '@a Route::post('/customers/ajax', ['uses' => CustomersController::class . '@ajax', 'laroute' => true])->name('customers.ajax'); Route::group([ 'roles' => ['user', 'admin'] ], function() { + Route::get('/customers/new', CrmController::class . '@createCustomer')->name('freescout-restricted-customers.create_customer'); + // The Crm module initialization will crash if no route named "crm.create_customer" is set. Route::get('/customers/new', CrmController::class . '@createCustomer')->name('crm.create_customer'); Route::post('/customers/new', CrmController::class . '@createCustomerSave'); Route::get('/crm/ajax-html/{action}/{param?}', ['uses' => CrmController::class . '@ajaxHtml'])->name('crm.ajax_html'); diff --git a/src/Customer.php b/src/Customer.php index 2678d1c..23b116f 100644 --- a/src/Customer.php +++ b/src/Customer.php @@ -6,6 +6,12 @@ namespace MMF\FreescoutRestrictedCustomers; +use App\Email; +use App\CustomerChannel; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Storage; +use Watson\Rememberable\Rememberable; use MMF\FreescoutRestrictedCustomers\Mailbox; use App\Customer as BaseCustomer; @@ -43,4 +49,19 @@ class Customer extends BaseCustomer { public function mailbox() { return $this->belongsTo(Mailbox::class); } + + /** + * Set empty fields. + */ + public function setData($data, $replace_data = true, $save = false) { + // Set the Mailbox this Customer should be linked to. + // TODO: Throw an error if the Mailbox is not set. + if ( isset($data['mailbox']) ) { + // TODO: Check that the current user is allowed to access this Mailbox. + $data['mailbox_id'] = $data['mailbox']; + unset($data['mailbox']); + } + + return parent::setData($data, $replace_data, $save); + } } diff --git a/src/FreescoutRestrictedCustomersServiceProvider.php b/src/FreescoutRestrictedCustomersServiceProvider.php index 1524c24..b6d3553 100644 --- a/src/FreescoutRestrictedCustomersServiceProvider.php +++ b/src/FreescoutRestrictedCustomersServiceProvider.php @@ -17,6 +17,7 @@ class FreescoutRestrictedCustomersServiceProvider extends ServiceProvider { public function boot() { $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); $this->registerRoutes(); + $this->loadViewsFrom(__DIR__.'/../resources/views', 'freescout-restricted-customers'); } protected function registerRoutes() { diff --git a/src/Http/Controllers/CrmController.php b/src/Http/Controllers/CrmController.php index 8a482a5..bd7d1d5 100644 --- a/src/Http/Controllers/CrmController.php +++ b/src/Http/Controllers/CrmController.php @@ -19,13 +19,16 @@ use MMF\FreescoutRestrictedCustomers\Customer; class CrmController extends BaseCrmController { public function createCustomer(Request $request) { - // TODO: Find a way to call parent::createCustomer while only overriding the Customer class, - // instead of overriding the whole method here. $customer = new Customer(); - return view('crm::create_customer', [ - 'customer' => $customer, - 'emails' => [''] + // Get the list of Mailboxes the current User has access to. + $user = auth()->user(); + $mailboxes = $user->mailboxesCanView(); + + return view('freescout-restricted-customers::create_customer', [ + 'customer' => $customer, + 'mailboxes' => $mailboxes, + 'emails' => [''], ]); } @@ -63,7 +66,7 @@ class CrmController extends BaseCrmController { } if ($fail || $validator->fails()) { - return redirect()->route('crm.create_customer') + return redirect()->route('freescout-restricted-customers.create_customer') ->withErrors($validator) ->withInput(); }