| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Tickets\Importer; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Ticket; |
| 6 |
use FluentSupport\App\Services\Helper; |
| 7 |
use FluentSupport\App\Models\Attachment; |
| 8 |
use FluentSupport\App\Models\Conversation; |
| 9 |
|
| 10 |
class AwesomeSupportTickets extends BaseImporter |
| 11 |
{ |
| 12 |
protected $handler = 'awesome-support'; |
| 13 |
|
| 14 |
public function stats() |
| 15 |
{ |
| 16 |
$ticketsCount = $this->getCount(); |
| 17 |
$replyCount = $this->db->table('posts') |
| 18 |
->where('post_type', 'ticket_reply') |
| 19 |
->count(); |
| 20 |
|
| 21 |
return [ |
| 22 |
'name' => esc_html('Awesome Support'), |
| 23 |
'tickets' => $ticketsCount, |
| 24 |
'replies' => $replyCount, |
| 25 |
'customers' => count( get_users( array( 'role' => 'wpas_user' ) ) ), |
| 26 |
'handler' => $this->handler, |
| 27 |
'last_migrated' => get_option('_fs_migrate_awesome_support') |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* This `doMigration` method will migrate ticket from other support system |
| 33 |
* @param int $page |
| 34 |
* @param string $handler |
| 35 |
* @return array |
| 36 |
*/ |
| 37 |
public function doMigration( $page, $handler ) |
| 38 |
{ |
| 39 |
$this->handler = $handler; |
| 40 |
$allCounts = $this->getCount(); |
| 41 |
|
| 42 |
if (!$allCounts) { |
| 43 |
return [ |
| 44 |
'message' => __('Sorry, no tickets available for import.', 'fluent-support'), |
| 45 |
'had_tickets' => 'no', |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
$tickets = $this->getTickets($this->limit, $page); |
| 50 |
$results = $this->migrateTickets($tickets); |
| 51 |
|
| 52 |
$hasMore = $page * $this->limit <= $allCounts; |
| 53 |
|
| 54 |
$remainingTickets = $allCounts - ($page * $this->limit); |
| 55 |
$completedTickets = intval(($page / $allCounts) * 100); |
| 56 |
|
| 57 |
$response = [ |
| 58 |
'handler' => $this->handler, |
| 59 |
'insert_ids' => $results['inserts'], |
| 60 |
'skips' => count($results['skips']), |
| 61 |
'has_more' => $hasMore, |
| 62 |
'completed' => $completedTickets, |
| 63 |
'total_pages' => ceil($allCounts / $this->limit), |
| 64 |
'imported_page' => $page, |
| 65 |
'next_page' => $page + 1, |
| 66 |
'total_tickets' => $allCounts, |
| 67 |
'remaining' => $remainingTickets |
| 68 |
]; |
| 69 |
|
| 70 |
if (!$hasMore) { |
| 71 |
$response['message'] = __('All tickets has been importer successfully', 'fluent-support'); |
| 72 |
update_option('_fs_migrate_awesome_support', current_time('mysql'), 'no'); |
| 73 |
} |
| 74 |
|
| 75 |
return $response; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* This `getTickets` method will get tickets from other support system |
| 80 |
* @param int $limit |
| 81 |
* @param int $page |
| 82 |
* @return object $tickets |
| 83 |
*/ |
| 84 |
public function getTickets( $limit, $page ) |
| 85 |
{ |
| 86 |
$args = [ |
| 87 |
'posts_per_page' => $limit, |
| 88 |
'paged' => $page |
| 89 |
]; |
| 90 |
|
| 91 |
$tickets = \wpas_get_tickets('any', $args); |
| 92 |
$formattedTickets = []; |
| 93 |
|
| 94 |
if ($tickets){ |
| 95 |
foreach($tickets as $ticket){ |
| 96 |
$customerData = $this->resolvePerson($ticket->post_author); |
| 97 |
|
| 98 |
if (!$customerData) { |
| 99 |
$customerData = $this->getUnknownCustomerData($ticket->post_author); |
| 100 |
} |
| 101 |
|
| 102 |
$replies = $this->getReplies($ticket); |
| 103 |
|
| 104 |
$ticketData = [ |
| 105 |
'origin_id' => intval($ticket->ID), |
| 106 |
'replies' => $replies, |
| 107 |
'source' => sanitize_text_field($this->handler), |
| 108 |
'title' => sanitize_text_field($ticket->post_title), |
| 109 |
'content' => wp_kses_post($ticket->post_content), |
| 110 |
'mailbox_id' => intval($this->mailbox->id), |
| 111 |
'response_count' => count($replies), |
| 112 |
'status' => 'active', |
| 113 |
'created_at' => $ticket->post_date, |
| 114 |
'updated_at' => $ticket->post_modified, |
| 115 |
'last_customer_response' => $ticket->post_modified, |
| 116 |
'waiting_since' => $ticket->post_modified |
| 117 |
]; |
| 118 |
|
| 119 |
$ticketStatus = $this->getMeta('_wpas_status', $ticket->ID); |
| 120 |
|
| 121 |
if ($ticketStatus == 'closed') { |
| 122 |
$ticketData['status'] = 'closed'; |
| 123 |
$ticketData['resolved_at'] = $this->getMeta('_ticket_closed_on', $ticket->ID); |
| 124 |
} else if (!$replies) { |
| 125 |
$ticketData['status'] = 'new'; |
| 126 |
} |
| 127 |
|
| 128 |
$customer = $this->getPerson($customerData, 'customer'); |
| 129 |
|
| 130 |
if(!$customer) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
$ticketData['customer'] = $customer; |
| 135 |
$ticketData['attachments'] = $this->getAttachments($ticket->ID); |
| 136 |
|
| 137 |
$formattedTickets[] = $ticketData; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $formattedTickets; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* This `getMeta` method will get associated meta data of a ticket |
| 146 |
* @param string $metaKey |
| 147 |
* @param int $postId |
| 148 |
* @return array |
| 149 |
*/ |
| 150 |
public function getMeta($metaKey, $postId) |
| 151 |
{ |
| 152 |
return get_post_meta($postId, $metaKey, true); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* This `getReplies` method will get associated replies with a ticket by it's id |
| 157 |
* @param object $ticket |
| 158 |
* @return array $formattedReplies |
| 159 |
*/ |
| 160 |
public function getReplies($ticket) |
| 161 |
{ |
| 162 |
$replies = $this->db->table('posts') |
| 163 |
->where('post_type', 'ticket_reply') |
| 164 |
->where('post_parent', $ticket->ID) |
| 165 |
->oldest('ID') |
| 166 |
->get(); |
| 167 |
|
| 168 |
$formattedReplies = []; |
| 169 |
|
| 170 |
foreach ($replies as $reply) { |
| 171 |
$formattedReplies[] = [ |
| 172 |
'content' => $reply->post_content, |
| 173 |
'conversation_type' => 'response', |
| 174 |
'created_at' => $reply->post_date, |
| 175 |
'updated_at' => $reply->post_modified, |
| 176 |
'is_customer_reply' => ($ticket->post_author === $reply->post_author), |
| 177 |
'user' => $this->resolvePerson($reply->post_author), |
| 178 |
'attachments' => $this->getAttachments($reply->ID) |
| 179 |
]; |
| 180 |
} |
| 181 |
|
| 182 |
return $formattedReplies; |
| 183 |
} |
| 184 |
|
| 185 |
protected function getAttachments($threadId) |
| 186 |
{ |
| 187 |
$attachments = $this->db->table('posts') |
| 188 |
->where('post_type', 'attachment') |
| 189 |
->where('post_parent', $threadId) |
| 190 |
->oldest('ID') |
| 191 |
->get(); |
| 192 |
|
| 193 |
$formattedAttachments = []; |
| 194 |
|
| 195 |
foreach ($attachments as $attachment) { |
| 196 |
$formattedAttachments[] = [ |
| 197 |
'full_url' => sanitize_url($attachment->guid), |
| 198 |
'title' => $attachment->post_title, |
| 199 |
'file_path' => get_attached_file($attachment->ID), |
| 200 |
'driver' => 'local', |
| 201 |
'status' => 'active', |
| 202 |
'file_type' => $attachment->post_mime_type |
| 203 |
]; |
| 204 |
} |
| 205 |
|
| 206 |
return $formattedAttachments; |
| 207 |
} |
| 208 |
|
| 209 |
protected function getCount() |
| 210 |
{ |
| 211 |
return count( \wpas_get_tickets( 'any' ) ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* This `deleteTickets` method will delete tickets with all available data |
| 216 |
* @param int $page //In some scenario it can be id of ticket |
| 217 |
* @return array $response |
| 218 |
*/ |
| 219 |
public function deleteTickets($page) |
| 220 |
{ |
| 221 |
$hasmore = true; |
| 222 |
|
| 223 |
$args = [ |
| 224 |
'posts_per_page' => $this->limit, |
| 225 |
'paged' => $page |
| 226 |
]; |
| 227 |
|
| 228 |
$tickets = \wpas_get_tickets('any', $args); |
| 229 |
|
| 230 |
if (!$tickets) { |
| 231 |
$hasmore = false; |
| 232 |
} |
| 233 |
|
| 234 |
if ($tickets) { |
| 235 |
foreach($tickets as $ticket){ |
| 236 |
wp_delete_post($ticket->ID, true); |
| 237 |
|
| 238 |
$this->db->table('posts') |
| 239 |
->whereIn('post_type', ['ticket_reply', 'ticket_history']) |
| 240 |
->where('post_parent', $ticket->ID) |
| 241 |
->oldest('ID') |
| 242 |
->delete(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
$response = [ |
| 247 |
'has_more' => $hasmore, |
| 248 |
'next_page' => (int) $page + 1 |
| 249 |
]; |
| 250 |
|
| 251 |
if (!$hasmore) { |
| 252 |
$response['message'] = __('All tickets has been deleted successfully', 'fluent-support'); |
| 253 |
} |
| 254 |
|
| 255 |
return $response; |
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|