From 1cb4209be244233f4ac0a26f1ee1c9476d29c78b Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Mon, 13 Feb 2017 22:34:15 -0500 Subject: [PATCH] Working more on create ticket endpoint --- api/ApplicationContext.php | 37 ++++++++++++------ api/BusinessLogic/Helpers.php | 6 +++ .../CustomFields/CustomFieldValidator.php | 4 +- .../Tickets/NewTicketValidator.php | 12 +++--- .../Categories/CategoryController.php | 4 +- api/Controllers/JsonRetriever.php | 22 +++++++++++ api/Controllers/Tickets/TicketController.php | 39 +++++++++++++++++-- api/DataAccess/Security/BanGateway.php | 8 ++-- api/Link.php | 9 +---- api/autoload.php | 2 +- api/index.php | 5 ++- 11 files changed, 109 insertions(+), 39 deletions(-) create mode 100644 api/Controllers/JsonRetriever.php diff --git a/api/ApplicationContext.php b/api/ApplicationContext.php index aa09011c..08c26c40 100644 --- a/api/ApplicationContext.php +++ b/api/ApplicationContext.php @@ -4,7 +4,12 @@ use BusinessLogic\Categories\CategoryRetriever; use BusinessLogic\Security\BanRetriever; use BusinessLogic\Security\UserContextBuilder; +use BusinessLogic\Tickets\Autoassigner; use BusinessLogic\Tickets\TicketRetriever; +use BusinessLogic\Tickets\TicketCreator; +use BusinessLogic\Tickets\NewTicketValidator; +use BusinessLogic\Tickets\TicketValidators; +use BusinessLogic\Tickets\TrackingIdGenerator; use DataAccess\Categories\CategoryGateway; use DataAccess\Security\BanGateway; use DataAccess\Security\UserGateway; @@ -17,20 +22,30 @@ class ApplicationContext { function __construct() { $this->get = array(); - // Categories - $this->get['CategoryGateway'] = new CategoryGateway(); - $this->get['CategoryRetriever'] = new CategoryRetriever($this->get['CategoryGateway']); + // User Context + $this->get[UserGateway::class] = new UserGateway(); + $this->get[UserContextBuilder::class] = new UserContextBuilder($this->get[UserGateway::class]); - // Tickets - $this->get['TicketGateway'] = new TicketGateway(); - $this->get['TicketRetriever'] = new TicketRetriever($this->get['TicketGateway']); + // Categories + $this->get[CategoryGateway::class] = new CategoryGateway(); + $this->get[CategoryRetriever::class] = new CategoryRetriever($this->get[CategoryGateway::class]); // Bans - $this->get['BanGateway'] = new BanGateway(); - $this->get['BanRetriever'] = new BanRetriever($this->get['BanGateway']); + $this->get[BanGateway::class] = new BanGateway(); + $this->get[BanRetriever::class] = new BanRetriever($this->get[BanGateway::class]); - // User Context - $this->get['UserGateway'] = new UserGateway(); - $this->get['UserContextBuilder'] = new UserContextBuilder($this->get['UserGateway']); + // Tickets + $this->get[TicketGateway::class] = new TicketGateway(); + $this->get[TicketRetriever::class] = new TicketRetriever($this->get[TicketGateway::class]); + $this->get[TicketValidators::class] = new TicketValidators($this->get[TicketGateway::class]); + $this->get[TrackingIdGenerator::class] = new TrackingIdGenerator($this->get[TicketGateway::class]); + $this->get[Autoassigner::class] = new Autoassigner(); + $this->get[NewTicketValidator::class] = new NewTicketValidator($this->get[CategoryRetriever::class], + $this->get[BanRetriever::class], + $this->get[TicketValidators::class]); + $this->get[TicketCreator::class] = new TicketCreator($this->get[NewTicketValidator::class], + $this->get[TrackingIdGenerator::class], + $this->get[Autoassigner::class], + $this->get[TicketGateway::class]); } } \ No newline at end of file diff --git a/api/BusinessLogic/Helpers.php b/api/BusinessLogic/Helpers.php index cb5bf624..61f6af2d 100644 --- a/api/BusinessLogic/Helpers.php +++ b/api/BusinessLogic/Helpers.php @@ -20,4 +20,10 @@ class Helpers { static function hashToken($token) { return hash('sha512', $token); } + + static function safeArrayGet($array, $key) { + return $array !== null && array_key_exists($key, $array) + ? $array[$key] + : null; + } } \ No newline at end of file diff --git a/api/BusinessLogic/Tickets/CustomFields/CustomFieldValidator.php b/api/BusinessLogic/Tickets/CustomFields/CustomFieldValidator.php index 037d828e..77bbd336 100644 --- a/api/BusinessLogic/Tickets/CustomFields/CustomFieldValidator.php +++ b/api/BusinessLogic/Tickets/CustomFields/CustomFieldValidator.php @@ -18,7 +18,7 @@ class CustomFieldValidator { return false; } - return count($customField['category']) === 0 || - in_array($categoryId, $customField['category']); + return count($customField['Categories']) === 0 || + in_array($categoryId, $customField['Categories']); } } \ No newline at end of file diff --git a/api/BusinessLogic/Tickets/NewTicketValidator.php b/api/BusinessLogic/Tickets/NewTicketValidator.php index 5f6d6a23..61af85e8 100644 --- a/api/BusinessLogic/Tickets/NewTicketValidator.php +++ b/api/BusinessLogic/Tickets/NewTicketValidator.php @@ -1,10 +1,4 @@ $value) { $customFieldNumber = intval(str_replace('custom', '', $key)); + + //TODO test this + if (!array_key_exists($customFieldNumber, $ticketRequest->customFields)) { + continue; + } + if ($value['use'] == 1 && CustomFieldValidator::isCustomFieldInCategory($customFieldNumber, intval($ticketRequest->category), false, $heskSettings)) { $custom_field_value = $ticketRequest->customFields[$customFieldNumber]; if (empty($custom_field_value)) { diff --git a/api/Controllers/Categories/CategoryController.php b/api/Controllers/Categories/CategoryController.php index acbfe7bb..6265d2fd 100644 --- a/api/Controllers/Categories/CategoryController.php +++ b/api/Controllers/Categories/CategoryController.php @@ -2,7 +2,7 @@ namespace Controllers\Category; -use BusinessLogic\Category\CategoryRetriever; +use BusinessLogic\Categories\CategoryRetriever; class CategoryController { function get($id) { @@ -18,7 +18,7 @@ class CategoryController { global $hesk_settings, $applicationContext, $userContext; /* @var $categoryRetriever CategoryRetriever */ - $categoryRetriever = $applicationContext->get['CategoryRetriever']; + $categoryRetriever = $applicationContext->get[CategoryRetriever::class]; return $categoryRetriever->getAllCategories($hesk_settings, $userContext); } diff --git a/api/Controllers/JsonRetriever.php b/api/Controllers/JsonRetriever.php new file mode 100644 index 00000000..ee590448 --- /dev/null +++ b/api/Controllers/JsonRetriever.php @@ -0,0 +1,22 @@ +get['TicketRetriever']; + $ticketRetriever = $applicationContext->get[TicketRetriever::class]; output($ticketRetriever->getTicketById($id, $hesk_settings, $userContext)); } @@ -22,8 +25,38 @@ class TicketController { /* @var $ticketCreator TicketCreator */ $ticketCreator = $applicationContext->get[TicketCreator::class]; - //-- TODO Parse POST data + $jsonRequest = JsonRetriever::getJsonData(); - $ticketCreator->createTicketByCustomer(null, $hesk_settings, $modsForHeskSettings, $userContext); + $ticketCreator->createTicketByCustomer($this->buildTicketRequestFromJson($jsonRequest), $hesk_settings, $modsForHeskSettings, $userContext); + } + + /** + * @param $json array + * @return CreateTicketByCustomerModel + */ + private function buildTicketRequestFromJson($json) { + $ticketRequest = new CreateTicketByCustomerModel(); + $ticketRequest->name = Helpers::safeArrayGet($json, 'name'); + $ticketRequest->email = Helpers::safeArrayGet($json, 'email'); + $ticketRequest->category = Helpers::safeArrayGet($json, 'category'); + $ticketRequest->priority = Helpers::safeArrayGet($json, 'priority'); + $ticketRequest->subject = Helpers::safeArrayGet($json, 'subject'); + $ticketRequest->message = Helpers::safeArrayGet($json, 'message'); + $ticketRequest->html = Helpers::safeArrayGet($json, 'html'); + $ticketRequest->location = Helpers::safeArrayGet($json, 'location'); + $ticketRequest->suggestedKnowledgebaseArticleIds = Helpers::safeArrayGet($json, 'suggestedArticles'); + $ticketRequest->userAgent = Helpers::safeArrayGet($json, 'userAgent'); + $ticketRequest->screenResolution = Helpers::safeArrayGet($json, 'screenResolution'); + $ticketRequest->customFields = array(); + + $jsonCustomFields = Helpers::safeArrayGet($json, 'customFields'); + + if ($jsonCustomFields !== null && !empty($jsonCustomFields)) { + foreach ($jsonCustomFields as $key => $value) { + $ticketRequest->customFields[intval($key)] = $value; + } + } + + return $ticketRequest; } } \ No newline at end of file diff --git a/api/DataAccess/Security/BanGateway.php b/api/DataAccess/Security/BanGateway.php index 78139819..74cacdb7 100644 --- a/api/DataAccess/Security/BanGateway.php +++ b/api/DataAccess/Security/BanGateway.php @@ -19,8 +19,8 @@ class BanGateway extends CommonDao { $rs = hesk_dbQuery("SELECT `bans`.`id` AS `id`, `bans`.`email` AS `email`, `users`.`id` AS `banned_by`, `bans`.`dt` AS `dt` - FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "_banned_emails` AS `bans` - LEFT JOIN `" . hesk_dbEscape($heskSettings['db_pfix']) . "_users` AS `users` + FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "banned_emails` AS `bans` + LEFT JOIN `" . hesk_dbEscape($heskSettings['db_pfix']) . "users` AS `users` ON `bans`.`banned_by` = `users`.`id` AND `users`.`active` = '1'"); @@ -51,8 +51,8 @@ class BanGateway extends CommonDao { $rs = hesk_dbQuery("SELECT `bans`.`id` AS `id`, `bans`.`ip_from` AS `ip_from`, `bans`.`ip_to` AS `ip_to`, `bans`.`ip_display` AS `ip_display`, `users`.`id` AS `banned_by`, `bans`.`dt` AS `dt` - FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "_banned_ips` AS `bans` - LEFT JOIN `" . hesk_dbEscape($heskSettings['db_pfix']) . "_users` AS `users` + FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "banned_ips` AS `bans` + LEFT JOIN `" . hesk_dbEscape($heskSettings['db_pfix']) . "users` AS `users` ON `bans`.`banned_by` = `users`.`id` AND `users`.`active` = '1'"); diff --git a/api/Link.php b/api/Link.php index 090e666d..6ae9a884 100644 --- a/api/Link.php +++ b/api/Link.php @@ -177,14 +177,7 @@ class Link if( isset( $instanceOfHandler ) ) { if( method_exists( $instanceOfHandler, $method ) ) { - try { - $newParams = call_user_func_array( array( $instanceOfHandler, $method ), $matched ); - } catch ( Exception $exception ){ - $string = str_replace("\n", ' ', var_export($exception, TRUE)); - error_log($string); //Log to error file only if display errors has been declared - header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); - die(); - } + $newParams = call_user_func_array( array( $instanceOfHandler, $method ), $matched ); } } if( isset( $newParams ) && $newParams ) { diff --git a/api/autoload.php b/api/autoload.php index bb2c67a4..1a2db8c2 100644 --- a/api/autoload.php +++ b/api/autoload.php @@ -18,4 +18,4 @@ require_once(__DIR__ . '/../inc/custom_fields.inc.php'); // Load the ApplicationContext $applicationContext = new \ApplicationContext(); -$modsForHeskSettings = mfh_getSettings(); \ No newline at end of file +//$modsForHeskSettings = mfh_getSettings(); \ No newline at end of file diff --git a/api/index.php b/api/index.php index 27c7eeb3..07e97f30 100644 --- a/api/index.php +++ b/api/index.php @@ -28,7 +28,7 @@ function buildUserContext($xAuthToken) { global $applicationContext, $userContext, $hesk_settings; /* @var $userContextBuilder \BusinessLogic\Security\UserContextBuilder */ - $userContextBuilder = $applicationContext->get['UserContextBuilder']; + $userContextBuilder = $applicationContext->get[\BusinessLogic\Security\UserContextBuilder::class]; $userContext = $userContextBuilder->buildUserContext($xAuthToken, $hesk_settings); } @@ -52,7 +52,7 @@ function exceptionHandler($exception) { $castedException = $exception; print_error("Fought an uncaught exception", sprintf("%s\n\n%s", $castedException->failingQuery, $exception->getTraceAsString())); } else { - print_error("Fought an uncaught exception", sprintf("%s\n\n%s", $exception->getMessage(), $exception->getTraceAsString())); + print_error("Fought an uncaught exception of type " . get_class($exception), sprintf("%s\n\n%s", $exception->getMessage(), $exception->getTraceAsString())); } } @@ -85,6 +85,7 @@ Link::all(array( '/v1/categories/{i}' => '\Controllers\Category\CategoryController', // Tickets '/v1/tickets/{i}' => '\Controllers\Tickets\TicketController', + '/v1/tickets' => '\Controllers\Tickets\TicketController', // Any URL that doesn't match goes to the 404 handler '404' => 'handle404'