diff --git a/api/BusinessLogic/Navigation/CustomNavElement.php b/api/BusinessLogic/Navigation/CustomNavElement.php index d720d6b7..2e0e7261 100644 --- a/api/BusinessLogic/Navigation/CustomNavElement.php +++ b/api/BusinessLogic/Navigation/CustomNavElement.php @@ -7,10 +7,10 @@ class CustomNavElement { /* @var $id int*/ public $id; - /* @var $text string[string] */ + /* @var $text string[] */ public $text; - /* @var $subtext string[string]|null */ + /* @var $subtext string[]|null */ public $subtext; /* @var $imageUrl string|null */ diff --git a/api/BusinessLogic/Navigation/CustomNavElementHandler.php b/api/BusinessLogic/Navigation/CustomNavElementHandler.php index 0a159116..4a8cad62 100644 --- a/api/BusinessLogic/Navigation/CustomNavElementHandler.php +++ b/api/BusinessLogic/Navigation/CustomNavElementHandler.php @@ -3,6 +3,7 @@ namespace BusinessLogic\Navigation; // TODO Test! +use BusinessLogic\Exceptions\ApiFriendlyException; use DataAccess\Navigation\CustomNavElementGateway; class CustomNavElementHandler { @@ -18,15 +19,25 @@ class CustomNavElementHandler { return $this->customNavElementGateway->getAllCustomNavElements($heskSettings); } - function deleteCustomNavElement() { + function getCustomNavElement($id, $heskSettings) { + $elements = $this->getAllCustomNavElements($heskSettings); - } + if (isset($elements[$id])) { + return $elements[$id]; + } - function saveCustomNavElement() { + throw new ApiFriendlyException("Custom nav element {$id} not found!", "Element Not Found", 404); + } + function deleteCustomNavElement($id, $heskSettings) { + $this->customNavElementGateway->deleteCustomNavElement($id, $heskSettings); } - function createCustomNavElement() { + function saveCustomNavElement($element, $heskSettings) { + $this->customNavElementGateway->saveCustomNavElement($element, $heskSettings); + } + function createCustomNavElement($element, $heskSettings) { + return $this->customNavElementGateway->createCustomNavElement($element, $heskSettings); } } \ No newline at end of file diff --git a/api/Controllers/Navigation/CustomNavElementController.php b/api/Controllers/Navigation/CustomNavElementController.php index a36bfe78..2387e506 100644 --- a/api/Controllers/Navigation/CustomNavElementController.php +++ b/api/Controllers/Navigation/CustomNavElementController.php @@ -3,15 +3,85 @@ namespace Controllers\Navigation; +use BusinessLogic\Helpers; +use BusinessLogic\Navigation\CustomNavElement; use BusinessLogic\Navigation\CustomNavElementHandler; +use Controllers\InternalApiController; +use Controllers\JsonRetriever; -class CustomNavElementController { +class CustomNavElementController extends InternalApiController { static function getAll() { global $applicationContext, $hesk_settings; + self::checkForInternalUseOnly(); + /* @var $handler CustomNavElementHandler */ $handler = $applicationContext->get[CustomNavElementHandler::class]; output($handler->getAllCustomNavElements($hesk_settings)); } + + function get($id) { + global $applicationContext, $hesk_settings; + + $this->checkForInternalUseOnly(); + + /* @var $handler CustomNavElementHandler */ + $handler = $applicationContext->get[CustomNavElementHandler::class]; + + output($handler->getCustomNavElement($id, $hesk_settings)); + } + + function post() { + global $applicationContext, $hesk_settings; + + $this->checkForInternalUseOnly(); + + /* @var $handler CustomNavElementHandler */ + $handler = $applicationContext->get[CustomNavElementHandler::class]; + + $data = JsonRetriever::getJsonData(); + $element = $handler->createCustomNavElement($this->buildElementModel($data), $hesk_settings); + + return output($element, 201); + } + + function put($id) { + global $applicationContext, $hesk_settings; + + $this->checkForInternalUseOnly(); + + /* @var $handler CustomNavElementHandler */ + $handler = $applicationContext->get[CustomNavElementHandler::class]; + + $data = JsonRetriever::getJsonData(); + $handler->saveCustomNavElement($this->buildElementModel($data, $id), $hesk_settings); + + return http_response_code(204); + } + + function delete($id) { + global $applicationContext, $hesk_settings; + + $this->checkForInternalUseOnly(); + + /* @var $handler CustomNavElementHandler */ + $handler = $applicationContext->get[CustomNavElementHandler::class]; + + $handler->deleteCustomNavElement($id, $hesk_settings); + + return http_response_code(204); + } + + private function buildElementModel($data, $id = null) { + $element = new CustomNavElement(); + $element->id = $id; + $element->place = intval(Helpers::safeArrayGet($data, 'place')); + $element->fontIcon = Helpers::safeArrayGet($data, 'fontIcon'); + $element->imageUrl = Helpers::safeArrayGet($data, 'imageUrl'); + $element->text = Helpers::safeArrayGet($data, 'text'); + $element->subtext = Helpers::safeArrayGet($data, 'subtext'); + + return $element; + } } \ No newline at end of file diff --git a/api/DataAccess/Navigation/CustomNavElementGateway.php b/api/DataAccess/Navigation/CustomNavElementGateway.php index 4beaf021..77671891 100644 --- a/api/DataAccess/Navigation/CustomNavElementGateway.php +++ b/api/DataAccess/Navigation/CustomNavElementGateway.php @@ -17,13 +17,15 @@ class CustomNavElementGateway extends CommonDao { ON `t1`.`id` = `t2`.`nav_element_id`"); $elements = array(); + + /* @var $element CustomNavElement */ $element = null; $previousId = -1; while ($row = hesk_dbFetchAssoc($rs)) { $id = intval($row['id']); if ($previousId !== $id) { if ($element !== null) { - $elements[] = $element; + $elements[$element->id] = $element; } $element = new CustomNavElement(); $element->id = $id; @@ -48,4 +50,103 @@ class CustomNavElementGateway extends CommonDao { return $elements; } + + /** + * @param $id int + * @param $heskSettings array + */ + function deleteCustomNavElement($id, $heskSettings) { + $this->init(); + + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element_to_text` + WHERE `nav_element_id` = " . intval($id)); + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element` + WHERE `id` = " . intval($id)); + + $this->close(); + } + + /** + * @param $element CustomNavElement + * @param $heskSettings array + */ + function saveCustomNavElement($element, $heskSettings) { + $this->init(); + + //-- Delete previous records - easier than inserting/updating + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element_to_text` + WHERE `nav_element_id` = " . intval($element->id)); + + $languageTextAndSubtext = array(); + foreach ($element->text as $key => $text) { + $languageTextAndSubtext[$key]['text'] = $text; + } + foreach ($element->subtext as $key => $subtext) { + $languageTextAndSubtext[$key]['subtext'] = $subtext; + } + + foreach ($languageTextAndSubtext as $key => $values) { + $subtext = 'NULL'; + if (isset($values['subtext'])) { + $subtext = "'" . hesk_dbEscape($values['subtext']) . "'"; + } + hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element_to_text` + (`nav_element_id`, `language`, `text`, `subtext`) VALUES (" . intval($element->id) . ", + '" . hesk_dbEscape($key) . "', + '" . hesk_dbEscape($values['text']) . "', + " . $subtext . ")"); + } + + $imageUrl = $element->imageUrl == null ? 'NULL' : "'" . hesk_dbEscape($element->imageUrl) . "'"; + $fontIcon = $element->fontIcon == null ? 'NULL' : "'" . hesk_dbEscape($element->fontIcon) . "'"; + hesk_dbQuery("UPDATE `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element` + SET `image_url` = {$imageUrl}, + `font_icon` = {$fontIcon}, + `place` = " . intval($element->place) . + " WHERE `id` = " . intval($element->id)); + + $this->close(); + } + + /** + * @param $element CustomNavElement + * @param $heskSettings array + * @return CustomNavElement + */ + function createCustomNavElement($element, $heskSettings) { + $this->init(); + + $imageUrl = $element->imageUrl == null ? 'NULL' : "'" . hesk_dbEscape($element->imageUrl) . "'"; + $fontIcon = $element->fontIcon == null ? 'NULL' : "'" . hesk_dbEscape($element->fontIcon) . "'"; + hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element` + (`image_url`, `font_icon`, `place`) + VALUES ({$imageUrl}, {$fontIcon}, " . intval($element->place) . ")"); + + $element->id = hesk_dbInsertID(); + + + $languageTextAndSubtext = array(); + foreach ($element->text as $key => $text) { + $languageTextAndSubtext[$key]['text'] = $text; + } + foreach ($element->subtext as $key => $subtext) { + $languageTextAndSubtext[$key]['subtext'] = $subtext; + } + + foreach ($languageTextAndSubtext as $key => $values) { + $subtext = 'NULL'; + if (isset($values['subtext'])) { + $subtext = "'" . hesk_dbEscape($values['subtext']) . "'"; + } + hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element_to_text` + (`nav_element_id`, `language`, `text`, `subtext`) VALUES (" . intval($element->id) . ", + '" . hesk_dbEscape($key) . "', + '" . hesk_dbEscape($values['text']) . "', + " . $subtext . ")"); + } + + $this->close(); + + return $element; + } } \ No newline at end of file diff --git a/api/index.php b/api/index.php index 8c34cb35..9404b9c2 100644 --- a/api/index.php +++ b/api/index.php @@ -187,7 +187,9 @@ Link::all(array( // Resend email response '/v1-internal/staff/tickets/{i}/resend-email' => \Controllers\Tickets\ResendTicketEmailToCustomerController::class, // Custom Navigation - '/v1-internal/custom-navigation' => \Controllers\Navigation\CustomNavElementController::class . '::getAll', + '/v1-internal/custom-navigation/all' => \Controllers\Navigation\CustomNavElementController::class . '::getAll', + '/v1-internal/custom-navigation' => \Controllers\Navigation\CustomNavElementController::class, + '/v1-internal/custom-navigation/{i}' => \Controllers\Navigation\CustomNavElementController::class, // Any URL that doesn't match goes to the 404 handler '404' => 'handle404'