diff --git a/admin/service_messages.php b/admin/service_messages.php index 9ba0b9a9..b83d9378 100644 --- a/admin/service_messages.php +++ b/admin/service_messages.php @@ -15,6 +15,7 @@ define('IN_SCRIPT', 1); define('HESK_PATH', '../'); define('PAGE_TITLE', 'ADMIN_SERVICE_MESSAGES'); define('MFH_PAGE_LAYOUT', 'TOP_ONLY'); +define('EXTRA_JS', ''); /* Get all the required files and functions */ require(HESK_PATH . 'hesk_settings.inc.php'); @@ -31,26 +32,8 @@ hesk_isLoggedIn(); hesk_checkPermission('can_service_msg'); // Define required constants -define('LOAD_TABS', 1); define('WYSIWYG', 1); -// What should we do? -if ($action = hesk_REQUEST('a')) { - if ($action == 'edit_sm') { - edit_sm(); - } elseif (defined('HESK_DEMO')) { - hesk_process_messages($hesklang['ddemo'], 'service_messages.php', 'NOTICE'); - } elseif ($action == 'new_sm') { - new_sm(); - } elseif ($action == 'save_sm') { - save_sm(); - } elseif ($action == 'order_sm') { - order_sm(); - } elseif ($action == 'remove_sm') { - remove_sm(); - } -} - /* Print header */ require_once(HESK_PATH . 'inc/headerAdmin.inc.php'); @@ -60,638 +43,293 @@ require_once(HESK_PATH . 'inc/show_admin_nav.inc.php');
+
+

+ +

