| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Integrations\FluentCrm; |
| 4 |
|
| 5 |
use FluentCrm\App\Services\Funnel\BaseAction; |
| 6 |
use FluentCrm\App\Services\Funnel\FunnelHelper; |
| 7 |
use FluentSupport\App\Models\Agent; |
| 8 |
use FluentSupport\App\Models\Customer; |
| 9 |
use FluentSupport\App\Models\MailBox; |
| 10 |
use FluentSupport\App\Models\Ticket; |
| 11 |
use FluentSupport\App\Services\Helper; |
| 12 |
use FluentSupport\Framework\Support\Arr; |
| 13 |
|
| 14 |
class CreateTicketAction extends BaseAction |
| 15 |
{ |
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
$this->actionName = 'fs_create_ticket'; |
| 19 |
$this->priority = 23; |
| 20 |
parent::__construct(); |
| 21 |
|
| 22 |
add_filter('fluentcrm_ajax_options_fs_inboxes', function ($options) { |
| 23 |
$inboxes = MailBox::select(['id', 'name'])->get(); |
| 24 |
$formattedInboxes = []; |
| 25 |
foreach ($inboxes as $inbox) { |
| 26 |
$formattedInboxes[] = [ |
| 27 |
'id' => $inbox->id, |
| 28 |
'title' => $inbox->name |
| 29 |
]; |
| 30 |
} |
| 31 |
return $formattedInboxes; |
| 32 |
}); |
| 33 |
add_filter('fluentcrm_ajax_options_fs_agents', function ($options) { |
| 34 |
$agents = Agent::select(['id', 'first_name', 'last_name'])->where('status', 'active')->get(); |
| 35 |
$formattedAgents = []; |
| 36 |
foreach ($agents as $agent) { |
| 37 |
$formattedAgents[] = [ |
| 38 |
'id' => $agent->id, |
| 39 |
'title' => $agent->full_name |
| 40 |
]; |
| 41 |
} |
| 42 |
return $formattedAgents; |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
public function getBlock() |
| 47 |
{ |
| 48 |
return [ |
| 49 |
'category' => __('Fluent Support', 'fluent-support'), |
| 50 |
'title' => __('Create Support Ticket', 'fluent-support'), |
| 51 |
'ticket_content' => __('Add a support ticket for contact', 'fluent-support'), |
| 52 |
'icon' => 'fc-icon-apply_tag',//fluentCrmMix('images/funnel_icons/apply_tag.svg'), |
| 53 |
'settings' => [ |
| 54 |
'ticket_title' => '', |
| 55 |
'ticket_description' => '', |
| 56 |
'default_agent' => '', |
| 57 |
'business_inbox' => '' |
| 58 |
], |
| 59 |
'priority' => 'normal' |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
public function getBlockFields() |
| 64 |
{ |
| 65 |
$formattedPriorities = []; |
| 66 |
foreach (Helper::adminTicketPriorities() as $key => $label) { |
| 67 |
$formattedPriorities[] = [ |
| 68 |
'id' => $key, |
| 69 |
'title' => $label |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
return [ |
| 74 |
'title' => __('Create Support Ticket', 'fluent-support'), |
| 75 |
'sub_title' => __('Add a support ticket for contact', 'fluent-support'), |
| 76 |
'fields' => [ |
| 77 |
'ticket_title' => [ |
| 78 |
'type' => 'input-text-popper', |
| 79 |
'label' => __('Ticket Title (you can use any smart code)', 'fluent-support'), |
| 80 |
'placeholder' => __('Ticket Title', 'fluent-support') |
| 81 |
], |
| 82 |
'ticket_content' => [ |
| 83 |
'type' => 'html_editor', |
| 84 |
'label' => __('Ticket Description (you can use any smart code)', 'fluent-support'), |
| 85 |
'placeholder' => __('Ticket Description', 'fluent-support'), |
| 86 |
'smart_codes' => true, |
| 87 |
'context_codes' => true |
| 88 |
], |
| 89 |
'business_inbox' => [ |
| 90 |
'type' => 'rest_selector', |
| 91 |
'option_key' => 'fs_inboxes', |
| 92 |
'placeholder' => 'Business Inbox', |
| 93 |
'label' => 'Default Business Inbox' |
| 94 |
], |
| 95 |
'default_agent' => [ |
| 96 |
'type' => 'rest_selector', |
| 97 |
'option_key' => 'fs_agents', |
| 98 |
'placeholder' => 'Select Agent (optional)', |
| 99 |
'label' => 'Default Assigned Agent (optional)' |
| 100 |
], |
| 101 |
'priority' => [ |
| 102 |
'options' => $formattedPriorities, |
| 103 |
'label' => 'Priority (Admin)', |
| 104 |
'placeholder' => 'Select Priority', |
| 105 |
'type' => 'select' |
| 106 |
] |
| 107 |
] |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
public function handle($subscriber, $sequence, $funnelSubscriberId, $funnelMetric) |
| 112 |
{ |
| 113 |
if (empty($sequence->settings['ticket_title']) || empty($sequence->settings['ticket_content'])) { |
| 114 |
FunnelHelper::changeFunnelSubSequenceStatus($funnelSubscriberId, $sequence->id, 'skipped'); |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
$subscriber->funnel_subscriber_id = $funnelSubscriberId; |
| 119 |
|
| 120 |
$emailSubject = apply_filters('fluent_crm/parse_campaign_email_text', Arr::get($sequence->settings, 'ticket_title'), $subscriber); |
| 121 |
$emailBody = apply_filters('fluent_crm/parse_campaign_email_text', Arr::get($sequence->settings, 'ticket_content'), $subscriber); |
| 122 |
|
| 123 |
$ticketData = [ |
| 124 |
'title' => substr(sanitize_text_field($emailSubject), 0, 192), |
| 125 |
'content' => wp_unslash(wp_kses_post($emailBody)), |
| 126 |
'source' => 'fluent-crm' |
| 127 |
]; |
| 128 |
|
| 129 |
if (!empty($sequence->settings['business_inbox'])) { |
| 130 |
$ticketData['mailbox_id'] = (int)$sequence->settings['business_inbox']; |
| 131 |
} |
| 132 |
|
| 133 |
if (empty($ticketData['mailbox_id'])) { |
| 134 |
$defaultMailBox = Helper::getDefaultMailBox(); |
| 135 |
if ($defaultMailBox) { |
| 136 |
$ticketData['mailbox_id'] = $defaultMailBox->id; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (!empty($sequence->settings['default_agent'])) { |
| 141 |
$ticketData['agent_id'] = (int) $sequence->settings['default_agent']; |
| 142 |
} |
| 143 |
|
| 144 |
if (!empty($sequence->settings['priority'])) { |
| 145 |
$ticketData['priority'] = sanitize_text_field($sequence->settings['priority']); |
| 146 |
} |
| 147 |
|
| 148 |
$customerData = Arr::only($subscriber->toArray(), (new Customer())->getFillable()); |
| 149 |
|
| 150 |
$customerData['user_id'] = $subscriber->getWpUserId(); |
| 151 |
$customerData = array_filter($customerData); |
| 152 |
|
| 153 |
$customer = Customer::maybeCreateCustomer($customerData); |
| 154 |
|
| 155 |
$ticketData['customer_id'] = $customer->id; |
| 156 |
$ticketData = apply_filters('fluent_support/create_ticket_data', $ticketData, $customer); |
| 157 |
do_action('fluent_support/before_ticket_create', $ticketData, $customer); |
| 158 |
$ticket = Ticket::create($ticketData); |
| 159 |
do_action('fluent_support/ticket_created', $ticket, $customer); |
| 160 |
|
| 161 |
return true; |
| 162 |
} |
| 163 |
} |
| 164 |
|