| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services; |
| 4 |
|
| 5 |
use FluentSupport\App\App; |
| 6 |
use FluentSupport\App\Models\Agent; |
| 7 |
use FluentSupport\App\Models\Customer; |
| 8 |
use FluentSupport\App\Models\MailBox; |
| 9 |
use FluentSupport\App\Models\Meta; |
| 10 |
use FluentSupport\App\Models\Person; |
| 11 |
use FluentSupport\App\Services\EmailNotification\Settings; |
| 12 |
use FluentSupport\Framework\Support\Arr; |
| 13 |
|
| 14 |
/** |
| 15 |
* Helper - REST API Helper Class |
| 16 |
* |
| 17 |
* App helper for REST API |
| 18 |
* |
| 19 |
* @package FluentSupport\App\Services |
| 20 |
* |
| 21 |
* @version 1.0.0 |
| 22 |
*/ |
| 23 |
class Helper |
| 24 |
{ |
| 25 |
public static function FluentSupport($module = null) |
| 26 |
{ |
| 27 |
return App::getInstance($module); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get agent information by user id |
| 32 |
* The function will get user id as parameter or get id from session and return agent information |
| 33 |
* @param null $userId |
| 34 |
* @return false |
| 35 |
*/ |
| 36 |
public static function getAgentByUserId($userId = null) |
| 37 |
{ |
| 38 |
if ($userId === null) { |
| 39 |
$userId = get_current_user_id(); |
| 40 |
} |
| 41 |
if (!$userId) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
return Agent::where('user_id', $userId)->first(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* This function will return the list of ticket priorities list for customer |
| 50 |
* |
| 51 |
* @return mixed |
| 52 |
*/ |
| 53 |
public static function customerTicketPriorities() |
| 54 |
{ |
| 55 |
return apply_filters('fluent_support/customer_ticket_priorities', [ |
| 56 |
'normal' => __('Normal', 'fluent-support'), |
| 57 |
'medium' => __('Medium', 'fluent-support'), |
| 58 |
'critical' => __('Critical', 'fluent-support') |
| 59 |
]); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* This function will return the list of ticket priorities list for Admin |
| 64 |
* |
| 65 |
* @return mixed |
| 66 |
*/ |
| 67 |
public static function adminTicketPriorities() |
| 68 |
{ |
| 69 |
return apply_filters('fluent_support/admin_ticket_priorities', [ |
| 70 |
'normal' => __('Normal', 'fluent-support'), |
| 71 |
'medium' => __('Medium', 'fluent-support'), |
| 72 |
'critical' => __('Critical', 'fluent-support') |
| 73 |
]); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* This function will return ticket status group |
| 79 |
* |
| 80 |
* @return mixed |
| 81 |
*/ |
| 82 |
public static function ticketStatusGroups() |
| 83 |
{ |
| 84 |
return apply_filters('fluent_support/ticket_status_groups', [ |
| 85 |
'open' => ['new', 'active'], |
| 86 |
'active' => ['active'], |
| 87 |
'closed' => ['closed'], |
| 88 |
'new' => ['new'], |
| 89 |
'all' => [] |
| 90 |
]); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* This function will return ticket status list |
| 95 |
* |
| 96 |
* @return mixed |
| 97 |
*/ |
| 98 |
public static function ticketStatuses() |
| 99 |
{ |
| 100 |
return apply_filters('fluent_support/ticket_statuses', [ |
| 101 |
'new' => __('New', 'fluent-support'), |
| 102 |
'active' => __('Active', 'fluent-support'), |
| 103 |
'closed' => __('Closed', 'fluent-support'), |
| 104 |
]); |
| 105 |
} |
| 106 |
|
| 107 |
public static function getTkStatusesByGroupName($groupName) |
| 108 |
{ |
| 109 |
$groups = self::ticketStatusGroups(); |
| 110 |
return Arr::get($groups, $groupName, []); |
| 111 |
} |
| 112 |
|
| 113 |
public static function ticketAcceptedFileMiles() |
| 114 |
{ |
| 115 |
$groups = self::getMimeGroups(); |
| 116 |
$globalSettings = (new Settings())->globalBusinessSettings(); |
| 117 |
|
| 118 |
if (empty($globalSettings['accepted_file_types'])) { |
| 119 |
return apply_filters('fluent_support/accepted_ticket_mimes', []); |
| 120 |
} |
| 121 |
|
| 122 |
$mimes = []; |
| 123 |
$typesGroups = Arr::only($groups, $globalSettings['accepted_file_types']); |
| 124 |
foreach ($typesGroups as $mimesGroup) { |
| 125 |
$mimes = array_merge($mimes, $mimesGroup['mimes']); |
| 126 |
} |
| 127 |
|
| 128 |
return apply_filters('fluent_support/accepted_ticket_mimes', $mimes); |
| 129 |
} |
| 130 |
|
| 131 |
public static function getAcceptedMimeHeadings() |
| 132 |
{ |
| 133 |
$groups = self::getMimeGroups(); |
| 134 |
$globalSettings = (new Settings())->globalBusinessSettings(); |
| 135 |
|
| 136 |
if (empty($globalSettings['accepted_file_types'])) { |
| 137 |
return []; |
| 138 |
} |
| 139 |
|
| 140 |
$mimeNames = []; |
| 141 |
$typesGroups = Arr::only($groups, $globalSettings['accepted_file_types']); |
| 142 |
foreach ($typesGroups as $mimesGroup) { |
| 143 |
$mimeNames[] = $mimesGroup['title']; |
| 144 |
} |
| 145 |
|
| 146 |
return $mimeNames; |
| 147 |
} |
| 148 |
|
| 149 |
public static function getFileUploadMessage() |
| 150 |
{ |
| 151 |
$mimeHeadings = self::getAcceptedMimeHeadings(); |
| 152 |
$settings = (new Settings())->globalBusinessSettings(); |
| 153 |
$maxFileSize = absint($settings['max_file_size']); |
| 154 |
|
| 155 |
return sprintf(__('Supported Types: %s and max file size: %dMB', 'fluent-support'), implode(', ', $mimeHeadings), $maxFileSize); |
| 156 |
} |
| 157 |
|
| 158 |
public static function getMimeGroups() |
| 159 |
{ |
| 160 |
return [ |
| 161 |
'images' => [ |
| 162 |
'title' => __('Photos', 'fluent-support'), |
| 163 |
'mimes' => [ |
| 164 |
'image/gif', |
| 165 |
'image/ief', |
| 166 |
'image/jpeg', |
| 167 |
'image/webp', |
| 168 |
'image/pjpeg', |
| 169 |
'image/ktx', |
| 170 |
'image/png' |
| 171 |
] |
| 172 |
], |
| 173 |
'csv' => [ |
| 174 |
'title' => __('CSV', 'fluent-support'), |
| 175 |
'mimes' => [ |
| 176 |
'application/csv', |
| 177 |
'application/txt', |
| 178 |
'text/csv', |
| 179 |
'text/plain', |
| 180 |
'text/comma-separated-values', |
| 181 |
'text/anytext', |
| 182 |
] |
| 183 |
], |
| 184 |
'documents' => [ |
| 185 |
'title' => __('PDF/Docs', 'fluent-support'), |
| 186 |
'mimes' => [ |
| 187 |
'application/excel', |
| 188 |
'application/vnd.ms-excel', |
| 189 |
'application/vnd.msexcel', |
| 190 |
'application/octet-stream', |
| 191 |
'application/pdf', |
| 192 |
'application/msword', |
| 193 |
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' |
| 194 |
] |
| 195 |
], |
| 196 |
'zip' => [ |
| 197 |
'title' => __('Zip', 'fluent-support'), |
| 198 |
'mimes' => [ |
| 199 |
'application/zip' |
| 200 |
] |
| 201 |
], |
| 202 |
'json' => [ |
| 203 |
'title' => __('JSON', 'fluent-support'), |
| 204 |
'mimes' => [ |
| 205 |
'application/json', |
| 206 |
'application/jsonml+json' |
| 207 |
] |
| 208 |
] |
| 209 |
]; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* getOption method will return settings using key |
| 214 |
* This method will get key as parameter, fetch data from database, beautify the data and return |
| 215 |
* @param $key |
| 216 |
* @param string $default |
| 217 |
* @return mixed|string |
| 218 |
*/ |
| 219 |
public static function getOption($key, $default = '') |
| 220 |
{ |
| 221 |
//Get settings from meta table using the key |
| 222 |
$data = Meta::where('object_type', 'option') |
| 223 |
->where('key', $key) |
| 224 |
->first(); |
| 225 |
|
| 226 |
if ($data) { |
| 227 |
$value = maybe_unserialize($data->value); |
| 228 |
if ($value) { |
| 229 |
return $value; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return $default; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* updateOption method will update or insert settings |
| 238 |
* This method will get key and value as parameter, check exists or not. If exist update value by key, else insert value for the key |
| 239 |
* @param $key |
| 240 |
* @param $value |
| 241 |
* @return mixed |
| 242 |
*/ |
| 243 |
public static function updateOption($key, $value) |
| 244 |
{ |
| 245 |
//Get settings from meta table using the key |
| 246 |
$data = Meta::where('object_type', 'option') |
| 247 |
->where('key', $key) |
| 248 |
->first(); |
| 249 |
|
| 250 |
//If data is available, update existing data and return |
| 251 |
if ($data) { |
| 252 |
return Meta::where('id', $data->id) |
| 253 |
->update([ |
| 254 |
'value' => maybe_serialize($value) |
| 255 |
]); |
| 256 |
} |
| 257 |
|
| 258 |
//If newly submit, create new record and return |
| 259 |
return Meta::insert([ |
| 260 |
'object_type' => 'option', |
| 261 |
'key' => $key, |
| 262 |
'value' => maybe_serialize($value) |
| 263 |
]); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* getIntegrationOption method will return the integration settings by integration key |
| 268 |
* @param $key |
| 269 |
* @param string $default |
| 270 |
* @return mixed|string |
| 271 |
*/ |
| 272 |
public static function getIntegrationOption($key, $default = '') |
| 273 |
{ |
| 274 |
$data = Meta::where('object_type', 'integration_settings') |
| 275 |
->where('key', $key) |
| 276 |
->first(); |
| 277 |
|
| 278 |
if ($data) { |
| 279 |
$value = maybe_unserialize($data->value); |
| 280 |
if ($value) { |
| 281 |
return $value; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return $default; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* updateIntegrationOption method will update existing settings or create new settings by integration key |
| 290 |
* @param $key |
| 291 |
* @param $value |
| 292 |
* @return mixed |
| 293 |
*/ |
| 294 |
public static function updateIntegrationOption($key, $value) |
| 295 |
{ |
| 296 |
$data = Meta::where('object_type', 'integration_settings') |
| 297 |
->where('key', $key) |
| 298 |
->first(); |
| 299 |
|
| 300 |
if ($data) { |
| 301 |
return Meta::where('id', $data->id) |
| 302 |
->update([ |
| 303 |
'value' => maybe_serialize($value) |
| 304 |
]); |
| 305 |
} |
| 306 |
|
| 307 |
return Meta::insert([ |
| 308 |
'object_type' => 'integration_settings', |
| 309 |
'key' => $key, |
| 310 |
'value' => maybe_serialize($value) |
| 311 |
]); |
| 312 |
} |
| 313 |
|
| 314 |
public static function getTicketViewUrl($ticket) |
| 315 |
{ |
| 316 |
$baseUrl = self::getPortalBaseUrl(); |
| 317 |
|
| 318 |
return $baseUrl . '/#/ticket/' . $ticket->id . '/view'; |
| 319 |
} |
| 320 |
|
| 321 |
public static function getTicketViewSignedUrl($ticket) |
| 322 |
{ |
| 323 |
if (!self::isPublicSignedTicketEnabled()) { |
| 324 |
return self::getTicketViewUrl($ticket); |
| 325 |
} |
| 326 |
|
| 327 |
$baseUrl = self::getPortalBaseUrl(); |
| 328 |
|
| 329 |
$baseUrl = add_query_arg([ |
| 330 |
'fs_view' => 'ticket', |
| 331 |
'support_hash' => $ticket->hash, |
| 332 |
'ticket_id' => $ticket->id |
| 333 |
], $baseUrl); |
| 334 |
|
| 335 |
return $baseUrl . '#/ticket/' . $ticket->id . '/view'; |
| 336 |
} |
| 337 |
|
| 338 |
public static function isPublicSignedTicketEnabled() |
| 339 |
{ |
| 340 |
$businessSettings = self::getBusinessSettings(); |
| 341 |
|
| 342 |
return (Arr::get($businessSettings, 'disable_public_ticket') != 'yes'); |
| 343 |
} |
| 344 |
|
| 345 |
public static function getTicketAdminUrl($ticket) |
| 346 |
{ |
| 347 |
$baseUrl = self::getPortalAdminBaseUrl(); |
| 348 |
return $baseUrl . 'tickets/' . $ticket->id . '/view'; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* getPortalBaseUrl will get the portal page id and return link of the page |
| 353 |
* @return mixed |
| 354 |
*/ |
| 355 |
public static function getPortalBaseUrl() |
| 356 |
{ |
| 357 |
$businessSettings = self::getBusinessSettings(); |
| 358 |
$baseUrl = get_permalink($businessSettings['portal_page_id']); |
| 359 |
$baseUrl = rtrim($baseUrl, '/\\'); |
| 360 |
return apply_filters('fluent_support/portal_base_url', $baseUrl); |
| 361 |
} |
| 362 |
|
| 363 |
public static function getPortalAdminBaseUrl() |
| 364 |
{ |
| 365 |
return apply_filters('fluent_support/portal_admin_base_url', admin_url('admin.php?page=fluent-support/#/')); |
| 366 |
} |
| 367 |
|
| 368 |
public static function getBusinessSettings() |
| 369 |
{ |
| 370 |
static $settings; |
| 371 |
|
| 372 |
if ($settings) { |
| 373 |
return $settings; |
| 374 |
} |
| 375 |
|
| 376 |
$settings = (new Settings())->globalBusinessSettings(); |
| 377 |
|
| 378 |
return $settings; |
| 379 |
} |
| 380 |
|
| 381 |
public static function getTicketMeta($ticketId, $key, $default = '') |
| 382 |
{ |
| 383 |
$data = Meta::where('object_type', 'ticket_meta') |
| 384 |
->where('key', $key) |
| 385 |
->where('object_id', $ticketId) |
| 386 |
->first(); |
| 387 |
|
| 388 |
if ($data) { |
| 389 |
$value = maybe_unserialize($data->value); |
| 390 |
if ($value) { |
| 391 |
return $value; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
return $default; |
| 396 |
} |
| 397 |
|
| 398 |
public static function updateTicketMeta($ticketId, $key, $value) |
| 399 |
{ |
| 400 |
$data = Meta::where('object_type', 'ticket_meta') |
| 401 |
->where('key', $key) |
| 402 |
->where('object_id', $ticketId) |
| 403 |
->first(); |
| 404 |
|
| 405 |
if ($data) { |
| 406 |
return Meta::where('id', $data->id) |
| 407 |
->update([ |
| 408 |
'value' => maybe_serialize($value) |
| 409 |
]); |
| 410 |
} |
| 411 |
|
| 412 |
return Meta::insert([ |
| 413 |
'object_type' => 'ticket_meta', |
| 414 |
'object_id' => $ticketId, |
| 415 |
'key' => $key, |
| 416 |
'value' => maybe_serialize($value) |
| 417 |
]); |
| 418 |
} |
| 419 |
|
| 420 |
public static function getWPPages() |
| 421 |
{ |
| 422 |
$pages = (self::FluentSupport())->app->db |
| 423 |
->table('posts') |
| 424 |
->select(['ID', 'post_title']) |
| 425 |
->where('post_type', 'page') |
| 426 |
->where('post_status', 'publish') |
| 427 |
->orderBy('ID', 'DESC') |
| 428 |
->get(); |
| 429 |
$formattedPages = []; |
| 430 |
foreach ($pages as $page) { |
| 431 |
$formattedPages[] = [ |
| 432 |
'id' => strval($page->ID), |
| 433 |
'title' => $page->post_title |
| 434 |
]; |
| 435 |
} |
| 436 |
return $formattedPages; |
| 437 |
} |
| 438 |
|
| 439 |
public static function getDefaultMailBox() |
| 440 |
{ |
| 441 |
$mailbox = MailBox::where('is_default', 'yes')->first(); |
| 442 |
|
| 443 |
if ($mailbox) { |
| 444 |
return $mailbox; |
| 445 |
} |
| 446 |
|
| 447 |
return MailBox::orderBy('id', 'ASC')->first(); |
| 448 |
} |
| 449 |
|
| 450 |
public static function getCurrentAgent() |
| 451 |
{ |
| 452 |
return Agent::where('user_id', get_current_user_id())->first(); |
| 453 |
} |
| 454 |
|
| 455 |
public static function getCurrentCustomer() |
| 456 |
{ |
| 457 |
return Customer::where('user_id', get_current_user_id())->first(); |
| 458 |
} |
| 459 |
|
| 460 |
public static function getCurrentPerson() |
| 461 |
{ |
| 462 |
return Person::where('user_id', get_current_user_id())->first(); |
| 463 |
} |
| 464 |
|
| 465 |
public static function getFluentCRMTagConfig() |
| 466 |
{ |
| 467 |
if (!defined('FLUENTCRM')) { |
| 468 |
return [ |
| 469 |
'can_add_tags' => false, |
| 470 |
'tags' => [], |
| 471 |
'logo' => FLUENT_SUPPORT_PLUGIN_URL . 'assets/images/fluentcrm-logo.svg', |
| 472 |
'icon' => FLUENT_SUPPORT_PLUGIN_URL . 'assets/images/fluent-crm-icon.png', |
| 473 |
]; |
| 474 |
} |
| 475 |
|
| 476 |
$canAddTags = \FluentCrm\App\Services\PermissionManager::currentUserCan('fcrm_manage_contacts'); |
| 477 |
|
| 478 |
$canAddTags = apply_filters('fluent_support/can_user_add_tags_to_customer', $canAddTags); |
| 479 |
$crmTags = []; |
| 480 |
if ($canAddTags) { |
| 481 |
$crmTags = \FluentCrm\App\Models\Tag::select(['id', 'title'])->orderBy('title', 'ASC')->get(); |
| 482 |
} |
| 483 |
|
| 484 |
return [ |
| 485 |
'can_add_tags' => $canAddTags, |
| 486 |
'tags' => $crmTags, |
| 487 |
'logo' => FLUENT_SUPPORT_PLUGIN_URL . 'assets/images/fluentcrm-logo.svg', |
| 488 |
'icon' => FLUENT_SUPPORT_PLUGIN_URL . 'assets/images/fluent-crm-icon.png', |
| 489 |
]; |
| 490 |
|
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* getFluentCrmContactData method will get information from fluent crm using user email |
| 495 |
* @param $customer |
| 496 |
* @return array|false |
| 497 |
*/ |
| 498 |
public static function getFluentCrmContactData($customer) |
| 499 |
{ |
| 500 |
if (!defined('FLUENTCRM')) { |
| 501 |
return false; |
| 502 |
} |
| 503 |
//Get contact info from FluentCRM using customer email |
| 504 |
$contact = \FluentCrmApi('contacts')->getContactByUserRef($customer->email); |
| 505 |
if ($contact) { |
| 506 |
$tags = $contact->tags; |
| 507 |
$urlBase = apply_filters('fluentcrm_menu_url_base', admin_url('admin.php?page=fluentcrm-admin#/')); |
| 508 |
$crmProfileUrl = $urlBase . 'subscribers/' . $contact->id; |
| 509 |
|
| 510 |
//Return contact data |
| 511 |
return [ |
| 512 |
'id' => $contact->id, |
| 513 |
'first_name' => $contact->first_name, |
| 514 |
'last_name' => $contact->last_name, |
| 515 |
'full_name' => $contact->full_name, |
| 516 |
'name_mismatch' => $contact->full_name != $customer->full_name, |
| 517 |
'tags' => $tags, |
| 518 |
'status' => $contact->status, |
| 519 |
'stats' => $contact->stats(), |
| 520 |
'view_url' => $crmProfileUrl |
| 521 |
]; |
| 522 |
} |
| 523 |
|
| 524 |
return false; |
| 525 |
} |
| 526 |
} |
| 527 |
|