Add endpoints for re-ordering elements, add arrows to page

merge-requests/60/head
Mike Koch 7 years ago
parent 25922d99f3
commit add02b208d

@ -51,7 +51,7 @@ require_once(HESK_PATH . 'inc/show_admin_nav.inc.php');
<th>Text</th>
<th>Subtext</th>
<th>Image URL / Font Icon</th>
<th>Place</th>
<th>URL</th>
<th>Actions</th>
</tr>
</thead>
@ -193,7 +193,7 @@ require_once(HESK_PATH . 'inc/show_admin_nav.inc.php');
<p style="display: none" id="lang_no_custom_nav_elements_found"><?php echo $hesklang['no_custom_nav_elements_found']; ?></p>
<script type="text/html" id="nav-element-template">
<tr>
<td><span data-property="id"></span></td>
<td><span data-property="id" data-value="x"></span></td>
<td><span>
<ul data-property="text" class="list-unstyled"></ul>
</span></td>
@ -201,15 +201,24 @@ require_once(HESK_PATH . 'inc/show_admin_nav.inc.php');
<ul data-property="subtext" class="list-unstyled"></ul>
</span></td>
<td><span data-property="image-or-font"></span></td>
<td style="display: none"><span data-property="place-id"></span></td>
<td><span data-property="place"></span></td>
<td><span data-property="url"></span></td>
<td>
<a href="#" data-action="edit">
<i class="fa fa-pencil icon-link orange"
<i class="fa fa-fw fa-pencil icon-link orange"
data-toggle="tooltip" title="<?php echo $hesklang['edit']; ?>"></i>
</a>
<a href="#" data-action="sort"
data-direction="up">
<i class="fa fa-fw fa-arrow-up icon-link green"
data-toggle="tooltip" title="<?php echo $hesklang['move_up']; ?>"></i>
</a>
<a href="#" data-action="sort"
data-direction="down">
<i class="fa fa-fw fa-arrow-down icon-link green"
data-toggle="tooltip" title="<?php echo $hesklang['move_dn'] ?>"></i>
</a>
<a href="#" data-action="delete">
<i class="fa fa-times icon-link red"
<i class="fa fa-fw fa-times icon-link red"
data-toggle="tooltip" title="<?php echo $hesklang['delete']; ?>"></i>
</a>
</td>

@ -21,4 +21,10 @@ class CustomNavElement {
/* @var $place int */
public $place;
/* @var $url string */
public $url;
/* @var $sort int */
public $sort;
}

