PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
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 1.5.6 All 67 releases
fluent-support / app / Services / Notifications / NotificationFormatter.php

NotificationFormatter.php in Fluent Support – Helpdesk & Customer Support Ticket System trunk, at app/Services/Notifications/NotificationFormatter.php

225 lines 9.1 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\Services\Notifications;
4
5 class NotificationFormatter
6 {
7 public function formatPaginated($notifications)
8 {
9 $formattedNotifications = $notifications->toArray();
10 $formattedNotifications['data'] = $this->formatMany($formattedNotifications['data']);
11
12 return $formattedNotifications;
13 }
14
15 public function formatMany($notifications)
16 {
17 $formattedNotifications = [];
18
19 foreach ($notifications as $notification) {
20 $formattedNotifications[] = $this->format($notification);
21 }
22
23 return $formattedNotifications;
24 }
25
26 public function format($notification)
27 {
28 $notification = is_array($notification) ? $notification : $notification->toArray();
29 $displayData = NotificationEventMap::displayData($notification['event_type'] ?? '');
30
31 $notification = $this->toSafePayload($notification);
32 $notification['event_label'] = $displayData['label'];
33 $notification['event_icon'] = $displayData['icon'];
34 $notification['summary'] = $this->getSummary($notification);
35
36 return $notification;
37 }
38
39 protected function toSafePayload($notification)
40 {
41 return [
42 'id' => (int) ($notification['id'] ?? 0),
43 'actor_id' => !empty($notification['actor_id']) ? (int) $notification['actor_id'] : null,
44 'ticket_id' => !empty($notification['ticket_id']) ? (int) $notification['ticket_id'] : null,
45 'conversation_id' => !empty($notification['conversation_id']) ? (int) $notification['conversation_id'] : null,
46 'event_type' => $notification['event_type'] ?? '',
47 'category' => $notification['category'] ?? '',
48 'payload' => $this->sanitizePayload($notification['payload'] ?? []),
49 'actor' => $this->formatActor($notification['actor'] ?? null),
50 'ticket' => $this->formatTicket($notification['ticket'] ?? null),
51 'recipients' => $this->formatRecipients($notification['recipients'] ?? []),
52 'created_at' => $notification['created_at'] ?? null,
53 'updated_at' => $notification['updated_at'] ?? null,
54 ];
55 }
56
57 protected function sanitizePayload($payload)
58 {
59 $payload = is_array($payload) ? $payload : [];
60 $safePayload = [];
61
62 foreach (['ticket_title', 'ticket_priority', 'conversation_type', 'content_preview', 'status', 'assignment_type', 'workflow_name', 'summary'] as $key) {
63 if (array_key_exists($key, $payload)) {
64 $safePayload[$key] = sanitize_text_field($payload[$key]);
65 }
66 }
67
68 foreach (['customer', 'agent', 'actor', 'assigner', 'assigned_agent'] as $personKey) {
69 if (!empty($payload[$personKey]) && is_array($payload[$personKey])) {
70 $safePayload[$personKey] = $this->formatPayloadPerson($payload[$personKey]);
71 }
72 }
73
74 if (!empty($payload['mentioned_handles']) && is_array($payload['mentioned_handles'])) {
75 $safePayload['mentioned_handles'] = array_values(array_map('sanitize_text_field', $payload['mentioned_handles']));
76 }
77
78 return $safePayload;
79 }
80
81 protected function formatPayloadPerson($person)
82 {
83 return array_filter([
84 'id' => !empty($person['id']) ? (int) $person['id'] : null,
85 'person_type' => !empty($person['person_type']) ? sanitize_text_field($person['person_type']) : null,
86 'name' => !empty($person['name']) ? sanitize_text_field($person['name']) : null,
87 'photo' => !empty($person['photo']) ? esc_url_raw($person['photo']) : null,
88 ], function ($value) {
89 return $value !== null && $value !== '';
90 });
91 }
92
93 protected function formatActor($actor)
94 {
95 if (empty($actor) || !is_array($actor)) {
96 return null;
97 }
98
99 return [
100 'id' => !empty($actor['id']) ? (int) $actor['id'] : null,
101 'full_name' => !empty($actor['full_name']) ? sanitize_text_field($actor['full_name']) : '',
102 'photo' => !empty($actor['photo']) ? esc_url_raw($actor['photo']) : '',
103 ];
104 }
105
106 protected function formatTicket($ticket)
107 {
108 if (empty($ticket) || !is_array($ticket)) {
109 return null;
110 }
111
112 return [
113 'id' => !empty($ticket['id']) ? (int) $ticket['id'] : null,
114 'title' => !empty($ticket['title']) ? sanitize_text_field($ticket['title']) : '',
115 ];
116 }
117
118 protected function formatRecipients($recipients)
119 {
120 if (!is_array($recipients)) {
121 return [];
122 }
123
124 return array_map(function ($recipient) {
125 return [
126 'id' => !empty($recipient['id']) ? (int) $recipient['id'] : null,
127 'user_id' => !empty($recipient['user_id']) ? (int) $recipient['user_id'] : null,
128 'is_read' => !empty($recipient['is_read']) ? (int) $recipient['is_read'] : 0,
129 'read_at' => $recipient['read_at'] ?? null,
130 ];
131 }, $recipients);
132 }
133
134 protected function getSummary($notification)
135 {
136 $eventType = NotificationEventMap::normalizeEventType($notification['event_type'] ?? '');
137 $actorName = $this->getActorName($notification);
138 $ticketTitle = $this->getTicketTitle($notification);
139
140 switch ($eventType) {
141 case NotificationEventMap::AGENT_MENTIONED:
142 return $ticketTitle
143 ? sprintf(__('%1$s mentioned you in %2$s', 'fluent-support'), $actorName, $ticketTitle)
144 : sprintf(__('%1$s mentioned you', 'fluent-support'), $actorName);
145
146 case NotificationEventMap::AGENT_REPLIED:
147 return $ticketTitle
148 ? sprintf(__('%1$s replied to a ticket in %2$s', 'fluent-support'), $actorName, $ticketTitle)
149 : sprintf(__('%1$s replied to a ticket', 'fluent-support'), $actorName);
150
151 case NotificationEventMap::TICKET_ASSIGNED:
152 return $ticketTitle
153 ? sprintf(__('%1$s assigned a ticket in %2$s', 'fluent-support'), $actorName, $ticketTitle)
154 : sprintf(__('%1$s assigned a ticket', 'fluent-support'), $actorName);
155
156 case NotificationEventMap::TICKET_REASSIGNED:
157 return $ticketTitle
158 ? sprintf(__('%1$s reassigned a ticket in %2$s', 'fluent-support'), $actorName, $ticketTitle)
159 : sprintf(__('%1$s reassigned a ticket', 'fluent-support'), $actorName);
160
161 case NotificationEventMap::CUSTOMER_REPLIED:
162 return $ticketTitle
163 ? sprintf(__('%1$s replied to a ticket in %2$s', 'fluent-support'), $actorName, $ticketTitle)
164 : sprintf(__('%1$s replied to a ticket', 'fluent-support'), $actorName);
165
166 case NotificationEventMap::TICKET_CLOSED:
167 return $ticketTitle
168 ? sprintf(__('%1$s closed a ticket in %2$s', 'fluent-support'), $actorName, $ticketTitle)
169 : sprintf(__('%1$s closed a ticket', 'fluent-support'), $actorName);
170
171 case NotificationEventMap::TICKET_REOPENED:
172 return $ticketTitle
173 ? sprintf(__('%1$s reopened a ticket in %2$s', 'fluent-support'), $actorName, $ticketTitle)
174 : sprintf(__('%1$s reopened a ticket', 'fluent-support'), $actorName);
175
176 case NotificationEventMap::WORKFLOW_TRIGGERED:
177 return $ticketTitle
178 ? sprintf(__('%1$s triggered automation in %2$s', 'fluent-support'), $actorName, $ticketTitle)
179 : sprintf(__('%1$s triggered automation', 'fluent-support'), $actorName);
180 }
181
182 return $ticketTitle
183 ? sprintf(__('%1$s updated a ticket in %2$s', 'fluent-support'), $actorName, $ticketTitle)
184 : sprintf(__('%1$s updated a ticket', 'fluent-support'), $actorName);
185 }
186
187 protected function getActorName($notification)
188 {
189 if (!empty($notification['actor']['full_name'])) {
190 return $notification['actor']['full_name'];
191 }
192
193 if (!empty($notification['payload']['customer']['name'])) {
194 return $notification['payload']['customer']['name'];
195 }
196
197 if (!empty($notification['payload']['agent']['name'])) {
198 return $notification['payload']['agent']['name'];
199 }
200
201 if (!empty($notification['payload']['actor']['name'])) {
202 return $notification['payload']['actor']['name'];
203 }
204
205 if (!empty($notification['payload']['assigner']['name'])) {
206 return $notification['payload']['assigner']['name'];
207 }
208
209 return __('Someone', 'fluent-support');
210 }
211
212 protected function getTicketTitle($notification)
213 {
214 if (!empty($notification['ticket']['title'])) {
215 return $notification['ticket']['title'];
216 }
217
218 if (!empty($notification['payload']['ticket_title'])) {
219 return $notification['payload']['ticket_title'];
220 }
221
222 return '';
223 }
224 }
225