PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.2
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.2
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 / Services / MailerInbox / MailBoxService.php

MailBoxService.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.2, at app/Services/MailerInbox/MailBoxService.php

277 lines 8.0 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\MailerInbox;
4
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\Framework\Support\Arr;
12 use Exception;
13
14 class MailBoxService
15 {
16 /**
17 * This `getMailBoxes` method is used to get all mailboxes.
18 * @return array
19 */
20 public function getMailBoxes ()
21 {
22 $mailboxes = MailBox::all();
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 * This `deleteMailBox` method is used to delete a mailbox.
35 * @param int $mailBoxId The id of the mailbox to be deleted.
36 * @param int $fallbackId The id of the fallback mailbox to be used.
37 * @return array
38 */
39 public function deleteMailBox($mailBoxId, $fallbackId )
40 {
41 if ( $mailBoxId == $fallbackId ) {
42 throw new \Exception('Fallback Box can not be the same as MailBox ID');
43 }
44
45 $box = MailBox::findOrFail($mailBoxId);
46 $fallbackBox = MailBox::findOrFail($fallbackId);
47
48 // if the mailbox is default, then make the fallback mailbox default
49 if($box->is_default == 'yes'){
50 $fallbackBox->is_default = 'yes';
51 $fallbackBox->save();
52 }
53
54 /*
55 * Action before delete a mailbox
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 // let's transfer the tickets now
68 Ticket::syncMailBoxId($mailBoxId, $fallbackId);
69
70 /*
71 * Action on mailbox delete
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 * This `moveTickets` method is used to move tickets from one mailbox to another.
86 * @param array $data
87 * @param int $mailBoxId
88 * @return array
89 */
90 public function moveTickets ($data, $mailBoxId )
91 {
92 $this->validateTicketMoving( $data, $mailBoxId );
93
94 $newBox = MailBox::findOrFail($data['new_box_id']);
95 $oldBox = MailBox::findOrFail($mailBoxId);
96
97 /**
98 * Action before tickets moved
99 * @since v1.5.7
100 * @param array $data
101 * @param object $oldBox Current mailbox
102 * @param object $newBox Mailbox where selected tickets will be moved
103 */
104 do_action( 'fluent_support/before_move_tickets', $data, $oldBox, $newBox );
105
106 if (!empty($data['ticket_ids'])) {
107 Ticket::whereIn('id', $data['ticket_ids'])
108 ->update([
109 'mailbox_id' => $newBox->id
110 ]);
111 } else {
112 // Move all ticket for the MailBox
113 Ticket::where('mailbox_id', $mailBoxId)
114 ->update([
115 'mailbox_id' => $newBox->id
116 ]);
117 }
118 /**
119 * Action after tickets moved
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 * This `setAsDefault` method will set a business box as default. Update is_default column to yes for the selected mailbox and no for others.
135 * @param int $mailBoxId
136 * @return array
137 */
138 public function setAsDefault ($mailBoxId )
139 {
140 $box = MailBox::findOrFail($mailBoxId);
141
142 $box->is_default = 'yes';
143 $box->save();
144
145 MailBox::where('id', '!=', $mailBoxId)
146 ->update([
147 'is_default' => 'no'
148 ]);
149
150 return [
151 'message' => __('Selected Business has been set as default', 'fluent-support')
152 ];
153 }
154
155 /**
156 * This `getEmailSetups` method is used to get the email setups
157 * @param int $boxId
158 * @return array
159 */
160 public function getEmailsSetups ( $boxId )
161 {
162 $settings = new Settings();
163 $box = MailBox::findOrFail( $boxId );
164
165 $types = $settings->getEmailSettingsKeys();
166
167 $req = [];
168 foreach ( $types as $type ) {
169 $req[] = $settings->getBoxEmailSettings( $box, $type );
170 }
171
172 return [
173 'email_configs' => $req,
174 'email_keys' => $types
175 ];
176 }
177
178 /**
179 * This `saveEmailSettings` method is used to save the email settings for the mailbox
180 * @param object $request
181 * @param int $boxId
182 * @return array
183 */
184 public function saveEmailSettings ($emailType, $boxId, $data )
185 {
186 $settings = new Settings();
187
188 $data['email_body'] = wp_kses_post($data['email_body']);
189
190 $box = MailBox::findOrFail($boxId);
191
192 $settings->saveBoxEmailSettings($box, $emailType, $data);
193
194 return [
195 'message' => __('Settings has been updated', 'fluent-support')
196 ];
197 }
198
199 /**
200 * This `getTickets` method is used to get the tickets for the given mailbox
201 * @param array $filters
202 * @param int $boxId
203 * @return array
204 */
205 public function getTickets ($filters, $boxId )
206 {
207 if (!$boxId) {
208 throw new \Exception('MailBox ID must be provided');
209 }
210
211 $ticketsQuery = Ticket::getTicketsQuery();
212
213 $ticketsQuery->where('mailbox_id', $boxId);
214
215 if ( $filters['customer_id'] ) {
216 $ticketsQuery->where('customer_id', $filters['customer_id']);
217 }
218
219 if ( $filters['product_id'] ) {
220 $ticketsQuery->where('product_id', $filters['product_id']);
221 }
222
223 if ( $filters['ticket_title'] ) {
224 $ticketsQuery->where('title', 'LIKE', "%". $filters['ticket_title'] ."%");
225 }
226
227 return [
228 'tickets' => $ticketsQuery->paginate()
229 ];
230 }
231
232 private function validateTicketMoving ( $data, $mailBoxId )
233 {
234
235 if ($data['new_box_id'] == $mailBoxId) {
236 throw new \Exception('New Box can not be the same as MailBox ID');
237 }
238
239 if ( $data['move_type'] == 'Custom' && empty($data['ticket_ids']) ) {
240 throw new \Exception('Invalid request submitted, Select ticket first');
241 }
242 $ticketIDs = $data['ticket_ids'];
243 $newMailBoxId = (int)$data['new_box_id'];
244
245 foreach ($ticketIDs as $ticketID) {
246 $ticket = Ticket::findOrFail($ticketID);
247 if (!$ticket->agent_id) {
248 continue;
249 }
250
251 $agent = Agent::find($ticket->agent_id);
252 if (!$agent) {
253 continue;
254 }
255
256 $restrictions = $agent->getMeta('agent_restrictions', []);
257 if (!empty($restrictions['restrictedBusinessBoxes'])) {
258 $restrictedBoxes = array_map('intval', $restrictions['restrictedBusinessBoxes']);
259
260 if (in_array($newMailBoxId, $restrictedBoxes)) {
261 throw new \Exception('Agent is restricted for this mailbox ticket');
262 }
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 }
276 }
277