| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\MailBox; |
| 6 |
use FluentSupport\App\Models\Ticket; |
| 7 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 8 |
use FluentSupport\Framework\Request\Request; |
| 9 |
|
| 10 |
class MailBoxController extends Controller |
| 11 |
{ |
| 12 |
public function index(Request $request) |
| 13 |
{ |
| 14 |
$mailboxes = MailBox::all(); |
| 15 |
|
| 16 |
foreach ($mailboxes as $mailbox) { |
| 17 |
$mailbox->tickets_count = Ticket::where('mailbox_id', $mailbox->id)->count(); |
| 18 |
} |
| 19 |
|
| 20 |
return [ |
| 21 |
'mailboxes' => $mailboxes |
| 22 |
]; |
| 23 |
} |
| 24 |
|
| 25 |
public function get(Request $request, $id) |
| 26 |
{ |
| 27 |
$mailbox = MailBox::findOrFail($id); |
| 28 |
|
| 29 |
return $this->sendSuccess([ |
| 30 |
'mailbox' => $mailbox |
| 31 |
]); |
| 32 |
} |
| 33 |
|
| 34 |
public function save(Request $request) |
| 35 |
{ |
| 36 |
$data = $request->get('business'); |
| 37 |
$data = wp_unslash($data); |
| 38 |
|
| 39 |
$this->validate($data, [ |
| 40 |
'name' => 'required', |
| 41 |
'email' => 'required' |
| 42 |
]); |
| 43 |
|
| 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 |
return [ |
| 61 |
'message' => __('Mailbox has been created successfully', 'fluent-support'), |
| 62 |
'mailbox' => $mailbox |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public function update(Request $request, $mailBoxId) |
| 67 |
{ |
| 68 |
$data = $request->get('business'); |
| 69 |
$data = wp_unslash($data); |
| 70 |
|
| 71 |
$this->validate($data, [ |
| 72 |
'name' => 'required', |
| 73 |
'email' => 'required' |
| 74 |
]); |
| 75 |
|
| 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 |
]); |
| 82 |
} |
| 83 |
|
| 84 |
$mailbox->fill($data); |
| 85 |
$mailbox->save(); |
| 86 |
|
| 87 |
return [ |
| 88 |
'message' => __('Mailbox has been saved', 'fluent-support'), |
| 89 |
'mailbox' => $mailbox |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
public function delete(Request $request, $mailBoxId) |
| 94 |
{ |
| 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 |
} |
| 102 |
|
| 103 |
$box = MailBox::findOrFail($mailBoxId); |
| 104 |
$fallbackBox = MailBox::findOrFail($fallbackId); |
| 105 |
|
| 106 |
|
| 107 |
do_action('fluent_support/before_delete_email_box', $box, $fallbackBox); |
| 108 |
$box->deleteAllMeta(); |
| 109 |
MailBox::where('id', $mailBoxId)->delete(); |
| 110 |
|
| 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 |
return [ |
| 120 |
'message' => __('Selected Business has been deleted', 'fluent-support') |
| 121 |
]; |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
public function getEmailSettings(Request $request, Settings $settings, $boxId) |
| 126 |
{ |
| 127 |
$box = MailBox::findOrFail($boxId); |
| 128 |
$emailType = $request->get('email_type'); |
| 129 |
|
| 130 |
return [ |
| 131 |
'email_settings' => $settings->getBoxEmailSettings($box, $emailType) |
| 132 |
]; |
| 133 |
} |
| 134 |
|
| 135 |
public function getEmailsSetups(Request $request, Settings $settings, $boxId) |
| 136 |
{ |
| 137 |
$box = MailBox::findOrFail($boxId); |
| 138 |
|
| 139 |
$types = $settings->getEmailSettingsKeys(); |
| 140 |
|
| 141 |
$req = []; |
| 142 |
foreach($types as $type) { |
| 143 |
$req[] = $settings->getBoxEmailSettings($box, $type); |
| 144 |
} |
| 145 |
|
| 146 |
return [ |
| 147 |
'email_configs' => $req, |
| 148 |
'email_keys' => $types |
| 149 |
]; |
| 150 |
} |
| 151 |
|
| 152 |
public function saveEmailSettings(Request $request, Settings $settings, $boxId) |
| 153 |
{ |
| 154 |
$data = wp_unslash($request->get('email_settings')); |
| 155 |
$this->validate($data, [ |
| 156 |
'email_subject' => 'required', |
| 157 |
'email_body' => 'required' |
| 158 |
]); |
| 159 |
|
| 160 |
$data['email_body'] = wp_kses_post($data['email_body']); |
| 161 |
|
| 162 |
$box = MailBox::findOrFail($boxId); |
| 163 |
$emailType = $request->get('email_type'); |
| 164 |
|
| 165 |
$settings->saveBoxEmailSettings($box, $emailType, $data); |
| 166 |
|
| 167 |
return [ |
| 168 |
'message' => __('Settings has been updated', 'fluent-support') |
| 169 |
]; |
| 170 |
} |
| 171 |
} |
| 172 |
|