@ -31,6 +31,7 @@ class CustomNavElementHandler {
function deleteCustomNavElement($id, $heskSettings) {
$this->customNavElementGateway->deleteCustomNavElement($id, $heskSettings);
$this->customNavElementGateway->resortAllElements($heskSettings);
}
function saveCustomNavElement($element, $heskSettings) {
@ -38,6 +39,23 @@ class CustomNavElementHandler {
}
function createCustomNavElement($element, $heskSettings) {
return $this->customNavElementGateway->createCustomNavElement($element, $heskSettings);
$element = $this->customNavElementGateway->createCustomNavElement($element, $heskSettings);
$this->customNavElementGateway->resortAllElements($heskSettings);
return $element;
}
function sortCustomNavElement($elementId, $direction, $heskSettings) {
/* @var $element CustomNavElement */
$element = $this->customNavElementGateway->getAllCustomNavElements($heskSettings)[$elementId];
if ($direction === 'up') {
$element->sort -= 15;
} else {
$element->sort += 15;
}
$this->customNavElementGateway->saveCustomNavElement($element, $heskSettings);
$this->customNavElementGateway->resortAllElements($heskSettings);
}
}

@ -21,6 +21,17 @@ class CustomNavElementController extends InternalApiController {
output($handler->getAllCustomNavElements($hesk_settings));
}
static function sort($id, $direction) {
global $applicationContext, $hesk_settings;
self::staticCheckForInternalUseOnly();
/* @var $handler CustomNavElementHandler */
$handler = $applicationContext->get[CustomNavElementHandler::class];
$handler->sortCustomNavElement($id, $direction, $hesk_settings);
}
function get($id) {
global $applicationContext, $hesk_settings;
@ -81,6 +92,8 @@ class CustomNavElementController extends InternalApiController {
$element->imageUrl = Helpers::safeArrayGet($data, 'imageUrl');
$element->text = Helpers::safeArrayGet($data, 'text');
$element->subtext = Helpers::safeArrayGet($data, 'subtext');
$element->url = Helpers::safeArrayGet($data, 'url');
$element->sort = intval(Helpers::safeArrayGet($data, 'sort'));
return $element;
}

@ -117,11 +117,16 @@ class CustomNavElementGateway extends CommonDao {
function createCustomNavElement($element, $heskSettings) {
$this->init();
$rs = hesk_dbQuery("SELECT MAX(`sort`) FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element`
WHERE `place` = " . intval($element->place));
$maxSort = hesk_dbFetchAssoc($rs);
$sortValue = intval($maxSort['sort']) + 1;
$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) . ")");
(`image_url`, `font_icon`, `place`, `sort`)
VALUES ({$imageUrl}, {$fontIcon}, " . intval($element->place) . ", " . $sortValue . ")");
$element->id = hesk_dbInsertID();
@ -150,4 +155,23 @@ class CustomNavElementGateway extends CommonDao {
return $element;
}
function resortAllElements($heskSettings) {
$this->init();
$rs = hesk_dbQuery("SELECT `id` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element`
ORDER BY `place` ASC, `sort` ASC");
$sortValue = 10;
while ($row = hesk_dbFetchAssoc($rs)) {
hesk_dbQuery("UPDATE `" . hesk_dbEscape($heskSettings['db_pfix']) . "custom_nav_element`
SET `sort` = " . intval($sortValue) . "
WHERE `id` = " . intval($row['id']));
$sortValue += 10;
}
$this->close();
}
}

@ -190,6 +190,7 @@ Link::all(array(
'/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,
'/v1-internal/custom-navigation/{i}/sort/{s}' => \Controllers\Navigation\CustomNavElementController::class . '::sort',
// Any URL that doesn't match goes to the 404 handler
'404' => 'handle404'

@ -998,6 +998,7 @@ function execute310Scripts() {
image_url TEXT,
font_icon VARCHAR(200),
place INT NOT NULL,
url VARCHAR(500) NOT NULL,
sort INT NOT NULL)");
executeQuery("CREATE TABLE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "custom_nav_element_to_text`
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

@ -125,29 +125,38 @@ function loadTable(modalToClose) {
$('#table-body').append('<tr><td colspan="6" class="bg-gray"><i><b>' + places[1] + '</b></i></td></tr>');
var currentPlace = 1;
var addedElementToPlace = false;
var first = true;
var lastElement = null;
$.each(data, function() {
if (this.place !== currentPlace) {
if (!addedElementToPlace) {
$('#table-body').append('<tr><td colspan="6">' + notFoundText + '</td></tr>');
}
if (lastElement !== null) {
//-- Hide the down arrow on the last element
$('[data-value="' + lastElement.id + '"]').parent().parent()
.find('[data-direction="down"]').find('i').removeClass('fa-arrow-down');
lastElement = null;
}
$('#table-body').append('<tr><td colspan="6" class="bg-gray"><i><b>' + places[this.place] + '</b></i></td></tr>');
currentPlace = this.place;
console.log(this);
addedElementToPlace = false;
first = true;
}
var $template = $($('#nav-element-template').html());
$template.find('span[data-property="id"]').text(this.id);
$template.find('span[data-property="id"]').text(this.id).attr('data-value', this.id);
if (this.imageUrl === null) {
$template.find('span[data-property="image-or-font"]').html('<i class="' + escape(this.fontIcon) + '"></i>');
} else {
$template.find('span[data-property="image-or-font"]').text(this.imageUrl);
}
$template.find('span[data-property="place"]').text(places[this.place]);
$template.find('span[data-property="place-id"]').text(this.place);
$template.find('span[data-property="url"]').text(places[this.url]);
var text = '';
$.each(this.text, function(key, value) {
@ -164,11 +173,17 @@ function loadTable(modalToClose) {
}
$template.find('ul[data-property="subtext"]').html(subtext);
if (first) {
$template.find('[data-direction="up"]').find('i').removeClass('fa-arrow-up');
first = false;
}
$('#table-body').append($template);
elements[this.id] = this;
addedElementToPlace = true;
lastElement = this;
});
//-- Add missing headers if no elements are in them
@ -181,6 +196,12 @@ function loadTable(modalToClose) {
$('#table-body').append('<tr><td colspan="6">' + notFoundText + '</td></tr>');
}
if (lastElement) {
//-- Hide the down arrow on the last element
$('[data-value="' + lastElement.id + '"]').parent().parent()
.find('[data-direction="down"]').find('i').removeClass('fa-arrow-down');
}
if (modalToClose !== undefined) {
modalToClose.modal('hide');
}

Loading…
Cancel
Save