diff --git a/html/includes/forms/attach-alert-template.inc.php b/html/includes/forms/attach-alert-template.inc.php index 96162abb5f..7305cbb58e 100644 --- a/html/includes/forms/attach-alert-template.inc.php +++ b/html/includes/forms/attach-alert-template.inc.php @@ -14,31 +14,60 @@ use LibreNMS\Authentication\LegacyAuth; -header('Content-type: text/plain'); +header('Content-type: application/json'); if (!LegacyAuth::user()->hasGlobalAdmin()) { - die('ERROR: You need to be admin'); + $response = array( + 'status' => 'error', + 'message' => 'Need to be admin', + ); + echo _json_encode($response); + exit; } +$status = 'error'; +$message = 'Ops. Something happened. Enable debug and check librenms.log'; +set_debug(true); if (!is_numeric($_POST['template_id'])) { - echo 'ERROR: No template selected'; - exit; + $message = 'ERROR: No template selected'; } else { - $rules = preg_split('/,/', mres($_POST['rule_id'])); + $rules = preg_split('/,/', $_POST['rule_id']); $ids = []; + foreach (dbFetchRows('SELECT `alert_rule_id` FROM `alert_template_map` WHERE `alert_templates_id` = ?', array($_POST['template_id'])) as $rule) { + $old_rules[] = $rule['alert_rule_id']; + } foreach ($rules as $rule_id) { - $db_id = dbInsert(array('alert_rule_id' => $rule_id, 'alert_templates_id' => mres($_POST['template_id'])), 'alert_template_map'); + $db_id = dbInsert(array('alert_rule_id' => $rule_id, 'alert_templates_id' =>$_POST['template_id']), 'alert_template_map'); if ($db_id > 0) { $ids[] = $db_id; + $new_rules[] = $rule_id; } else { - echo 'ERROR: Alert rules have not been attached to this template.'; - exit; + $status = 'error'; + $message = 'Alert rules have not been attached to this template.'; } } if (!empty($ids)) { dbDelete('alert_template_map', 'id NOT IN ' . dbGenPlaceholders(count($ids)) . ' AND alert_templates_id =?', array_merge($ids, [$_POST['template_id']])); - echo "Alert rules have been attached to this template."; - exit; + $status = 'ok'; + $message = "Alert rules have been attached to this template."; } }//end if +//$old_rules = array_diff($old_rules, $new_rules); +foreach ($old_rules as $rule) { + $rule_name[] = dbFetchCell("SELECT `name` FROM `alert_rules` WHERE `id` = ". $rule); +} +foreach ($new_rules as $template) { + $template_name[] = dbFetchCell("SELECT `name` FROM `alert_templates` WHERE `id` = ". $_POST['template_id']); + $nrule_name[] = dbFetchCell("SELECT `name` FROM `alert_rules` WHERE `id` = ". $template); +} +$response = array( + 'status' => $status, + 'message' => $message, + 'new_rules' => $new_rules, + 'nrule_name' => $nrule_name, + 'old_rules' => $old_rules, + 'rule_name' => $rule_name, + 'template_name' => $template_name +); +echo _json_encode($response); diff --git a/html/includes/modal/attach_alert_template.inc.php b/html/includes/modal/attach_alert_template.inc.php index 6f8ea71625..005a0deefc 100644 --- a/html/includes/modal/attach_alert_template.inc.php +++ b/html/includes/modal/attach_alert_template.inc.php @@ -3,6 +3,7 @@ * LibreNMS * * Copyright (c) 2014 Neil Lathwood + * Copyright (c) 2018 TheGreatDoc * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -32,11 +33,17 @@ if (!LegacyAuth::user()->hasGlobalAdmin()) {
@@ -69,6 +76,7 @@ $('#attach-alert-template').on('show.bs.modal', function(e) { $.each( output.rule_id, function( i, elem) { elem = parseInt(elem); selected_items.push(elem); + $("#rules_list option[value='"+ elem + "']").attr('disabled', false); }); $('#rules_list').val(selected_items); } @@ -92,17 +100,29 @@ $('#alert-template-attach').click('', function(event) { type: 'POST', url: 'ajax_form.php', data: { type: "attach-alert-template", template_id: template_id, rule_id: rules }, - dataType: "html", - success: function(msg) { - if(msg.indexOf("ERROR:") <= -1) { - toastr.success(msg); + dataType: "json", + success: function(data) { + if (data.status == 'ok') { + toastr.success(data.message); $("#attach-alert-template").modal('hide'); + $.each( data.old_rules, function(index, itemData){ + if (itemData != "--Clear Rules--") { + $("#rules_list option[value=" + itemData + "]").prop('disabled', false).text(data.rule_name[index]); + } + }); + $.each( data.new_rules, function(index, itemData){ + if (itemData != "--Clear Rules--") { + $("#rules_list option[value=" + itemData + "]").prop('disabled', true).text(data.nrule_name[index] + ' - Used in template:' + data.template_name[index]); + } + }); } else { - $('#template_error').html('
'+msg+'
'); + //$('#template_error').html('
'+msg+'
'); + toastr.error(data.message); } }, - error: function() { - $("#template_error").html('
The alert rules could not be attached to this template.
'); + error: function(data) { + //$("#template_error").html('
The alert rules could not be attached to this template.
'); + toastr.error(data.message); } }); });