2014-11-30 17:49:52 +00:00
< ? php
/*
* LibreNMS
*
* Copyright ( c ) 2014 Neil Lathwood < https :// github . com / laf / http :// www . lathwood . co . uk / fa >
*
* 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
* Free Software Foundation , either version 3 of the License , or ( at your
* option ) any later version . Please see LICENSE . txt at the top level of
* the source code distribution for details .
*/
2018-09-11 07:51:35 -05:00
use LibreNMS\Authentication\LegacyAuth ;
2018-04-07 15:55:28 -05:00
2018-09-11 07:51:35 -05:00
if ( ! LegacyAuth :: user () -> hasGlobalAdmin ()) {
2014-11-30 17:49:52 +00:00
die ( 'ERROR: You need to be admin' );
}
?>
< div class = " modal fade bs-example-modal-lg " id = " alert-template " tabindex = " -1 " role = " dialog " aria - labelledby = " Create " aria - hidden = " true " >
< div class = " modal-dialog modal-lg " >
< div class = " modal-content " >
< div class = " modal-header " >
< button type = " button " class = " close " data - dismiss = " modal " aria - hidden = " true " >& times ; </ button >
2018-07-14 22:15:43 +01:00
< h4 class = " modal-title " id = " Create " > Alert Template :: < a href = " https://docs.librenms.org/Alerting/Templates/ " >< i class = " fa fa-book fa-1x " ></ i > Docs </ a ></ h4 >
2014-11-30 17:49:52 +00:00
</ div >
< div class = " modal-body " >
< div class = " row " >
< div class = " col-md-12 " >
< div class = " form-group " >
2018-07-14 22:15:43 +01:00
< label for = " name " > Template name : </ label >
< input type = " text " class = " form-control input-sm " id = " name " name = " name " >
2014-11-30 17:49:52 +00:00
</ div >
< div class = " form-group " >
2018-07-14 22:15:43 +01:00
< label for = " template " > Template : </ label >
2018-10-25 22:31:12 +02:00
< textarea class = " form-control " id = " template " name = " template " style = " font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; " rows = " 15 " ></ textarea >
</ div >
< div class = " form-group " >
< label for = " rules_list " > Attach template to rules : </ label >
< select id = " rules_list " name = " rules_list[] " class = " form-control " multiple = " multiple " ></ select >
2014-11-30 17:49:52 +00:00
</ div >
< div class = " form-group " >
2018-07-14 22:15:43 +01:00
< label for = " title " > Alert title : </ label >
< input type = " text " class = " form-control input-sm " id = " title " name = " title " placeholder = " Alert Title " >
2014-11-30 17:49:52 +00:00
</ div >
< div class = " form-group " >
2018-07-14 22:15:43 +01:00
< label for = " title_rec " > Recovery title : </ label >
< input type = " text " class = " form-control input-sm " id = " title_rec " name = " title_rec " placeholder = " Recovery Title " >
2014-11-30 17:49:52 +00:00
</ div >
2018-07-14 22:15:43 +01:00
< button type = " button " class = " btn btn-primary btn-sm " name = " create-template " id = " create-template " > Create template </ button >
2019-02-19 00:33:51 -06:00
<!--// FIXME remove Deprecated template -->
< button type = " button " class = " btn btn-default btn-sm " name = " convert-template " id = " convert-template " title = " Convert template to new syntax " style = " display: none " > Convert template </ button >
2014-11-30 17:49:52 +00:00
</ div >
</ div >
</ div >
</ div >
</ div >
</ div >
2018-10-25 22:31:12 +02:00
< script type = " text/javascript " >
2014-11-30 17:49:52 +00:00
$ ( '#alert-template' ) . on ( 'show.bs.modal' , function ( event ) {
var button = $ ( event . relatedTarget );
2017-07-19 01:54:14 +03:00
var template_id = $ ( '#template_id' ) . val ();
2017-08-12 22:21:13 +03:00
var default_template = $ ( '#default_template' ) . val ();
2017-06-30 06:03:44 +03:00
2017-07-19 01:54:14 +03:00
if ( template_id != null && template_id != '' ) {
2017-08-12 22:21:13 +03:00
if ( default_template == " 1 " ) {
$ ( '#create-template' ) . after ( '<span class="pull-right"><button class="btn btn-primary btn-sm" id="reset-default">Reset to Default</button></span>' );
$ ( '#name' ) . prop ( " disabled " , true );
}
2017-06-30 06:03:44 +03:00
$ ( '#create-template' ) . text ( 'Update template' );
2018-10-25 22:31:12 +02:00
}
$ . ajax ({
type : " POST " ,
url : " ajax_form.php " ,
data : { type : " parse-alert-template " , template_id : template_id },
dataType : " json " ,
success : function ( output ) {
$ ( '#template' ) . val ( output [ 'template' ]);
$ ( '#name' ) . val ( output [ 'name' ]);
$ ( '#title' ) . val ( output [ 'title' ]);
$ ( '#title_rec' ) . val ( output [ 'title_rec' ]);
var selected_rules = [];
$ . each ( output . rules , function ( i , rule ) {
var ruleElem = $ ( '<option>' , {
value : rule . id ,
text : rule . name
}) . attr ( 'data-usedby' , '' );
if ( rule . selected ) {
selected_rules . push ( parseInt ( rule . id ));
} else if ( rule . used !== '' ) {
ruleElem . attr ( 'data-usedby' , rule . used ) . prop ( " disabled " , true );
2018-07-14 22:15:43 +01:00
}
2018-10-25 22:31:12 +02:00
$ ( '#rules_list' ) . append ( ruleElem );
});
$ ( '#rules_list' ) . select2 ({
theme : " bootstrap " ,
dropdownAutoWidth : true ,
width : " auto " ,
allowClear : true ,
placeholder : " Nothing selected " ,
templateResult : function ( data ) {
if ( data . id && data . element . dataset . usedby !== '' ) {
return $ (
'<span>' + data . text + ' <span class="label label-default">Used in template "' + data . element . dataset . usedby + '"</span></span>'
);
} else if ( data . id && data . selected ) {
return $ (
'<span><i class="fa fa-check"></i> ' + data . text + '</span>'
);
}
return data . text ;
}
}) . val ( selected_rules ) . trigger ( " change " );
2019-02-19 00:33:51 -06:00
//FIXME remove Deprecated template
if ( output [ 'template' ] . indexOf ( " { /if} " ) >= 0 ){
toastr . info ( 'The old template syntax is no longer supported. Please see https://docs.librenms.org/Alerting/Old_Templates/' );
$ ( '#convert-template' ) . show ();
}
2018-10-25 22:31:12 +02:00
}
});
2014-11-30 17:49:52 +00:00
});
2017-08-12 22:21:13 +03:00
2016-06-13 19:44:14 +00:00
$ ( '#alert-template' ) . on ( 'hide.bs.modal' , function ( event ) {
$ ( '#template_id' ) . val ( '' );
2017-01-27 23:31:46 +00:00
$ ( '#template' ) . val ( '' );
$ ( '#line' ) . val ( '' );
$ ( '#value' ) . val ( '' );
$ ( '#name' ) . val ( '' );
2018-10-25 22:31:12 +02:00
$ ( '#rules_list' ) . find ( 'option' ) . remove () . end () . select2 ( 'destroy' );
2017-06-30 06:03:44 +03:00
$ ( '#create-template' ) . text ( 'Create template' );
2017-08-12 22:21:13 +03:00
$ ( '#default-template' ) . val ( '0' );
$ ( '#reset-default' ) . remove ();
$ ( '#name' ) . prop ( " disabled " , false );
$ ( '#error' ) . val ( '' );
2019-02-19 00:33:51 -06:00
//FIXME remove Deprecated template
$ ( '#convert-template' ) . hide ();
2016-06-13 19:44:14 +00:00
});
2014-11-30 17:49:52 +00:00
$ ( '#create-template' ) . click ( '' , function ( e ) {
e . preventDefault ();
2018-10-25 22:31:12 +02:00
var rules_items = [];
$ ( '#rules_list :selected' ) . each ( function ( i , selectedElement ) {
rules_items . push ( $ ( selectedElement ) . val ());
});
2014-11-30 17:49:52 +00:00
var template = $ ( " #template " ) . val ();
var template_id = $ ( " #template_id " ) . val ();
var name = $ ( " #name " ) . val ();
2015-08-30 17:27:31 +01:00
var title = $ ( " #title " ) . val ();
var title_rec = $ ( " #title_rec " ) . val ();
2018-10-25 22:31:12 +02:00
alertTemplateAjaxOps ( template , name , template_id , title , title_rec , rules_items . join ( ',' ));
2014-11-30 17:49:52 +00:00
});
2019-02-19 00:33:51 -06:00
//FIXME remove Deprecated template
$ ( '#convert-template' ) . click ( '' , function ( e ) {
e . preventDefault ();
var template = $ ( " #template " ) . val ();
var title = $ ( " #title " ) . val ();
$ . ajax ({
type : " POST " ,
url : " ajax_form.php " ,
data : { type : " convert-template " , template : template , title : title },
dataType : " json " ,
success : function ( output ) {
if ( output . status === 'ok' ) {
toastr . success ( output . message );
$ ( " #convert-template " ) . hide ();
$ ( " #template " ) . val ( output . template );
$ ( " #title " ) . val ( output . title );
} else {
toastr . error ( output . message );
}
},
error : function (){
toastr . error ( 'An error occurred updating this alert template.' );
}
});
});
2018-10-25 22:31:12 +02:00
function alertTemplateAjaxOps ( template , name , template_id , title , title_rec , rules ) {
2017-08-12 22:21:13 +03:00
$ . ajax ({
type : " POST " ,
url : " ajax_form.php " ,
2018-10-25 22:31:12 +02:00
data : { type : " alert-templates " , template : template , name : name , template_id : template_id , title : title , title_rec : title_rec , rules : rules },
2018-04-07 15:22:08 +03:00
dataType : " json " ,
2018-07-14 22:15:43 +01:00
success : function ( output ) {
2018-04-07 15:22:08 +03:00
if ( output . status == 'ok' ) {
toastr . success ( output . message );
2017-08-12 22:21:13 +03:00
$ ( " #alert-template " ) . modal ( 'hide' );
2018-04-07 15:22:08 +03:00
if ( template_id != null && template_id != '' ) {
$ ( '#templatetable tbody tr' ) . each ( function ( i , row ) {
if ( $ ( row ) . children () . eq ( 0 ) . text () == template_id ) {
$ ( row ) . children () . eq ( 1 ) . text ( name );
return false ;
}
});
} else {
var newrow = [{ id : output . newid , templatename : name }];
$ ( '#templatetable' ) . bootgrid ( " append " , newrow );
}
2017-08-12 22:21:13 +03:00
} else {
2018-07-14 22:15:43 +01:00
toastr . error ( output . message );
2017-08-12 22:21:13 +03:00
}
},
error : function (){
2018-07-14 22:15:43 +01:00
toastr . error ( 'An error occurred updating this alert template.' );
2017-08-12 22:21:13 +03:00
}
});
}
2014-11-30 17:49:52 +00:00
</ script >