| @@ -2,170 +2,252 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Http\Controllers; |
| 4 | 4 | |
| 5 | 5 | use FluentSupport\App\Models\MailBox; |
| 6 | -use FluentSupport\App\Models\Ticket; | |
| 7 | 6 | use FluentSupport\App\Services\EmailNotification\Settings; |
| 8 | -use FluentSupport\Framework\Request\Request; | |
| 7 | +use FluentSupport\App\Services\Helper; | |
| 8 | +use FluentSupport\App\Services\MailerInbox\MailBoxService; | |
| 9 | +use FluentSupport\Framework\Http\Request\Request; | |
| 9 | 10 | |
| 10 | 11 | class MailBoxController extends Controller |
| 11 | 12 | { |
| 12 | - public function index(Request $request) | |
| 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) | |
| 13 | 19 | { |
| 14 | - $mailboxes = MailBox::all(); | |
| 20 | + return $mailboxService->getMailBoxes(); | |
| 21 | + } | |
| 15 | 22 | |
| 16 | - foreach ($mailboxes as $mailbox) { | |
| 17 | - $mailbox->tickets_count = Ticket::where('mailbox_id', $mailbox->id)->count(); | |
| 18 | - } | |
| 19 | - | |
| 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 | + { | |
| 20 | 31 | return [ |
| 21 | - 'mailboxes' => $mailboxes | |
| 32 | + 'mailbox' => $mailBox->getMailBox( $id ) | |
| 22 | 33 | ]; |
| 34 | + | |
| 23 | 35 | } |
| 24 | 36 | |
| 25 | - public function get(Request $request, $id) | |
| 26 | - { | |
| 27 | - $mailbox = MailBox::findOrFail($id); | |
| 28 | 37 | |
| 29 | - return $this->sendSuccess([ | |
| 30 | - 'mailbox' => $mailbox | |
| 31 | - ]); | |
| 32 | - } | |
| 33 | - | |
| 34 | - public function save(Request $request) | |
| 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) | |
| 35 | 46 | { |
| 36 | - $data = $request->get('business'); | |
| 37 | - $data = wp_unslash($data); | |
| 47 | + $data = wp_unslash( $request->get('business', null) ); | |
| 48 | + $data = $this->sanitizeMailboxData($data); | |
| 38 | 49 | |
| 39 | 50 | $this->validate($data, [ |
| 40 | - 'name' => 'required', | |
| 51 | + 'name' => 'required', | |
| 41 | 52 | 'email' => 'required' |
| 42 | 53 | ]); |
| 43 | 54 | |
| 44 | - if($data['box_type'] == 'email') { | |
| 45 | - $data['settings'] = [ | |
| 46 | - 'admin_email_address' => '' | |
| 47 | - ]; | |
| 48 | - } else { | |
| 49 | - $data['settings'] = [ | |
| 50 | - 'admin_email_address' => $data['email'] | |
| 51 | - ]; | |
| 52 | - } | |
| 53 | - | |
| 54 | - if (!MailBox::first()) { | |
| 55 | - $data['is_default'] = 'yes'; | |
| 56 | - } | |
| 57 | - | |
| 58 | - $mailbox = MailBox::create($data); | |
| 59 | - | |
| 60 | 55 | return [ |
| 61 | 56 | 'message' => __('Mailbox has been created successfully', 'fluent-support'), |
| 62 | - 'mailbox' => $mailbox | |
| 57 | + 'mailbox' => $mailBox->createMailBox( $data ) | |
| 63 | 58 | ]; |
| 64 | 59 | } |
| 65 | 60 | |
| 66 | - public function update(Request $request, $mailBoxId) | |
| 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) | |
| 67 | 70 | { |
| 68 | - $data = $request->get('business'); | |
| 69 | - $data = wp_unslash($data); | |
| 71 | + try{ | |
| 72 | + $data = wp_unslash( $request->get('business', null) ); | |
| 73 | + $data = $this->sanitizeMailboxData($data); | |
| 70 | 74 | |
| 71 | - $this->validate($data, [ | |
| 72 | - 'name' => 'required', | |
| 73 | - 'email' => 'required' | |
| 74 | - ]); | |
| 75 | + $this->validate($data, [ | |
| 76 | + 'name' => 'required', | |
| 77 | + 'email' => 'required' | |
| 78 | + ]); | |
| 75 | 79 | |
| 76 | - $mailbox = MailBox::findOrFail($mailBoxId); | |
| 77 | - | |
| 78 | - if ($data['box_type'] == 'email' && empty($data['mapped_email'])) { | |
| 79 | - return $this->sendError([ | |
| 80 | - 'message' => __('Mapped Email Address is required', 'fluent-support') | |
| 81 | - ]); | |
| 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 | + ]; | |
| 82 | 88 | } |
| 83 | - | |
| 84 | - $mailbox->fill($data); | |
| 85 | - $mailbox->save(); | |
| 86 | - | |
| 87 | - return [ | |
| 88 | - 'message' => __('Mailbox has been saved', 'fluent-support'), | |
| 89 | - 'mailbox' => $mailbox | |
| 90 | - ]; | |
| 91 | 89 | } |
| 92 | 90 | |
| 93 | - public function delete(Request $request, $mailBoxId) | |
| 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) | |
| 94 | 100 | { |
| 95 | - $fallbackId = $request->get('fallback_id'); | |
| 96 | - | |
| 97 | - if($fallbackId == $mailBoxId) { | |
| 98 | - return $this->sendError([ | |
| 99 | - 'message' => __('Fallback Box can not be the same as MailBox ID', 'fluent-support') | |
| 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 | + ]; | |
| 101 | 107 | } |
| 108 | + } | |
| 102 | 109 | |
| 103 | - $box = MailBox::findOrFail($mailBoxId); | |
| 104 | - $fallbackBox = MailBox::findOrFail($fallbackId); | |
| 105 | 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 | + } | |
| 106 | 128 | |
| 107 | - do_action('fluent_support/before_delete_email_box', $box, $fallbackBox); | |
| 108 | - $box->deleteAllMeta(); | |
| 109 | - MailBox::where('id', $mailBoxId)->delete(); | |
| 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'); | |
| 110 | 140 | |
| 111 | - // lets transfer the tickets now | |
| 112 | - Ticket::where('mailbox_id', $mailBoxId) | |
| 113 | - ->update([ | |
| 114 | - 'mailbox_id' => $fallbackBox->id | |
| 115 | - ]); | |
| 116 | - | |
| 117 | - do_action('fluent_support/mailbox_deleted', $mailBoxId, $fallbackBox); | |
| 118 | - | |
| 119 | 141 | return [ |
| 120 | - 'message' => __('Selected Business has been deleted', 'fluent-support') | |
| 142 | + 'email_settings' => $settings->getBoxEmailSettings($box, $emailType) | |
| 121 | 143 | ]; |
| 122 | - | |
| 123 | 144 | } |
| 124 | 145 | |
| 125 | - public function getEmailSettings(Request $request, Settings $settings, $boxId) | |
| 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 ) | |
| 126 | 153 | { |
| 127 | - $box = MailBox::findOrFail($boxId); | |
| 128 | - $emailType = $request->get('email_type'); | |
| 129 | - | |
| 130 | - return [ | |
| 131 | - 'email_settings' => $settings->getBoxEmailSettings($box, $emailType) | |
| 132 | - ]; | |
| 154 | + return $mailBoxService->getEmailsSetups($id); | |
| 133 | 155 | } |
| 134 | 156 | |
| 135 | - public function getEmailsSetups(Request $request, Settings $settings, $boxId) | |
| 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 ) | |
| 136 | 166 | { |
| 137 | - $box = MailBox::findOrFail($boxId); | |
| 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 | + ] : []; | |
| 138 | 177 | |
| 139 | - $types = $settings->getEmailSettingsKeys(); | |
| 178 | + $emailType = $request->getSafe('email_type', 'sanitize_text_field'); | |
| 140 | 179 | |
| 141 | - $req = []; | |
| 142 | - foreach($types as $type) { | |
| 143 | - $req[] = $settings->getBoxEmailSettings($box, $type); | |
| 144 | - } | |
| 180 | + $this->validate($data, [ | |
| 181 | + 'email_subject' => 'required', | |
| 182 | + 'email_body' => 'required' | |
| 183 | + ]); | |
| 145 | 184 | |
| 146 | - return [ | |
| 147 | - 'email_configs' => $req, | |
| 148 | - 'email_keys' => $types | |
| 149 | - ]; | |
| 185 | + return $mailBoxService->saveEmailSettings( $emailType, $id, $data ); | |
| 150 | 186 | } |
| 151 | 187 | |
| 152 | - public function saveEmailSettings(Request $request, Settings $settings, $boxId) | |
| 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 ) | |
| 153 | 195 | { |
| 154 | - $data = wp_unslash($request->get('email_settings')); | |
| 155 | - $this->validate($data, [ | |
| 156 | - 'email_subject' => 'required', | |
| 157 | - 'email_body' => 'required' | |
| 158 | - ]); | |
| 196 | + return $mailBoxService->setAsDefault( $id ); | |
| 197 | + } | |
| 159 | 198 | |
| 160 | - $data['email_body'] = wp_kses_post($data['email_body']); | |
| 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 | + ] : []; | |
| 161 | 217 | |
| 162 | - $box = MailBox::findOrFail($boxId); | |
| 163 | - $emailType = $request->get('email_type'); | |
| 218 | + return $mailBoxService->getTickets( $filters, $id ); | |
| 219 | + } | |
| 164 | 220 | |
| 165 | - $settings->saveBoxEmailSettings($box, $emailType, $data); | |
| 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 | + } | |
| 166 | 232 | |
| 167 | - return [ | |
| 168 | - 'message' => __('Settings has been updated', 'fluent-support') | |
| 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', | |
| 169 | 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; | |
| 170 | 252 | } |
| 171 | 253 | } |