Sorting is now possible

merge-requests/60/head
Mike Koch 7 years ago
parent 11f9266f10
commit ab5840babe

@ -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];
$elements = $this->customNavElementGateway->getAllCustomNavElements($heskSettings);
$elementToChange = null;
foreach ($elements as $element) {
if ($element->id === intval($elementId)) {
$elementToChange = $element;
}
}
if ($direction === 'up') {
$element->sort -= 15;
if ($direction === Direction::UP) {
$elementToChange->sort -= 15;
} else {
$element->sort += 15;
$elementToChange->sort += 15;
}
$this->customNavElementGateway->saveCustomNavElement($element, $heskSettings);
$this->customNavElementGateway->saveCustomNavElement($elementToChange, $heskSettings);
$this->customNavElementGateway->resortAllElements($heskSettings);
}
}

@ -0,0 +1,9 @@
<?php
namespace BusinessLogic\Navigation;
class Direction {
const UP = 'up';
const DOWN = 'down';
}

@ -29,7 +29,7 @@ class CustomNavElementController extends InternalApiController {
/* @var $handler CustomNavElementHandler */
$handler = $applicationContext->get[CustomNavElementHandler::class];
$handler->sortCustomNavElement($id, $direction, $hesk_settings);
$handler->sortCustomNavElement(intval($id), $direction, $hesk_settings);
}
function get($id) {

@ -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));

@ -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();
}
})
});
}
Loading…
Cancel
Save