| 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\App\Services\TicketHelper; |
| 9 |
use FluentSupport\App\Services\TicketQueryService; |
| 10 |
use FluentSupport\Framework\Request\Request; |
| 11 |
|
| 12 |
class MailBoxController extends Controller |
| 13 |
{ |
| 14 |
/** |
| 15 |
* index method will return the list of business inbox |
| 16 |
* @param Request $request |
| 17 |
* @return array |
| 18 |
*/ |
| 19 |
public function index(Request $request) |
| 20 |
{ |
| 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 |
]; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* get method will fetch and return information related to business box |
| 34 |
* @param Request $request |
| 35 |
* @param $id |
| 36 |
* @return mixed |
| 37 |
*/ |
| 38 |
public function get(Request $request, $id) |
| 39 |
{ |
| 40 |
$mailbox = MailBox::findOrFail($id); |
| 41 |
|
| 42 |
return $this->sendSuccess([ |
| 43 |
'mailbox' => $mailbox |
| 44 |
]); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* Save method will create new business box |
| 50 |
* @param Request $request |
| 51 |
* @return array |
| 52 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 53 |
*/ |
| 54 |
public function save(Request $request) |
| 55 |
{ |
| 56 |
$data = $request->get('business'); |
| 57 |
$data = wp_unslash($data); |
| 58 |
|
| 59 |
$this->validate($data, [ |
| 60 |
'name' => 'required', |
| 61 |
'email' => 'required' |
| 62 |
]); |
| 63 |
|
| 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 |
return [ |
| 81 |
'message' => __('Mailbox has been created successfully', 'fluent-support'), |
| 82 |
'mailbox' => $mailbox |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Update method will update existing information for a business by mailbox id |
| 88 |
* @param Request $request |
| 89 |
* @param $mailBoxId |
| 90 |
* @return array |
| 91 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 92 |
*/ |
| 93 |
public function update(Request $request, $mailBoxId) |
| 94 |
{ |
| 95 |
$data = $request->get('business'); |
| 96 |
$data = wp_unslash($data); |
| 97 |
|
| 98 |
$this->validate($data, [ |
| 99 |
'name' => 'required', |
| 100 |
'email' => 'required' |
| 101 |
]); |
| 102 |
|
| 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 |
]); |
| 109 |
} |
| 110 |
|
| 111 |
$mailbox->fill($data); |
| 112 |
$mailbox->save(); |
| 113 |
|
| 114 |
return [ |
| 115 |
'message' => __('Mailbox has been saved', 'fluent-support'), |
| 116 |
'mailbox' => $mailbox |
| 117 |
]; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* delete method will delete a business from mailbox and replaced with alternative |
| 122 |
* @param Request $request |
| 123 |
* @param $mailBoxId |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
public function delete(Request $request, $mailBoxId) |
| 127 |
{ |
| 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 |
]); |
| 134 |
} |
| 135 |
|
| 136 |
$box = MailBox::findOrFail($mailBoxId); |
| 137 |
$fallbackBox = MailBox::findOrFail($fallbackId); |
| 138 |
|
| 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) |
| 172 |
{ |
| 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 |
]); |
| 181 |
} |
| 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 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* getEmailSettings method will get and return the mailbox email settings |
| 211 |
* @param Request $request |
| 212 |
* @param Settings $settings |
| 213 |
* @param $boxId |
| 214 |
* @return array |
| 215 |
*/ |
| 216 |
public function getEmailSettings(Request $request, Settings $settings, $boxId) |
| 217 |
{ |
| 218 |
$box = MailBox::findOrFail($boxId); |
| 219 |
$emailType = $request->get('email_type'); |
| 220 |
|
| 221 |
return [ |
| 222 |
'email_settings' => $settings->getBoxEmailSettings($box, $emailType) |
| 223 |
]; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 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 |
| 231 |
* @return array |
| 232 |
*/ |
| 233 |
public function getEmailsSetups(Request $request, Settings $settings, $boxId) |
| 234 |
{ |
| 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 |
]; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* saveEmailSettings method will save the email settings for a business box using box id |
| 252 |
* @param Request $request |
| 253 |
* @param Settings $settings |
| 254 |
* @param $boxId |
| 255 |
* @return array |
| 256 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 257 |
*/ |
| 258 |
public function saveEmailSettings(Request $request, Settings $settings, $boxId) |
| 259 |
{ |
| 260 |
$data = wp_unslash($request->get('email_settings')); |
| 261 |
$this->validate($data, [ |
| 262 |
'email_subject' => 'required', |
| 263 |
'email_body' => 'required' |
| 264 |
]); |
| 265 |
|
| 266 |
$data['email_body'] = wp_kses_post($data['email_body']); |
| 267 |
|
| 268 |
$box = MailBox::findOrFail($boxId); |
| 269 |
$emailType = $request->get('email_type'); |
| 270 |
|
| 271 |
$settings->saveBoxEmailSettings($box, $emailType, $data); |
| 272 |
|
| 273 |
return [ |
| 274 |
'message' => __('Settings has been updated', 'fluent-support') |
| 275 |
]; |
| 276 |
} |
| 277 |
|
| 278 |
public function getTickets(Request $request, $mailbox_id) |
| 279 |
{ |
| 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); |
| 299 |
} |
| 300 |
|
| 301 |
if ($product_id) { |
| 302 |
$ticketsQuery->where('product_id', $product_id); |
| 303 |
} |
| 304 |
|
| 305 |
if ($ticket_title) { |
| 306 |
$ticketsQuery->where('title', 'LIKE', "%$ticket_title%"); |
| 307 |
} |
| 308 |
|
| 309 |
$tickets = $ticketsQuery->paginate(); |
| 310 |
|
| 311 |
return [ |
| 312 |
'tickets' => $tickets |
| 313 |
]; |
| 314 |
} |
| 315 |
} |
| 316 |
|