| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Activity; |
| 6 |
use FluentSupport\App\Services\Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Writes ticket lifecycle events to the activity log. |
| 10 |
*/ |
| 11 |
|
| 12 |
class ActivityLogger |
| 13 |
{ |
| 14 |
|
| 15 |
public function init() |
| 16 |
{ |
| 17 |
if (Helper::areLogsDisabled('_activity_settings')) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
add_action('fluent_support/ticket_created', function ($ticket, $customer) { |
| 22 |
|
| 23 |
$customer->last_response_at = current_time('mysql'); |
| 24 |
$customer->save(); |
| 25 |
|
| 26 |
$agent = Helper::getAgentByUserId(); |
| 27 |
|
| 28 |
if ($agent) { |
| 29 |
$description = sprintf( |
| 30 |
'%1$s created a %2$s on behalf of %3$s', |
| 31 |
$this->getPersonMarkup($agent), |
| 32 |
$this->getTicketMarkup($ticket), |
| 33 |
$this->getPersonMarkup($customer) |
| 34 |
); |
| 35 |
$personId = $agent->id; |
| 36 |
$personType = $agent->person_type; |
| 37 |
} else { |
| 38 |
$description = sprintf('%1$s created a %2$s via %3$s', $this->getPersonMarkup($customer), $this->getTicketMarkup($ticket), $ticket->source); |
| 39 |
$personId = $customer->id; |
| 40 |
$personType = $customer->person_type; |
| 41 |
} |
| 42 |
|
| 43 |
$log = [ |
| 44 |
'event_type' => 'fluent_support/ticket_created', |
| 45 |
'person_id' => $personId, |
| 46 |
'person_type' => $personType, |
| 47 |
'object_id' => $ticket->id, |
| 48 |
'object_type' => 'ticket', |
| 49 |
'description' => $description |
| 50 |
]; |
| 51 |
|
| 52 |
Activity::create($log); |
| 53 |
}, 20, 2); |
| 54 |
|
| 55 |
add_action('fluent_support/response_added_by_customer', function ($response, $ticket, $person) { |
| 56 |
|
| 57 |
$person->last_response_at = current_time('mysql'); |
| 58 |
$person->save(); |
| 59 |
|
| 60 |
$description = sprintf('Customer %1$s added a response on %2$s via %3$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket), $response->source); |
| 61 |
|
| 62 |
$log = [ |
| 63 |
'event_type' => 'fluent_support/response_added_by_customer', |
| 64 |
'person_id' => $person->id, |
| 65 |
'person_type' => $person->person_type, |
| 66 |
'object_id' => $ticket->id, |
| 67 |
'object_type' => 'ticket', |
| 68 |
'description' => $description |
| 69 |
]; |
| 70 |
|
| 71 |
Activity::create($log); |
| 72 |
}, 20, 3); |
| 73 |
|
| 74 |
add_action('fluent_support/response_added_by_agent', function ($response, $ticket, $person) { |
| 75 |
$description = sprintf('%1$s added a response on %2$s via %3$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket), $response->source); |
| 76 |
|
| 77 |
$log = [ |
| 78 |
'event_type' => 'fluent_support/response_added_by_agent', |
| 79 |
'person_id' => $person->id, |
| 80 |
'person_type' => $person->person_type, |
| 81 |
'object_id' => $ticket->id, |
| 82 |
'object_type' => 'ticket', |
| 83 |
'description' => $description |
| 84 |
]; |
| 85 |
|
| 86 |
Activity::create($log); |
| 87 |
}, 20, 3); |
| 88 |
|
| 89 |
add_action('fluent_support/note_added_by_agent', function ($response, $ticket, $person) { |
| 90 |
$description = sprintf('%1$s added a note on %2$s via %3$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket), $response->source); |
| 91 |
|
| 92 |
$log = [ |
| 93 |
'event_type' => 'fluent_support/note_added_by_agent', |
| 94 |
'person_id' => $person->id, |
| 95 |
'person_type' => $person->person_type, |
| 96 |
'object_id' => $ticket->id, |
| 97 |
'object_type' => 'ticket', |
| 98 |
'description' => $description |
| 99 |
]; |
| 100 |
|
| 101 |
Activity::create($log); |
| 102 |
}, 20, 3); |
| 103 |
|
| 104 |
add_action('fluent_support/ticket_closed', function ($ticket, $person) { |
| 105 |
|
| 106 |
if ($person->person_type == 'customer') { |
| 107 |
$person->last_response_at = current_time('mysql'); |
| 108 |
$person->save(); |
| 109 |
} |
| 110 |
|
| 111 |
$description = sprintf('%1$s closed %2$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket)); |
| 112 |
|
| 113 |
$log = [ |
| 114 |
'event_type' => 'fluent_support/ticket_closed', |
| 115 |
'person_id' => $person->id, |
| 116 |
'person_type' => $person->person_type, |
| 117 |
'object_id' => $ticket->id, |
| 118 |
'object_type' => 'ticket', |
| 119 |
'description' => $description |
| 120 |
]; |
| 121 |
|
| 122 |
Activity::create($log); |
| 123 |
}, 20, 2); |
| 124 |
|
| 125 |
add_action('fluent_support/ticket_reopen', function ($ticket, $person) { |
| 126 |
|
| 127 |
if ($person->person_type == 'customer') { |
| 128 |
$person->last_response_at = current_time('mysql'); |
| 129 |
$person->save(); |
| 130 |
} |
| 131 |
|
| 132 |
$description = sprintf('%1$s reopened on %2$s', $this->getPersonMarkup($person), $this->getTicketMarkup($ticket)); |
| 133 |
|
| 134 |
$log = [ |
| 135 |
'event_type' => 'fluent_support/ticket_reopen', |
| 136 |
'person_id' => $person->id, |
| 137 |
'person_type' => $person->person_type, |
| 138 |
'object_id' => $ticket->id, |
| 139 |
'object_type' => 'ticket', |
| 140 |
'description' => $description |
| 141 |
]; |
| 142 |
|
| 143 |
Activity::create($log); |
| 144 |
}, 20, 2); |
| 145 |
|
| 146 |
add_action('fluent_support/agent_assigned_to_ticket', function ($assignedAgent, $ticket, $assigner) { |
| 147 |
|
| 148 |
$description = sprintf('Assign %1$s ticket to %2$s by %3$s', $this->getTicketMarkup($ticket), $this->getPersonMarkup($assignedAgent), $this->getPersonMarkup($assigner)); |
| 149 |
|
| 150 |
if($assigner && isset($assigner->id) && isset($assigner->person_type)){ |
| 151 |
$log = [ |
| 152 |
'event_type' => 'fluent_support/agent_assigned_to_ticket', |
| 153 |
'person_id' => $assigner->id, |
| 154 |
'person_type' => $assigner->person_type, |
| 155 |
'object_id' => $ticket->id, |
| 156 |
'object_type' => 'ticket', |
| 157 |
'description' => $description |
| 158 |
]; |
| 159 |
Activity::create($log); |
| 160 |
} |
| 161 |
}, 20, 3); |
| 162 |
|
| 163 |
add_action('fluent_support/ticket_deleted', function($agent, $ticketData) { |
| 164 |
if (!$agent || !$agent->id) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$description = sprintf('%s deleted %s(#%d) at %s', $this->getPersonMarkup($agent), esc_html($ticketData['title']), $ticketData['id'], current_time('mysql')); |
| 169 |
$log = [ |
| 170 |
'event_type' => 'fluent_support/ticket_deleted', |
| 171 |
'person_id' => $agent->id, |
| 172 |
'person_type' => 'agent', |
| 173 |
'object_id' => $ticketData['id'], |
| 174 |
'object_type' => 'ticket', |
| 175 |
'description' => $description |
| 176 |
]; |
| 177 |
Activity::create($log); |
| 178 |
}, 20, 2); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* @param $ticket |
| 183 |
* @param string|false $ticketText |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
public function getTicketMarkup($ticket, $ticketText = false) |
| 187 |
{ |
| 188 |
if (!$ticketText) { |
| 189 |
// translators: %s is the ticket title |
| 190 |
$ticketText = sprintf(__('Ticket: %s', 'fluent-support'), esc_html($ticket->title)); |
| 191 |
} |
| 192 |
|
| 193 |
return '<a class="fs_link_trans fs_tk" href="#view_ticket">' . $ticketText . '</a>'; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* @param $person |
| 198 |
* @return string |
| 199 |
*/ |
| 200 |
public function getPersonMarkup($person) |
| 201 |
{ |
| 202 |
if (!$person) { |
| 203 |
return ''; |
| 204 |
} |
| 205 |
|
| 206 |
$route = 'view_agent'; |
| 207 |
if (isset($person->person_type) && $person->person_type == 'customer') { |
| 208 |
$route = 'view_customer'; |
| 209 |
} |
| 210 |
return '<a class="fs_link_trans fs_pr" href="#' . $route . '">' . esc_html($person->full_name) . '</a>'; |
| 211 |
} |
| 212 |
} |
| 213 |
|