+
+ +
+
-
+var users = [];'; +$usersRs = hesk_dbQuery("SELECT `id`, `name` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "users` WHERE `active` = '1'"); +$users = array(); +while ($row = hesk_dbFetchAssoc($usersRs)) { + $users[] = $row; + echo "users[" . $row['id'] . "] = { + id: ".$row['id'].", + name: '".$row['name']."' + }\n"; +} +echo ''; +?> + + + 4 || $style < 0) { - $style = 0; - } - - $type = empty($_POST['type']) ? 0 : 1; - $icon = hesk_POST('icon'); - $title = hesk_input(hesk_POST('title')) or $hesk_error_buffer[] = $hesklang['sm_e_title']; - $message = hesk_getHTML(hesk_POST('message')); - - // Clean the HTML code - require(HESK_PATH . 'inc/htmlpurifier/HeskHTMLPurifier.php'); - $purifier = new HeskHTMLPurifier($hesk_settings['cache_dir']); - $message = $purifier->heskPurify($message); - - // Any errors? - if (count($hesk_error_buffer)) { - $_SESSION['edit_sm'] = true; - - $_SESSION['new_sm'] = array( - 'id' => $id, - 'style' => $style, - 'type' => $type, - 'title' => $title, - 'icon' => $icon, - 'message' => hesk_input(hesk_POST('message')), - ); - - $tmp = ''; - foreach ($hesk_error_buffer as $error) { - $tmp .= "
  • $error
  • \n"; - } - $hesk_error_buffer = $tmp; - - $hesk_error_buffer = $hesklang['rfm'] . '

    '; - hesk_process_messages($hesk_error_buffer, 'service_messages.php'); - } - - // Just preview the message? - if (isset($_POST['sm_preview'])) { - $_SESSION['preview_sm'] = true; - $_SESSION['edit_sm'] = true; - - $_SESSION['new_sm'] = array( - 'id' => $id, - 'style' => $style, - 'type' => $type, - 'title' => $title, - 'message' => $message, - 'icon' => $icon, - ); - - header('Location: service_messages.php'); - exit; - } - - // Update the service message in the database - hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages` SET - `author` = '" . intval($_SESSION['id']) . "', - `title` = '" . hesk_dbEscape($title) . "', - `message` = '" . hesk_dbEscape($message) . "', - `style` = '{$style}', - `type` = '{$type}', - `icon` = '{$icon}' - WHERE `id`={$id}"); - - $_SESSION['smord'] = $id; - hesk_process_messages($hesklang['sm_mdf'], 'service_messages.php', 'SUCCESS'); - -} // End save_sm() - - -function edit_sm() -{ - global $hesk_settings, $hesklang; - - // Get service messageID - $id = intval(hesk_GET('id')) or hesk_error($hesklang['sm_e_id']); - - // Get details from the database - $res = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages` WHERE `id`={$id} LIMIT 1"); - if (hesk_dbNumRows($res) != 1) { - hesk_error($hesklang['sm_not_found']); - } - $sm = hesk_dbFetchAssoc($res); - - $_SESSION['new_sm'] = $sm; - $_SESSION['edit_sm'] = true; - -} // End edit_sm() - - -function order_sm() -{ - global $hesk_settings, $hesklang; - - // A security check - hesk_token_check(); - - // Get ID and move parameters - $id = intval(hesk_GET('id')) or hesk_error($hesklang['sm_e_id']); - $move = intval(hesk_GET('move')); - $_SESSION['smord'] = $id; - - // Update article details - hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages` SET `order`=`order`+" . intval($move) . " WHERE `id`={$id}"); - - // Update order of all service messages - update_sm_order(); - - // Finish - header('Location: service_messages.php'); - exit(); - -} // End order_sm() - - -function update_sm_order() -{ - global $hesk_settings, $hesklang; - - // Get list of current service messages - $res = hesk_dbQuery("SELECT `id` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages` ORDER BY `order` ASC"); - - // Update database - $i = 10; - while ($sm = hesk_dbFetchAssoc($res)) { - hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages` SET `order`=" . intval($i) . " WHERE `id`='" . intval($sm['id']) . "'"); - $i += 10; - } - - return true; - -} // END update_sm_order() - - -function remove_sm() -{ - global $hesk_settings, $hesklang; - - // A security check - hesk_token_check(); - - // Get ID - $id = intval(hesk_GET('id')) or hesk_error($hesklang['sm_e_id']); - - // Delete the service message - hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages` WHERE `id`={$id}"); - - // Were we successful? - if (hesk_dbAffectedRows() == 1) { - hesk_process_messages($hesklang['sm_deleted'], './service_messages.php', 'SUCCESS'); - } else { - hesk_process_messages($hesklang['sm_not_found'], './service_messages.php'); - } - -} // End remove_sm() - - -function new_sm() -{ - global $hesk_settings, $hesklang, $listBox; - global $hesk_error_buffer; - - // A security check - # hesk_token_check('POST'); - - $hesk_error_buffer = array(); - - $style = intval(hesk_POST('style', 0)); - if ($style > 4 || $style < 0) { - $style = 0; - } - - $type = empty($_POST['type']) ? 0 : 1; - $icon = hesk_POST('icon'); - $title = hesk_input(hesk_POST('title')) or $hesk_error_buffer[] = $hesklang['sm_e_title']; - $message = hesk_getHTML(hesk_POST('message')); - - // Clean the HTML code - require(HESK_PATH . 'inc/htmlpurifier/HeskHTMLPurifier.php'); - $purifier = new HeskHTMLPurifier($hesk_settings['cache_dir']); - $message = $purifier->heskPurify($message); - - // Any errors? - if (count($hesk_error_buffer)) { - $_SESSION['new_sm'] = array( - 'style' => $style, - 'type' => $type, - 'title' => $title, - 'icon' => $icon, - 'message' => hesk_input(hesk_POST('message')), - ); - - $tmp = ''; - foreach ($hesk_error_buffer as $error) { - $tmp .= "
  • $error
  • \n"; - } - $hesk_error_buffer = $tmp; - - $hesk_error_buffer = $hesklang['rfm'] . '

    '; - hesk_process_messages($hesk_error_buffer, 'service_messages.php'); - } - - // Just preview the message? - if (isset($_POST['sm_preview'])) { - $_SESSION['preview_sm'] = true; - - $_SESSION['new_sm'] = array( - 'style' => $style, - 'type' => $type, - 'title' => $title, - 'icon' => $icon, - 'message' => $message, - ); - - header('Location: service_messages.php'); - exit; - } - - - - $_SESSION['smord'] = hesk_dbInsertID(); - hesk_process_messages($hesklang['sm_added'], 'service_messages.php', 'SUCCESS'); - -} // End new_sm() - ?> diff --git a/api/BusinessLogic/ServiceMessages/ServiceMessageHandler.php b/api/BusinessLogic/ServiceMessages/ServiceMessageHandler.php index d70f142f..9e70ceda 100644 --- a/api/BusinessLogic/ServiceMessages/ServiceMessageHandler.php +++ b/api/BusinessLogic/ServiceMessages/ServiceMessageHandler.php @@ -5,6 +5,7 @@ namespace BusinessLogic\ServiceMessages; // TODO Test use BusinessLogic\Exceptions\ValidationException; +use BusinessLogic\Navigation\Direction; use BusinessLogic\ValidationModel; use DataAccess\ServiceMessages\ServiceMessagesGateway; @@ -17,7 +18,7 @@ class ServiceMessageHandler extends \BaseClass { } function createServiceMessage($serviceMessage, $heskSettings) { - $this->validate($serviceMessage); + $this->validate($serviceMessage, $heskSettings); if ($serviceMessage->icon === null) { switch ($serviceMessage->style) { @@ -47,7 +48,7 @@ class ServiceMessageHandler extends \BaseClass { } function editServiceMessage($serviceMessage, $heskSettings) { - $this->validate($serviceMessage, false); + $this->validate($serviceMessage, $heskSettings, false); if ($serviceMessage->icon === null) { switch ($serviceMessage->style) { @@ -72,19 +73,52 @@ class ServiceMessageHandler extends \BaseClass { return $this->serviceMessageGateway->updateServiceMessage($serviceMessage, $heskSettings); } + function deleteServiceMessage($id, $heskSettings) { + $this->serviceMessageGateway->deleteServiceMessage($id, $heskSettings); + } + + function sortServiceMessage($id, $direction, $heskSettings) { + $serviceMessages = $this->serviceMessageGateway->getServiceMessages($heskSettings); + $serviceMessage = null; + foreach ($serviceMessages as $innerServiceMessage) { + if ($innerServiceMessage->id === intval($id)) { + $serviceMessage = $innerServiceMessage; + break; + } + } + + if ($serviceMessage === null) { + throw new \BaseException("Could not find category with ID {$id}!"); + } + + if ($direction === Direction::UP) { + $serviceMessage->order -= 15; + } else { + $serviceMessage->order += 15; + } + + $this->serviceMessageGateway->updateServiceMessage($serviceMessage, $heskSettings); + $this->serviceMessageGateway->resortAllServiceMessages($heskSettings); + } + /** * @param $serviceMessage ServiceMessage * @param bool $isNew * @throws ValidationException */ - private function validate($serviceMessage, $isNew = true) { + private function validate($serviceMessage, $heskSettings, $isNew = true) { $validationModel = new ValidationModel(); if ($isNew && $serviceMessage->createdBy < 1) { $validationModel->errorKeys[] = 'MISSING_CREATOR'; } + if ($serviceMessage->message === null || trim($serviceMessage->message) === '') { $validationModel->errorKeys[] = 'MISSING_MESSAGE'; + } else { + $htmlPurifier = new \HeskHTMLPurifier($heskSettings['cache_dir']); + $serviceMessage->message = $htmlPurifier->heskPurify($serviceMessage->message); } + if ($serviceMessage->title === null || trim($serviceMessage->title) === '') { $validationModel->errorKeys[] = 'MISSING_TITLE'; } diff --git a/api/Controllers/ServiceMessages/ServiceMessagesController.php b/api/Controllers/ServiceMessages/ServiceMessagesController.php index f17d6e14..94ff9776 100644 --- a/api/Controllers/ServiceMessages/ServiceMessagesController.php +++ b/api/Controllers/ServiceMessages/ServiceMessagesController.php @@ -6,6 +6,7 @@ use BusinessLogic\Security\UserContext; use BusinessLogic\ServiceMessages\ServiceMessage; use BusinessLogic\ServiceMessages\ServiceMessageHandler; use Controllers\JsonRetriever; +use Symfony\Component\EventDispatcher\Tests\Service; class ServiceMessagesController extends \BaseClass { function get() { @@ -41,6 +42,17 @@ class ServiceMessagesController extends \BaseClass { return output($element); } + function delete($id) { + global $applicationContext, $hesk_settings; + + /* @var $handler ServiceMessageHandler */ + $handler = $applicationContext->get(ServiceMessageHandler::clazz()); + + $handler->deleteServiceMessage($id, $hesk_settings); + + return http_response_code(204); + } + /** * @param $data array * @param $userContext UserContext diff --git a/api/DataAccess/ServiceMessages/ServiceMessagesGateway.php b/api/DataAccess/ServiceMessages/ServiceMessagesGateway.php index 9dfb1796..b2539d12 100644 --- a/api/DataAccess/ServiceMessages/ServiceMessagesGateway.php +++ b/api/DataAccess/ServiceMessages/ServiceMessagesGateway.php @@ -3,7 +3,6 @@ namespace DataAccess\ServiceMessages; -use BusinessLogic\DateTimeHelpers; use BusinessLogic\ServiceMessages\ServiceMessage; use BusinessLogic\ServiceMessages\ServiceMessageStyle; use DataAccess\CommonDao; @@ -49,6 +48,10 @@ class ServiceMessagesGateway extends CommonDao { return $serviceMessage; } + /** + * @param $heskSettings + * @return ServiceMessage[] + */ function getServiceMessages($heskSettings) { $this->init(); @@ -100,4 +103,31 @@ class ServiceMessagesGateway extends CommonDao { return $serviceMessage; } + + function deleteServiceMessage($id, $heskSettings) { + $this->init(); + + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages` + WHERE `id` = " . intval($id)); + + $this->close(); + } + + function resortAllServiceMessages($heskSettings) { + $this->init(); + + $rs = hesk_dbQuery("SELECT `id` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages` + ORDER BY `order` ASC"); + + $sortValue = 10; + while ($row = hesk_dbFetchAssoc($rs)) { + hesk_dbQuery("UPDATE `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages` + SET `order` = " . intval($sortValue) . " + WHERE `id` = " . intval($row['id'])); + + $sortValue += 10; + } + + $this->close(); + } } \ No newline at end of file diff --git a/api/autoload.php b/api/autoload.php index 0f14ff20..cf548485 100644 --- a/api/autoload.php +++ b/api/autoload.php @@ -12,6 +12,7 @@ require_once(__DIR__ . '/Core/output.php'); require_once(__DIR__ . '/../hesk_settings.inc.php'); require_once(__DIR__ . '/http_response_code.php'); require_once(__DIR__ . '/../inc/admin_functions.inc.php'); +require_once(__DIR__ . '/../inc/htmlpurifier/HeskHTMLPurifier.php'); hesk_load_api_database_functions(); diff --git a/api/index.php b/api/index.php index afa70f70..9c4b3e35 100644 --- a/api/index.php +++ b/api/index.php @@ -203,7 +203,15 @@ Link::all(array( // Settings '/v1/settings' => action(\Controllers\Settings\SettingsController::clazz(), RequestMethod::all()), // Service Messages - '/v1/service-messages' => action(\Controllers\ServiceMessages\ServiceMessagesController::clazz(), array(RequestMethod::GET, RequestMethod::POST, RequestMethod::PUT)), + '/v1/service-messages' => action(\Controllers\ServiceMessages\ServiceMessagesController::clazz(), + array(RequestMethod::GET, RequestMethod::POST, RequestMethod::PUT), + SecurityHandler::INTERNAL_OR_AUTH_TOKEN), + '/v1/service-messages/{i}' => action(\Controllers\ServiceMessages\ServiceMessagesController::clazz(), + array(RequestMethod::DELETE), + SecurityHandler::INTERNAL_OR_AUTH_TOKEN), + '/v1/service-messages/{i}/sort/{s}' => action(\Controllers\ServiceMessages\ServiceMessagesController::clazz() . '::sort', + array(RequestMethod::POST), + SecurityHandler::INTERNAL), /* Internal use only routes */ // Resend email response diff --git a/internal-api/js/service-messages.js b/internal-api/js/service-messages.js new file mode 100644 index 00000000..a2c3f950 --- /dev/null +++ b/internal-api/js/service-messages.js @@ -0,0 +1,362 @@ +var serviceMessages = []; + +$(document).ready(function() { + loadTable(); + bindEditModal(); + bindModalCancelCallback(); + bindFormSubmit(); + bindDeleteButton(); + bindCreateModal(); + bindSortButtons(); +}); + + +function loadTable() { + $('#overlay').show(); + var heskUrl = $('p#hesk-path').text(); + var $tableBody = $('#table-body'); + + $.ajax({ + method: 'GET', + url: heskUrl + 'api/index.php/v1/service-messages', + headers: { 'X-Internal-Call': true }, + success: function(data) { + $tableBody.html(''); + + if (data.length === 0) { + // TODO "No Service Messages Found" + $('#overlay').hide(); + return; + } + + var first = true; + var lastElement = null; + $.each(data, function() { + var $template = $($('#service-message-template').html()); + + $template.find('span[data-property="id"]').attr('data-value', this.id); + $template.find('span[data-property="title"]').html( + getFormattedTitle(this.icon, this.title, this.style)); + $template.find('span[data-property="author"]').text(users[this.createdBy].name); + if (this.published) { + $template.find('span[data-property="type"]').text(mfhLang.text('sm_published')); + } else { + $template.find('span[data-property="type"]').text(mfhLang.text('sm_draft')); + } + + $tableBody.append($template); + + serviceMessages[this.id] = this; + + lastElement = this; + + if (first) { + $template.find('[data-direction="up"]').css('visibility', 'hidden'); + first = false; + } + }); + + 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('error_retrieving_categories'), data.responseJSON); + console.error(data); + }, + complete: function() { + refreshBackgroundVolatileItems(); + $('[data-toggle="popover"]').popover({ + trigger: 'hover', + container: 'body' + }); + $('#overlay').hide(); + } + }); +} + +function getFormattedTitle(icon, title, style) { + var $template = $($('#service-message-title-template').html()); + + var alertClass = 'none'; + switch (style) { + case 'ERROR': + alertClass = 'alert alert-danger'; + break; + case 'NOTICE': + alertClass = 'alert alert-warning'; + break; + case 'INFO': + alertClass = 'alert alert-info'; + break; + case 'SUCCESS': + alertClass = 'alert alert-success'; + break; + } + $template.addClass(alertClass) + .find('[data-property="icon"]').addClass(icon).end() + .find('[data-property="title"]').text(title); + + return $template; +} + +function getServiceMessagePreview(icon, title, message, style) { + var $template = $('#service-message-preview-template').html(); + + var alertClass = 'none'; + switch (style) { + case 'ERROR': + alertClass = 'alert alert-danger'; + break; + case 'NOTICE': + alertClass = 'alert alert-warning'; + break; + case 'INFO': + alertClass = 'alert alert-info'; + break; + case 'SUCCESS': + alertClass = 'alert alert-success'; + break; + } + $template = $template.replace('none', alertClass) + .replace('{{TITLE}}', title) + .replace('{{MESSAGE}}', message); + $template = $($template); + if (icon !== '') { + $template.find('i.fa').removeClass('fa').addClass(icon); + } + + return $template; +} + +function bindEditModal() { + $(document).on('click', '[data-action="edit"]', function() { + var element = categories[$(this).parent().parent().find('[data-property="id"]').text()]; + var $modal = $('#category-modal'); + + $modal.find('#edit-label').show(); + $modal.find('#create-label').hide(); + + $modal.find('input[name="name"]').val(element.name).end() + .find('select[name="priority"]').val(element.priority).end() + .find('select[name="manager"]').val(element.manager === null ? 0 : element.manager).end() + .find('input[name="id"]').val(element.id).end() + .find('select[name="usage"]').val(element.usage).end() + .find('input[name="display-border"][value="' + (element.displayBorder ? 1 : 0) + '"]') + .prop('checked', 'checked').end(); + + var backgroundColor = element.backgroundColor; + var foregroundColor = element.foregroundColor; + var colorpickerOptions = { + format: 'hex', + color: backgroundColor + }; + $modal.find('input[name="background-color"]') + .colorpicker(colorpickerOptions).end().modal('show'); + + colorpickerOptions = { + format: 'hex' + }; + if (foregroundColor != '' && foregroundColor !== 'AUTO') { + colorpickerOptions.color = foregroundColor; + } + + $modal.find('input[name="foreground-color"]') + .colorpicker(colorpickerOptions).end().modal('show'); + + if (foregroundColor === '' || foregroundColor === 'AUTO') { + $modal.find('input[name="foreground-color"]').colorpicker('setValue', '#fff'); + $modal.find('input[name="foreground-color"]').val(''); + } + + $modal.find('input[name="cat-order"]').val(element.catOrder); + $modal.find('input[name="autoassign"][value="' + (element.autoAssign ? 1 : 0) + '"]') + .prop('checked', 'checked'); + $modal.find('input[name="type"][value="' + (element.type ? 1 : 0) + '"]') + .prop('checked', 'checked'); + $modal.find('textarea[name="description"]').val(element.description === null ? '' : element.description); + + $modal.modal('show'); + }); +} + +function bindCreateModal() { + $('#create-button').click(function() { + var $modal = $('#category-modal'); + $modal.find('#edit-label').hide(); + $modal.find('#create-label').show(); + + $modal.find('input[name="name"]').val(''); + $modal.find('select[name="priority"]').val(3); // Low priority + $modal.find('select[name="usage"]').val(0); // Tickets and events + $modal.find('input[name="id"]').val(-1); + $modal.find('textarea[name="description"]').val(''); + $modal.find('input[name="cat-order"]').val(''); + $modal.find('input[name="type"][value="0"]').prop('checked', 'checked'); + $modal.find('input[name="autoassign"][value="0"]').prop('checked', 'checked'); + $modal.find('input[name="display-border"][value="0"]') + .prop('checked', 'checked'); + + var colorpickerOptions = { + format: 'hex', + color: '#fff' + }; + $modal.find('input[name="background-color"]') + .colorpicker(colorpickerOptions).end().modal('show'); + $modal.find('input[name="background-color"]').val(''); + $modal.find('input[name="foreground-color"]') + .colorpicker(colorpickerOptions).end().modal('show'); + $modal.find('input[name="foreground-color"]').val(''); + + $modal.modal('show'); + }); +} + +function bindModalCancelCallback() { + $('.cancel-callback').click(function() { + var $editCategoryModal = $('#category-modal'); + + $editCategoryModal.find('input[name="background-color"]').val('').colorpicker('destroy').end(); + $editCategoryModal.find('input[name="foreground-color"]').val('').colorpicker('destroy').end(); + $editCategoryModal.find('input[name="display-border"][value="1"]').prop('checked'); + $editCategoryModal.find('input[name="display-border"][value="0"]').prop('checked'); + $editCategoryModal.find('input[name="autoassign"][value="1"]').prop('checked'); + $editCategoryModal.find('input[name="autoassign"][value="0"]').prop('checked'); + }); +} + +function bindFormSubmit() { + $('form#manage-category').submit(function(e) { + e.preventDefault(); + var heskUrl = $('p#hesk-path').text(); + + var $modal = $('#category-modal'); + + var foregroundColor = $modal.find('input[name="foreground-color"]').val(); + var manager = parseInt($modal.find('select[name="manager"]').val()); + var data = { + autoassign: $modal.find('input[name="autoassign"]').val() === 'true', + backgroundColor: $modal.find('input[name="background-color"]').val(), + description: $modal.find('textarea[name="description"]').val(), + displayBorder: $modal.find('input[name="display-border"]:checked').val() === '1', + foregroundColor: foregroundColor === '' ? 'AUTO' : foregroundColor, + name: $modal.find('input[name="name"]').val(), + priority: parseInt($modal.find('select[name="priority"]').val()), + manager: manager === 0 ? null : manager, + type: parseInt($modal.find('input[name="type"]:checked').val()), + usage: parseInt($modal.find('select[name="usage"]').val()), + catOrder: parseInt($modal.find('input[name="cat-order"]').val()) + }; + + var url = heskUrl + 'api/index.php/v1/categories/'; + var method = 'POST'; + + var categoryId = parseInt($modal.find('input[name="id"]').val()); + if (categoryId !== -1) { + url += categoryId; + method = 'PUT'; + } + + $modal.find('#action-buttons').find('.cancel-button').attr('disabled', 'disabled'); + $modal.find('#action-buttons').find('.save-button').attr('disabled', 'disabled'); + + $.ajax({ + method: 'POST', + url: url, + headers: { + 'X-Internal-Call': true, + 'X-HTTP-Method-Override': method + }, + data: JSON.stringify(data), + success: function(data) { + var format = undefined; + if (categoryId === -1) { + format = mfhLang.html('cat_name_added'); + mfhAlert.success(format.replace('%s', data.name)); + } else { + format = mfhLang.html('category_updated'); + mfhAlert.success(format.replace('%s', data.name)); + } + $modal.modal('hide'); + loadTable(); + }, + error: function(data) { + mfhAlert.errorWithLog(mfhLang.text('error_saving_updating_category'), data.responseJSON); + console.error(data); + }, + complete: function(data) { + $modal.find('#action-buttons').find('.cancel-button').removeAttr('disabled'); + $modal.find('#action-buttons').find('.save-button').removeAttr('disabled'); + } + }); + }); +} + +function bindDeleteButton() { + $(document).on('click', '[data-action="delete"]', function() { + $('#overlay').show(); + + var heskUrl = $('p#hesk-path').text(); + var element = categories[$(this).parent().parent().find('[data-property="id"]').text()]; + + $.ajax({ + method: 'POST', + url: heskUrl + 'api/index.php/v1/categories/' + element.id, + headers: { + 'X-Internal-Call': true, + 'X-HTTP-Method-Override': 'DELETE' + }, + success: function() { + mfhAlert.success(mfhLang.text('cat_removed')); + loadTable(); + }, + error: function(data) { + $('#overlay').hide(); + mfhAlert.errorWithLog(mfhLang.text('error_deleting_category'), data.responseJSON); + console.error(data); + } + }); + }); +} + +function bindGenerateLinkModal() { + var $modal = $('#generate-link-modal'); + + $modal.find('.input-group-addon').click(function() { + clipboard.copy($modal.find('input[type="text"]').val()); + mfhAlert.success(mfhLang.text('copied_to_clipboard')); + }); + + $(document).on('click', '[data-property="generate-link"] i.fa-code', function () { + var heskUrl = $('p#hesk-url').text(); + + var url = heskUrl + '/index.php?a=add&catid=' + $(this).parent().data('category-id'); + + $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