| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Tickets; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Attachment; |
| 6 |
use FluentSupport\App\Models\Conversation; |
| 7 |
use FluentSupport\App\Services\Helper; |
| 8 |
use FluentSupport\Framework\Support\Arr; |
| 9 |
|
| 10 |
class ResponseService |
| 11 |
{ |
| 12 |
/** |
| 13 |
* createResponse method is responsible for create responses to ticket by agent or customer |
| 14 |
* @param $data |
| 15 |
* @param $person |
| 16 |
* @param $ticket |
| 17 |
* @return array|false |
| 18 |
*/ |
| 19 |
public function createResponse($data, $person, $ticket) |
| 20 |
{ |
| 21 |
if (empty($data['content'])) { |
| 22 |
return false; |
| 23 |
} |
| 24 |
|
| 25 |
$convoType = Arr::get($data, 'conversation_type', 'response'); |
| 26 |
|
| 27 |
$content = wp_unslash(wp_kses_post($data['content'])); |
| 28 |
|
| 29 |
$response = [ |
| 30 |
'person_id' => $person->id, |
| 31 |
'ticket_id' => $ticket->id, |
| 32 |
'conversation_type' => $convoType, |
| 33 |
'content' => $content, |
| 34 |
'source' => Arr::get($data, 'source', 'web'), |
| 35 |
'content_hash' => md5($content) |
| 36 |
]; |
| 37 |
|
| 38 |
if(!empty($data['message_id'])) { |
| 39 |
$response['message_id'] = sanitize_text_field($data['message_id']); |
| 40 |
} |
| 41 |
|
| 42 |
$response = apply_filters('fluent_support/new_' . $person->person_type . '_' . $convoType, $response, $ticket, $person); |
| 43 |
|
| 44 |
$createdResponse = \FluentSupport\App\Models\Conversation::create($response); |
| 45 |
$createdResponse->load('person'); |
| 46 |
|
| 47 |
if ($attachments = Arr::get($data, 'attachments', [])) { |
| 48 |
Attachment::where('ticket_id', $ticket->id) |
| 49 |
->whereIn('file_hash', $attachments) |
| 50 |
->update([ |
| 51 |
'conversation_id' => $createdResponse->id, |
| 52 |
'status' => 'active' |
| 53 |
]); |
| 54 |
$createdResponse->load('attachments'); |
| 55 |
} |
| 56 |
|
| 57 |
if ($person->person_type == 'agent' && $ticket->status == 'new' && $convoType == 'response') { |
| 58 |
$ticket->status = 'active'; |
| 59 |
if ($ticket->created_at) { |
| 60 |
$ticket->first_response_time = strtotime(current_time('mysql')) - strtotime($ticket->created_at); |
| 61 |
} else { |
| 62 |
$ticket->first_response_time = 300; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$agentAdded = false; |
| 67 |
$updateData = []; |
| 68 |
|
| 69 |
if ($person->person_type == 'agent') { |
| 70 |
if (!$ticket->agent_id && $convoType == 'response') { |
| 71 |
$ticket->agent_id = $person->id; |
| 72 |
$agentAdded = true; |
| 73 |
$updateData = [ |
| 74 |
'agent_id' => $person->id |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
if ($convoType == 'response') { |
| 79 |
$ticket->last_agent_response = current_time('mysql'); |
| 80 |
$ticket->waiting_since = current_time('mysql'); |
| 81 |
} |
| 82 |
} else { |
| 83 |
|
| 84 |
if ($ticket->last_agent_response && strtotime($ticket->last_agent_response) > strtotime($ticket->last_customer_response)) { |
| 85 |
$ticket->waiting_since = current_time('mysql'); |
| 86 |
} |
| 87 |
|
| 88 |
$ticket->last_customer_response = current_time('mysql'); |
| 89 |
} |
| 90 |
|
| 91 |
$ticket->response_count += 1; |
| 92 |
|
| 93 |
$closed = false; |
| 94 |
if (Arr::get($data, 'close_ticket') == 'yes' && $ticket->status != 'closed') { |
| 95 |
$ticket->status = 'closed'; |
| 96 |
$ticket->resolved_at = current_time('mysql'); |
| 97 |
$ticket->closed_by = $person->id; |
| 98 |
$ticket->total_close_time = current_time('timestamp') - strtotime($ticket->created_at); |
| 99 |
$closed = true; |
| 100 |
Conversation::create([ |
| 101 |
'ticket_id' => $ticket->id, |
| 102 |
'person_id' => $person->id, |
| 103 |
'conversation_type' => 'internal_info', |
| 104 |
'content' => __('Ticket has been closed', 'fluent-support') |
| 105 |
]); |
| 106 |
} |
| 107 |
|
| 108 |
$ticket->save(); |
| 109 |
|
| 110 |
if ($agentAdded) { |
| 111 |
$assigner = Helper::getCurrentAgent(); |
| 112 |
$ticket->load('agent'); |
| 113 |
|
| 114 |
/* |
| 115 |
* Action on ticket assign to an agent |
| 116 |
* |
| 117 |
* @since v1.0.0 |
| 118 |
* @param object $person |
| 119 |
* @param object $ticket |
| 120 |
* @param object $assigner |
| 121 |
*/ |
| 122 |
do_action('fluent_support/agent_assigned_to_ticket', $person, $ticket, $assigner); |
| 123 |
$updateData['agent'] = $ticket->agent; |
| 124 |
} |
| 125 |
|
| 126 |
/* |
| 127 |
* Action on conversation |
| 128 |
* |
| 129 |
* @since v1.0.0 |
| 130 |
* @param string $convoType |
| 131 |
* @param string $personType |
| 132 |
* @param object $createdResponse |
| 133 |
* @param object $ticket |
| 134 |
* @param object $person |
| 135 |
*/ |
| 136 |
do_action('fluent_support/' . $convoType . '_added_by_' . $person->person_type, $createdResponse, $ticket, $person); |
| 137 |
|
| 138 |
|
| 139 |
if ($closed) { |
| 140 |
/* |
| 141 |
* Action on ticket close |
| 142 |
* |
| 143 |
* @since v1.0.0 |
| 144 |
* @param object $ticket |
| 145 |
* @param object $person |
| 146 |
*/ |
| 147 |
do_action('fluent_support/ticket_closed', $ticket, $person); |
| 148 |
|
| 149 |
/* |
| 150 |
* Action on ticket close |
| 151 |
* |
| 152 |
* @since v1.0.0 |
| 153 |
* @param string $personType |
| 154 |
* @param object $ticket |
| 155 |
* @param object $person |
| 156 |
*/ |
| 157 |
do_action('fluent_support/ticket_closed_by_' . $person->person_type, $ticket, $person); |
| 158 |
} |
| 159 |
|
| 160 |
return [ |
| 161 |
'response' => $createdResponse, |
| 162 |
'ticket' => $ticket, |
| 163 |
'update_data' => $updateData |
| 164 |
]; |
| 165 |
} |
| 166 |
} |
| 167 |
|