public function load_by_network_key($network_key, $ticket, $type, $debug = false) { switch ($type) { case 'ticket': $existing = shub_get_single('shub_message', 'network_key', $network_key); if ($existing) { // load it up. $this->load($existing['shub_message_id']); } if ($ticket && isset($ticket['ticket_id']) && $ticket['ticket_id'] == $network_key) { // get the messages from the API $api = $this->account->get_api(); $api_result = $api->api('ticket', 'message', array('ticket_ids' => $network_key)); if ($api_result && isset($api_result['tickets'][$network_key]) && count($api_result['tickets'][$network_key])) { //print_r($api_result); if (isset($ticket['staff']) && !empty($ticket['staff']['email'])) { $ticket['reply_from_shub_user_id'] = $this->account->get_api_user_to_id($ticket['staff']); } $all_comments = $api_result['tickets'][$network_key]; $comments = array(); foreach ($all_comments as $comment_id => $comment) { if (isset($comment['cache']) && $comment['cache'] == 'autoreply' || isset($comment['message_type_id']) && $comment['message_type_id'] == 3) { // this is an auto reply, don't bother importing it into the system here } else { $comment['id'] = $comment['ticket_message_id']; $comment['shub_user_id'] = $this->account->get_api_user_to_id($comment['user']); $comment['timestamp'] = $comment['message_time']; $comments[] = $comment; } } if (!$existing) { $this->create_new(); } $this->update('shub_account_id', $this->account->get('shub_account_id')); $this->update('shub_item_id', $this->item->get('shub_item_id')); // create/update a user entry for this comments. $shub_user_id = $this->account->get_api_user_to_id($ticket['user']); $this->update('shub_user_id', $shub_user_id); $this->update('title', $ticket['subject']); // latest comment goes in summary $this->update('summary', $comments[count($comments) - 1]['content']); $this->update('last_active', $ticket['last_message_timestamp']); $this->update('shub_type', $type); $this->update('shub_data', $ticket); $this->update('shub_link', $ticket['url']); $this->update('network_key', $network_key); if ($this->get('shub_status') != _shub_MESSAGE_STATUS_HIDDEN) { // we have to decide if we're updating the message status from answered to unanswered. // if this message status is already answered and the existing comment count matches the new comment count then we don't update the status // this is because we insert a placeholder "comment" into the db while the API does the push, and when we read again a few minutes later it overwrites this placeholder comment, so really it's not a new comment coming in just the one we posted through the API that takes a while to come back through. if ($this->get('shub_status') == _shub_MESSAGE_STATUS_ANSWERED && count($comments) == count($this->get_comments())) { // don't do anything } else { // comment count is different $this->update('shub_status', _shub_MESSAGE_STATUS_UNANSWERED); } } $this->update('comments', $comments); // add the extra fields from UCM into the ticket. if (!empty($ticket['extra']) && is_array($ticket['extra'])) { foreach ($ticket['extra'] as $extra_key => $extra_val) { $extra_val = trim($extra_val); if (strlen($extra_val)) { // add this one into the system. $ExtraField = new SupportHubExtra(); if (!$ExtraField->load_by('extra_name', $extra_key)) { $ExtraField->create_new(); $ExtraField->update('extra_name', $extra_key); } $ExtraField->save_and_link(array('extra_value' => $extra_val), 'ucm', $this->account->get('shub_account_id'), $this->get('shub_message_id'), $shub_user_id); } } } } return $this->get('shub_message_id'); } break; } return false; }