250 lines
6.6 KiB
PHP
250 lines
6.6 KiB
PHP
<?php
|
|
/*
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
|
|
*/
|
|
|
|
namespace Modules\MMFRestrictedCustomers\Http\Controllers;
|
|
|
|
use Validator;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Conversation;
|
|
use App\Http\Controllers\CustomersController as BaseCustomersController;
|
|
|
|
use Modules\MMFRestrictedCustomers\Entities\Customer;
|
|
|
|
class CustomersController extends BaseCustomersController {
|
|
/**
|
|
* Edit customer.
|
|
*/
|
|
public function update($id) {
|
|
$customer = Customer::findOrFail($id);
|
|
|
|
// Get the list of Mailboxes the current User has access to.
|
|
$mailboxes = auth()
|
|
->user()
|
|
->mailboxesCanView();
|
|
|
|
$customer_emails = $customer->emails;
|
|
if (count($customer_emails)) {
|
|
foreach ($customer_emails as $row) {
|
|
$emails[] = $row->email;
|
|
}
|
|
} else {
|
|
$emails = [''];
|
|
}
|
|
|
|
return view('mmfrestrictedcustomers::customers/update', [
|
|
'customer' => $customer,
|
|
'mailboxes' => $mailboxes,
|
|
'emails' => $emails,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* View customer conversations.
|
|
*
|
|
* @param intg $id
|
|
*/
|
|
public function conversations($id) {
|
|
// TODO: Find a way to call parent::conversations while only overriding the Customer class,
|
|
// instead of overriding the whole method here.
|
|
$customer = Customer::findOrFail($id);
|
|
|
|
$conversations = $customer->conversations()
|
|
->where('customer_id', $customer->id)
|
|
->whereIn('mailbox_id', auth()->user()->mailboxesIdsCanView())
|
|
->orderBy('created_at', 'desc')
|
|
->paginate(Conversation::DEFAULT_LIST_SIZE);
|
|
|
|
return view('customers/conversations', [
|
|
'customer' => $customer,
|
|
'conversations' => $conversations,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Customers ajax search.
|
|
*/
|
|
public function ajaxSearch(Request $request) {
|
|
// TODO: Find a way to call parent::ajaxSearch while only overriding the Customer class,
|
|
// instead of overriding the whole method here.
|
|
$response = [
|
|
'results' => [],
|
|
'pagination' => ['more' => false],
|
|
];
|
|
|
|
$q = $request->q;
|
|
|
|
$join_emails = false;
|
|
if ($request->search_by == 'all' || $request->search_by == 'email' || $request->exclude_email) {
|
|
$join_emails = true;
|
|
}
|
|
|
|
$select_list = ['customers.id', 'first_name', 'last_name'];
|
|
if ($join_emails) {
|
|
$select_list[] = 'emails.email';
|
|
}
|
|
if ($request->show_fields == 'phone') {
|
|
$select_list[] = 'phones';
|
|
}
|
|
$customers_query = Customer::select($select_list);
|
|
|
|
if ($join_emails) {
|
|
if ($request->allow_non_emails) {
|
|
$customers_query->leftJoin('emails', 'customers.id', '=', 'emails.customer_id');
|
|
} else {
|
|
$customers_query->join('emails', 'customers.id', '=', 'emails.customer_id');
|
|
}
|
|
}
|
|
|
|
// Group the search terms query to avoid them from messing up the restriction by Mailbox.
|
|
$customers_query->where(function($customers_query) use($request, $q) {
|
|
if ($request->search_by == 'all' || $request->search_by == 'email') {
|
|
$customers_query->where('emails.email', 'like', '%'.$q.'%');
|
|
}
|
|
if ($request->exclude_email) {
|
|
$customers_query->where('emails.email', '<>', $request->exclude_email);
|
|
}
|
|
if ($request->search_by == 'all' || $request->search_by == 'name') {
|
|
$customers_query->orWhere('first_name', 'like', '%'.$q.'%')
|
|
->orWhere('last_name', 'like', '%'.$q.'%');
|
|
}
|
|
if ($request->search_by == 'phone') {
|
|
$phone_numeric = \Helper::phoneToNumeric($q);
|
|
if (!$phone_numeric) {
|
|
$phone_numeric = $q;
|
|
}
|
|
$customers_query->where('customers.phones', 'like', '%'.$phone_numeric.'%');
|
|
}
|
|
});
|
|
|
|
// Get the list of Mailboxes the current User has access to.
|
|
$user = auth()->user();
|
|
$mailboxes = $user->mailboxesIdsCanView();
|
|
|
|
// Restrict the query to the Customers the current User is allowed to access.
|
|
$customers_query->whereIn('customers.mailbox_id', $mailboxes);
|
|
|
|
$customers = $customers_query->paginate(20);
|
|
|
|
foreach ($customers as $customer) {
|
|
$id = '';
|
|
$text = '';
|
|
|
|
if ($request->show_fields != 'all') {
|
|
switch ($request->show_fields) {
|
|
case 'email':
|
|
$text = $customer->email;
|
|
break;
|
|
case 'name':
|
|
$text = $customer->getFullName();
|
|
break;
|
|
case 'phone':
|
|
// Get phone which matches
|
|
$phones = $customer->getPhones();
|
|
foreach ($phones as $phone) {
|
|
$phone_numeric = \Helper::phoneToNumeric($q);
|
|
if (strstr($phone['value'], $q) || strstr($phone['n'] ?? '', $phone_numeric)) {
|
|
$text = $phone['value'];
|
|
if ($customer->getFullName()) {
|
|
$text .= ' — '.$customer->getFullName();
|
|
}
|
|
$id = $phone['value'];
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
$text = $customer->getNameAndEmail();
|
|
break;
|
|
}
|
|
} else {
|
|
$text = $customer->getNameAndEmail();
|
|
}
|
|
|
|
if (!$id) {
|
|
if (!empty($request->use_id)) {
|
|
$id = $customer->id;
|
|
} else {
|
|
$id = $customer->getMainEmail();
|
|
}
|
|
}
|
|
$response['results'][] = [
|
|
'id' => $id,
|
|
'text' => $text,
|
|
];
|
|
}
|
|
|
|
$response['pagination']['more'] = $customers->hasMorePages();
|
|
|
|
return \Response::json($response);
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
];
|
|
|
|
$user = auth()->user();
|
|
|
|
switch ($request->action) {
|
|
|
|
// Change conversation user
|
|
case 'create':
|
|
// First name or email must be specified
|
|
$validator = Validator::make($request->all(), [
|
|
'first_name' => 'required|string|max:255',
|
|
'last_name' => 'nullable|string|max:255',
|
|
'email' => 'required|email|unique:emails,email',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
foreach ($validator->errors()->getMessages()as $errors) {
|
|
foreach ($errors as $field => $message) {
|
|
$response['msg'] .= $message.' ';
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$response['msg']) {
|
|
$customer = Customer::create($request->email, $request->all());
|
|
if ($customer) {
|
|
$response['email'] = $request->email;
|
|
$response['status'] = 'success';
|
|
}
|
|
}
|
|
break;
|
|
|
|
// Conversations navigation
|
|
case 'customers_pagination':
|
|
|
|
$customers = app('App\Http\Controllers\ConversationsController')->searchCustomers($request, $user);
|
|
|
|
$response['status'] = 'success';
|
|
|
|
$response['html'] = view('mmfrestrictedcustomers::customers/partials/customers_table', [
|
|
'customers' => $customers,
|
|
])->render();
|
|
break;
|
|
|
|
default:
|
|
$response['msg'] = 'Unknown action';
|
|
break;
|
|
}
|
|
|
|
if ($response['status'] == 'error' && empty($response['msg'])) {
|
|
$response['msg'] = 'Unknown error occurred';
|
|
}
|
|
|
|
return \Response::json($response);
|
|
}
|
|
}
|