More work on manage categories page
This commit is contained in:
parent
d76cc27e8e
commit
38cea82821
@ -16,6 +16,7 @@ define('HESK_PATH', '../');
|
||||
define('VALIDATOR', 1);
|
||||
define('PAGE_TITLE', 'ADMIN_CATEGORIES');
|
||||
define('MFH_PAGE_LAYOUT', 'TOP_ONLY');
|
||||
define('EXTRA_JS', '<script src="'.HESK_PATH.'internal-api/js/manage-categories.js"></script>');
|
||||
|
||||
/* 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'])
|
||||
</script>
|
||||
|
||||
<?php
|
||||
echo mfh_get_hidden_fields_for_language(array());
|
||||
|
||||
require_once(HESK_PATH . 'inc/footer.inc.php');
|
||||
exit();
|
||||
|
||||
|
||||
@ -77,10 +77,12 @@ class ApplicationContext {
|
||||
|
||||
// Categories
|
||||
$this->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();
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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 ||
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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
|
||||
|
||||
105
internal-api/js/manage-categories.js
Normal file
105
internal-api/js/manage-categories.js
Normal file
@ -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('<tr><td colspan="6" class="bg-gray"><i><b>' + places[this.place] + '</b></i></td></tr>');
|
||||
currentPlace = this.place;
|
||||
addedElementToPlace = false;
|
||||
first = true;
|
||||
}
|
||||
|
||||
var $template = $($('#nav-element-template').html());
|
||||
|
||||
$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="url"]').text(this.url);
|
||||
|
||||
var text = '';
|
||||
$.each(this.text, function(key, value) {
|
||||
text += '<li><b>' + escape(key) + ':</b> ' + escape(value) + '</li>';
|
||||
});
|
||||
$template.find('ul[data-property="text"]').html(text);
|
||||
|
||||
var subtext = '-';
|
||||
if (this.place === 1) {
|
||||
subtext = '';
|
||||
$.each(this.subtext, function(key, value) {
|
||||
subtext += '<li><b>' + escape(key) + ':</b> ' + escape(value) + '</li>';
|
||||
});
|
||||
}
|
||||
$template.find('ul[data-property="subtext"]').html(subtext);
|
||||
|
||||
if (first) {
|
||||
$template.find('[data-direction="up"]').css('visibility', 'hidden');
|
||||
first = false;
|
||||
}
|
||||
|
||||
$tableBody.append($template);
|
||||
|
||||
elements[this.id] = this;
|
||||
|
||||
addedElementToPlace = true;
|
||||
lastElement = this;
|
||||
});
|
||||
|
||||
if (lastElement) {
|
||||
//-- Hide the down arrow on the last element
|
||||
$('[data-value="' + lastElement.id + '"]').parent().parent()
|
||||
.find('[data-direction="down"]').css('visibility', 'hidden');
|
||||
}
|
||||
},
|
||||
error: function(data) {
|
||||
mfhAlert.errorWithLog(mfhLang.text('failed_to_load_custom_nav_elements'), data.responseJSON);
|
||||
console.error(data);
|
||||
},
|
||||
complete: function() {
|
||||
$('#overlay').hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user