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 .
*/
2019-08-05 14:16:05 -05:00
if ( ! Auth :: user () -> hasGlobalAdmin ()) {
2016-08-18 20:28:22 -05:00
header ( 'Content-type: text/plain' );
2014-11-30 17:49:52 +00:00
die ( 'ERROR: You need to be admin' );
}
2018-10-25 22:31:12 +02:00
$template_id = $vars [ 'template_id' ];
$template_edit = is_numeric ( $template_id ) && $template_id > 0 ;
2014-11-30 17:49:52 +00:00
2018-10-25 22:31:12 +02:00
$rules = [];
$output = array (
'template' => '' ,
'name' => '' ,
'title' => '' ,
'title_rec' => '' ,
'type' => '' ,
'rules' => $rules ,
);
if ( $template_edit ) {
$template = dbFetchRow ( 'SELECT * FROM `alert_templates` WHERE `id` = ? LIMIT 1' , [ $template_id ]);
$output = [
2015-08-30 18:05:57 +01:00
'template' => $template [ 'template' ],
'name' => $template [ 'name' ],
'title' => $template [ 'title' ],
'title_rec' => $template [ 'title_rec' ],
2018-10-25 22:31:12 +02:00
'type' => $template [ 'type' ]
];
}
2020-06-07 14:46:33 +02:00
foreach ( dbFetchRows ( " SELECT `id`,`rule`,`name` FROM `alert_rules` order by `name` " , []) as $rule ) {
2018-10-25 22:31:12 +02:00
$is_selected = $template_edit ? dbFetchCell ( " SELECT `alert_templates_id` FROM `alert_template_map` WHERE `alert_rule_id` = ? AND `alert_templates_id` = ? " , [ $rule [ 'id' ], $template_id ]) : null ;
$is_available = dbFetchCell ( " SELECT `alert_templates_id` FROM `alert_template_map` WHERE `alert_rule_id` = ? " , [ $rule [ 'id' ]]);
$rules [] = [
'id' => $rule [ 'id' ],
'name' => $rule [ 'name' ],
'selected' => isset ( $is_selected ),
'used' => isset ( $is_available ) ? dbFetchCell ( " SELECT `name` FROM `alert_templates` WHERE `id` = ? " , [ $is_available ]) : '' ,
];
2014-11-30 17:49:52 +00:00
}
2018-10-25 22:31:12 +02:00
$output [ 'rules' ] = $rules ;
header ( 'Content-type: application/json' );
echo _json_encode ( $output );