| @@ -2,13 +2,12 @@ | ||
| 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\App\Services\TicketHelper; | |
| 9 | -use FluentSupport\App\Services\TicketQueryService; | |
| 10 | -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; | |
| 11 | 10 | |
| 12 | 11 | class MailBoxController extends Controller |
| 13 | 12 | { |
| 14 | 13 | /** |
| @@ -15,19 +14,11 @@ | ||
| 15 | 14 | * index method will return the list of business inbox |
| 16 | 15 | * @param Request $request |
| 17 | 16 | * @return array |
| 18 | 17 | */ |
| 19 | - public function index(Request $request) | |
| 18 | + public function index(MailBoxService $mailboxService) | |
| 20 | 19 | { |
| 21 | - $mailboxes = MailBox::all(); | |
| 22 | - | |
| 23 | - foreach ($mailboxes as $mailbox) { | |
| 24 | - $mailbox->tickets_count = Ticket::where('mailbox_id', $mailbox->id)->count(); | |
| 25 | - } | |
| 26 | - | |
| 27 | - return [ | |
| 28 | - 'mailboxes' => $mailboxes | |
| 29 | - ]; | |
| 20 | + return $mailboxService->getMailBoxes(); | |
| 30 | 21 | } |
| 31 | 22 | |
| 32 | 23 | /** |
| 33 | 24 | * get method will fetch and return information related to business box |
| @@ -34,15 +25,14 @@ | ||
| 34 | 25 | * @param Request $request |
| 35 | 26 | * @param $id |
| 36 | 27 | * @return mixed |
| 37 | 28 | */ |
| 38 | - public function get(Request $request, $id) | |
| 29 | + public function get( MailBox $mailBox, $id ) | |
| 39 | 30 | { |
| 40 | - $mailbox = MailBox::findOrFail($id); | |
| 31 | + return [ | |
| 32 | + 'mailbox' => $mailBox->getMailBox( $id ) | |
| 33 | + ]; | |
| 41 | 34 | |
| 42 | - return $this->sendSuccess([ | |
| 43 | - 'mailbox' => $mailbox | |
| 44 | - ]); | |
| 45 | 35 | } |
| 46 | 36 | |
| 47 | 37 | |
| 48 | 38 | /** |
| @@ -47,15 +37,16 @@ | ||
| 47 | 37 | |
| 48 | 38 | /** |
| 49 | 39 | * Save method will create new business box |
| 50 | 40 | * @param Request $request |
| 41 | + * @param MailBox $mailBox | |
| 51 | 42 | * @return array |
| 52 | 43 | * @throws \FluentSupport\Framework\Validator\ValidationException |
| 53 | 44 | */ |
| 54 | - public function save(Request $request) | |
| 45 | + public function save(Request $request, MailBox $mailBox) | |
| 55 | 46 | { |
| 56 | - $data = $request->get('business'); | |
| 57 | - $data = wp_unslash($data); | |
| 47 | + $data = wp_unslash( $request->get('business', null) ); | |
| 48 | + $data = $this->sanitizeMailboxData($data); | |
| 58 | 49 | |
| 59 | 50 | $this->validate($data, [ |
| 60 | 51 | 'name' => 'required', |
| 61 | 52 | 'email' => 'required' |
| @@ -60,164 +51,93 @@ | ||
| 60 | 51 | 'name' => 'required', |
| 61 | 52 | 'email' => 'required' |
| 62 | 53 | ]); |
| 63 | 54 | |
| 64 | - if ($data['box_type'] == 'email') { | |
| 65 | - $data['settings'] = [ | |
| 66 | - 'admin_email_address' => '' | |
| 67 | - ]; | |
| 68 | - } else { | |
| 69 | - $data['settings'] = [ | |
| 70 | - 'admin_email_address' => $data['email'] | |
| 71 | - ]; | |
| 72 | - } | |
| 73 | - | |
| 74 | - if (!MailBox::first()) { | |
| 75 | - $data['is_default'] = 'yes'; | |
| 76 | - } | |
| 77 | - | |
| 78 | - $mailbox = MailBox::create($data); | |
| 79 | - | |
| 80 | 55 | return [ |
| 81 | 56 | 'message' => __('Mailbox has been created successfully', 'fluent-support'), |
| 82 | - 'mailbox' => $mailbox | |
| 57 | + 'mailbox' => $mailBox->createMailBox( $data ) | |
| 83 | 58 | ]; |
| 84 | 59 | } |
| 85 | 60 | |
| 86 | 61 | /** |
| 87 | - * Update method will update existing information for a business by mailbox id | |
| 62 | + * This `update` method will update existing information for a business by mailbox id | |
| 88 | 63 | * @param Request $request |
| 89 | - * @param $mailBoxId | |
| 64 | + * @param MailBox $mailBox | |
| 65 | + * @param int $id | |
| 90 | 66 | * @return array |
| 91 | - * @throws \FluentSupport\Framework\Validator\ValidationException | |
| 67 | + * @throws \Exception | |
| 92 | 68 | */ |
| 93 | - public function update(Request $request, $mailBoxId) | |
| 69 | + public function update(Request $request, MailBox $mailBox, $id) | |
| 94 | 70 | { |
| 95 | - $data = $request->get('business'); | |
| 96 | - $data = wp_unslash($data); | |
| 71 | + try{ | |
| 72 | + $data = wp_unslash( $request->get('business', null) ); | |
| 73 | + $data = $this->sanitizeMailboxData($data); | |
| 97 | 74 | |
| 98 | - $this->validate($data, [ | |
| 99 | - 'name' => 'required', | |
| 100 | - 'email' => 'required' | |
| 101 | - ]); | |
| 75 | + $this->validate($data, [ | |
| 76 | + 'name' => 'required', | |
| 77 | + 'email' => 'required' | |
| 78 | + ]); | |
| 102 | 79 | |
| 103 | - $mailbox = MailBox::findOrFail($mailBoxId); | |
| 104 | - | |
| 105 | - if ($data['box_type'] == 'email' && empty($data['mapped_email'])) { | |
| 106 | - return $this->sendError([ | |
| 107 | - 'message' => __('Mapped Email Address is required', 'fluent-support') | |
| 108 | - ]); | |
| 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 | + ]; | |
| 109 | 88 | } |
| 110 | - | |
| 111 | - $mailbox->fill($data); | |
| 112 | - $mailbox->save(); | |
| 113 | - | |
| 114 | - return [ | |
| 115 | - 'message' => __('Mailbox has been saved', 'fluent-support'), | |
| 116 | - 'mailbox' => $mailbox | |
| 117 | - ]; | |
| 118 | 89 | } |
| 119 | 90 | |
| 120 | 91 | /** |
| 121 | - * delete method will delete a business from mailbox and replaced with alternative | |
| 92 | + * This `delete` method will delete a business from mailbox and replaced with alternative | |
| 122 | 93 | * @param Request $request |
| 123 | - * @param $mailBoxId | |
| 94 | + * @param MailBoxService $mailBoxService | |
| 95 | + * @param int $id | |
| 96 | + * @throws \Exception | |
| 124 | 97 | * @return array |
| 125 | 98 | */ |
| 126 | - public function delete(Request $request, $mailBoxId) | |
| 99 | + public function delete(Request $request, MailBoxService $mailBoxService, $id) | |
| 127 | 100 | { |
| 128 | - $fallbackId = $request->get('fallback_id'); | |
| 129 | - | |
| 130 | - if ($fallbackId == $mailBoxId) { | |
| 131 | - return $this->sendError([ | |
| 132 | - 'message' => __('Fallback Box can not be the same as MailBox ID', 'fluent-support') | |
| 133 | - ]); | |
| 101 | + try { | |
| 102 | + return $mailBoxService->deleteMailBox( $id, $request->getSafe('fallback_id', 'intval') ); | |
| 103 | + } catch (\Exception $e) { | |
| 104 | + return [ | |
| 105 | + 'message' => Helper::getSafeErrorMessage($e), | |
| 106 | + ]; | |
| 134 | 107 | } |
| 108 | + } | |
| 135 | 109 | |
| 136 | - $box = MailBox::findOrFail($mailBoxId); | |
| 137 | - $fallbackBox = MailBox::findOrFail($fallbackId); | |
| 138 | 110 | |
| 139 | - /* | |
| 140 | - * Action before delete a mailbox | |
| 141 | - * | |
| 142 | - * @since v1.0.0 | |
| 143 | - * @param object $box Mailbox | |
| 144 | - * @param object $fallbackBox Fallback mailbox | |
| 145 | - */ | |
| 146 | - do_action('fluent_support/before_delete_email_box', $box, $fallbackBox); | |
| 147 | - $box->deleteAllMeta(); | |
| 148 | - MailBox::where('id', $mailBoxId)->delete(); | |
| 149 | - | |
| 150 | - // lets transfer the tickets now | |
| 151 | - Ticket::where('mailbox_id', $mailBoxId) | |
| 152 | - ->update([ | |
| 153 | - 'mailbox_id' => $fallbackBox->id | |
| 154 | - ]); | |
| 155 | - | |
| 156 | - /* | |
| 157 | - * Action on mailbox delete | |
| 158 | - * | |
| 159 | - * @since v1.0.0 | |
| 160 | - * @param integer $mailBoxId | |
| 161 | - * @param object $fallbackBox Fallback mailbox | |
| 162 | - */ | |
| 163 | - do_action('fluent_support/mailbox_deleted', $mailBoxId, $fallbackBox); | |
| 164 | - | |
| 165 | - return [ | |
| 166 | - 'message' => __('Selected Business has been deleted', 'fluent-support') | |
| 167 | - ]; | |
| 168 | - | |
| 169 | - } | |
| 170 | - | |
| 171 | - public function moveTickets(Request $request, $mailBoxId) | |
| 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) | |
| 172 | 120 | { |
| 173 | - $newBoxId = $request->get('new_box_id'); | |
| 174 | - $ticketIds = $request->get('ticket_ids', []); | |
| 175 | - $move_type = $request->get('move_type'); | |
| 176 | - | |
| 177 | - if ($newBoxId == $mailBoxId) { | |
| 178 | - return $this->sendError([ | |
| 179 | - 'message' => __('New Box can not be the same as MailBox ID', 'fluent-support') | |
| 180 | - ]); | |
| 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)); | |
| 181 | 126 | } |
| 182 | - | |
| 183 | - if ($move_type == 'Custom' && empty($ticketIds)) { | |
| 184 | - return $this->sendError([ | |
| 185 | - 'message' => __('Invalid request submitted, Select ticket first', 'fluent-support') | |
| 186 | - ]); | |
| 187 | - } | |
| 188 | - | |
| 189 | - $newBox = MailBox::findOrFail($newBoxId); | |
| 190 | - | |
| 191 | - if (!empty($ticketIds)) { | |
| 192 | - Ticket::whereIn('id', $ticketIds) | |
| 193 | - ->update([ | |
| 194 | - 'mailbox_id' => $newBox->id | |
| 195 | - ]); | |
| 196 | - } else { | |
| 197 | - // Move all ticket for the MailBox | |
| 198 | - Ticket::where('mailbox_id', $mailBoxId) | |
| 199 | - ->update([ | |
| 200 | - 'mailbox_id' => $newBox->id | |
| 201 | - ]); | |
| 202 | - } | |
| 203 | - | |
| 204 | - return [ | |
| 205 | - 'message' => __('All ticket moves to the selected Business', 'fluent-support') | |
| 206 | - ]; | |
| 207 | 127 | } |
| 208 | 128 | |
| 209 | 129 | /** |
| 210 | - * getEmailSettings method will get and return the mailbox email settings | |
| 130 | + * This `getEmailSettings` method will get and return the mailbox email settings | |
| 211 | 131 | * @param Request $request |
| 212 | 132 | * @param Settings $settings |
| 213 | - * @param $boxId | |
| 133 | + * @param $id | |
| 214 | 134 | * @return array |
| 215 | 135 | */ |
| 216 | - public function getEmailSettings(Request $request, Settings $settings, $boxId) | |
| 136 | + public function getEmailSettings(Request $request, Settings $settings, $id) | |
| 217 | 137 | { |
| 218 | - $box = MailBox::findOrFail($boxId); | |
| 219 | - $emailType = $request->get('email_type'); | |
| 138 | + $box = MailBox::findOrFail($id); | |
| 139 | + $emailType = $request->getSafe('email_type', 'sanitize_text_field'); | |
| 220 | 140 | |
| 221 | 141 | return [ |
| 222 | 142 | 'email_settings' => $settings->getBoxEmailSettings($box, $emailType) |
| 223 | 143 | ]; |
| @@ -223,93 +143,111 @@ | ||
| 223 | 143 | ]; |
| 224 | 144 | } |
| 225 | 145 | |
| 226 | 146 | /** |
| 227 | - * getEmailsSetups method will return email settings for a business box by box id | |
| 228 | - * @param Request $request | |
| 229 | - * @param Settings $settings | |
| 230 | - * @param $boxId | |
| 147 | + * This `getEmailsSetups` method will return email settings for a business box by box id | |
| 148 | + * @param MailBoxService $mailBoxService | |
| 149 | + * @param $id | |
| 231 | 150 | * @return array |
| 232 | 151 | */ |
| 233 | - public function getEmailsSetups(Request $request, Settings $settings, $boxId) | |
| 152 | + public function getEmailsSetups( MailBoxService $mailBoxService, $id ) | |
| 234 | 153 | { |
| 235 | - $box = MailBox::findOrFail($boxId); | |
| 236 | - | |
| 237 | - $types = $settings->getEmailSettingsKeys(); | |
| 238 | - | |
| 239 | - $req = []; | |
| 240 | - foreach ($types as $type) { | |
| 241 | - $req[] = $settings->getBoxEmailSettings($box, $type); | |
| 242 | - } | |
| 243 | - | |
| 244 | - return [ | |
| 245 | - 'email_configs' => $req, | |
| 246 | - 'email_keys' => $types | |
| 247 | - ]; | |
| 154 | + return $mailBoxService->getEmailsSetups($id); | |
| 248 | 155 | } |
| 249 | 156 | |
| 250 | 157 | /** |
| 251 | - * saveEmailSettings method will save the email settings for a business box using box id | |
| 158 | + * This `saveEmailSettings` method will save the email settings for a business box using box id | |
| 252 | 159 | * @param Request $request |
| 253 | 160 | * @param Settings $settings |
| 254 | - * @param $boxId | |
| 161 | + * @param $id | |
| 255 | 162 | * @return array |
| 256 | 163 | * @throws \FluentSupport\Framework\Validator\ValidationException |
| 257 | 164 | */ |
| 258 | - public function saveEmailSettings(Request $request, Settings $settings, $boxId) | |
| 165 | + public function saveEmailSettings( Request $request, MailBoxService $mailBoxService, $id ) | |
| 259 | 166 | { |
| 260 | - $data = wp_unslash($request->get('email_settings')); | |
| 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 | + | |
| 261 | 180 | $this->validate($data, [ |
| 262 | 181 | 'email_subject' => 'required', |
| 263 | 182 | 'email_body' => 'required' |
| 264 | 183 | ]); |
| 265 | 184 | |
| 266 | - $data['email_body'] = wp_kses_post($data['email_body']); | |
| 185 | + return $mailBoxService->saveEmailSettings( $emailType, $id, $data ); | |
| 186 | + } | |
| 267 | 187 | |
| 268 | - $box = MailBox::findOrFail($boxId); | |
| 269 | - $emailType = $request->get('email_type'); | |
| 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 | + } | |
| 270 | 198 | |
| 271 | - $settings->saveBoxEmailSettings($box, $emailType, $data); | |
| 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 | + ] : []; | |
| 272 | 217 | |
| 273 | - return [ | |
| 274 | - 'message' => __('Settings has been updated', 'fluent-support') | |
| 275 | - ]; | |
| 218 | + return $mailBoxService->getTickets( $filters, $id ); | |
| 276 | 219 | } |
| 277 | 220 | |
| 278 | - public function getTickets(Request $request, $mailbox_id) | |
| 221 | + /** | |
| 222 | + * Sanitize mailbox data array | |
| 223 | + * | |
| 224 | + * @param mixed $data | |
| 225 | + * @return array | |
| 226 | + */ | |
| 227 | + private function sanitizeMailboxData($data) | |
| 279 | 228 | { |
| 280 | - $customer_id = $request->get('filters.customer_id'); | |
| 281 | - $product_id = $request->get('filters.product_id'); | |
| 282 | - $ticket_title = $request->get('filters.ticket_title'); | |
| 283 | - | |
| 284 | - $ticketsQuery = (new Ticket())->with([ | |
| 285 | - 'customer' => function ($query) use ($customer_id) { | |
| 286 | - $query->select(['first_name', 'last_name', 'email', 'id', 'avatar']); | |
| 287 | - }, 'agent' => function ($query) { | |
| 288 | - $query->select(['first_name', 'last_name', 'id']); | |
| 289 | - }, | |
| 290 | - 'product', | |
| 291 | - 'tags', | |
| 292 | - 'preview_response' => function ($query) { | |
| 293 | - $query->orderBy('id', 'desc'); | |
| 294 | - } | |
| 295 | - ])->where('mailbox_id', $mailbox_id); | |
| 296 | - | |
| 297 | - if ($customer_id) { | |
| 298 | - $ticketsQuery->where('customer_id', $customer_id); | |
| 229 | + if (!is_array($data)) { | |
| 230 | + return []; | |
| 299 | 231 | } |
| 300 | 232 | |
| 301 | - if ($product_id) { | |
| 302 | - $ticketsQuery->where('product_id', $product_id); | |
| 303 | - } | |
| 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 | + ]; | |
| 304 | 241 | |
| 305 | - if ($ticket_title) { | |
| 306 | - $ticketsQuery->where('title', 'LIKE', "%$ticket_title%"); | |
| 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 | + } | |
| 307 | 249 | } |
| 308 | 250 | |
| 309 | - $tickets = $ticketsQuery->paginate(); | |
| 310 | - | |
| 311 | - return [ | |
| 312 | - 'tickets' => $tickets | |
| 313 | - ]; | |
| 251 | + return $sanitized; | |
| 314 | 252 | } |
| 315 | 253 | } |