mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Disable used rules in template map for select them (#9212)
* Disable used rules in template map for select them * WIP Enable/Disable options dynamically * Enable/Disable options dynamically * Even better. Selected options for template are not disabled * Didnt work so reverting back * Update attach_alert_template.inc.php
This commit is contained in:
committed by
Neil Lathwood
parent
86d862fb25
commit
1a89e27de7
@@ -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);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
* LibreNMS
|
||||
*
|
||||
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
|
||||
* Copyright (c) 2018 TheGreatDoc <https://github.com/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()) {
|
||||
<div class="form-group">
|
||||
<label for="rules_list">Select rules</label>
|
||||
<select multiple="multiple" class="form-control" id="rules_list" name="rules_list" size="10">
|
||||
<option></option>
|
||||
<option>--Clear Rules--</option>
|
||||
<?php
|
||||
|
||||
foreach (dbFetchRows("SELECT `id`,`rule`,`name` FROM `alert_rules`", array()) as $rule) {
|
||||
echo '<option value="'.$rule['id'].'">'.$rule['name'].'</option>';
|
||||
$is_avail = dbFetchCell("SELECT `alert_templates_id` FROM `alert_template_map` WHERE `alert_rule_id` = ?", [$rule['id']]);
|
||||
if (!isset($is_avail)) {
|
||||
echo '<option value="' . $rule['id'] . '">' . $rule['name'] . '</option>';
|
||||
} else {
|
||||
$template = dbFetchCell("SELECT `name` FROM `alert_templates` WHERE `id` = ?", [$is_avail]);
|
||||
echo '<option value="' . $rule['id'] . '" disabled>' . $rule['name'] . ' - Used in template: ' . $template . '</option>';
|
||||
}
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
@@ -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('<div class="alert alert-danger">'+msg+'</div>');
|
||||
//$('#template_error').html('<div class="alert alert-danger">'+msg+'</div>');
|
||||
toastr.error(data.message);
|
||||
}
|
||||
},
|
||||
error: function() {
|
||||
$("#template_error").html('<div class="alert alert-danger">The alert rules could not be attached to this template.</div>');
|
||||
error: function(data) {
|
||||
//$("#template_error").html('<div class="alert alert-danger">The alert rules could not be attached to this template.</div>');
|
||||
toastr.error(data.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user