| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Attachment; |
| 6 |
use FluentSupport\App\Models\Conversation; |
| 7 |
use FluentSupport\App\Models\Customer; |
| 8 |
use FluentSupport\App\Models\Ticket; |
| 9 |
use FluentSupport\App\Services\Helper; |
| 10 |
use FluentSupport\App\Services\Includes\FileSystem; |
| 11 |
use FluentSupport\App\Services\ProfileInfoService; |
| 12 |
use FluentSupport\Framework\Request\Request; |
| 13 |
use FluentSupport\Framework\Support\Arr; |
| 14 |
|
| 15 |
class CustomerController extends Controller |
| 16 |
{ |
| 17 |
public function index(Request $request) |
| 18 |
{ |
| 19 |
$customersQuery = Customer::orderBy('id', 'DESC') |
| 20 |
->orderBy($request->get('order_by', 'id'), $request->get('order_type', 'ASC')); |
| 21 |
|
| 22 |
if ($request->get('search')) { |
| 23 |
$customersQuery->searchBy($request->get('search')); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
$status = $request->get('status'); |
| 28 |
if ($status && $status != 'all') { |
| 29 |
$customersQuery->filterByStatues([$status]); |
| 30 |
} |
| 31 |
|
| 32 |
$customers = $customersQuery->paginate(); |
| 33 |
|
| 34 |
foreach ($customers as $customer) { |
| 35 |
$customer->total_tickets = $customer->getTicketCounts(); |
| 36 |
$customer->total_responses = $customer->getResponseCounts(); |
| 37 |
if ($customer->user_id) { |
| 38 |
$customer->user_profile = admin_url('user-edit.php?user_id=' . $customer->user_id); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return [ |
| 43 |
'customers' => $customers, |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
public function getCustomer(Request $request, $customerId) |
| 48 |
{ |
| 49 |
$customer = Customer::findOrFail($customerId); |
| 50 |
|
| 51 |
$data = [ |
| 52 |
'customer' => $customer |
| 53 |
]; |
| 54 |
|
| 55 |
$with = $request->get('with', []); |
| 56 |
|
| 57 |
if (in_array('widgets', $with)) { |
| 58 |
$data['widgets'] = ProfileInfoService::getProfileExtraWidgets($customer); |
| 59 |
} |
| 60 |
|
| 61 |
if (in_array('tickets', $with)) { |
| 62 |
$data['tickets'] = Ticket::select(['id', 'title', 'status', 'customer_id', 'created_at']) |
| 63 |
->where('customer_id', $customer->id) |
| 64 |
->orderBy('id', 'DESC') |
| 65 |
->limit(20) |
| 66 |
->get(); |
| 67 |
} |
| 68 |
|
| 69 |
if(in_array('fluentcrm_profile', $with)) { |
| 70 |
$data['fluentcrm_profile'] = Helper::getFluentCrmContactData($customer); |
| 71 |
} |
| 72 |
|
| 73 |
return $data; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
public function create(Request $request) |
| 78 |
{ |
| 79 |
$data = $request->all(); |
| 80 |
$this->validate($data, [ |
| 81 |
'email' => 'required|email|unique:fs_persons' |
| 82 |
]); |
| 83 |
|
| 84 |
$email = $data['email']; |
| 85 |
|
| 86 |
$data = Arr::only($data, (new Customer)->getFillable()); |
| 87 |
|
| 88 |
$user = get_user_by('email', $email); |
| 89 |
|
| 90 |
if ($user) { |
| 91 |
$data['user_id'] = $user->ID; |
| 92 |
if (empty($data['first_name'])) { |
| 93 |
$data['first_name'] = $user->first_name; |
| 94 |
} |
| 95 |
if (empty($data['last_name'])) { |
| 96 |
$data['last_name'] = $user->last_name; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
$customer = Customer::create($data); |
| 101 |
|
| 102 |
return [ |
| 103 |
'message' => __('Customer has been added', 'fluent-support'), |
| 104 |
'customer' => $customer |
| 105 |
]; |
| 106 |
} |
| 107 |
|
| 108 |
public function update(Request $request, $customerId) |
| 109 |
{ |
| 110 |
$customer = Customer::findOrFail($customerId); |
| 111 |
$data = $request->all(); |
| 112 |
$this->validate($data, [ |
| 113 |
'email' => 'required|email', |
| 114 |
'first_name' => 'required' |
| 115 |
]); |
| 116 |
|
| 117 |
if ($otherCustomer = Customer::where('id', '!=', $customerId)->where('email', $data['email'])->first()) { |
| 118 |
return $this->sendError([ |
| 119 |
'message' => __('Another Customer has same email address', 'fluent-support'), |
| 120 |
'errors' => [ |
| 121 |
'email' => [ |
| 122 |
'unique' => __('Email address has been assigned to other customer', 'fluent-support') |
| 123 |
] |
| 124 |
] |
| 125 |
], 423); |
| 126 |
} |
| 127 |
|
| 128 |
$validKeys = (new Customer)->getFillable(); |
| 129 |
unset($validKeys['hash']); |
| 130 |
unset($validKeys['user_id']); |
| 131 |
|
| 132 |
$updateData = Arr::only($data, $validKeys); |
| 133 |
|
| 134 |
$user = get_user_by('email', $data['email']); |
| 135 |
|
| 136 |
if ($user) { |
| 137 |
$updateData['user_id'] = $user->ID; |
| 138 |
} |
| 139 |
|
| 140 |
Customer::where('id', $customer->id) |
| 141 |
->update($updateData); |
| 142 |
|
| 143 |
return [ |
| 144 |
'message' => __('Customer has been updated', 'fluent-support'), |
| 145 |
'customer' => Customer::findOrFail($customerId) |
| 146 |
]; |
| 147 |
} |
| 148 |
|
| 149 |
public function delete(Request $request, $customerId) |
| 150 |
{ |
| 151 |
$customer = Customer::findOrFail($customerId); |
| 152 |
|
| 153 |
$tickets = Ticket::where('customer_id', $customer->id)->get(); |
| 154 |
|
| 155 |
foreach ($tickets as $ticket) { |
| 156 |
$ticket->deleteTicket(); |
| 157 |
} |
| 158 |
|
| 159 |
$customer->delete(); |
| 160 |
return [ |
| 161 |
'message' => __('Customer Deleted Successfully', 'fluent-support') |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
public function addOrUpdateProfileImage(Request $request) |
| 166 |
{ |
| 167 |
$customer = Customer::findOrFail($request->get('customer_id')); |
| 168 |
|
| 169 |
$uploadedImage = FileSystem::setSubDir('customer_avatars')->put($request->files()); |
| 170 |
|
| 171 |
if($avatar = $uploadedImage[0]['url']){ |
| 172 |
$customer->avatar = $avatar; |
| 173 |
$customer->save(); |
| 174 |
|
| 175 |
return[ |
| 176 |
'message' => __('Profile picture has been updated successfully', 'fluent-support'), |
| 177 |
'image' => $customer->avatar, |
| 178 |
'customer' => $customer |
| 179 |
]; |
| 180 |
} |
| 181 |
|
| 182 |
else{ |
| 183 |
return $this->sendError([ |
| 184 |
'message' => __('Something went wrong while updating the profile picture', 'fluent-support') |
| 185 |
]); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|