diff --git a/admin/manage_categories.php b/admin/manage_categories.php index 3471288a..725178ac 100644 --- a/admin/manage_categories.php +++ b/admin/manage_categories.php @@ -16,6 +16,7 @@ define('HESK_PATH', '../'); define('VALIDATOR', 1); define('PAGE_TITLE', 'ADMIN_CATEGORIES'); define('MFH_PAGE_LAYOUT', 'TOP_ONLY'); +define('EXTRA_JS', ''); /* Get all the required files and functions */ require(HESK_PATH . 'hesk_settings.inc.php'); @@ -632,6 +633,8 @@ $res = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) get[CategoryGateway::class] = new CategoryGateway(); - $this->get[CategoryRetriever::class] = new CategoryRetriever($this->get[CategoryGateway::class]); + $this->get[CategoryRetriever::class] = new CategoryRetriever($this->get[CategoryGateway::class], + $this->get[ModsForHeskSettingsGateway::class]); $this->get[CategoryHandler::class] = new CategoryHandler( $this->get[CategoryGateway::class], - $this->get[PermissionChecker::class]); + $this->get[PermissionChecker::class], + $this->get[ModsForHeskSettingsGateway::class]); // Bans $this->get[BanGateway::class] = new BanGateway(); diff --git a/api/BusinessLogic/Categories/CategoryHandler.php b/api/BusinessLogic/Categories/CategoryHandler.php index dd68a176..87020fe1 100644 --- a/api/BusinessLogic/Categories/CategoryHandler.php +++ b/api/BusinessLogic/Categories/CategoryHandler.php @@ -9,6 +9,7 @@ use BusinessLogic\Security\PermissionChecker; use BusinessLogic\Security\UserPrivilege; use BusinessLogic\ValidationModel; use DataAccess\Categories\CategoryGateway; +use DataAccess\Settings\ModsForHeskSettingsGateway; class CategoryHandler { /* @var $categoryGateway CategoryGateway */ @@ -17,19 +18,27 @@ class CategoryHandler { /* @var $permissionChecker PermissionChecker */ private $permissionChecker; - function __construct($categoryGateway, $permissionChecker) { + /* @var $modsForHeskSettingsGateway ModsForHeskSettingsGateway */ + private $modsForHeskSettingsGateway; + + function __construct($categoryGateway, $permissionChecker, $modsForHeskSettingsGateway) { $this->categoryGateway = $categoryGateway; $this->permissionChecker = $permissionChecker; + $this->modsForHeskSettingsGateway = $modsForHeskSettingsGateway; } /** * @param $category Category + * @param $userContext * @param $heskSettings array * @return Category The newly created category with ID * @throws ValidationException When validation fails + * @throws \Exception When the newly created category was not retrieved */ //TODO Test function createCategory($category, $userContext, $heskSettings) { + $modsForHeskSettings = $this->modsForHeskSettingsGateway->getAllSettings($heskSettings); + $validationModel = $this->validate($category, $userContext); if (count($validationModel->errorKeys) > 0) { @@ -38,9 +47,15 @@ class CategoryHandler { $id = $this->categoryGateway->createCategory($category, $heskSettings); - $allCategories = $this->categoryGateway->getAllCategories($heskSettings); + $allCategories = $this->categoryGateway->getAllCategories($heskSettings, $modsForHeskSettings); - return $allCategories[$id]; + foreach ($allCategories as $innerCategory) { + if ($innerCategory->id === $id) { + return $innerCategory; + } + } + + throw new \Exception("Newly created category {$id} lost! :O"); } /** @@ -99,11 +114,15 @@ class CategoryHandler { /** * @param $category Category + * @param $userContext * @param $heskSettings array * @return Category * @throws ValidationException + * @throws \Exception When the category is missing */ function editCategory($category, $userContext, $heskSettings) { + $modsForHeskSettings = $this->modsForHeskSettingsGateway->getAllSettings($heskSettings); + $validationModel = $this->validate($category, $userContext, false); if (count($validationModel->errorKeys) > 0) { @@ -113,9 +132,15 @@ class CategoryHandler { $this->categoryGateway->updateCategory($category, $heskSettings); $this->categoryGateway->resortAllCategories($heskSettings); - $allCategories = $this->categoryGateway->getAllCategories($heskSettings); + $allCategories = $this->categoryGateway->getAllCategories($heskSettings, $modsForHeskSettings); - return $allCategories[$category->id]; + foreach ($allCategories as $innerCategory) { + if ($innerCategory->id === $category->id) { + return $innerCategory; + } + } + + throw new \Exception("Category {$category->id} vanished! :O"); } function deleteCategory($id, $userContext, $heskSettings) { diff --git a/api/BusinessLogic/Categories/CategoryRetriever.php b/api/BusinessLogic/Categories/CategoryRetriever.php index 4a46466f..548765c9 100644 --- a/api/BusinessLogic/Categories/CategoryRetriever.php +++ b/api/BusinessLogic/Categories/CategoryRetriever.php @@ -4,6 +4,7 @@ namespace BusinessLogic\Categories; use BusinessLogic\Security\UserContext; use DataAccess\Categories\CategoryGateway; +use DataAccess\Settings\ModsForHeskSettingsGateway; class CategoryRetriever { /** @@ -11,8 +12,14 @@ class CategoryRetriever { */ private $categoryGateway; - function __construct($categoryGateway) { + /** + * @param $modsForHeskSettingsGateway ModsForHeskSettingsGateway + */ + private $modsForHeskSettingsGateway; + + function __construct($categoryGateway, $modsForHeskSettingsGateway) { $this->categoryGateway = $categoryGateway; + $this->modsForHeskSettingsGateway = $modsForHeskSettingsGateway; } /** @@ -21,7 +28,9 @@ class CategoryRetriever { * @return array */ function getAllCategories($heskSettings, $userContext) { - $categories = $this->categoryGateway->getAllCategories($heskSettings); + $modsForHeskSettings = $this->modsForHeskSettingsGateway->getAllSettings($heskSettings); + + $categories = $this->categoryGateway->getAllCategories($heskSettings, $modsForHeskSettings); foreach ($categories as $category) { $category->accessible = $userContext->admin || diff --git a/api/Controllers/Categories/CategoryController.php b/api/Controllers/Categories/CategoryController.php index cb63f050..53ccf22c 100644 --- a/api/Controllers/Categories/CategoryController.php +++ b/api/Controllers/Categories/CategoryController.php @@ -13,11 +13,13 @@ class CategoryController { function get($id) { $categories = self::getAllCategories(); - if (!isset($categories[$id])) { - throw new ApiFriendlyException("Category {$id} not found!", "Category Not Found", 404); + foreach ($categories as $category) { + if ($category->id === $id) { + return output($category); + } } - output($categories[$id]); + throw new ApiFriendlyException("Category {$id} not found!", "Category Not Found", 404); } static function printAllCategories() { diff --git a/api/DataAccess/Categories/CategoryGateway.php b/api/DataAccess/Categories/CategoryGateway.php index 5776ba30..2191373d 100644 --- a/api/DataAccess/Categories/CategoryGateway.php +++ b/api/DataAccess/Categories/CategoryGateway.php @@ -12,14 +12,18 @@ class CategoryGateway extends CommonDao { * @param $hesk_settings * @return Category[] */ - function getAllCategories($hesk_settings) { + function getAllCategories($hesk_settings, $modsForHesk_settings) { $this->init(); + $sortColumn = $modsForHesk_settings['category_order_column']; + $sql = 'SELECT `cat`.*, COUNT(`tickets`.`id`) AS `number_of_tickets` FROM `' . hesk_dbEscape($hesk_settings['db_pfix']) . 'categories` `cat` LEFT JOIN `' . hesk_dbEscape($hesk_settings['db_pfix']) . 'tickets` `tickets` - ON `cat`.`id` = `tickets`.`category` - GROUP BY `cat`.`id`'; + ON `cat`.`id` = `tickets`.`category` + GROUP BY `cat`.`id` + ORDER BY `cat`.`' . $sortColumn . '` ASC'; + $response = hesk_dbQuery($sql); @@ -40,7 +44,7 @@ class CategoryGateway extends CommonDao { $category->manager = intval($row['manager']) == 0 ? NULL : intval($row['manager']); $category->description = $row['mfh_description']; $category->numberOfTickets = intval($row['number_of_tickets']); - $results[$category->id] = $category; + $results[] = $category; } $this->close(); diff --git a/api/index.php b/api/index.php index fbe12a8b..25b77ee3 100644 --- a/api/index.php +++ b/api/index.php @@ -187,7 +187,7 @@ Link::before('globalBefore'); Link::all(array( // Categories - '/v1/categories/all' => action(\Controllers\Categories\CategoryController::class . '::printAllCategories', [RequestMethod::GET]), + '/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]), '/v1/categories/{i}' => action(\Controllers\Categories\CategoryController::class, [RequestMethod::GET, RequestMethod::PUT, RequestMethod::DELETE]), // Tickets diff --git a/internal-api/js/manage-categories.js b/internal-api/js/manage-categories.js new file mode 100644 index 00000000..d826c295 --- /dev/null +++ b/internal-api/js/manage-categories.js @@ -0,0 +1,105 @@ +var categories = []; + +$(document).ready(function() { + loadTable(); +}); + + +function loadTable() { + $('#overlay').show(); + var heskUrl = $('p#hesk-path').text(); + var $tableBody = $('#table-body'); + + $.ajax({ + method: 'GET', + url: heskUrl + 'api/index.php/v1/categories/all', + headers: { 'X-Internal-Call': true }, + success: function(data) { + $tableBody.html(''); + elements = []; + + if (data.length === 0) { + mfhAlert.error("I couldn't find any categories :(", "No categories found"); + $('#overlay').hide(); + return; + } + + var sortedElements = []; + + $.each(data, function() { + + }); + + var currentPlace = 0; + var addedElementToPlace = false; + var first = true; + var lastElement = null; + $.each(data, function() { + if (this.place !== currentPlace) { + if (lastElement !== null) { + //-- Hide the down arrow on the last element + $('[data-value="' + lastElement.id + '"]').parent().parent() + .find('[data-direction="down"]').css('visibility', 'hidden'); + lastElement = null; + } + + $('#table-body').append('