PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.4.1
Fluent Support – Helpdesk & Customer Support Ticket System v1.4.1
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Http / Controllers / CustomerController.php

CustomerController.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.4.1, at app/Http/Controllers/CustomerController.php

160 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\ProfileInfoService;
11 use FluentSupport\Framework\Request\Request;
12 use FluentSupport\Framework\Support\Arr;
13
14 class CustomerController extends Controller
15 {
16 public function index(Request $request)
17 {
18 $customersQuery = Customer::orderBy('id', 'DESC')
19 ->orderBy($request->get('order_by', 'id'), $request->get('order_type', 'ASC'));
20
21 if ($request->get('search')) {
22 $customersQuery->searchBy($request->get('search'));
23 }
24
25
26 $status = $request->get('status');
27 if ($status && $status != 'all') {
28 $customersQuery->filterByStatues([$status]);
29 }
30
31 $customers = $customersQuery->paginate();
32
33 foreach ($customers as $customer) {
34 $customer->total_tickets = $customer->getTicketCounts();
35 $customer->total_responses = $customer->getResponseCounts();
36 if ($customer->user_id) {
37 $customer->user_profile = admin_url('user-edit.php?user_id=' . $customer->user_id);
38 }
39 }
40
41 return [
42 'customers' => $customers,
43 ];
44 }
45
46 public function getCustomer(Request $request, $customerId)
47 {
48 $customer = Customer::findOrFail($customerId);
49
50 $data = [
51 'customer' => $customer
52 ];
53
54 $with = $request->get('with', []);
55
56 if (in_array('widgets', $with)) {
57 $data['widgets'] = ProfileInfoService::getProfileExtraWidgets($customer);
58 }
59
60 if (in_array('tickets', $with)) {
61 $data['tickets'] = Ticket::select(['id', 'title', 'status', 'customer_id', 'created_at'])
62 ->where('customer_id', $customer->id)
63 ->orderBy('id', 'DESC')
64 ->limit(20)
65 ->get();
66 }
67
68 if(in_array('fluentcrm_profile', $with)) {
69 $data['fluentcrm_profile'] = Helper::getFluentCrmContactData($customer);
70 }
71
72 return $data;
73
74 }
75
76 public function create(Request $request)
77 {
78 $data = $request->all();
79 $this->validate($data, [
80 'email' => 'required|email|unique:fs_persons'
81 ]);
82
83 $email = $data['email'];
84
85 $data = Arr::only($data, (new Customer)->getFillable());
86
87 $user = get_user_by('email', $email);
88
89 if ($user) {
90 $data['user_id'] = $user->ID;
91 if (empty($data['first_name'])) {
92 $data['first_name'] = $user->first_name;
93 }
94 if (empty($data['last_name'])) {
95 $data['last_name'] = $user->last_name;
96 }
97 }
98
99 $customer = Customer::create($data);
100
101 return [
102 'message' => __('Customer has been added', 'fluent-support'),
103 'customer' => $customer
104 ];
105 }
106
107 public function update(Request $request, $customerId)
108 {
109 $customer = Customer::findOrFail($customerId);
110 $data = $request->all();
111 $this->validate($data, [
112 'email' => 'required|email',
113 'first_name' => 'required'
114 ]);
115
116 if ($otherCustomer = Customer::where('id', '!=', $customerId)->where('email', $data['email'])->first()) {
117 return $this->sendError([
118 'message' => __('Another Customer has same email address', 'fluent-support'),
119 'errors' => [
120 'email' => [
121 'unique' => __('Email address has been assigned to other customer', 'fluent-support')
122 ]
123 ]
124 ], 423);
125 }
126
127 $validKeys = (new Customer)->getFillable();
128 unset($validKeys['hash']);
129 unset($validKeys['user_id']);
130
131 $updateData = Arr::only($data, $validKeys);
132
133 $user = get_user_by('email', $data['email']);
134
135 if ($user) {
136 $updateData['user_id'] = $user->ID;
137 }
138
139 Customer::where('id', $customer->id)
140 ->update($updateData);
141
142 return [
143 'message' => __('Customer has been updated', 'fluent-support'),
144 'customer' => Customer::findOrFail($customerId)
145 ];
146 }
147
148 public function delete(Request $request, $customerId)
149 {
150 $customer = Customer::findOrFail($customerId);
151 Ticket::where('customer_id', $customerId)->delete();
152 Attachment::where('person_id', $customerId)->delete();
153 Conversation::where('person_id', $customerId)->delete();
154 $customer->delete();
155 return [
156 'message' => __('Customer Deleted Successfully', 'fluent-support')
157 ];
158 }
159 }
160