From 45726bd388fdf1a69db575f23f9e78dd8390c027 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Tue, 5 Sep 2017 13:06:56 -0400 Subject: [PATCH] Categories can be sorted --- admin/manage_categories.php | 233 ------------------ .../Categories/CategoryHandler.php | 27 ++ .../Categories/CategoryController.php | 9 + api/index.php | 1 + internal-api/js/manage-categories.js | 24 ++ language/en/text.php | 1 + 6 files changed, 62 insertions(+), 233 deletions(-) diff --git a/admin/manage_categories.php b/admin/manage_categories.php index 30b89afa..0acb4d09 100644 --- a/admin/manage_categories.php +++ b/admin/manage_categories.php @@ -445,237 +445,4 @@ echo mfh_get_hidden_fields_for_language(array( require_once(HESK_PATH . 'inc/footer.inc.php'); exit(); - -function new_cat() -{ - global $hesk_settings, $hesklang; - - /* A security check */ - hesk_token_check('POST'); - - /* Options */ - $_SESSION['cat_autoassign'] = hesk_POST('autoassign') == 'Y' ? 1 : 0; - $_SESSION['cat_type'] = hesk_POST('type') == 'Y' ? 1 : 0; - - // Default priority - $_SESSION['cat_priority'] = intval(hesk_POST('priority', 3)); - if ($_SESSION['cat_priority'] < 0 || $_SESSION['cat_priority'] > 3) { - $_SESSION['cat_priority'] = 3; - } - - /* Category name */ - $catname = hesk_input(hesk_POST('name'), $hesklang['enter_cat_name'], 'manage_categories.php'); - - $background_color = hesk_POST('background-color', '#ffffff'); - $foreground_color = hesk_POST('foreground-color', '#000000'); - $display_border = hesk_POST('display-border', 0); - if ($foreground_color == '') { - $foreground_color = 'AUTO'; - $display_border = 0; - } - - $usage = hesk_POST('usage', 0); - - /* Do we already have a category with this name? */ - $res = hesk_dbQuery("SELECT `id` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` WHERE `name` LIKE '" . hesk_dbEscape(hesk_dbLike($catname)) . "' LIMIT 1"); - if (hesk_dbNumRows($res) != 0) { - $_SESSION['catname'] = $catname; - hesk_process_messages($hesklang['cndupl'], 'manage_categories.php'); - } - - /* Get the latest cat_order */ - $res = hesk_dbQuery("SELECT `cat_order` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` ORDER BY `cat_order` DESC LIMIT 1"); - $row = hesk_dbFetchRow($res); - $my_order = $row[0] + 10; - - hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` - (`name`,`cat_order`,`autoassign`,`type`, `priority`, `background_color`, `foreground_color`, `display_border_outline`, `usage`) VALUES - ('" . hesk_dbEscape($catname) . "','" . intval($my_order) . "','" . intval($_SESSION['cat_autoassign']) . "', - '" . intval($_SESSION['cat_type']) . "','{$_SESSION['cat_priority']}', '" . hesk_dbEscape($background_color) . "', - '" . hesk_dbEscape($foreground_color) . "', '" . intval($display_border) . "', " . intval($usage) . ")"); - - hesk_cleanSessionVars('catname'); - hesk_cleanSessionVars('cat_autoassign'); - hesk_cleanSessionVars('cat_type'); - hesk_cleanSessionVars('cat_priority'); - - $_SESSION['selcat2'] = hesk_dbInsertID(); - - hesk_process_messages(sprintf($hesklang['cat_name_added'], '' . stripslashes($catname) . ''), 'manage_categories.php', 'SUCCESS'); -} // End new_cat() - - -function update_category() -{ - global $hesk_settings, $hesklang; - - /* A security check */ - hesk_token_check('POST'); - - $_SERVER['PHP_SELF'] = 'manage_categories.php?catid=' . intval(hesk_POST('catid')); - - $catid = hesk_isNumber(hesk_POST('id'), $hesklang['choose_cat_ren'], $_SERVER['PHP_SELF']); - $_SESSION['selcat'] = $catid; - $_SESSION['selcat2'] = $catid; - - $catname = hesk_input(hesk_POST('name'), $hesklang['cat_ren_name'], $_SERVER['PHP_SELF']); - $_SESSION['catname2'] = $catname; - - $background_color = hesk_POST('background-color', '#ffffff'); - $foreground_color = hesk_POST('foreground-color', '#000000'); - $display_border = hesk_POST('display-border', 0); - if ($foreground_color == '') { - $foreground_color = 'AUTO'; - $display_border = 0; - } - $manager = hesk_POST('manager', 0); - $priority = hesk_POST('priority', 0); - $usage = hesk_POST('usage', 0); - - - hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` SET `name`='" . hesk_dbEscape($catname) . "', - `priority` = '" . hesk_dbEscape($priority) . "', - `manager` = " . intval($manager) . ", - `background_color` = '" . hesk_dbEscape($background_color) . "', - `foreground_color` = '" . hesk_dbEscape($foreground_color) . "', - `display_border_outline` = '" . intval($display_border) . "', - `usage` = " . intval($usage) . " - WHERE `id`='" . intval($catid) . "'"); - - unset($_SESSION['selcat']); - unset($_SESSION['catname2']); - - hesk_process_messages(sprintf($hesklang['category_updated'], stripslashes($catname)), $_SERVER['PHP_SELF'], 'SUCCESS'); -} // End rename_cat() - - -function remove() -{ - global $hesk_settings, $hesklang; - - /* A security check */ - hesk_token_check(); - - $_SERVER['PHP_SELF'] = 'manage_categories.php'; - - $mycat = intval(hesk_GET('catid')) or hesk_error($hesklang['no_cat_id']); - if ($mycat == 1) { - hesk_process_messages($hesklang['cant_del_default_cat'], $_SERVER['PHP_SELF']); - } - - hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` WHERE `id`='" . intval($mycat) . "'"); - if (hesk_dbAffectedRows() != 1) { - hesk_error("$hesklang[int_error]: $hesklang[cat_not_found]."); - } - - hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "tickets` SET `category`=1 WHERE `category`='" . intval($mycat) . "'"); - - hesk_process_messages($hesklang['cat_removed_db'], $_SERVER['PHP_SELF'], 'SUCCESS'); -} // End remove() - - -function order_cat() -{ - global $hesk_settings, $hesklang; - - /* A security check */ - hesk_token_check(); - - $catid = intval(hesk_GET('catid')) or hesk_error($hesklang['cat_move_id']); - $_SESSION['selcat2'] = $catid; - - $cat_move = intval(hesk_GET('move')); - - hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` SET `cat_order`=`cat_order`+" . intval($cat_move) . " WHERE `id`='" . intval($catid) . "'"); - if (hesk_dbAffectedRows() != 1) { - hesk_error("$hesklang[int_error]: $hesklang[cat_not_found]."); - } - - /* Update all category fields with new order */ - $res = hesk_dbQuery("SELECT `id` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` ORDER BY `cat_order` ASC"); - - $i = 10; - while ($mycat = hesk_dbFetchAssoc($res)) { - hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` SET `cat_order`=" . intval($i) . " WHERE `id`='" . intval($mycat['id']) . "'"); - $i += 10; - } - - header('Location: manage_categories.php'); - exit(); -} // End order_cat() - - -function toggle_autoassign() -{ - global $hesk_settings, $hesklang; - - /* A security check */ - hesk_token_check(); - - $catid = intval(hesk_GET('catid')) or hesk_error($hesklang['cat_move_id']); - $_SESSION['selcat2'] = $catid; - - if (intval(hesk_GET('s'))) { - $autoassign = 1; - $tmp = $hesklang['caaon']; - } else { - $autoassign = 0; - $tmp = $hesklang['caaoff']; - } - - /* Update auto-assign settings */ - $res = hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` SET `autoassign`='" . intval($autoassign) . "' WHERE `id`='" . intval($catid) . "'"); - if (hesk_dbAffectedRows() != 1) { - hesk_process_messages($hesklang['int_error'] . ': ' . $hesklang['cat_not_found'], './manage_categories.php'); - } - - hesk_process_messages($tmp, './manage_categories.php', 'SUCCESS'); - -} // End toggle_autoassign() - - -function toggle_type() -{ - global $hesk_settings, $hesklang; - - /* A security check */ - hesk_token_check(); - - $catid = intval(hesk_GET('catid')) or hesk_error($hesklang['cat_move_id']); - $_SESSION['selcat2'] = $catid; - - if (intval(hesk_GET('s'))) { - $type = 1; - $tmp = $hesklang['cpriv']; - } else { - $type = 0; - $tmp = $hesklang['cpub']; - } - - /* Update auto-assign settings */ - hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` SET `type`='{$type}' WHERE `id`='" . intval($catid) . "'"); - if (hesk_dbAffectedRows() != 1) { - hesk_process_messages($hesklang['int_error'] . ': ' . $hesklang['cat_not_found'], './manage_categories.php'); - } - - hesk_process_messages($tmp, './manage_categories.php', 'SUCCESS'); - -} // End toggle_type() - -function get_manager($user_id, $user_array) { - global $hesklang; - - if ($user_id == 0) { - return $hesklang['no_manager']; - } - - foreach ($user_array as $user) { - if ($user['id'] == $user_id) { - return $user['name']; - } - } - - return 'Error!'; -} - ?> diff --git a/api/BusinessLogic/Categories/CategoryHandler.php b/api/BusinessLogic/Categories/CategoryHandler.php index 87020fe1..2df16882 100644 --- a/api/BusinessLogic/Categories/CategoryHandler.php +++ b/api/BusinessLogic/Categories/CategoryHandler.php @@ -5,6 +5,7 @@ namespace BusinessLogic\Categories; use BusinessLogic\Exceptions\AccessViolationException; use BusinessLogic\Exceptions\ValidationException; +use BusinessLogic\Navigation\Direction; use BusinessLogic\Security\PermissionChecker; use BusinessLogic\Security\UserPrivilege; use BusinessLogic\ValidationModel; @@ -151,4 +152,30 @@ class CategoryHandler { $this->categoryGateway->deleteCategory($id, $heskSettings); $this->categoryGateway->resortAllCategories($heskSettings); } + + function sortCategory($id, $direction, $heskSettings) { + $modsForHeskSettings = $this->modsForHeskSettingsGateway->getAllSettings($heskSettings); + + $categories = $this->categoryGateway->getAllCategories($heskSettings, $modsForHeskSettings); + $category = null; + foreach ($categories as $innerCategory) { + if ($innerCategory->id === intval($id)) { + $category = $innerCategory; + break; + } + } + + if ($category === null) { + throw new \Exception("Could not find category with ID {$id}!"); + } + + if ($direction === Direction::UP) { + $category->catOrder -= 15; + } else { + $category->catOrder += 15; + } + + $this->categoryGateway->updateCategory($category, $heskSettings); + $this->categoryGateway->resortAllCategories($heskSettings); + } } \ No newline at end of file diff --git a/api/Controllers/Categories/CategoryController.php b/api/Controllers/Categories/CategoryController.php index 902f7250..8daa326f 100644 --- a/api/Controllers/Categories/CategoryController.php +++ b/api/Controllers/Categories/CategoryController.php @@ -98,4 +98,13 @@ class CategoryController { return http_response_code(204); } + + static function sort($id, $direction) { + global $applicationContext, $hesk_settings; + + /* @var $handler CategoryHandler */ + $handler = $applicationContext->get[CategoryHandler::class]; + + $handler->sortCategory(intval($id), $direction, $hesk_settings); + } } \ No newline at end of file diff --git a/api/index.php b/api/index.php index ffaf590d..05dba913 100644 --- a/api/index.php +++ b/api/index.php @@ -190,6 +190,7 @@ Link::all(array( '/v1/categories/all' => action(\Controllers\Categories\CategoryController::class . '::printAllCategories', [RequestMethod::GET], SecurityHandler::INTERNAL_OR_AUTH_TOKEN), '/v1/categories' => action(\Controllers\Categories\CategoryController::class, [RequestMethod::POST], SecurityHandler::INTERNAL_OR_AUTH_TOKEN), '/v1/categories/{i}' => action(\Controllers\Categories\CategoryController::class, [RequestMethod::GET, RequestMethod::PUT, RequestMethod::DELETE], SecurityHandler::INTERNAL_OR_AUTH_TOKEN), + '/v1-internal/categories/{i}/sort/{s}' => action(\Controllers\Categories\CategoryController::class . '::sort', [RequestMethod::POST], SecurityHandler::INTERNAL), // Tickets '/v1/tickets' => action(\Controllers\Tickets\CustomerTicketController::class), // Tickets - Staff diff --git a/internal-api/js/manage-categories.js b/internal-api/js/manage-categories.js index 7b426270..767be236 100644 --- a/internal-api/js/manage-categories.js +++ b/internal-api/js/manage-categories.js @@ -8,6 +8,7 @@ $(document).ready(function() { bindDeleteButton(); bindCreateModal(); bindGenerateLinkModal(); + bindSortButtons(); }); @@ -349,4 +350,27 @@ function bindGenerateLinkModal() { $modal.find('input[type="text"]').val(url).end().modal('show'); }); +} + +function bindSortButtons() { + $(document).on('click', '[data-action="sort"]', function() { + $('#overlay').show(); + var heskUrl = $('p#hesk-path').text(); + var direction = $(this).data('direction'); + var element = categories[$(this).parent().parent().parent().find('[data-property="id"]').text()]; + + $.ajax({ + method: 'POST', + url: heskUrl + 'api/index.php/v1-internal/categories/' + element.id + '/sort/' + direction, + headers: { 'X-Internal-Call': true }, + success: function() { + loadTable(); + }, + error: function(data) { + mfhAlert.errorWithLog(mfhLang.text('error_sorting_categories'), data.responseJSON); + console.error(data); + $('#overlay').hide(); + } + }) + }); } \ No newline at end of file diff --git a/language/en/text.php b/language/en/text.php index fab88193..a58ba6fe 100644 --- a/language/en/text.php +++ b/language/en/text.php @@ -2177,6 +2177,7 @@ $hesklang['error_deleting_category'] = 'An error occurred when trying to delete $hesklang['cat_private'] = 'Private'; $hesklang['cat_public'] = 'Public'; $hesklang['cat_name_description'] = 'Name / Description'; +$hesklang['error_sorting_categories'] = 'An error occurred sorting categories!'; // DO NOT CHANGE BELOW if (!defined('IN_SCRIPT')) die('PHP syntax OK!');