| @@ -2,25 +2,49 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Hooks\Handlers; |
| 4 | 4 | |
| 5 | 5 | use FluentSupport\App\Models\Activity; |
| 6 | +use FluentSupport\App\Services\Helper; | |
| 6 | 7 | |
| 8 | +/** | |
| 9 | + * Writes ticket lifecycle events to the activity log. | |
| 10 | + */ | |
| 11 | + | |
| 7 | 12 | class ActivityLogger |
| 8 | 13 | { |
| 14 | + | |
| 9 | 15 | public function init() |
| 10 | 16 | { |
| 11 | - // Ticket Related activities | |
| 17 | + if (Helper::areLogsDisabled('_activity_settings')) { | |
| 18 | + return; | |
| 19 | + } | |
| 20 | + | |
| 12 | 21 | add_action('fluent_support/ticket_created', function ($ticket, $customer) { |
| 13 | 22 | |
| 14 | 23 | $customer->last_response_at = current_time('mysql'); |
| 15 | 24 | $customer->save(); |
| 16 | 25 | |
| 17 | - $description = sprintf('%1$s created a %2$s via %3$s', $this->getPersonMarkup($customer), $this->getTicketMarkup($ticket), $ticket->source); | |
| 26 | + $agent = Helper::getAgentByUserId(); | |
| 18 | 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 | + | |
| 19 | 43 | $log = [ |
| 20 | 44 | 'event_type' => 'fluent_support/ticket_created', |
| 21 | - 'person_id' => $customer->id, | |
| 22 | - 'person_type' => $customer->person_type, | |
| 45 | + 'person_id' => $personId, | |
| 46 | + 'person_type' => $personType, | |
| 23 | 47 | 'object_id' => $ticket->id, |
| 24 | 48 | 'object_type' => 'ticket', |
| 25 | 49 | 'description' => $description |
| 26 | 50 | ]; |
| @@ -117,24 +141,72 @@ | ||
| 117 | 141 | ]; |
| 118 | 142 | |
| 119 | 143 | Activity::create($log); |
| 120 | 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); | |
| 121 | 179 | } |
| 122 | 180 | |
| 181 | + /** | |
| 182 | + * @param $ticket | |
| 183 | + * @param string|false $ticketText | |
| 184 | + * @return string | |
| 185 | + */ | |
| 123 | 186 | public function getTicketMarkup($ticket, $ticketText = false) |
| 124 | 187 | { |
| 125 | 188 | if (!$ticketText) { |
| 126 | - $ticketText = sprintf(__('Ticket: %s', 'fluent-support'), $ticket->title); | |
| 189 | + // translators: %s is the ticket title | |
| 190 | + $ticketText = sprintf(__('Ticket: %s', 'fluent-support'), esc_html($ticket->title)); | |
| 127 | 191 | } |
| 128 | 192 | |
| 129 | 193 | return '<a class="fs_link_trans fs_tk" href="#view_ticket">' . $ticketText . '</a>'; |
| 130 | 194 | } |
| 131 | 195 | |
| 196 | + /** | |
| 197 | + * @param $person | |
| 198 | + * @return string | |
| 199 | + */ | |
| 132 | 200 | public function getPersonMarkup($person) |
| 133 | 201 | { |
| 202 | + if (!$person) { | |
| 203 | + return ''; | |
| 204 | + } | |
| 205 | + | |
| 134 | 206 | $route = 'view_agent'; |
| 135 | - if ($person->person_type == 'customer') { | |
| 207 | + if (isset($person->person_type) && $person->person_type == 'customer') { | |
| 136 | 208 | $route = 'view_customer'; |
| 137 | 209 | } |
| 138 | - return '<a class="fs_link_trans fs_pr" href="#' . $route . '">' . $person->full_name . '</a>'; | |
| 210 | + return '<a class="fs_link_trans fs_pr" href="#' . $route . '">' . esc_html($person->full_name) . '</a>'; | |
| 139 | 211 | } |
| 140 | 212 | } |