function fetchFromZendesk() { $GLOBALS['log']->error("Refetch ticket " . $this->nice_id); $this->initGroups(); if (!empty($this->nice_id)) { $xml = $this->connection->get('tickets/' . $this->nice_id . '/events'); $comments = new SimpleXMLElement($xml); $this->comments = array(); foreach ($comments->comment as $comment) { $this->comments[] = array('author_id' => $comment->{'author-id'}, 'created_at' => strftime('%F %T', strtotime($comment->{'created-at'})), 'is_public' => $comment->{'is-public'} == 'true', 'type' => $comment->type, 'value' => str_replace(array("\r\n", "\n", "\r"), '<br/>', $comment->value), 'author_name' => $comment->author->name, 'author_email' => $comment->author->email, 'author_photo_url' => str_replace('http://', 'https://', $comment->author->{'photo-url'})); $this->users[(int) $comment->{'author-id'}] = $comment->author->name; } $xml = $this->connection->get(ZENDESK_TICKETS, array('id' => $this->nice_id)); $ticket = new SimpleXMLElement($xml); $this->subject = $ticket->subject; $this->name = '#' . $this->nice_id . ' \'' . $this->subject . '\''; $this->description = $ticket->description; $this->tags = $ticket->{'current-tags'}; $this->cc_list = $ticket->{'current-collaborators'}; $this->score = $ticket->score; $this->status = (int) $ticket->{'status-id'}; $this->type = (int) $ticket->{'ticket-type-id'}; $this->priority = (int) $ticket->{'priority-id'}; $this->via = (int) $ticket->{'via-id'}; $this->group_id = (int) $ticket->{'group-id'}; $this->group_name = $this->groups[$this->group_id]['name']; $this->organization_id = (int) $ticket->{'organization-id'}; $this->organization_name = $this->getOrganizationName($this->organization_id); $this->requester_id = (int) $ticket->{'requester-id'}; $this->requester_name = array_key_exists($this->requester_id, $this->users) ? $this->users[$this->requester_id] : $this->requester_id; $this->assignee_id = (int) $ticket->{'assignee-id'}; $this->assignee_name = array_key_exists($this->assignee_id, $this->users) ? $this->users[$this->assignee_id] : $this->assignee_id; $this->created_at = strftime('%F %T', strtotime($ticket->{'created-at'})); $this->updated_at = strftime('%F %T', strtotime($ticket->{'updated-at'})); $this->assigned_at = $ticket->{'initially-assigned-at'} != '' ? strftime('%F %T', strtotime($ticket->{'initially-assigned-at'})) : null; $this->solved_at = $ticket->{'solved-at'} != '' ? strftime('%F %T', strtotime($ticket->{'solved-at'})) : null; $this->due_date = $this->type == 4 ? strftime('%F', strtotime($ticket->{'due-date'})) : null; $this->initFields(); foreach ($ticket->{'ticket-field-entries'}->{'ticket-field-entry'} as $customfield) { $field_id = (int) $customfield->{'ticket-field-id'}; if (array_key_exists($field_id, $this->fields)) { $this->fields[$field_id]['value'] = $customfield->value; if (array_key_exists('options', $this->fields[$field_id])) { $this->fields[$field_id]['display'] = $this->fields[$field_id]['options'][(string) $customfield->value]; } else { $this->fields[$field_id]['display'] = preg_replace('/\\n/', '<br/>', (string) $customfield->value); } } } $perm = $ticket->permissions; if ($perm) { $this->can_update = $perm->{'can-update-ticket'} == 'true'; $this->can_edit_tags = $perm->{'can-edit-ticket-tags'} == 'true'; //$this->can_comment = ($perm->{'can-make-comments'} == 'true'); $this->can_comment = true; $this->can_public_comment = $perm->{'can-make-public-comments'} == 'true'; } else { $this->can_update = true; $this->can_edit_tags = true; $this->can_comment = true; $this->can_public_comment = true; } parent::save(false); } }
private function retrieveTickets() { $tickets = array(); try { $xml = $this->connection->get(ZENDESK_SEARCH, array('query' => array('query' => $this->getZendeskQuery(), 'per_page' => $this->per_page, 'page' => $this->page, 'sort' => $this->sort, 'order_by' => $this->order_by))); } catch (Exception $e) { if ($e->getMessage() == "Query too long") { $this->error_message = "Cannot display ticket information as there are too many contacts to search. Please set \"Zendesk Organization\" for this account."; } else { $this->error_message = $e->getMessage(); } return $tickets; } if ($xml === false) { $this->error_message = "Could not connect to Zendesk " . $this->connection->url() . " using " . $this->connection->username; } else { libxml_use_internal_errors(true); try { $results = new SimpleXMLElement($xml); } catch (Exception $e) { $this->error_message = "Unrecognized response from Zendesk " . $this->connection->url(); } if ($this->error_message == null) { $this->result_count = $results->attributes()->count; $this->pages = ceil($this->result_count / $this->per_page); foreach ($results->record as $result) { $nice_id = $result->{'nice-id'}; $ticket = new zd_Tickets_sugar(); $ticket->retrieve_by_string_fields(array('nice_id' => $nice_id)); $ticket->nice_id = $nice_id; $ticket->subject = $result->subject; $ticket->status = (int) $result->{'status-id'}; $ticket->type = (int) $result->{'ticket-type-id'}; $ticket->priority = (int) $result->{'priority-id'}; $ticket->created_at = strftime('%F %T', strtotime($result->{'created-at'})); $ticket->updated_at = strftime('%F %T', strtotime($result->{'updated-at'})); $ticket->description = $result->description; $ticket->save(); $tickets[] = $ticket; } } } return $tickets; }