| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\MailBox; |
| 6 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 7 |
use FluentSupport\App\Services\Helper; |
| 8 |
use FluentSupport\App\Services\MailerInbox\MailBoxService; |
| 9 |
use FluentSupport\Framework\Http\Request\Request; |
| 10 |
|
| 11 |
class MailBoxController extends Controller |
| 12 |
{ |
| 13 |
/** |
| 14 |
* index method will return the list of business inbox |
| 15 |
* @param Request $request |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
public function index(MailBoxService $mailboxService) |
| 19 |
{ |
| 20 |
return $mailboxService->getMailBoxes(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* get method will fetch and return information related to business box |
| 25 |
* @param Request $request |
| 26 |
* @param $id |
| 27 |
* @return mixed |
| 28 |
*/ |
| 29 |
public function get( MailBox $mailBox, $id ) |
| 30 |
{ |
| 31 |
return [ |
| 32 |
'mailbox' => $mailBox->getMailBox( $id ) |
| 33 |
]; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Save method will create new business box |
| 40 |
* @param Request $request |
| 41 |
* @param MailBox $mailBox |
| 42 |
* @return array |
| 43 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 44 |
*/ |
| 45 |
public function save(Request $request, MailBox $mailBox) |
| 46 |
{ |
| 47 |
$data = wp_unslash( $request->get('business', null) ); |
| 48 |
$data = $this->sanitizeMailboxData($data); |
| 49 |
|
| 50 |
$this->validate($data, [ |
| 51 |
'name' => 'required', |
| 52 |
'email' => 'required' |
| 53 |
]); |
| 54 |
|
| 55 |
return [ |
| 56 |
'message' => __('Mailbox has been created successfully', 'fluent-support'), |
| 57 |
'mailbox' => $mailBox->createMailBox( $data ) |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* This `update` method will update existing information for a business by mailbox id |
| 63 |
* @param Request $request |
| 64 |
* @param MailBox $mailBox |
| 65 |
* @param int $id |
| 66 |
* @return array |
| 67 |
* @throws \Exception |
| 68 |
*/ |
| 69 |
public function update(Request $request, MailBox $mailBox, $id) |
| 70 |
{ |
| 71 |
try{ |
| 72 |
$data = wp_unslash( $request->get('business', null) ); |
| 73 |
$data = $this->sanitizeMailboxData($data); |
| 74 |
|
| 75 |
$this->validate($data, [ |
| 76 |
'name' => 'required', |
| 77 |
'email' => 'required' |
| 78 |
]); |
| 79 |
|
| 80 |
return [ |
| 81 |
'message' => __( 'Mailbox has been saved', 'fluent-support' ), |
| 82 |
'mailbox' => $mailBox->updateMailBox( $data, $id ) |
| 83 |
]; |
| 84 |
}catch (\Exception $e){ |
| 85 |
return [ |
| 86 |
'message' => Helper::getSafeErrorMessage($e), |
| 87 |
]; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* This `delete` method will delete a business from mailbox and replaced with alternative |
| 93 |
* @param Request $request |
| 94 |
* @param MailBoxService $mailBoxService |
| 95 |
* @param int $id |
| 96 |
* @throws \Exception |
| 97 |
* @return array |
| 98 |
*/ |
| 99 |
public function delete(Request $request, MailBoxService $mailBoxService, $id) |
| 100 |
{ |
| 101 |
try { |
| 102 |
return $mailBoxService->deleteMailBox( $id, $request->getSafe('fallback_id', 'intval') ); |
| 103 |
} catch (\Exception $e) { |
| 104 |
return [ |
| 105 |
'message' => Helper::getSafeErrorMessage($e), |
| 106 |
]; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* This `moveTickets` method will move tickets from one mailbox to another |
| 113 |
* @param Request $request |
| 114 |
* @param MailBoxService $mailBoxService |
| 115 |
* @param int $id |
| 116 |
* @throws \Exception |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public function moveTickets(Request $request, MailBoxService $mailBoxService, $id) |
| 120 |
{ |
| 121 |
try { |
| 122 |
$data = $request->only(['ticket_ids', 'new_box_id', 'move_type']); |
| 123 |
return $mailBoxService->moveTickets( $data, $id ); |
| 124 |
} catch (\Exception $e) { |
| 125 |
return $this->sendError(Helper::getSafeErrorMessage($e)); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* This `getEmailSettings` method will get and return the mailbox email settings |
| 131 |
* @param Request $request |
| 132 |
* @param Settings $settings |
| 133 |
* @param $id |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
public function getEmailSettings(Request $request, Settings $settings, $id) |
| 137 |
{ |
| 138 |
$box = MailBox::findOrFail($id); |
| 139 |
$emailType = $request->getSafe('email_type', 'sanitize_text_field'); |
| 140 |
|
| 141 |
return [ |
| 142 |
'email_settings' => $settings->getBoxEmailSettings($box, $emailType) |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* This `getEmailsSetups` method will return email settings for a business box by box id |
| 148 |
* @param MailBoxService $mailBoxService |
| 149 |
* @param $id |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
public function getEmailsSetups( MailBoxService $mailBoxService, $id ) |
| 153 |
{ |
| 154 |
return $mailBoxService->getEmailsSetups($id); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* This `saveEmailSettings` method will save the email settings for a business box using box id |
| 159 |
* @param Request $request |
| 160 |
* @param Settings $settings |
| 161 |
* @param $id |
| 162 |
* @return array |
| 163 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 164 |
*/ |
| 165 |
public function saveEmailSettings( Request $request, MailBoxService $mailBoxService, $id ) |
| 166 |
{ |
| 167 |
$data = wp_unslash($request->get('email_settings', null)); |
| 168 |
$data = is_array($data) ? [ |
| 169 |
'key' => isset($data['key']) ? sanitize_key($data['key']) : '', |
| 170 |
'title' => isset($data['title']) ? sanitize_text_field($data['title']) : '', |
| 171 |
'email_subject' => isset($data['email_subject']) ? sanitize_text_field($data['email_subject']) : '', |
| 172 |
'email_body' => isset($data['email_body']) ? wp_kses_post($data['email_body']) : '', |
| 173 |
'status' => isset($data['status']) ? sanitize_text_field($data['status']) : '', |
| 174 |
'can_edit_subject' => isset($data['can_edit_subject']) ? sanitize_text_field($data['can_edit_subject']) : '', |
| 175 |
'send_attachments' => isset($data['send_attachments']) ? sanitize_text_field($data['send_attachments']) : '', |
| 176 |
] : []; |
| 177 |
|
| 178 |
$emailType = $request->getSafe('email_type', 'sanitize_text_field'); |
| 179 |
|
| 180 |
$this->validate($data, [ |
| 181 |
'email_subject' => 'required', |
| 182 |
'email_body' => 'required' |
| 183 |
]); |
| 184 |
|
| 185 |
return $mailBoxService->saveEmailSettings( $emailType, $id, $data ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* This `setAsDefault` method will set a business box as default |
| 190 |
* @param MailBoxService $mailBoxService |
| 191 |
* @param $id |
| 192 |
* @return array |
| 193 |
*/ |
| 194 |
public function setAsDefault( MailBoxService $mailBoxService, $id ) |
| 195 |
{ |
| 196 |
return $mailBoxService->setAsDefault( $id ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* This `getTickets` method will return the list of tickets for a business box |
| 201 |
* @param Request $request |
| 202 |
* @param MailBox $mailBox |
| 203 |
* @param int $id |
| 204 |
* @return array |
| 205 |
*/ |
| 206 |
public function getTickets(Request $request, MailBoxService $mailBoxService, $id) |
| 207 |
{ |
| 208 |
$filters = $request->get('filters', null); |
| 209 |
$filters = is_array($filters) ? [ |
| 210 |
'status_type' => isset($filters['status_type']) ? sanitize_text_field($filters['status_type']) : '', |
| 211 |
'customer_id' => isset($filters['customer_id']) ? intval($filters['customer_id']) : 0, |
| 212 |
'product_id' => isset($filters['product_id']) ? intval($filters['product_id']) : 0, |
| 213 |
'mailbox_id' => isset($filters['mailbox_id']) ? intval($filters['mailbox_id']) : 0, |
| 214 |
'ticket_title' => isset($filters['ticket_title']) ? sanitize_text_field($filters['ticket_title']) : '', |
| 215 |
'notes' => isset($filters['notes']) ? sanitize_text_field($filters['notes']) : '', |
| 216 |
] : []; |
| 217 |
|
| 218 |
return $mailBoxService->getTickets( $filters, $id ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Sanitize mailbox data array |
| 223 |
* |
| 224 |
* @param mixed $data |
| 225 |
* @return array |
| 226 |
*/ |
| 227 |
private function sanitizeMailboxData($data) |
| 228 |
{ |
| 229 |
if (!is_array($data)) { |
| 230 |
return []; |
| 231 |
} |
| 232 |
|
| 233 |
$sanitized = [ |
| 234 |
'name' => isset($data['name']) ? sanitize_text_field($data['name']) : '', |
| 235 |
'email' => isset($data['email']) ? sanitize_email($data['email']) : '', |
| 236 |
'box_type' => isset($data['box_type']) ? sanitize_key($data['box_type']) : '', |
| 237 |
'mapped_email' => isset($data['mapped_email']) ? sanitize_email($data['mapped_email']) : '', |
| 238 |
'email_footer' => isset($data['email_footer']) ? wp_kses_post($data['email_footer']) : '', |
| 239 |
'is_default' => isset($data['is_default']) ? sanitize_text_field($data['is_default']) : 'no', |
| 240 |
]; |
| 241 |
|
| 242 |
// Handle nested settings array |
| 243 |
if (isset($data['settings']) && is_array($data['settings'])) { |
| 244 |
$sanitized['settings'] = map_deep($data['settings'], 'sanitize_text_field'); |
| 245 |
// Preserve email in settings |
| 246 |
if (isset($data['settings']['admin_email_address'])) { |
| 247 |
$sanitized['settings']['admin_email_address'] = sanitize_email($data['settings']['admin_email_address']); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
return $sanitized; |
| 252 |
} |
| 253 |
} |
| 254 |
|