2017-01-30 22:10:14 -05:00
< ? php
namespace DataAccess\Tickets ;
2017-04-15 22:38:46 -04:00
use BusinessLogic\Attachments\AttachmentType ;
2017-03-21 22:11:19 -04:00
use BusinessLogic\Tickets\Attachment ;
2017-01-31 22:26:46 -05:00
use BusinessLogic\Tickets\Ticket ;
2017-02-13 12:52:43 -05:00
use BusinessLogic\Tickets\TicketGatewayGeneratedFields ;
2017-01-31 22:26:46 -05:00
use DataAccess\CommonDao ;
class TicketGateway extends CommonDao {
2017-02-11 22:07:10 -05:00
/**
* @ param $id int
* @ param $heskSettings array
* @ return Ticket | null
*/
2017-01-31 22:26:46 -05:00
function getTicketById ( $id , $heskSettings ) {
$this -> init ();
$rs = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets` WHERE `id` = " . intval ( $id ));
2017-02-11 22:07:10 -05:00
if ( hesk_dbNumRows ( $rs ) === 0 ) {
return null ;
}
2017-01-31 22:26:46 -05:00
$row = hesk_dbFetchAssoc ( $rs );
2017-02-11 22:07:10 -05:00
$linkedTicketsRs = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets` WHERE `parent` = " . intval ( $id ));
2017-01-31 22:26:46 -05:00
2017-02-28 22:03:08 -05:00
$repliesRs = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " replies` WHERE `replyto` = " . intval ( $id ) . " ORDER BY `id` ASC " );
$ticket = Ticket :: fromDatabaseRow ( $row , $linkedTicketsRs , $repliesRs , $heskSettings );
2017-01-31 22:26:46 -05:00
$this -> close ();
return $ticket ;
}
2017-02-11 22:07:10 -05:00
/**
* @ param $emailAddress string
* @ param $heskSettings array
* @ return array | null
*/
2017-01-30 22:10:14 -05:00
function getTicketsByEmail ( $emailAddress , $heskSettings ) {
2017-02-11 22:07:10 -05:00
$this -> init ();
2017-01-30 22:10:14 -05:00
$rs = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets`
WHERE `email` = '" . hesk_dbEscape($emailAddress) . "' " );
2017-02-11 22:07:10 -05:00
if ( hesk_dbNumRows ( $rs ) === 0 ) {
return null ;
}
2017-01-31 22:26:46 -05:00
$tickets = array ();
while ( $row = hesk_dbFetchAssoc ( $rs )) {
2017-02-11 22:07:10 -05:00
$linkedTicketsRs =
hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets` WHERE `parent` = " . intval ( $row [ 'id' ]));
2017-04-23 12:00:05 -04:00
$repliesRs = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " replies` WHERE `replyto` = " . intval ( $row [ 'id' ]) . " ORDER BY `id` ASC " );
2017-01-30 22:10:14 -05:00
2017-04-15 22:38:46 -04:00
$tickets [] = Ticket :: fromDatabaseRow ( $row , $linkedTicketsRs , $repliesRs , $heskSettings );
2017-01-31 22:26:46 -05:00
}
2017-02-11 22:07:10 -05:00
$this -> close ();
return $tickets ;
}
2017-05-01 22:05:21 -04:00
/**
* @ param $trackingId string
* @ param $heskSettings array
* @ return bool
*/
function doesTicketExist ( $trackingId , $heskSettings ) {
$this -> init ();
$rs = hesk_dbQuery ( " SELECT 1 FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets`
WHERE `trackid` = '" . hesk_dbEscape($trackingId) . "' " );
$ticketExists = hesk_dbNumRows ( $rs ) > 0 ;
$this -> close ();
return $ticketExists ;
}
2017-02-11 22:07:10 -05:00
/**
* @ param $trackingId string
* @ param $heskSettings array
* @ return Ticket | null
*/
function getTicketByTrackingId ( $trackingId , $heskSettings ) {
$this -> init ();
2017-04-23 12:00:05 -04:00
$rs = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets` WHERE `trackid` = " . intval ( $trackingId ));
2017-02-11 22:07:10 -05:00
if ( hesk_dbNumRows ( $rs ) === 0 ) {
return null ;
}
$row = hesk_dbFetchAssoc ( $rs );
$linkedTicketsRs = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets` WHERE `parent` = " . intval ( $trackingId ));
2017-04-23 12:00:05 -04:00
$repliesRs = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " replies` WHERE `replyto` = " . intval ( $row [ 'id' ]) . " ORDER BY `id` ASC " );
2017-02-11 22:07:10 -05:00
2017-04-15 22:38:46 -04:00
$ticket = Ticket :: fromDatabaseRow ( $row , $linkedTicketsRs , $repliesRs , $heskSettings );
2017-02-11 22:07:10 -05:00
$this -> close ();
return $ticket ;
2017-01-30 22:10:14 -05:00
}
2017-02-12 00:50:30 -05:00
2017-04-24 22:11:28 -04:00
/**
* @ param $trackingId string
* @ param $heskSettings array
* @ return Ticket | null
*/
function getTicketByMergedTrackingId ( $trackingId , $heskSettings ) {
$this -> init ();
$rs = hesk_dbQuery ( " SELECT `trackid` FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets` WHERE `merged` LIKE '%# " . hesk_dbEscape ( $trackingId ) . " #%' " );
if ( hesk_dbNumRows ( $rs ) === 0 ) {
return null ;
}
$row = hesk_dbFetchAssoc ( $rs );
$actualTrackingId = $row [ 'trackid' ];
$this -> close ();
return $this -> getTicketByTrackingId ( $actualTrackingId , $heskSettings );
}
2017-02-12 00:50:30 -05:00
/**
* @ param $ticket Ticket
2017-02-20 22:07:39 -05:00
* @ param $isEmailVerified
2017-02-12 00:50:30 -05:00
* @ param $heskSettings
2017-02-13 12:52:43 -05:00
* @ return TicketGatewayGeneratedFields
2017-02-12 00:50:30 -05:00
*/
2017-02-20 22:07:39 -05:00
function createTicket ( $ticket , $isEmailVerified , $heskSettings ) {
2017-02-16 21:46:47 -05:00
$this -> init ();
2017-02-12 00:50:30 -05:00
$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 ]) : '' ) . " ' " ;
}
2017-02-16 21:46:47 -05:00
$suggestedArticles = 'NULL' ;
2017-02-12 00:50:30 -05:00
if ( $ticket -> suggestedArticles !== null && ! empty ( $ticket -> suggestedArticles )) {
2017-02-16 21:46:47 -05:00
$suggestedArticles = " ' " . implode ( ',' , $ticket -> suggestedArticles ) . " ' " ;
2017-02-12 00:50:30 -05:00
}
$latitude = $ticket -> location !== null
&& isset ( $ticket -> location [ 0 ])
2017-02-16 21:46:47 -05:00
&& $ticket -> location [ 0 ] !== null ? $ticket -> location [ 0 ] : 'E-0' ;
2017-02-12 00:50:30 -05:00
$longitude = $ticket -> location !== null
&& isset ( $ticket -> location [ 1 ])
2017-02-16 21:46:47 -05:00
&& $ticket -> location [ 1 ] !== null ? $ticket -> location [ 1 ] : 'E-0' ;
2017-02-12 00:50:30 -05:00
$userAgent = $ticket -> userAgent !== null ? $ticket -> userAgent : '' ;
$screenResolutionWidth = $ticket -> screenResolution !== null
&& isset ( $ticket -> screenResolution [ 0 ])
2017-02-14 12:45:02 -05:00
&& $ticket -> screenResolution [ 0 ] !== null ? intval ( $ticket -> screenResolution [ 0 ]) : 'NULL' ;
2017-02-12 00:50:30 -05:00
$screenResolutionHeight = $ticket -> screenResolution !== null
&& isset ( $ticket -> screenResolution [ 1 ])
2017-02-14 12:45:02 -05:00
&& $ticket -> screenResolution [ 1 ] !== null ? intval ( $ticket -> screenResolution [ 1 ]) : 'NULL' ;
2017-02-12 00:50:30 -05:00
2017-02-14 22:03:46 -05:00
$ipAddress = $ticket -> ipAddress !== null
&& $ticket -> ipAddress !== '' ? $ticket -> ipAddress : '' ;
2017-02-20 22:07:39 -05:00
$tableName = $isEmailVerified ? 'tickets' : 'stage_tickets' ;
$sql = " INSERT INTO ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . $tableName . " `
2017-02-12 00:50:30 -05:00
(
`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 (),
2017-02-16 21:46:47 -05:00
" . $suggestedArticles . " ,
'" . hesk_dbEscape($ipAddress) . "' ,
2017-02-14 22:03:46 -05:00
'" . hesk_dbEscape($ticket->language) . "' ,
2017-02-12 00:50:30 -05:00
'" . intval($ticket->openedBy) . "' ,
'" . intval($ticket->ownerId) . "' ,
'" . hesk_dbEscape($ticket->getAttachmentsForDatabase()) . "' ,
'' ,
2017-02-14 12:45:02 -05:00
" . intval( $ticket->statusId ) . " ,
2017-02-12 00:50:30 -05:00
'" . 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 }
)
" ;
2017-02-12 22:10:42 -05:00
2017-02-13 12:52:43 -05:00
hesk_dbQuery ( $sql );
2017-02-16 21:46:47 -05:00
$id = hesk_dbInsertID ();
2017-02-13 12:52:43 -05:00
2017-02-20 22:07:39 -05:00
$rs = hesk_dbQuery ( 'SELECT `dt`, `lastchange` FROM `' . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . $tableName . '` WHERE `id` = ' . intval ( $id ));
2017-02-14 12:45:02 -05:00
$row = hesk_dbFetchAssoc ( $rs );
2017-02-13 12:52:43 -05:00
$generatedFields = new TicketGatewayGeneratedFields ();
2017-02-16 21:46:47 -05:00
$generatedFields -> id = $id ;
2017-02-13 12:52:43 -05:00
$generatedFields -> dateCreated = $row [ 'dt' ];
$generatedFields -> dateModified = $row [ 'lastchange' ];
2017-02-16 21:46:47 -05:00
$this -> close ();
2017-02-13 12:52:43 -05:00
return $generatedFields ;
2017-02-12 00:50:30 -05:00
}
2017-03-21 22:11:19 -04:00
/**
* @ param $ticketId int
* @ param $attachments Attachment []
* @ param $heskSettings array
*
* Crappy logic that should just be pulled from the attachments table , but using for backwards compatibility
*/
function updateAttachmentsForTicket ( $ticketId , $attachments , $heskSettings ) {
$this -> init ();
2017-04-15 22:38:46 -04:00
$this -> updateAttachmentsFor ( $ticketId , $attachments , AttachmentType :: MESSAGE , $heskSettings );
$this -> close ();
}
2017-03-21 22:11:19 -04:00
2017-04-15 22:38:46 -04:00
private function updateAttachmentsFor ( $id , $attachments , $attachmentType , $heskSettings ) {
2017-03-21 22:11:19 -04:00
$attachmentStrings = array ();
foreach ( $attachments as $attachment ) {
$attachmentStrings [] = " { $attachment -> id } # { $attachment -> fileName } # { $attachment -> savedName } " ;
}
$attachmentStringToSave = implode ( ',' , $attachmentStrings );
2017-04-15 22:38:46 -04:00
$tableName = $attachmentType == AttachmentType :: MESSAGE ? 'tickets' : 'replies' ;
hesk_dbQuery ( " UPDATE ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . $tableName . " `
2017-03-21 22:11:19 -04:00
SET `attachments` = '" . hesk_dbEscape($attachmentStringToSave) . "'
2017-04-15 22:38:46 -04:00
WHERE `id` = " . intval( $id ));
}
/**
* @ param $replyId int
* @ param $attachments Attachment []
* @ param $heskSettings array
*
* Crappy logic that should just be pulled from the attachments table , but using for backwards compatibility
*/
function updateAttachmentsForReply ( $replyId , $attachments , $heskSettings ) {
$this -> init ();
$this -> updateAttachmentsFor ( $replyId , $attachments , AttachmentType :: REPLY , $heskSettings );
$this -> close ();
}
2017-04-23 12:00:05 -04:00
function deleteRepliesForTicket ( $ticketId , $heskSettings ) {
$this -> init ();
hesk_dbQuery ( " DELETE FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " replies` WHERE `replyto` = " . intval ( $ticketId ));
$this -> close ();
}
function deleteReplyDraftsForTicket ( $ticketId , $heskSettings ) {
$this -> init ();
hesk_dbQuery ( " DELETE FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " reply_drafts` WHERE `ticket`= " . intval ( $ticketId ));
$this -> close ();
}
function deleteNotesForTicket ( $ticketId , $heskSettings ) {
$this -> init ();
hesk_dbQuery ( " DELETE FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " notes` WHERE `ticket`=' " . intval ( $ticketId ) . " ' " );
$this -> close ();
}
2017-04-15 22:38:46 -04:00
/**
* @ param $ticketId int
* @ param $heskSettings array
*/
function deleteTicket ( $ticketId , $heskSettings ) {
2017-04-23 22:08:48 -04:00
$this -> init ();
2017-04-15 22:38:46 -04:00
hesk_dbQuery ( " DELETE FROM ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets` WHERE `id` = " . intval ( $ticketId ));
2017-04-23 22:08:48 -04:00
$this -> close ();
2017-03-21 22:11:19 -04:00
}
2017-04-27 22:04:28 -04:00
/**
* @ param $ticket Ticket
* @ param $heskSettings array
*/
function updateBasicTicketInfo ( $ticket , $heskSettings ) {
$this -> init ();
// Escaped vars
$subject = hesk_dbEscape ( $ticket -> subject );
$message = hesk_dbEscape ( $ticket -> message );
$language = hesk_dbEscape ( $ticket -> language );
$name = hesk_dbEscape ( $ticket -> name );
$email = hesk_dbEscape ( $ticket -> email );
// Prepare SQL for custom fields
$customSql = '' ;
for ( $i = 1 ; $i <= 50 ; $i ++ )
{
$customSql .= " , `custom { $i } ` = ' " . ( isset ( $ticket -> customFields [ $i ]) ? hesk_dbEscape ( $ticket -> customFields [ $i ]) : '' ) . " ' " ;
}
hesk_dbQuery ( " UPDATE ` " . hesk_dbEscape ( $heskSettings [ 'db_pfix' ]) . " tickets`
2017-04-28 12:36:48 -04:00
SET `subject` = '{$subject}' ,
`message` = '{$message}' ,
`language` = '{$language}' ,
`name` = '{$name}' ,
`email` = '{$email}' ,
`html` = " . ( $ticket->usesHtml ? 1 : 0) . " ,
2017-04-27 22:04:28 -04:00
{ $customSql }
WHERE `id` = " . intval( $ticket->id ));
$this -> close ();
}
2017-01-30 22:10:14 -05:00
}