2024-07-03 12:02:11 +00:00
< ? php
/*
2024-07-06 16:56:09 +00:00
SPDX - License - Identifier : AGPL - 3.0 - only
2024-07-03 12:02:11 +00:00
SPDX - FileCopyrightText : © 2024 Millions Missing FRANCE < info @ millionsmissing . fr >
*/
2024-07-10 15:16:13 +00:00
namespace Modules\MMFRestrictedCustomers\Http\Controllers ;
2024-07-03 12:02:11 +00:00
use Validator ;
2024-07-11 09:55:29 +00:00
2024-07-03 12:02:11 +00:00
use Illuminate\Http\Request ;
2024-07-11 09:55:29 +00:00
use App\Conversation ;
2024-07-03 12:02:11 +00:00
use Modules\Crm\Http\Controllers\CrmController as BaseCrmController ;
2024-07-11 09:55:29 +00:00
2024-07-10 15:26:43 +00:00
use Modules\MMFRestrictedCustomers\Entities\Customer ;
use Modules\MMFRestrictedCustomers\Entities\Email ;
use Modules\MMFRestrictedCustomers\Entities\Mailbox ;
2024-07-03 12:02:11 +00:00
class CrmController extends BaseCrmController {
public function createCustomer ( Request $request ) {
$customer = new Customer ();
2024-07-05 12:18:54 +00:00
// Get the list of Mailboxes the current User has access to.
$user = auth () -> user ();
$mailboxes = $user -> mailboxesCanView ();
2024-07-10 15:16:13 +00:00
return view ( 'mmfrestrictedcustomers::create_customer' , [
2024-07-05 12:18:54 +00:00
'customer' => $customer ,
'mailboxes' => $mailboxes ,
'emails' => [ '' ],
2024-07-03 12:02:11 +00:00
]);
}
public function createCustomerSave ( Request $request ) {
2024-07-11 09:55:29 +00:00
// TODO: Find a way to call parent::createCustomerSave while only overriding the Email class,
2024-07-03 12:02:11 +00:00
// instead of overriding the whole method here.
$validator = Validator :: make ( $request -> all (), [
'first_name' => 'nullable|string|max:255|required_without:emails.0' ,
'last_name' => 'nullable|string|max:255' ,
'city' => 'nullable|string|max:255' ,
'state' => 'nullable|string|max:255' ,
'zip' => 'nullable|string|max:12' ,
'country' => 'nullable|string|max:2' ,
//'emails' => 'array|required_without:first_name',
//'emails.1' => 'nullable|email|required_without:first_name',
'emails.*' => 'nullable|email|distinct|required_without:first_name' ,
]);
$validator -> setAttributeNames ([
//'emails.1' => __('Email'),
'emails.*' => __ ( 'Email' ),
]);
// Check email uniqueness.
$fail = false ;
2024-07-09 12:00:52 +00:00
$mailbox = Mailbox :: find ( $request -> mailbox );
2024-07-03 12:02:11 +00:00
foreach ( $request -> emails as $i => $email ) {
$sanitized_email = Email :: sanitizeEmail ( $email );
if ( $sanitized_email ) {
2024-07-09 12:00:52 +00:00
$email_exists = Email :: alreadyExistsInMailbox ( $mailbox , $sanitized_email );
2024-07-03 12:02:11 +00:00
if ( $email_exists ) {
$validator -> getMessageBag () -> add ( 'emails.' . $i , __ ( 'A customer with this email already exists.' ));
$fail = true ;
}
}
}
if ( $fail || $validator -> fails ()) {
2024-07-10 15:16:13 +00:00
return redirect () -> route ( 'mmfrestrictedcustomers.create_customer' )
2024-07-03 12:02:11 +00:00
-> withErrors ( $validator )
-> withInput ();
}
$customer = new Customer ();
$customer -> setData ( $request -> all ());
$customer -> save ();
$customer -> syncEmails ( $request -> emails );
\Eventy :: action ( 'customer.created' , $customer );
\Session :: flash ( 'flash_success_unescaped' , __ ( 'Customer saved successfully.' ));
\Session :: flash ( 'customer.updated' , 1 );
// Create customer.
if ( $customer -> id ) {
return redirect () -> route ( 'customers.update' , [ 'id' => $customer -> id ]);
} else {
// Something went wrong.
return $this -> createCustomer ( $request );
}
}
/**
* Ajax controller .
*/
public function ajax ( Request $request ) {
// TODO: Find a way to call parent::ajax while only overriding the Customer class,
// instead of overriding the whole method here.
$response = [
'status' => 'error' ,
'msg' => '' , // this is error message
];
switch ( $request -> action ) {
// Delete customer.
case 'delete_customer' :
$has_conversations = Conversation :: where ( 'customer_id' , $request -> customer_id ) -> first ();
if ( $has_conversations ) {
$response [ 'msg' ] = __ ( " This customer has conversations. In order to delete the customer you need to completely delete all customer's conversations first. " );
}
if ( ! $response [ 'msg' ]) {
$customer = Customer :: find ( $request -> customer_id );
if ( $customer ) {
$customer -> deleteCustomer ();
$response [ 'msg_success' ] = __ ( 'Customer deleted' );
$response [ 'status' ] = 'success' ;
} else {
$response [ 'msg' ] = __ ( 'Customer not found' );
}
}
break ;
case 'delete_without_conv' :
// Delete customers by bunches.
do {
$customers = $this -> getCustomersWithoutConvQuery ()
-> limit ( 100 )
-> get ();
foreach ( $customers as $customer ) {
$customer -> deleteCustomer ();
}
} while ( count ( $customers ));
$response [ 'msg_success' ] = __ ( 'Customers deleted' );
$response [ 'status' ] = 'success' ;
break ;
default :
$response [ 'msg' ] = 'Unknown action' ;
break ;
}
if ( $response [ 'status' ] == 'error' && empty ( $response [ 'msg' ])) {
$response [ 'msg' ] = 'Unknown error occured' ;
}
return \Response :: json ( $response );
}
}