PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.1
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.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 / CustomerPortalController.php

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

328 lines 11.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 Exception;
6 use FluentSupport\App\Http\Requests\TicketResponseRequest;
7 use FluentSupport\App\Models\Product;
8 use FluentSupport\App\Models\Ticket;
9 use FluentSupport\App\Models\Conversation;
10 use FluentSupport\App\Services\CustomerPortalService;
11 use FluentSupport\App\Services\Helper;
12 use FluentSupport\Framework\Request\Request;
13
14 /**
15 * CustomerPortalController class for REST API
16 * This class is responsible for getting data for all request related to customer and customer portal
17 * @package FluentSupport\App\Http\Controllers
18 *
19 * @version 1.0.0
20 */
21 class CustomerPortalController extends Controller
22 {
23 /**
24 * getTickets will generate ticket information with customer and agents by customer
25 * @param Request $request
26 * @param CustomerPortalService $customerPortalService
27 * @return array
28 * @throws Exception
29 */
30 public function getTickets(Request $request, CustomerPortalService $customerPortalService)
31 {
32
33 $onBehalf = $request->getSafe('on_behalf', 'sanitize_text_field');
34 $userIP = $request->getIp();
35
36 $requestedStatus = $request->getSafe('filter_type', 'sanitize_text_field');
37
38 $ticketOptions = $request->getSafe([
39 'search' => 'sanitize_text_field',
40 'filters.product_id' => 'intval',
41 'sorting.sort_type' => 'sanitize_sql_orderby',
42 'sorting.sort_by' => 'sanitize_sql_orderby'
43 ]);
44
45 try {
46 $customer = $customerPortalService->resolveCustomer($onBehalf, $userIP);
47 return [
48 'tickets' => $customerPortalService->getTickets($customer, $requestedStatus, $ticketOptions)
49 ];
50 } catch (Exception $e) {
51 return $this->sendError([
52 'message' => $e->getMessage(),
53 'error_type' => $e->getCode()
54 ]);
55 }
56 }
57
58 /**
59 * createTicket method will create ticket submitted by customers
60 * @param Request $request
61 * @return array
62 * @throws \FluentSupport\Framework\Validator\ValidationException
63 */
64 public function createTicket(Request $request, CustomerPortalService $customerPortalService)
65 {
66 $dataRules = $this->app->applyCustomFilters('custom_field_required_before_ticket_create', [
67 'required_fields' => [
68 'title' => 'required',
69 'content' => 'required'
70 ],
71 'error_messages' => [
72 'title.required' => __('Title is required', 'fluent-support'),
73 'content.required' => __('Content is required', 'fluent-support')
74 ]
75 ]);
76
77 $settings = Helper::getOption('_ticket_form_settings', []);
78
79 if (!empty($settings['product_required_field']) && $settings['product_required_field'] === 'yes') {
80 $productCount = Product::count();
81 if ($productCount > 0) {
82 $dataRules['required_fields']['product_id'] = 'required';
83 $dataRules['error_messages']['product_id.required'] = __('Product is required', 'fluent-support');
84 }
85 }
86
87 $defaultData = [
88 'ticket_title' => $request->get('title'),
89 'ticket_content' => $request->get('content')
90 ];
91
92 if ($request->has('product_id')) {
93 $defaultData['ticket_product_id'] = $request->get('product_id');
94 }
95
96 if ($request->has('client_priority')) {
97 $defaultData['ticket_client_priority'] = $request->get('client_priority');
98 }
99
100 $dataRules = $this->app->applyCustomFilters('custom_field_required_by_conditions_before_ticket_create', [
101 'required_fields' => $dataRules['required_fields'],
102 'error_messages' => $dataRules['error_messages'],
103 'custom_data' => $request->get('custom_data', []),
104 'default_data' => $defaultData
105 ]);
106
107 if (!isset($dataRules['required_fields']) && !isset($dataRules['error_messages'])) {
108 return $this->sendError([
109 'message' => __('Invalid form data submitted', 'fluent-support'),
110 'error_type' => '400'
111 ], 400);
112 }
113
114 $data = $this->validate($request->get(), $dataRules['required_fields'], $dataRules['error_messages']);
115
116 $data['title'] = sanitize_text_field($data['title']);
117 $data['content'] = wp_kses_post($data['content']);
118
119 $onBehalf = $request->getSafe('on_behalf', 'sanitize_text_field');
120 $userIP = $request->getIp();
121
122 try {
123 $customer = $customerPortalService->resolveCustomer($onBehalf, $userIP, true);
124
125 $canCreateTicket = apply_filters('fluent_support/can_customer_create_ticket', true, $customer, $data);
126
127 if (!$canCreateTicket || is_wp_error($canCreateTicket)) {
128 $isWpError = is_wp_error($canCreateTicket);
129
130 $message = ($isWpError) ? $canCreateTicket->get_error_message() : __('Sorry you can not create ticket', 'fluent-support');
131 $errorCode = ($isWpError) ? $canCreateTicket->get_error_code() : 'general_error';
132
133
134 throw new \Exception($message, $errorCode);
135 }
136
137 if ($customer && $messageId = Helper::generateMessageID($customer->email)) {
138 $data['message_id'] = $messageId;
139 }
140
141 $defaultMailbox = Helper::getDefaultMailBox();
142 $ticket = $customerPortalService->createTicket($customer, $data, $request->getSafe('mailbox_id', 'intval', $defaultMailbox->id));
143
144 return [
145 'message' => __('Ticket has been created successfully', 'fluent-support'),
146 'ticket' => $ticket
147 ];
148 } catch (\Exception $e) {
149 return $this->sendError([
150 'message' => $e->getMessage(),
151 'error_type' => $e->getCode()
152 ]);
153 }
154 }
155
156 /**
157 * getTicket method will get the ticket information with customer and agent as well as response in a ticket by ticket id
158 * @param Request $request
159 * @param $ticket_id
160 * @return array
161 */
162 public function getTicket(Request $request, CustomerPortalService $customerPortalService, $ticket_id)
163 {
164 $customerAdditionalData = $this->getCustomerAdditionalData($request);
165
166 try {
167 return $customerPortalService->getTicket($customerAdditionalData, $ticket_id);
168 } catch (Exception $e) {
169 return $this->sendError([
170 'message' => $e->getMessage(),
171 'error_type' => $e->getCode()
172 ]);
173 }
174 }
175
176 /**
177 * createResponse method will create response by customer in a ticket by ticket id
178 * @param Request $request
179 * @param $ticket_id
180 * @return array|\WP_REST_Response
181 * @throws \FluentSupport\Framework\Validator\ValidationException
182 */
183 public function createResponse(TicketResponseRequest $request, CustomerPortalService $customerPortalService, $ticket_id)
184 {
185
186 $customerAdditionalData = $this->getCustomerAdditionalData($request);
187
188 $ticket = Ticket::findOrFail($ticket_id);
189
190 $data = $request->sanitize();
191
192 $canCreateResponse = apply_filters('fluent_support/can_customer_create_response', true, $ticket->customer, $ticket, $data);
193
194 if (!$canCreateResponse || is_wp_error($canCreateResponse)) {
195 return [
196 'type' => 'error',
197 'message' => (is_wp_error($canCreateResponse)) ? $canCreateResponse->get_error_message() : __('Sorry you can not create response', 'fluent-support')
198 ];
199 }
200
201 try {
202 return $customerPortalService->createResponse($customerAdditionalData, $ticket_id, $data);
203 } catch (Exception $e) {
204 return $this->sendError([
205 'message' => $e->getMessage(),
206 'error_type' => $e->getCode()
207 ]);
208 }
209 }
210
211 /**
212 * This `closeTicket` is responsible for closing ticket by ticket id
213 * @param Request $request
214 * @param $ticket_id
215 * @return array
216 */
217 public function closeTicket(Request $request, CustomerPortalService $customerPortalService, $ticket_id)
218 {
219 $customerAdditionalData = $this->getCustomerAdditionalData($request);
220
221 try {
222 return $customerPortalService->closeTicket($customerAdditionalData, $ticket_id);
223 } catch (Exception $e) {
224 return $this->sendError([
225 'message' => $e->getMessage(),
226 'error_type' => $e->getCode()
227 ]);
228 }
229 }
230
231 /**
232 * closeTicket method will re-open a ticket by customer using ticket id
233 * @param Request $request
234 * @param $ticket_id
235 * @return array
236 */
237 public function reOpenTicket(Request $request, CustomerPortalService $customerPortalService, $ticket_id)
238 {
239 $customerAdditionalData = $this->getCustomerAdditionalData($request);
240
241 try {
242 return $customerPortalService->reOpenTicket($customerAdditionalData, $ticket_id);
243 } catch (Exception $e) {
244 return $this->sendError([
245 'message' => $e->getMessage(),
246 'error_type' => $e->getCode()
247 ]);
248 }
249 }
250
251 public function agentFeedbackRating(Request $request, CustomerPortalService $customerPortalService, $ticketId)
252 {
253 // just for validation
254 $ticket = Ticket::with(['customer'])->findOrFail($ticketId);
255 $customerAdditionalData = $this->getCustomerAdditionalData($request);
256 $customer = $customerPortalService->getCustomer($customerAdditionalData, $ticket);
257 $customerPortalService->checkCustomerTicketAccess($customer, $ticket, 'feedback');
258
259 $conversationID = $request->getSafe('conversation_id', 'intval');
260 $approvalStatus = $request->getSafe('approval_status', 'sanitize_text_field');
261
262 try {
263 return $customerPortalService->addUserFeedback($approvalStatus, $conversationID);
264 } catch (Exception $e) {
265 return $this->sendError([
266 'message' => $e->getMessage(),
267 'error_type' => $e->getCode()
268 ]);
269 }
270 }
271
272 /**
273 * getPublicOptions method will return the list of product and customer priorities
274 * @return array
275 */
276 public function getPublicOptions()
277 {
278 $products = Product::select(['id', 'title'])->get();
279
280 return [
281 'support_products' => $products,
282 'customer_ticket_priorities' => Helper::customerTicketPriorities()
283 ];
284 }
285
286 /**
287 * getCustomFieldsRender method will return the list of custom fields
288 * @return array|array[]
289 */
290 public function getCustomFieldsRender()
291 {
292 if (!defined('FLUENTSUPPORTPRO')) {
293 return [
294 'custom_fields_rendered' => []
295 ];
296 }
297
298 return [
299 'custom_fields_rendered' => \FluentSupportPro\App\Services\CustomFieldsService::getRenderedPublicFields()
300 ];
301 }
302
303
304 /**
305 * logout method will logout the customer
306 * @return mixed
307 */
308 public function logout()
309 {
310 wp_logout();
311
312 return $this->sendSuccess([
313 'message' => __('You have been logged out', 'fluent-support')
314 ]);
315 }
316
317 private function getCustomerAdditionalData($request)
318 {
319 $customerAdditionalData = [
320 'intended_ticket_hash' => $request->getSafe('intended_ticket_hash', 'sanitize_text_field'),
321 'on_behalf' => $request->getSafe('on_behalf', 'sanitize_text_field'),
322 'user_ip' => $request->getIp()
323 ];
324
325 return $customerAdditionalData;
326 }
327 }
328