PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
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 trunk, at app/Http/Controllers/AgentController.php

276 lines 9.3 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\Modules\StatModule;
7 use FluentSupport\App\Services\AvatarUploder;
8 use FluentSupport\App\Services\Helper;
9 use FluentSupport\Framework\Http\Request\Request;
10 use FluentSupport\App\Modules\PermissionManager;
11 use FluentSupport\App\Http\Requests\AgentCreateRequest;
12
13 /**
14 * AgentController class for REST API
15 * This class is responsible for getting data for all request related to agent
16 *
17 * @package FluentSupport\App\Http\Controllers
18 *
19 * @version 1.0.0
20 */
21 class AgentController extends Controller
22 {
23 public function index(Request $request, Agent $agent)
24 {
25 return [
26 'agents' => $agent->getAgents($request->getSafe('search', 'sanitize_text_field')),
27 'permissions' => PermissionManager::getReadablePermissionGroups(),
28 'businessBoxes' => PermissionManager::getMailboxesForRestriction(),
29 ];
30 }
31
32 /**
33 * addAgent method will add new agent in person table
34 * @param AgentCreateRequest $request
35 * @return \WP_REST_Response | array
36 * @throws \FluentSupport\Framework\Validator\ValidationException
37 */
38 public function addAgent(AgentCreateRequest $request, Agent $agent)
39 {
40 $data = $this->sanitizeAgentPayload($request, true);
41
42 try {
43 return [
44 'message' => __('Support Staff has been added', 'fluent-support'),
45 'agent' => $agent->createAgent($data)
46 ];
47 } catch (\Exception $e) {
48 return $this->sendError([
49 'message' => Helper::getSafeErrorMessage($e)
50 ]);
51 }
52 }
53
54 /**
55 * updateAgent method will update the information of an exiting agent
56 * @param AgentCreateRequest $request
57 * @param Agent $agent
58 * @param $agent_id
59 * @return \WP_REST_Response | array
60 * @throws \FluentSupport\Framework\Validator\ValidationException
61 */
62 public function updateAgent(AgentCreateRequest $request, Agent $agent, $agent_id)
63 {
64 $agent = $agent::findOrFail($agent_id);
65 $data = $this->sanitizeAgentPayload($request);
66
67 if (!$agent->user_id && ($user = get_user_by('email', $agent->email))) {
68 $agent->user_id = $user->ID;
69 }
70
71 try {
72 return [
73 'message' => __('Support Staff has been updated', 'fluent-support'),
74 'agent' => $agent->updateAgent($data, $agent)
75 ];
76 } catch (\Exception $e) {
77 return $this->sendError([
78 'message' => Helper::getSafeErrorMessage($e)
79 ]);
80 }
81 }
82
83 /**
84 * deleteAgent will delete an exiting agent and add an alternative agent as replacement
85 * @param Request $request
86 * @param Agent $agent
87 * @param $agent_id
88 * @return \WP_REST_Response | array
89 * @throws \FluentSupport\Framework\Validator\ValidationException
90 */
91 public function deleteAgent(Request $request, Agent $agent, $agent_id)
92 {
93 try {
94 $agent->deleteAgent($request->getSafe('fallback_agent_id', 'intval'), $agent_id);
95
96 return [
97 'message' => __('Support Staff has been deleted', 'fluent-support')
98 ];
99 } catch (\Exception $e) {
100 return $this->sendError([
101 'message' => Helper::getSafeErrorMessage($e)
102 ]);
103 }
104 }
105
106 /**
107 * @param Request $request
108 * @return \WP_REST_Response | array
109 */
110 public function myStats(Request $request)
111 {
112 // Get logged in agent information.
113 $agent = Helper::getAgentByUserId();
114
115 try {
116 // Get ticket statistics.
117 $stats = StatModule::getAgentStat($agent->id);
118 $with = $request->get('with', []);
119 $with = is_array($with) ? map_deep($with, 'sanitize_text_field') : [];
120
121 $response = (new Agent())->getAgentStat($stats, $with, $agent->id);
122
123 if (defined('FLUENTSUPPORTPRO')) {
124 $response['dashboard_notice'] = apply_filters('fluent_support/dashboard_notice', '', $agent);
125 }
126
127 return $response;
128 } catch (\Exception $e) {
129 return $this->sendError([
130 'message' => Helper::getSafeErrorMessage($e)
131 ]);
132 }
133 }
134
135 public function agentPerformance()
136 {
137 try {
138 if (PermissionManager::currentUserCan('fst_agent_today_performance')) {
139 $agentTodayStats = StatModule::getAgentTodayStats();
140
141 return ['agent_today_stats' => $agentTodayStats];
142 }
143 } catch (\Exception $e) {
144 return $this->sendError([
145 'message' => Helper::getSafeErrorMessage($e)
146 ]);
147 }
148
149 return [];
150 }
151
152 /**
153 * addOrUpdateProfileImage method will upload profile picture for a given agent id
154 * For a successful upload it's required to send file object, agent id and the user type(agent)
155 * @param Request $request
156 * @param AvatarUploder $avatarUploder
157 * @return \WP_REST_Response | array
158 */
159 public function addOrUpdateProfileImage(Request $request, AvatarUploder $avatarUploder)
160 {
161 try {
162 return $avatarUploder->addOrUpdateProfileImage($request->files(), $request->getSafe('agent_id', 'intval'), 'agent');
163 } catch (\Exception $e) {
164 return $this->sendError([
165 'message' => Helper::getSafeErrorMessage($e)
166 ]);
167 }
168 }
169
170 /**
171 * resetAvatar method will restore a Support Staff avatar
172 * For a successful upload it's required to send file object, Support Staff id and the user type(Support Staff)
173 *
174 * No Agent type-hint here: route-model binding resolves inside the
175 * permission callback, before any policy runs, which lets unauthenticated
176 * callers probe agent ID existence (FS-PERM-001). Resolve after auth.
177 * @param int|string $agent
178 * @return array
179 */
180 public function resetAvatar($agent)
181 {
182 try {
183 $agent = Agent::findOrFail((int) $agent);
184 $agent->restoreAvatar();
185
186 return [
187 'message' => __('Support Staff avatar reset to gravatar default', 'fluent-support')
188 ];
189 } catch (\Exception $e) {
190 return $this->sendError([
191 'message' => Helper::getSafeErrorMessage($e)
192 ]);
193 }
194 }
195
196 public function getAgentInsights(Request $request, Agent $agent)
197 {
198 return [
199 'agents' => $agent->agentInsights($request->getSafe('search', 'sanitize_text_field')),
200 ];
201 }
202
203 public function ping(Request $request)
204 {
205 return [
206 'ping' => 'pong',
207 ];
208 }
209
210 /**
211 * Sanitize payload data for create/update operations.
212 *
213 * @param AgentCreateRequest $request
214 * @param bool $includeEmail
215 * @return array
216 */
217 private function sanitizeAgentPayload(AgentCreateRequest $request, $includeEmail = false)
218 {
219 $data = [
220 'first_name' => $request->getSafe('first_name', 'sanitize_text_field'),
221 'last_name' => $request->getSafe('last_name', 'sanitize_text_field'),
222 'title' => $request->getSafe('title', 'sanitize_text_field'),
223 'permissions' => is_array($request->get('permissions')) ? array_map('sanitize_key', $request->get('permissions')) : [],
224 'telegram_chat_id' => $request->getSafe('telegram_chat_id', 'sanitize_text_field'),
225 'slack_user_id' => $request->getSafe('slack_user_id', 'sanitize_text_field'),
226 'whatsapp_number' => $request->getSafe('whatsapp_number', 'sanitize_text_field'),
227 'restrictions' => $this->sanitizeRestrictions($request->get('restrictions')),
228 ];
229
230 if ($request->has('agent_signature')) {
231 // Already unslashed at the request boundary; sanitize only, so literal
232 // backslashes in the signature survive.
233 $data['agent_signature'] = wp_kses_post($request->get('agent_signature', ''));
234 }
235
236 if ($request->has('agent_signature_enabled')) {
237 $data['agent_signature_enabled'] = $request->get('agent_signature_enabled') === 'yes' ? 'yes' : 'no';
238 }
239
240 if ($includeEmail) {
241 $data['email'] = $request->getSafe('email', 'sanitize_email');
242 }
243
244 return $data;
245 }
246
247 /**
248 * Sanitize restrictions data for agent
249 *
250 * @param mixed $restrictions
251 * @return array
252 */
253 private function sanitizeRestrictions($restrictions)
254 {
255 if (!is_array($restrictions)) {
256 return [
257 'restrictedBusinessBoxes' => [],
258 'businessBoxRestrictions' => false,
259 ];
260 }
261
262 $restrictedBusinessBoxes = isset($restrictions['restrictedBusinessBoxes']) && is_array($restrictions['restrictedBusinessBoxes'])
263 ? array_map('absint', $restrictions['restrictedBusinessBoxes'])
264 : [];
265
266 $businessBoxRestrictions = isset($restrictions['businessBoxRestrictions'])
267 ? rest_sanitize_boolean($restrictions['businessBoxRestrictions'])
268 : false;
269
270 return [
271 'restrictedBusinessBoxes' => $restrictedBusinessBoxes,
272 'businessBoxRestrictions' => $businessBoxRestrictions,
273 ];
274 }
275 }
276