| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Tickets\Importer; |
| 4 |
|
| 5 |
class SupportCandyTickets extends BaseImporter |
| 6 |
{ |
| 7 |
protected $handler = 'support-candy'; |
| 8 |
|
| 9 |
/** |
| 10 |
* This `supportCandyStats` method will fetch all available stats of Support Candy. |
| 11 |
*/ |
| 12 |
public function stats() |
| 13 |
{ |
| 14 |
$ticketsCount = $this->getCount(); |
| 15 |
$replyCount = $this->db->table('psmsc_threads') |
| 16 |
->where('type', 'reply') |
| 17 |
->count(); |
| 18 |
|
| 19 |
return [ |
| 20 |
'name' => 'Support Candy', |
| 21 |
'tickets' => $ticketsCount, |
| 22 |
'replies' => $replyCount, |
| 23 |
'customers' => $this->db->table('psmsc_customers')->count(), |
| 24 |
'handler' => $this->handler, |
| 25 |
'last_migrated' => get_option('_fs_migrate_support_candy') |
| 26 |
]; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* This `doMigration` method will migrate ticket from other support system |
| 31 |
* @param int $page |
| 32 |
* @param string $handler |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public function doMigration($page, $handler) |
| 36 |
{ |
| 37 |
$this->handler = $handler; |
| 38 |
$allCounts = $this->getCount(); |
| 39 |
|
| 40 |
if (!$allCounts) { |
| 41 |
return [ |
| 42 |
'message' => __('Sorry, no tickets available for import.', 'fluent-support'), |
| 43 |
'had_tickets' => 'no', |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
$tickets = $this->getTickets($this->limit, $page); |
| 48 |
|
| 49 |
$results = $this->migrateTickets($tickets); |
| 50 |
$hasMore = $page * $this->limit <= $allCounts; |
| 51 |
|
| 52 |
$remainingTickets = $allCounts - ($page * $this->limit); |
| 53 |
$completedTickets = intval(($page / $allCounts) * 100); |
| 54 |
|
| 55 |
$response = [ |
| 56 |
'handler' => $this->handler, |
| 57 |
'insert_ids' => $results['inserts'], |
| 58 |
'skips' => count($results['skips']), |
| 59 |
'has_more' => $hasMore, |
| 60 |
'completed' => $completedTickets, |
| 61 |
'imported_page' => $page, |
| 62 |
'total_pages' => ceil($allCounts / $this->limit), |
| 63 |
'next_page' => $page + 1, |
| 64 |
'total_tickets' => $allCounts, |
| 65 |
'remaining' => $remainingTickets |
| 66 |
]; |
| 67 |
|
| 68 |
if (!$hasMore) { |
| 69 |
$response['message'] = __('All tickets has been importer successfully', 'fluent-support'); |
| 70 |
update_option('_fs_migrate_support_candy', current_time('mysql'), 'no'); |
| 71 |
} |
| 72 |
|
| 73 |
return $response; |
| 74 |
} |
| 75 |
|
| 76 |
// This `getTickets` will fetch tickets by limit and page number |
| 77 |
private function getTickets($limit, $page) |
| 78 |
{ |
| 79 |
global $wpdb; |
| 80 |
|
| 81 |
$tickets = $this->db->table('psmsc_tickets') |
| 82 |
->select([ |
| 83 |
'psmsc_tickets.*', |
| 84 |
'psmsc_threads.body', |
| 85 |
'psmsc_threads.type' |
| 86 |
]) |
| 87 |
->selectRaw($wpdb->prefix . 'psmsc_threads.id as thread_id') |
| 88 |
->join('psmsc_threads', 'psmsc_threads.ticket', '=', 'psmsc_tickets.id') |
| 89 |
->where('psmsc_threads.type', '=', 'report') |
| 90 |
->orderBy('psmsc_tickets.id', 'ASC') |
| 91 |
->offset(($page - 1) * $limit) |
| 92 |
->limit($limit) |
| 93 |
->get(); |
| 94 |
|
| 95 |
$formattedTickets = []; |
| 96 |
|
| 97 |
if ($tickets) { |
| 98 |
foreach ($tickets as $ticket) { |
| 99 |
$customerData = $this->getTicketCustomer($ticket->customer); |
| 100 |
|
| 101 |
if (!$customerData) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
$replies = $this->getReplies($ticket); |
| 106 |
$ticketData = [ |
| 107 |
'origin_id' => $ticket->id, |
| 108 |
'replies' => $replies, |
| 109 |
'source' => $this->handler, |
| 110 |
'title' => $ticket->subject, |
| 111 |
'content' => wp_kses_post($ticket->body), |
| 112 |
'mailbox_id' => $this->mailbox->id, |
| 113 |
'response_count' => count($replies), |
| 114 |
'status' => 'active', |
| 115 |
'created_at' => $ticket->date_created, |
| 116 |
'updated_at' => $ticket->date_updated, |
| 117 |
'last_customer_response' => $ticket->date_created, |
| 118 |
'waiting_since' => $ticket->date_created |
| 119 |
]; |
| 120 |
|
| 121 |
$ticket->resolved_at = NULL; |
| 122 |
if ($ticket->date_closed && $ticket->date_closed != '0000-00-00 00:00:00') { |
| 123 |
$ticketData['status'] = 'closed'; |
| 124 |
$ticketData['resolved_at'] = $ticket->date_closed; |
| 125 |
} else if (!$replies) { |
| 126 |
$ticketData['status'] = 'new'; |
| 127 |
} |
| 128 |
|
| 129 |
$customer = $this->getPerson($customerData, 'customer'); |
| 130 |
|
| 131 |
if(!$customer) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
|
| 135 |
$ticketData['customer'] = $customer; |
| 136 |
|
| 137 |
if ($ticket->assigned_agent) { |
| 138 |
$ticketData['agent'] = $this->getAgentPerson($ticket->assigned_agent); |
| 139 |
} |
| 140 |
|
| 141 |
$ticketData['attachments'] = $this->getAttachments($ticket->thread_id); |
| 142 |
|
| 143 |
$formattedTickets[] = $ticketData; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return $formattedTickets; |
| 148 |
} |
| 149 |
|
| 150 |
protected function getAttachments($threadId) |
| 151 |
{ |
| 152 |
$attachments = $this->db->table('psmsc_attachments') |
| 153 |
->where('source_id', $threadId) |
| 154 |
->orderBY('id', 'ASC') |
| 155 |
->get(); |
| 156 |
|
| 157 |
$formattedAttachments = []; |
| 158 |
|
| 159 |
foreach ($attachments as $attachment) { |
| 160 |
$uploadDir = wp_get_upload_dir(); |
| 161 |
$fileUrl = str_replace($uploadDir['basedir'], $uploadDir['baseurl'], $attachment->file_path); |
| 162 |
|
| 163 |
$fileInfo = wp_check_filetype($attachment->file_path); |
| 164 |
|
| 165 |
$formattedAttachments[] = [ |
| 166 |
'full_url' => $fileUrl, |
| 167 |
'title' => $attachment->name, |
| 168 |
'file_path' => $attachment->file_path, |
| 169 |
'driver' => 'local', |
| 170 |
'status' => 'active', |
| 171 |
'file_type' => (!empty($fileInfo['type'])) ? $fileInfo['type'] : '' |
| 172 |
]; |
| 173 |
} |
| 174 |
|
| 175 |
return $formattedAttachments; |
| 176 |
} |
| 177 |
|
| 178 |
protected function getReplies($ticket) |
| 179 |
{ |
| 180 |
$replies = $this->db->table('psmsc_threads') |
| 181 |
->select([ |
| 182 |
'psmsc_threads.*', |
| 183 |
'psmsc_customers.user', |
| 184 |
'psmsc_customers.name', |
| 185 |
'psmsc_customers.email' |
| 186 |
]) |
| 187 |
->join('psmsc_customers', 'psmsc_customers.id', '=', 'psmsc_threads.customer') |
| 188 |
->where('psmsc_threads.type', 'reply') |
| 189 |
->where('psmsc_threads.ticket', $ticket->id) |
| 190 |
->orderBy('psmsc_threads.id', 'ASC') |
| 191 |
->get(); |
| 192 |
|
| 193 |
$formattedReplies = []; |
| 194 |
|
| 195 |
foreach ($replies as $reply) { |
| 196 |
$formattedReplies[] = [ |
| 197 |
'content' => $reply->body, |
| 198 |
'conversation_type' => 'response', |
| 199 |
'created_at' => $reply->date_created, |
| 200 |
'updated_at' => $reply->date_updated, |
| 201 |
'is_customer_reply' => ($ticket->customer === $reply->customer), |
| 202 |
'user' => [ |
| 203 |
'user_id' => $reply->user, |
| 204 |
'email' => $reply->email, |
| 205 |
'full_name' => $reply->name |
| 206 |
], |
| 207 |
'attachments' => $this->getAttachments($reply->id) |
| 208 |
]; |
| 209 |
} |
| 210 |
|
| 211 |
return $formattedReplies; |
| 212 |
} |
| 213 |
|
| 214 |
private function getCount() |
| 215 |
{ |
| 216 |
return $this->db->table('psmsc_tickets')->count(); |
| 217 |
} |
| 218 |
|
| 219 |
private function getTicketCustomer($customerId) |
| 220 |
{ |
| 221 |
$customer = $this->db->table('psmsc_customers') |
| 222 |
->where('id', $customerId) |
| 223 |
->first(); |
| 224 |
|
| 225 |
if (!$customer) { |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
return [ |
| 230 |
'user_id' => $customer->user, |
| 231 |
'full_name' => $customer->name, |
| 232 |
'email' => $customer->email |
| 233 |
]; |
| 234 |
} |
| 235 |
|
| 236 |
protected function getAgentPerson($agentId) |
| 237 |
{ |
| 238 |
static $agents = []; |
| 239 |
|
| 240 |
if (isset($agents[$agentId])) { |
| 241 |
return $agents[$agentId]; |
| 242 |
} |
| 243 |
|
| 244 |
$agent = $this->db->table('psmsc_agents') |
| 245 |
->where('id', $agentId) |
| 246 |
->first(); |
| 247 |
|
| 248 |
if (!$agent) { |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
$agents[$agentId] = $this->getPerson([ |
| 253 |
'user_id' => $agent->user, |
| 254 |
'full_name' => $agent->name |
| 255 |
], 'agent'); |
| 256 |
return $agents[$agentId]; |
| 257 |
} |
| 258 |
|
| 259 |
private function resolveAgent($ticketId, $customerId) |
| 260 |
{ |
| 261 |
$agentUser = $this->db->table('psmsc_threads') |
| 262 |
->where('ticket', $ticketId) |
| 263 |
->where('type', 'reply') |
| 264 |
->oldest('ID') |
| 265 |
->where('customer', '!=', $customerId) |
| 266 |
->select(['customer']) |
| 267 |
->first(); |
| 268 |
|
| 269 |
$agent = $this->db->table('psmsc_agents') |
| 270 |
->where('id', $agentUser->customer) |
| 271 |
->first(); |
| 272 |
|
| 273 |
return $this->getPerson([ |
| 274 |
'user_id' => $agent->user, |
| 275 |
'full_name' => $agent->name |
| 276 |
], 'agent'); |
| 277 |
} |
| 278 |
|
| 279 |
public function deleteTickets($page) |
| 280 |
{ |
| 281 |
$tables = [ |
| 282 |
'psmsc_agents', |
| 283 |
'psmsc_attachments', |
| 284 |
'psmsc_categories', |
| 285 |
'psmsc_custom_fields', |
| 286 |
'psmsc_customers', |
| 287 |
'psmsc_email_notifications', |
| 288 |
'psmsc_email_otp', |
| 289 |
'psmsc_holidays', |
| 290 |
'psmsc_logs', |
| 291 |
'psmsc_options', |
| 292 |
'psmsc_priorities', |
| 293 |
'psmsc_scheduled_tasks', |
| 294 |
'psmsc_statuses', |
| 295 |
'psmsc_threads', |
| 296 |
'psmsc_tickets', |
| 297 |
'psmsc_wh_exceptions', |
| 298 |
'psmsc_working_hrs' |
| 299 |
]; |
| 300 |
|
| 301 |
global $wpdb; |
| 302 |
foreach ($tables as $table) { |
| 303 |
$wpdb->query("TRUNCATE TABLE {$wpdb->prefix}{$table}"); |
| 304 |
} |
| 305 |
|
| 306 |
return [ |
| 307 |
'has_more' => false, |
| 308 |
'message' => __('All Support Candy Tickets and associated data has been deleted. You may now deactivate Support Candy Plugin and start using Fluent Support', 'fluent-support') |
| 309 |
]; |
| 310 |
} |
| 311 |
} |
| 312 |
|