From ab5840babea5247f32ec929357bf5c315cb4e0af Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Sun, 21 May 2017 12:03:05 -0400 Subject: [PATCH] Sorting is now possible --- .../Navigation/CustomNavElementHandler.php | 27 ++++++++++++------- api/BusinessLogic/Navigation/Direction.php | 9 +++++++ .../Navigation/CustomNavElementController.php | 2 +- .../Navigation/CustomNavElementGateway.php | 7 +++-- internal-api/js/manage-custom-nav-elements.js | 24 +++++++++++++++++ 5 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 api/BusinessLogic/Navigation/Direction.php diff --git a/api/BusinessLogic/Navigation/CustomNavElementHandler.php b/api/BusinessLogic/Navigation/CustomNavElementHandler.php index 010bc4a3..fea67554 100644 --- a/api/BusinessLogic/Navigation/CustomNavElementHandler.php +++ b/api/BusinessLogic/Navigation/CustomNavElementHandler.php @@ -22,8 +22,10 @@ class CustomNavElementHandler { function getCustomNavElement($id, $heskSettings) { $elements = $this->getAllCustomNavElements($heskSettings); - if (isset($elements[$id])) { - return $elements[$id]; + foreach ($elements as $element) { + if ($element->id === intval($id)) { + return output($element); + } } throw new ApiFriendlyException("Custom nav element {$id} not found!", "Element Not Found", 404); @@ -47,15 +49,22 @@ class CustomNavElementHandler { function sortCustomNavElement($elementId, $direction, $heskSettings) { /* @var $element CustomNavElement */ - $element = $this->customNavElementGateway->getAllCustomNavElements($heskSettings)[$elementId]; - - if ($direction === 'up') { - $element->sort -= 15; - } else { - $element->sort += 15; + $elements = $this->customNavElementGateway->getAllCustomNavElements($heskSettings); + $elementToChange = null; + foreach ($elements as $element) { + if ($element->id === intval($elementId)) { + $elementToChange = $element; + } } - $this->customNavElementGateway->saveCustomNavElement($element, $heskSettings); + + if ($direction === Direction::UP) { + $elementToChange->sort -= 15; + } else { + $elementToChange->sort += 15; + } + + $this->customNavElementGateway->saveCustomNavElement($elementToChange, $heskSettings); $this->customNavElementGateway->resortAllElements($heskSettings); } } \ No newline at end of file diff --git a/api/BusinessLogic/Navigation/Direction.php b/api/BusinessLogic/Navigation/Direction.php new file mode 100644 index 00000000..f14a00cf --- /dev/null +++ b/api/BusinessLogic/Navigation/Direction.php @@ -0,0 +1,9 @@ +get[CustomNavElementHandler::class]; - $handler->sortCustomNavElement($id, $direction, $hesk_settings); + $handler->sortCustomNavElement(intval($id), $direction, $hesk_settings); } function get($id) { diff --git a/api/DataAccess/Navigation/CustomNavElementGateway.php b/api/DataAccess/Navigation/CustomNavElementGateway.php index 81de0679..6ecfbe5e 100644 --- a/api/DataAccess/Navigation/CustomNavElementGateway.php +++ b/api/DataAccess/Navigation/CustomNavElementGateway.php @@ -10,7 +10,8 @@ class CustomNavElementGateway extends CommonDao { function getAllCustomNavElements($heskSettings) { $this->init(); - $columns = '`t1`.`id`, `t1`.`image_url`, `t1`.`font_icon`, `t1`.`place`, `t1`.`url`, `t2`.`language`, `t2`.`text`, `t2`.`subtext`'; + $columns = '`t1`.`id`, `t1`.`image_url`, `t1`.`font_icon`, `t1`.`place`, `t1`.`url`, `t1`.`sort`, + `t2`.`language`, `t2`.`text`, `t2`.`subtext`'; $rs = hesk_dbQuery("SELECT {$columns} FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element` AS `t1` INNER JOIN `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element_to_text` AS `t2` @@ -26,7 +27,7 @@ class CustomNavElementGateway extends CommonDao { $id = intval($row['id']); if ($previousId !== $id) { if ($element !== null) { - $elements[$element->id] = $element; + $elements[] = $element; } $element = new CustomNavElement(); $element->id = $id; @@ -34,6 +35,7 @@ class CustomNavElementGateway extends CommonDao { $element->imageUrl = $row['image_url']; $element->fontIcon = $row['font_icon']; $element->url = $row['url']; + $element->sort = $row['sort']; $element->text = array(); $element->subtext = array(); } @@ -105,6 +107,7 @@ class CustomNavElementGateway extends CommonDao { SET `image_url` = {$imageUrl}, `font_icon` = {$fontIcon}, `url` = '" . hesk_dbEscape($element->url) . "', + `sort` = " . intval($element->sort) . ", `place` = " . intval($element->place) . " WHERE `id` = " . intval($element->id)); diff --git a/internal-api/js/manage-custom-nav-elements.js b/internal-api/js/manage-custom-nav-elements.js index 876fd2b5..328fd9c6 100644 --- a/internal-api/js/manage-custom-nav-elements.js +++ b/internal-api/js/manage-custom-nav-elements.js @@ -5,6 +5,7 @@ $(document).ready(function() { bindEditModal(); bindCreateModal(); bindDeleteButton(); + bindSortButtons(); $('[data-toggle="nav-iconpicker"]').iconpicker({ iconset: ['fontawesome', 'octicon'], @@ -319,4 +320,27 @@ function bindDeleteButton() { } }); }); +} + +function bindSortButtons() { + $(document).on('click', '[data-action="sort"]', function() { + $('#overlay').show(); + var heskUrl = $('#heskUrl').text(); + var direction = $(this).data('direction'); + var element = elements[$(this).parent().parent().find('[data-property="id"]').text()]; + + $.ajax({ + method: 'POST', + url: heskUrl + '/api/v1-internal/custom-navigation/' + element.id + '/sort/' + direction, + headers: { 'X-Internal-Call': true }, + success: function() { + console.log('Resorted'); + loadTable(); + }, + error: function(data) { + console.error(data); + $('#overlay').hide(); + } + }) + }); } \ No newline at end of file