PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.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 All 69 releases
← All changes | app/Services/MailerInbox/MailBoxService.php +265 -0 1.4.0 → 2.4.5 View file →
@@ -2,10 +2,275 @@
2 2
3 3 namespace FluentSupport\App\Services\MailerInbox;
4 4
5 5 use FluentSupport\App\App;
6 +use FluentSupport\App\Models\Agent;
7 +use FluentSupport\App\Models\MailBox;
8 +use FluentSupport\App\Models\Meta;
9 +use FluentSupport\App\Models\Ticket;
10 +use FluentSupport\App\Services\EmailNotification\Settings;
11 +use FluentSupport\App\Services\Tickets\AgentTicketAccess;
6 12 use FluentSupport\Framework\Support\Arr;
13 +use Exception;
7 14
8 15 class MailBoxService
9 16 {
17 + /**
18 + * @return array
19 + */
20 + public function getMailBoxes ()
21 + {
22 + $mailboxes = MailBox::all();
10 23
24 + foreach ($mailboxes as $mailbox) {
25 + $mailbox->tickets_count = Ticket::countTicketByMailBoxId($mailbox->id);
26 + }
27 +
28 + return [
29 + 'mailboxes' => $mailboxes
30 + ];
31 + }
32 +
33 + /**
34 + * Delete a mailbox and move its tickets (and default flag) to the fallback.
35 + *
36 + * @param int $mailBoxId
37 + * @param int $fallbackId
38 + * @return array
39 + */
40 + public function deleteMailBox($mailBoxId, $fallbackId )
41 + {
42 + if ( $mailBoxId == $fallbackId ) {
43 + throw new \Exception(esc_html__('Fallback Box cannot be the same as MailBox ID', 'fluent-support'));
44 + }
45 +
46 + $box = MailBox::findOrFail($mailBoxId);
47 + $fallbackBox = MailBox::findOrFail($fallbackId);
48 +
49 + if($box->is_default == 'yes'){
50 + $fallbackBox->is_default = 'yes';
51 + $fallbackBox->save();
52 + }
53 +
54 + /*
55 + * Fires before a mailbox is deleted.
56 + *
57 + * @since v1.0.0
58 + * @param object $box Mailbox
59 + * @param object $fallbackBox Fallback mailbox
60 + */
61 + do_action('fluent_support/before_delete_email_box', $box, $fallbackBox);
62 +
63 + $this->deleteAllMailBoxMeta($mailBoxId);
64 +
65 + MailBox::where('id', $mailBoxId)->delete();
66 +
67 + // Not a move — fluent_support/mailbox_deleted below is the event for this.
68 + Ticket::syncMailBoxId($mailBoxId, $fallbackId);
69 +
70 + /*
71 + * Fires after a mailbox is deleted.
72 + *
73 + * @since v1.0.0
74 + * @param integer $mailBoxId
75 + * @param object $fallbackBox Fallback mailbox
76 + */
77 + do_action('fluent_support/mailbox_deleted', $mailBoxId, $fallbackBox);
78 +
79 + return [
80 + 'message' => __('Selected Business has been deleted', 'fluent-support')
81 + ];
82 + }
83 +
84 + /**
85 + * Move the selected tickets, or all of the mailbox's tickets, to $data['new_box_id'].
86 + *
87 + * @param array $data
88 + * @param int $mailBoxId
89 + * @return array
90 + */
91 + public function moveTickets ($data, $mailBoxId )
92 + {
93 + $this->validateTicketMoving( $data, $mailBoxId );
94 +
95 + $newBox = MailBox::findOrFail($data['new_box_id']);
96 + $oldBox = MailBox::findOrFail($mailBoxId);
97 +
98 + /**
99 + * Fires before tickets move to another mailbox.
100 + * @since v1.5.7
101 + * @param array $data
102 + * @param object $oldBox Current mailbox
103 + * @param object $newBox Mailbox where selected tickets will be moved
104 + */
105 + do_action( 'fluent_support/before_move_tickets', $data, $oldBox, $newBox );
106 +
107 + if (!empty($data['ticket_ids'])) {
108 + Ticket::whereIn('id', $data['ticket_ids'])
109 + ->update([
110 + 'mailbox_id' => $newBox->id
111 + ]);
112 + } else {
113 + Ticket::where('mailbox_id', $mailBoxId)
114 + ->update([
115 + 'mailbox_id' => $newBox->id
116 + ]);
117 + }
118 + /**
119 + * Fires after tickets move to another mailbox.
120 + * @since v1.5.7
121 + * @param array $data
122 + * @param object $oldBox Mailbox where selected tickets were moved from
123 + * @param object $newBox Mailbox where selected tickets were moved to
124 + */
125 + do_action( 'fluent_support/tickets_moved', $data, $oldBox, $newBox );
126 +
127 + return [
128 + 'message' => __('All ticket moves to the selected Business', 'fluent-support')
129 + ];
130 +
131 + }
132 +
133 + /**
134 + * @param int $mailBoxId
135 + * @return array
136 + */
137 + public function setAsDefault ($mailBoxId )
138 + {
139 + $box = MailBox::findOrFail($mailBoxId);
140 +
141 + $box->is_default = 'yes';
142 + $box->save();
143 +
144 + MailBox::where('id', '!=', $mailBoxId)
145 + ->update([
146 + 'is_default' => 'no'
147 + ]);
148 +
149 + return [
150 + 'message' => __('Selected Business has been set as default', 'fluent-support')
151 + ];
152 + }
153 +
154 + /**
155 + * @param int $boxId
156 + * @return array
157 + */
158 + public function getEmailsSetups ( $boxId )
159 + {
160 + $settings = new Settings();
161 + $box = MailBox::findOrFail( $boxId );
162 +
163 + $types = $settings->getEmailSettingsKeys();
164 +
165 + $req = [];
166 + foreach ( $types as $type ) {
167 + $req[] = $settings->getBoxEmailSettings( $box, $type );
168 + }
169 +
170 + return [
171 + 'email_configs' => $req,
172 + 'email_keys' => $types
173 + ];
174 + }
175 +
176 + /**
177 + * @param string $emailType
178 + * @param int $boxId
179 + * @param array $data
180 + * @return array
181 + */
182 + public function saveEmailSettings ($emailType, $boxId, $data )
183 + {
184 + $settings = new Settings();
185 +
186 + $data['email_body'] = wp_kses_post($data['email_body']);
187 +
188 + $box = MailBox::findOrFail($boxId);
189 +
190 + $settings->saveBoxEmailSettings($box, $emailType, $data);
191 +
192 + return [
193 + 'message' => __('Settings has been updated', 'fluent-support')
194 + ];
195 + }
196 +
197 + /**
198 + * @param array $filters
199 + * @param int $boxId
200 + * @return array
201 + */
202 + public function getTickets ($filters, $boxId )
203 + {
204 + if (!$boxId) {
205 + throw new \Exception(esc_html__('MailBox ID must be provided', 'fluent-support'));
206 + }
207 +
208 + $ticketsQuery = Ticket::getTicketsQuery();
209 +
210 + $ticketsQuery->where('mailbox_id', $boxId);
211 +
212 + if ( !empty($filters['customer_id']) ) {
213 + $ticketsQuery->where('customer_id', $filters['customer_id']);
214 + }
215 +
216 + if ( !empty($filters['product_id']) ) {
217 + $ticketsQuery->where('product_id', $filters['product_id']);
218 + }
219 +
220 + if ( !empty($filters['ticket_title']) ) {
221 + $ticketsQuery->where('title', 'LIKE', "%". $filters['ticket_title'] ."%");
222 + }
223 +
224 + return [
225 + 'tickets' => $ticketsQuery->paginate()
226 + ];
227 + }
228 +
229 + private function validateTicketMoving ( $data, $mailBoxId )
230 + {
231 +
232 + if ($data['new_box_id'] == $mailBoxId) {
233 + throw new \Exception(esc_html__('New Box cannot be the same as MailBox ID', 'fluent-support'));
234 + }
235 +
236 + if ( $data['move_type'] == 'Custom' && empty($data['ticket_ids']) ) {
237 + throw new \Exception(esc_html__('Invalid request submitted. Please select a ticket first', 'fluent-support'));
238 + }
239 + $ticketIDs = $data['ticket_ids'];
240 + $newMailBoxId = (int)$data['new_box_id'];
241 +
242 + // Current-user mailbox access is checked by callers that act on behalf
243 + // of the logged-in user (MailBoxController::moveTickets). This service
244 + // is also called by Pro workflows and MCP, which run on behalf of an
245 + // automation/agent-tool call rather than the current browser session,
246 + // and already enforce their own access checks before calling here.
247 +
248 + foreach ($ticketIDs as $ticketID) {
249 + $ticket = Ticket::findOrFail($ticketID);
250 + if (!$ticket->agent_id) {
251 + continue;
252 + }
253 +
254 + $agent = Agent::find($ticket->agent_id);
255 + if (!$agent) {
256 + continue;
257 + }
258 +
259 + $restrictedBoxes = (new AgentTicketAccess())->getRestrictedMailboxIds($agent);
260 +
261 + if (in_array($newMailBoxId, $restrictedBoxes, true)) {
262 + throw new \Exception(esc_html__('Agent is restricted for this mailbox ticket', 'fluent-support'));
263 + }
264 + }
265 + return true;
266 + }
267 +
268 + private function deleteAllMailBoxMeta($id)
269 + {
270 + $class = __NAMESPACE__ . '\MailBox';
271 +
272 + Meta::where('object_type', $class)
273 + ->where('object_id', $id)
274 + ->delete();
275 + }
11 276 }