Ticket API and return 404 if no results

This commit is contained in:
Mike Koch 2015-11-02 12:55:32 -05:00
parent 79d4574e19
commit 490dcdb764
7 changed files with 57 additions and 214 deletions

View File

@ -18,6 +18,10 @@ if ($request_method == 'GET') {
} else { } else {
$results = get_canned_response($hesk_settings); $results = get_canned_response($hesk_settings);
} }
if ($results == NULL) {
return http_response_code(404);
}
output($results); output($results);
} else { } else {
return http_response_code(405); return http_response_code(405);

21
api/dao/ticket_dao.php Normal file
View File

@ -0,0 +1,21 @@
<?php
function get_ticket_for_id($hesk_settings, $id = NULL) {
$sql = "SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "tickets` ";
if ($id != NULL) {
$sql .= "WHERE `id` = ".intval($id);
}
$response = hesk_dbQuery($sql);
if (hesk_dbNumRows($response) == 0) {
return NULL;
}
$results = [];
while ($row = hesk_dbFetchAssoc($response)) {
$results[] = $row;
}
return $id == NULL ? $results : $results[0];
}

View File

@ -19,6 +19,10 @@ if ($request_method == 'GET') {
} else { } else {
$results = get_ticket_template($hesk_settings); $results = get_ticket_template($hesk_settings);
} }
if ($results == NULL) {
return http_response_code(404);
}
output($results); output($results);
} else { } else {
return http_response_code(405); return http_response_code(405);

28
api/ticket/index.php Normal file
View File

@ -0,0 +1,28 @@
<?php
define('IN_SCRIPT', 1);
define('HESK_PATH', '../../');
define('API_PATH', '../');
require_once(HESK_PATH . 'hesk_settings.inc.php');
require_once(HESK_PATH . 'inc/common.inc.php');
require_once(API_PATH . 'core/output.php');
require_once(API_PATH . 'dao/ticket_dao.php');
hesk_load_api_database_functions();
hesk_dbConnect();
// Routing
$request_method = $_SERVER['REQUEST_METHOD'];
if ($request_method == 'GET') {
if (isset($_GET['id'])) {
$results = get_ticket_for_id($hesk_settings, $_GET['id']);
} else {
$results = get_ticket_for_id($hesk_settings);
}
if ($results == NULL) {
return http_response_code(404);
}
output($results);
} else {
return http_response_code(405);
}

View File

@ -1,51 +0,0 @@
<?php
class Ticket{
public $id;
public $trackingId;
public $name;
public $email;
public $category;
public $priority;
public $subject;
public $message;
public $dateCreated;
public $dateModified;
public $ip;
public $language;
public $status;
public $owner;
public $timeWorked;
public $lastReplier;
public $replierId;
public $isArchived;
public $isLocked;
public $attachments;
public $merged;
public $history;
public $custom1;
public $custom2;
public $custom3;
public $custom4;
public $custom5;
public $custom6;
public $custom7;
public $custom8;
public $custom9;
public $custom10;
public $custom11;
public $custom12;
public $custom13;
public $custom14;
public $custom15;
public $custom16;
public $custom17;
public $custom18;
public $custom19;
public $custom20;
public function __construct()
{
}
}
?>

View File

@ -1,144 +0,0 @@
<?php
require(__DIR__ . '/../models/ticket.php');
class TicketRepository {
private function __construct() {
}
public static function getTicketForId($id, $settings) {
$connection = new mysqli($settings['db_host'], $settings['db_user'], $settings['db_pass'], $settings['db_name']);
if ($connection->connect_error)
{
return ('An error occurred when establishing a connection to the database.');
}
$sql = self::getDefaultSql($settings).
'WHERE T.id = '.$id;
$results = $connection->query($sql);
//-- There will only ever be one result, as ID is the primary key on the tickets table.
$result = $results->fetch_assoc();
$connection->close();
return self::generateTicketModel($result);
}
private static function generateTicketModel($result) {
$ticket = new Ticket();
settype($result['id'], 'int');
$ticket->id = $result['id'];
$ticket->trackingId = $result['trackid'];
$ticket->name = $result['ContactName'];
$ticket->email = $result['email'];
settype($result['category'], 'int');
$ticket->category = $result['category'];
settype($result['priority'], 'int');
$ticket->priority = $result['priority'];
$ticket->subject = $result['subject'];
$ticket->message = $result['message'];
//-- Convert these to times so the receiver can use whatever format they want to display the date/time without extra work.
$ticket->dateCreated = strtotime($result['dt']);
$ticket->dateModified = strtotime($result['lastchange']);
$ticket->ip = $result['ip'];
$ticket->language = $result['language'];
settype($result['status'], 'int');
$ticket->status = $result['status'];
settype($result['owner'], 'int');
$ticket->owner = $result['owner'];
$ticket->timeWorked = $result['time_worked'];
settype($result['lastreplier'], 'int');
$ticket->lastReplier = $result['lastreplier'];
settype($result['replierid'], 'int');
$ticket->replierId = $result['replierid'];
$ticket->isArchived = ($result['archive'] ? true : false);
$ticket->isLocked = ($result['locked'] ? true : false);
$ticket->attachments = $result['attachments'];
//-- explode handles splitting the list into an array, array_filter removes the empty string elements (""), and array_values resets the indicies.
$ticket->merged = array_values(array_filter(explode('#',$result['merged'])));
//-- Not currently returning history, as it can contain a metric shit-ton of HTML code and will cludder up the JSON response.
//$ticket->history = $result['history'];
$ticket->custom1 = $result['custom1'] == '' ? null : $result['custom1'];
$ticket->custom2 = $result['custom2'] == '' ? null : $result['custom2'];
$ticket->custom3 = $result['custom3'] == '' ? null : $result['custom3'];
$ticket->custom4 = $result['custom4'] == '' ? null : $result['custom4'];
$ticket->custom5 = $result['custom5'] == '' ? null : $result['custom5'];
$ticket->custom6 = $result['custom6'] == '' ? null : $result['custom6'];
$ticket->custom7 = $result['custom7'] == '' ? null : $result['custom7'];
$ticket->custom8 = $result['custom8'] == '' ? null : $result['custom8'];
$ticket->custom9 = $result['custom9'] == '' ? null : $result['custom9'];
$ticket->custom10 = $result['custom10'] == '' ? null : $result['custom10'];
$ticket->custom11 = $result['custom11'] == '' ? null : $result['custom11'];
$ticket->custom12 = $result['custom12'] == '' ? null : $result['custom12'];
$ticket->custom13 = $result['custom13'] == '' ? null : $result['custom13'];
$ticket->custom14 = $result['custom14'] == '' ? null : $result['custom14'];
$ticket->custom15 = $result['custom15'] == '' ? null : $result['custom15'];
$ticket->custom16 = $result['custom16'] == '' ? null : $result['custom16'];
$ticket->custom17 = $result['custom17'] == '' ? null : $result['custom17'];
$ticket->custom18 = $result['custom18'] == '' ? null : $result['custom18'];
$ticket->custom19 = $result['custom19'] == '' ? null : $result['custom19'];
$ticket->custom20 = $result['custom20'] == '' ? null : $result['custom20'];
return $ticket;
}
private function getDefaultSql($settings) {
return 'SELECT T.id, '.
'T.trackid, '.
'T.name AS "ContactName", '.
'T.email, '.
'T.category, '.
'T.priority, '.
'T.subject, '.
'T.message, '.
'T.dt, '.
'T.lastchange, '.
'T.ip, '.
'T.language, '.
'T.status, '.
'T.owner, '.
'T.time_worked, '.
'T.lastreplier, '.
'T.replierid, '.
'T.archive, '.
'T.locked, '.
'T.attachments, '.
'T.merged, '.
'T.custom1, '.
'T.custom2, '.
'T.custom3, '.
'T.custom4, '.
'T.custom5, '.
'T.custom6, '.
'T.custom7, '.
'T.custom8, '.
'T.custom9, '.
'T.custom10, '.
'T.custom11, '.
'T.custom12, '.
'T.custom13, '.
'T.custom14, '.
'T.custom15, '.
'T.custom16, '.
'T.custom17, '.
'T.custom18, '.
'T.custom19, '.
'T.custom20 '.
'FROM '.$settings['db_pfix'].'tickets T ';
}
}

View File

@ -1,19 +0,0 @@
<?php
//-- This service will return ticket information for a specific ticket ID (NOT TRACKING ID)
header('Content-Type: application/json');
define('IN_SCRIPT',1);
define('HESK_PATH','../../');
include(HESK_PATH . 'hesk_settings.inc.php');
include(__DIR__ . '/../repositories/ticketRepository.php');
if(isset($_GET['id']))
{
$ticket = TicketRepository::getTicketForId($_GET['id'], $hesk_settings);
echo json_encode($ticket);
}
else
{
header(http_response_code(400));
}