PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.5.5
Fluent Support – Helpdesk & Customer Support Ticket System v1.5.5
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 / AgentController.php

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

345 lines 11.2 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\Agent;
6 use FluentSupport\App\Models\Attachment;
7 use FluentSupport\App\Models\Person;
8 use FluentSupport\App\Models\Product;
9 use FluentSupport\App\Models\Conversation;
10 use FluentSupport\App\Models\Ticket;
11 use FluentSupport\App\Modules\Reporting\Reporting;
12 use FluentSupport\App\Modules\StatModule;
13 use FluentSupport\App\Services\Helper;
14 use FluentSupport\App\Services\Includes\FileSystem;
15 use FluentSupport\App\Services\TicketHelper;
16 use FluentSupport\Framework\Request\Request;
17 use FluentSupport\App\Modules\PermissionManager;
18 use FluentSupport\Framework\Support\Arr;
19
20 /**
21 * AgentController class for REST API
22 * This class is responsible for getting data for all request related to agent
23 *
24 * @package FluentSupport\App\Http\Controllers
25 *
26 * @version 1.0.0
27 */
28 class AgentController extends Controller
29 {
30 public function index(Request $request)
31 {
32 $agents = Agent::orderBy('id', 'DESC')
33 ->searchBy($request->get('search'))
34 ->paginate();
35
36 foreach ($agents as $agent) {
37 $agent->permissions = PermissionManager::getUserPermissions($agent->user_id);
38 if ($agent->user_id) {
39 $agent->user_profile = admin_url('user-edit.php?user_id=' . $agent->user_id);
40 }
41 $agent->replies_count = Conversation::where('person_id', $agent->id)->count();
42 $agent->interactions_count = Conversation::where('person_id', $agent->id)->groupBy('ticket_id')->get()->count();
43
44 $agent->telegram_chat_id = $agent->getMeta('telegram_chat_id');
45 $agent->slack_user_id = $agent->getMeta('slack_user_id');
46 $agent->whatsapp_number = $agent->getMeta('whatsapp_number');
47 }
48
49 return [
50 'agents' => $agents,
51 'permissions' => PermissionManager::getReadablePermissionGroups()
52 ];
53 }
54
55 /**
56 * addAgent method will add new agent in person table
57 * @param Request $request
58 * @return array
59 * @throws \FluentSupport\Framework\Validator\ValidationException
60 */
61 public function addAgent(Request $request)
62 {
63 $data = $request->all();
64 $this->validate($data, [
65 'email' => 'required|email'
66 ]);
67
68 $email = $data['email'];
69
70 $user = get_user_by('email', $email);
71
72 if (!$user || is_wp_error($user)) {
73 return $this->sendError([
74 'message' => __('Sorry, Connected user could not be found with the provided email address', 'fluent-support')
75 ]);
76 }
77
78 if (empty($data['first_name'])) {
79 $data['first_name'] = $user->first_name;
80 }
81
82 if (empty($data['last_name'])) {
83 $data['last_name'] = $user->last_name;
84 }
85
86 // check if another agent has same email address
87 $exist = Person::where('email', $email)->first();
88 if ($exist) {
89 return $this->sendError([
90 'message' => __('Sorry, Another agent/person exist with the same email address. Please use a different email address', 'fluent-support')
91 ]);
92 }
93
94 $data['user_id'] = $user->ID;
95 $agent = Agent::create($data);
96 PermissionManager::attachPermissions($user, Arr::get($data, 'permissions', []));
97
98 if (!empty($data['telegram_chat_id'])) {
99 $chatId = sanitize_text_field($data['telegram_chat_id']);
100 $agent->updateMeta('telegram_chat_id', $chatId);
101 $agent->telegram_chat_id = $chatId;
102 }
103
104 if (!empty($data['slack_user_id'])) {
105 $chatId = sanitize_text_field($data['slack_user_id']);
106 $agent->updateMeta('slack_user_id', $chatId);
107 $agent->slack_user_id = $chatId;
108 }
109
110 if (!empty($data['whatsapp_number'])) {
111 $whatsappNumber = sanitize_text_field($data['whatsapp_number']);
112 $agent->updateMeta('whatsapp_number', $whatsappNumber);
113 $agent->whatsapp_number = $whatsappNumber;
114 }
115
116 return [
117 'message' => __('Support Staff has been added', 'fluent-support'),
118 'agent' => $agent
119 ];
120 }
121
122 /**
123 * updateAgent method will update the information of an exiting agent
124 * @param Request $request
125 * @param $agentId
126 * @return array
127 * @throws \FluentSupport\Framework\Validator\ValidationException
128 */
129 public function updateAgent(Request $request, $agentId)
130 {
131 $agent = Agent::findOrFail($agentId);
132 $data = $request->all();
133 $this->validate($data, [
134 'email' => 'required|email',
135 'first_name' => 'required',
136 'last_name' => 'required',
137 ]);
138
139 $user = get_user_by('ID', $agent->user_id);
140
141 if (!$user || is_wp_error($user)) {
142 return $this->sendError([
143 'message' => __('Sorry, Connected user could not be found with the provided email address', 'fluent-support')
144 ]);
145 }
146
147 PermissionManager::attachPermissions($user, Arr::get($data, 'permissions', []));
148
149 $updateData = Arr::only($data, ['first_name', 'last_name', 'title']);
150 $updateData['email'] = $user->user_email;
151
152 Agent::where('id', $agent->id)
153 ->update($updateData);
154
155 PermissionManager::attachPermissions($user, Arr::get($data, 'permissions', []));
156
157 $agent = Agent::findOrFail($agentId);
158
159 if (isset($data['telegram_chat_id'])) {
160 $chatId = sanitize_text_field($data['telegram_chat_id']);
161 $agent->updateMeta('telegram_chat_id', $chatId);
162 $agent->telegram_chat_id = $chatId;
163 }
164
165 if (isset($data['slack_user_id'])) {
166 $chatId = sanitize_text_field($data['slack_user_id']);
167 $agent->updateMeta('slack_user_id', $chatId);
168 $agent->slack_user_id = $chatId;
169 }
170
171 if (isset($data['whatsapp_number'])) {
172 $whatsappNumber = sanitize_text_field($data['whatsapp_number']);
173 $agent->updateMeta('whatsapp_number', $whatsappNumber);
174 $agent->whatsapp_number = $whatsappNumber;
175 }
176
177 return [
178 'message' => __('Support Staff has been updated', 'fluent-support'),
179 'agent' => $agent
180 ];
181 }
182
183 /**
184 * deleteAgent will delete an exiting agent and add an alternative agent as replacement
185 * @param Request $request
186 * @param $agentId
187 * @return array
188 */
189 public function deleteAgent(Request $request, $agentId)
190 {
191 $fallBackAgentId = $request->get('fallback_agent_id');
192
193 if ($fallBackAgentId == $agentId) {
194 return $this->sendError([
195 'message' => __('Old Agent and New agent is same person', 'fluent-support')
196 ]);
197 }
198
199 $agent = Agent::findOrFail($agentId);
200
201 PermissionManager::attachPermissions($agent->user_id, []);
202
203 $newAgent = Agent::findOrFail($fallBackAgentId);
204
205 Attachment::where('person_id', $agentId)->update([
206 'person_id' => $newAgent->id
207 ]);
208
209 Conversation::where('person_id', $agentId)->update([
210 'person_id' => $newAgent->id
211 ]);
212
213 Product::where('created_by', $agentId)->update([
214 'created_by' => $newAgent->id
215 ]);
216
217 Ticket::where('agent_id', $agentId)->update([
218 'agent_id' => $newAgent->id
219 ]);
220
221 $agent->deleteAllMeta();
222
223 Agent::where('id', $agentId)->delete();
224
225 return [
226 'message' => __('Selected support staff has been deleted', 'fluent-support')
227 ];
228
229 }
230
231 /**
232 * @param Request $request
233 * @return array
234 */
235 public function myStats(Request $request)
236 {
237 $agent = Helper::getAgentByUserId();//Get logged in agent information
238 //Get the statistics and responses by the agent
239 $response = $this->getAgentStat($request, $agent->id);
240
241 if (defined('FLUENTSUPPORTPRO')) {
242 $response['dashboard_notice'] = apply_filters('fluent_support/dashboard_notice', '', $agent);
243 }
244
245 return $response;
246
247 }
248
249 /**
250 * getAgentStat method will return ticket statistics by an agent id
251 *
252 * @param Request $request
253 * @param $agentId
254 * @return array
255 */
256 public function getAgentStat(Request $request, $agentId)
257 {
258 $agent = Agent::findOrFail($agentId);//Get agent information
259 $stats = StatModule::getAgentStat($agentId);//Get ticket statistics
260
261 //If the logged-in user has permission to manage unassigned tickets, get number of unassigned tickets
262 if (PermissionManager::currentUserCan('fst_manage_unassigned_tickets')) {
263 $stats['unassigned_tickets'] = [
264 'title' => __('Unassigned Tickets', 'fluent-support'),
265 'count' => Ticket::whereNull('agent_id')->where('status', '!=', 'closed')->count()
266 ];
267 }
268
269 $data = [
270 'stats' => $stats
271 ];
272
273 $with = $request->get('with', []);
274
275 //If the request come with suggested_tickets
276 if (in_array('suggested_tickets', $with)) {
277 //Get suggested tickets from ticketHelper
278 $data['suggested_tickets'] = TicketHelper::getSuggestedTickets($agent->id);
279 }
280
281 //If the request come with overall_stats
282 if (in_array('overall_stats', $with)) {
283 //Get overall status
284 $data['overall_stats'] = (new Reporting())->getActiveStats();
285 }
286
287 //If the request come with individual_stat
288 if (in_array('individual_stat', $with)) {
289 //get overall statistics by agent id
290 $data['individual_stat'] = (new Reporting())->getActiveStatByAgent($agent->id);
291 }
292 //If the request come with my_overall_stats
293 if (in_array('my_overall_stats', $with)) {
294 //Get the overall statistics by the agent
295 $data['my_overall_stats'] = StatModule::getAgentOverallStats($agent->id);
296 }
297
298 return $data;
299 }
300
301 /**
302 * addOrUpdateProfileImage method will upload profile picture for a given agent id
303 * @param Request $request
304 * @return array
305 */
306 public function addOrUpdateProfileImage(Request $request)
307 {
308 $allowExtension = [
309 'jpeg', 'jpe', 'jpg', 'png'
310 ];
311
312 $agent_id = $request->get('agent_id');
313 $file = $request->files();
314
315 $ext = $file['file']->getClientOriginalExtension();
316
317 if(!in_array($ext, $allowExtension)){
318 return $this->sendError([
319 'message' => __('Unsupported file submitted, please select an image file', 'fluent-support')
320 ]);
321 }
322
323 $agent = Agent::findOrFail($agent_id);
324
325 $uploadedImage = FileSystem::setSubDir('agents_avatars')->put($file);
326
327 if($avatar = $uploadedImage[0]['url']){
328 $agent->avatar = $avatar;
329 $agent->save();
330
331 return[
332 'message' => __('Profile picture has been updated successfully', 'fluent-support'),
333 'image' => $agent->avatar,
334 'agent' => $agent
335 ];
336 }
337
338 else{
339 return $this->sendError([
340 'message' => __('Something went wrong while updating the profile picture', 'fluent-support')
341 ]);
342 }
343 }
344 }
345