diff --git a/api/ApplicationContext.php b/api/ApplicationContext.php index 1c2c9307..09e4cdd4 100644 --- a/api/ApplicationContext.php +++ b/api/ApplicationContext.php @@ -6,9 +6,11 @@ namespace Core; use BusinessLogic\Category\CategoryRetriever; use BusinessLogic\Security\BanRetriever; use BusinessLogic\Security\UserContextBuilder; +use BusinessLogic\Tickets\TicketRetriever; use DataAccess\CategoryGateway; use DataAccess\Security\BanGateway; use DataAccess\Security\UserGateway; +use DataAccess\Tickets\TicketGateway; class ApplicationContext { public $get; @@ -20,6 +22,10 @@ class ApplicationContext { $this->get['CategoryGateway'] = new CategoryGateway(); $this->get['CategoryRetriever'] = new CategoryRetriever($this->get['CategoryGateway']); + // Tickets + $this->get['TicketGateway'] = new TicketGateway(); + $this->get['TicketRetriever'] = new TicketRetriever($this->get['TicketGateway']); + // Bans $this->get['BanGateway'] = new BanGateway(); $this->get['BanRetriever'] = new BanRetriever($this->get['BanGateway']); diff --git a/api/autoload.php b/api/autoload.php index dbc64116..d468c912 100644 --- a/api/autoload.php +++ b/api/autoload.php @@ -34,6 +34,16 @@ require_once(__DIR__ . '/businesslogic/security/BanRetriever.php'); require_once(__DIR__ . '/businesslogic/security/BannedEmail.php'); require_once(__DIR__ . '/businesslogic/security/BannedIp.php'); +// Tickets +require_once(__DIR__ . '/dao/ticket/TicketGateway.php'); +require_once(__DIR__ . '/businesslogic/ticket/Attachment.php'); +require_once(__DIR__ . '/businesslogic/ticket/Ticket.php'); +require_once(__DIR__ . '/businesslogic/ticket/CreateTicketByCustomerModel.php'); +require_once(__DIR__ . '/businesslogic/ticket/TicketValidators.php'); +require_once(__DIR__ . '/businesslogic/ticket/TicketCreator.php'); +require_once(__DIR__ . '/businesslogic/ticket/TicketRetriever.php'); +require_once(__DIR__ . '/controllers/TicketController.php'); + // Exceptions require_once(__DIR__ . '/businesslogic/exception/ApiFriendlyException.php'); require_once(__DIR__ . '/businesslogic/exception/InvalidAuthenticationTokenException.php'); diff --git a/api/businesslogic/ticket/Attachment.php b/api/businesslogic/ticket/Attachment.php new file mode 100644 index 00000000..125a8ced --- /dev/null +++ b/api/businesslogic/ticket/Attachment.php @@ -0,0 +1,27 @@ +id = $row['id']; + $ticket->trackingId = $row['trackid']; + $ticket->name = $row['name']; + $ticket->email = $row['email']; + $ticket->categoryId = $row['category']; + $ticket->priorityId = $row['priority']; + $ticket->subject = $row['subject']; + $ticket->message = $row['message']; + $ticket->dateCreated = $row['dt']; + $ticket->lastChanged = $row['lastchange']; + $ticket->firstReplyDate = $row['firstreply']; + $ticket->closedDate = $row['closedat']; + $ticket->suggestedArticles = explode(',', $row['articles']); + $ticket->ipAddress = $row['ip']; + $ticket->language = $row['language']; + $ticket->statusId = $row['status']; + $ticket->openedBy = $row['openedby']; + $ticket->firstReplyByUserId = $row['firstreplyby']; + $ticket->closedByUserId = $row['closedby']; + $ticket->numberOfReplies = $row['replies']; + $ticket->numberOfStaffReplies = $row['staffreplies']; + $ticket->ownerId = $row['owner']; + $ticket->timeWorked = $row['time_worked']; + $ticket->lastReplyBy = $row['lastreplier']; + $ticket->lastReplier = $row['replierid']; + $ticket->archived = intval($row['archive']) === 1; + $ticket->locked = intval($row['locked']) === 1; + + if (trim($row['attachments']) !== '') { + $attachments = explode(',', $row['attachments']); + $attachmentArray = array(); + foreach ($attachments as $attachment) { + $attachmentRow = explode('#', $attachment); + $attachmentModel = new Attachment(); + + $attachmentModel->id = $attachmentRow[0]; + $attachmentModel->fileName = $attachmentRow[1]; + $attachmentModel->savedName = $attachmentRow[2]; + + $attachmentArray[] = $attachmentModel; + } + $ticket->attachments = $attachmentArray; + } + + if (trim($row['merged']) !== '') { + $ticket->mergedTicketIds = explode(',', $row['merged']); + } + + $ticket->auditTrailHtml = $row['history']; + + $ticket->customFields = array(); + foreach ($heskSettings['custom_fields'] as $key => $value) { + if ($value['use'] == 1 && hesk_is_custom_field_in_category($key, intval($ticket->categoryId))) { + $ticket->customFields[str_replace('custom', '', $key)] = $row[$key]; + } + } + + $ticket->linkedTicketIds = array(); + while ($linkedTicketsRow = hesk_dbFetchAssoc($linkedTicketsRs)) { + $ticket->linkedTicketIds[] = $linkedTicketsRow['id']; + } + + $ticket->location = array(); + $ticket->location[0] = $row['latitude']; + $ticket->location[1] = $row['longitude']; + + $ticket->usesHtml = intval($row['html']) === 1; + $ticket->userAgent = $row['user_agent']; + $ticket->screenResolution = array(); + $ticket->screenResolution[0] = $row['screen_resolution_width']; + $ticket->screenResolution[1] = $row['screen_resolution_height']; + + $ticket->dueDate = $row['due_date']; + $ticket->dueDateOverdueEmailSent = $row['overdue_email_sent'] !== null && intval($row['overdue_email_sent']) === 1; + + return $ticket; + } + /** * @var int */ @@ -27,12 +107,12 @@ class Ticket { /** * @var int */ - public $category; + public $categoryId; /** * @var int */ - public $priority; + public $priorityId; /** * @var string @@ -65,7 +145,7 @@ class Ticket { public $closedDate; /** - * @var string|null + * @var string[]|null */ public $suggestedArticles; @@ -85,9 +165,9 @@ class Ticket { public $statusId; /** - * @var int (convert to enum) + * @var int */ - public $openedByUserId; + public $openedBy; /** * @var int|null @@ -120,7 +200,12 @@ class Ticket { public $timeWorked; /** - * @var int (convert to enum) + * @var int + */ + public $lastReplyBy; + + /** + * @var int|null */ public $lastReplier; @@ -135,7 +220,7 @@ class Ticket { public $locked; /** - * @var array|null (TODO clarify this later) + * @var Attachment[]|null */ public $attachments; @@ -150,7 +235,7 @@ class Ticket { public $auditTrailHtml; /** - * @var array (TODO clarify this later) + * @var string[] */ public $customFields; @@ -160,7 +245,7 @@ class Ticket { public $linkedTicketIds; /** - * @var float[2]|null + * @var float[]|null */ public $location; @@ -175,7 +260,7 @@ class Ticket { public $userAgent; /** - * @var int[2]|null + * @var int[]|null */ public $screenResolution; diff --git a/api/businesslogic/ticket/TicketRetriever.php b/api/businesslogic/ticket/TicketRetriever.php new file mode 100644 index 00000000..0ff84fae --- /dev/null +++ b/api/businesslogic/ticket/TicketRetriever.php @@ -0,0 +1,27 @@ +ticketGateway = $ticketGateway; + } + + function getTicketById($id, $heskSettings, $userContext) { + return $this->ticketGateway->getTicketById($id, $heskSettings); + } +} \ No newline at end of file diff --git a/api/controllers/TicketController.php b/api/controllers/TicketController.php new file mode 100644 index 00000000..57125ccb --- /dev/null +++ b/api/controllers/TicketController.php @@ -0,0 +1,17 @@ +get['TicketRetriever']; + + output($ticketRetriever->getTicketById($id, $hesk_settings, $userContext)); + } +} \ No newline at end of file diff --git a/api/dao/ticket/TicketGateway.php b/api/dao/ticket/TicketGateway.php index cb0f246e..9b05e594 100644 --- a/api/dao/ticket/TicketGateway.php +++ b/api/dao/ticket/TicketGateway.php @@ -3,11 +3,33 @@ namespace DataAccess\Tickets; -class TicketGateway { +use BusinessLogic\Tickets\Ticket; +use DataAccess\CommonDao; + +class TicketGateway extends CommonDao { + function getTicketById($id, $heskSettings) { + $this->init(); + + $rs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` WHERE `id` = " . intval($id)); + $row = hesk_dbFetchAssoc($rs); + $linkedTicketsRs = hesk_dbQuery("SELECT * FROM `hesk_tickets` WHERE `parent` = " . intval($id)); + + $ticket = Ticket::fromDatabaseRow($row, $linkedTicketsRs, $heskSettings); + + $this->close(); + + return $ticket; + } + function getTicketsByEmail($emailAddress, $heskSettings) { $rs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` WHERE `email` = '" . hesk_dbEscape($emailAddress) . "'"); + $tickets = array(); + + while ($row = hesk_dbFetchAssoc($rs)) { + $ticket = new Ticket(); + } } } \ No newline at end of file diff --git a/api/index.php b/api/index.php index 5b853c41..b58ce89a 100644 --- a/api/index.php +++ b/api/index.php @@ -83,6 +83,8 @@ Link::all(array( // Categories '/v1/categories' => '\Controllers\Category\CategoryController::printAllCategories', '/v1/categories/{i}' => '\Controllers\Category\CategoryController', + // Tickets + '/v1/tickets/{i}' => '\Controllers\Tickets\TicketController', // Any URL that doesn't match goes to the 404 handler '404' => 'handle404'