PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.4.0
Fluent Support – Helpdesk & Customer Support Ticket System v1.4.0
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.0, at app/Http/Controllers/CustomerController.php

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