From 044faa77f68cfa76eedec614c2c31585a6a56cde Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Sun, 12 Feb 2017 00:50:30 -0500 Subject: [PATCH] TicketGateway can now insert tickets --- api/BusinessLogic/Tickets/Ticket.php | 12 +++ api/DataAccess/Tickets/TicketGateway.php | 105 +++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/api/BusinessLogic/Tickets/Ticket.php b/api/BusinessLogic/Tickets/Ticket.php index 13b5a43a..6594562c 100644 --- a/api/BusinessLogic/Tickets/Ticket.php +++ b/api/BusinessLogic/Tickets/Ticket.php @@ -236,6 +236,15 @@ class Ticket { */ public $attachments; + function getAttachmentsForDatabase() { + $attachmentArray = array(); + foreach ($this->attachments as $attachment) { + $attachmentArray[] = $attachment->id . '#' . $attachment->fileName . '#' . $attachment->savedName; + } + + return implode(',', $attachmentArray); + } + /** * @var int[]|null */ @@ -272,6 +281,9 @@ class Ticket { public $userAgent; /** + * 0 => width + * 1 => height + * * @var int[]|null */ public $screenResolution; diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index c4a40a8c..f2101c7a 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -82,4 +82,109 @@ class TicketGateway extends CommonDao { return $ticket; } + + /** + * @param $ticket Ticket + * @param $heskSettings + */ + function createTicket($ticket, $heskSettings) { + global $hesklang; + + // If language is not set or default, set it to NULL. + if ($ticket->language === null || empty($ticket->language)) { + $language = (!$heskSettings['can_sel_lang']) ? HESK_DEFAULT_LANGUAGE : hesk_dbEscape($hesklang['LANGUAGE']); + } else { + $language = $ticket->language; + } + + $dueDate = $ticket->dueDate ? "'{$ticket->dueDate}'" : "NULL"; + // Prepare SQL for custom fields + $customWhere = ''; + $customWhat = ''; + + for ($i=1; $i<=50; $i++) + { + $customWhere .= ", `custom{$i}`"; + $customWhat .= ", '" . (isset($ticket->customFields[$i]) ? hesk_dbEscape($ticket->customFields[$i]) : '') . "'"; + } + + $suggestedArticles = ''; + if ($ticket->suggestedArticles !== null && !empty($ticket->suggestedArticles)) { + $suggestedArticles = implode(',', $ticket->suggestedArticles); + } + + $latitude = $ticket->location !== null + && isset($ticket->location[0]) + && $ticket->location[0] !== null ? $ticket->location[0] : ''; + $longitude = $ticket->location !== null + && isset($ticket->location[1]) + && $ticket->location[1] !== null ? $ticket->location[1] : ''; + $userAgent = $ticket->userAgent !== null ? $ticket->userAgent : ''; + $screenResolutionWidth = $ticket->screenResolution !== null + && isset($ticket->screenResolution[0]) + && $ticket->screenResolution[0] !== null ? intval($ticket->screenResolution[0]) : ''; + $screenResolutionHeight = $ticket->screenResolution !== null + && isset($ticket->screenResolution[1]) + && $ticket->screenResolution[1] !== null ? intval($ticket->screenResolution[1]) : ''; + + $sql = "INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` + ( + `trackid`, + `name`, + `email`, + `category`, + `priority`, + `subject`, + `message`, + `dt`, + `lastchange`, + `articles`, + `ip`, + `language`, + `openedby`, + `owner`, + `attachments`, + `merged`, + `status`, + `latitude`, + `longitude`, + `html`, + `user_agent`, + `screen_resolution_height`, + `screen_resolution_width`, + `due_date`, + `history` + {$customWhere} + ) + VALUES + ( + '" . hesk_dbEscape($ticket->trackingId) . "', + '" . hesk_dbEscape($ticket->name) . "', + '" . hesk_dbEscape($ticket->email) . "', + '" . intval($ticket->categoryId) . "', + '" . intval($ticket->priorityId) . "', + '" . hesk_dbEscape($ticket->subject) . "', + '" . hesk_dbEscape($ticket->message) . "', + NOW(), + NOW(), + " . $suggestedArticles . ", + '" . hesk_dbEscape($ticket->ipAddress) . "', + '" . hesk_dbEscape($language) . "', + '" . intval($ticket->openedBy) . "', + '" . intval($ticket->ownerId) . "', + '" . hesk_dbEscape($ticket->getAttachmentsForDatabase()) . "', + '', + '" . intval($ticket->statusId) . "', + '" . hesk_dbEscape($latitude) . "', + '" . hesk_dbEscape($longitude) . "', + '" . hesk_dbEscape($ticket->usesHtml) . "', + '" . hesk_dbEscape($userAgent) . "', + " . hesk_dbEscape($screenResolutionHeight) . ", + " . hesk_dbEscape($screenResolutionWidth) . ", + {$dueDate}, + '" . hesk_dbEscape($ticket->auditTrailHtml) . "' + {$customWhat} + ) + "; + } } \ No newline at end of file