2010-10-08 Sergio Martin <sergio.martin@artica.es>

* include/javascript/pandora.js
	include/functions_agents.php
	operation/agentes/ver_agente.php
	godmode/massive/massive_add_alerts.php
	godmode/massive/massive_delete_alerts.php: Improved the 
	addition/deletion of alerts in massive operations to 
	select not only the agents (the modules too) for pending
	task 3082862



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3371 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
zarzuelo 2010-10-08 11:16:57 +00:00
parent fe21083399
commit ec5776bfce
6 changed files with 322 additions and 46 deletions

View File

@ -1,3 +1,14 @@
2010-10-08 Sergio Martin <sergio.martin@artica.es>
* include/javascript/pandora.js
include/functions_agents.php
operation/agentes/ver_agente.php
godmode/massive/massive_add_alerts.php
godmode/massive/massive_delete_alerts.php: Improved the
addition/deletion of alerts in massive operations to
select not only the agents (the modules too) for pending
task 3082862
2010-10-08 Sergio Martin <sergio.martin@artica.es>
* include/functions_db.php: Fixed the function get_agentmodule_id

View File

@ -41,7 +41,7 @@ if (is_ajax ()) {
return;
}
function process_manage_add ($id_alert_template, $id_agents) {
function process_manage_add ($id_alert_template, $id_agents, $module_names) {
if (empty ($id_agents)) {
echo '<h3 class="error">'.__('No agents selected').'</h3>';
return false;
@ -52,71 +52,86 @@ function process_manage_add ($id_alert_template, $id_agents) {
return false;
}
process_sql_begin ();
$modules = get_agent_modules ($id_agents, 'id_agente_modulo', false, true);
$contfail = 0;
$contsuccess = 0;
foreach($modules as $module){
$success = create_alert_agent_module ($module, $id_alert_template);
if(!$success)
$contfail = $contfail + 1;
else
$contsuccess = $contsuccess + 1;
foreach($module_names as $module){
foreach($id_agents as $id_agent) {
$module_id = get_agentmodule_id($module, $id_agent);
$modules_id[] = $module_id['id_agente_modulo'];
}
}
if(count($module_names) == 1 && $module_names[0] == '0'){
$modules_id = get_agents_common_modules ($id_agents, false, true);
}
if ($contfail > 0) {
echo '<h3 class="error">'.__('There was an error adding the alerts, the operation has been cancelled').'</h3>';
echo '<h3 class="error">'.__('Could not add alerts').'</h3>';
process_sql_rollback ();
}
else {
echo '<h3 class="suc">'.__('Successfully added').' '.$contsuccess.' '.__('Alerts').'</h3>';
process_sql_commit ();
$conttotal = 0;
$contsuccess = 0;
foreach($modules_id as $module){
$success = create_alert_agent_module ($module, $id_alert_template);
if($success)
$contsuccess ++;
$conttotal ++;
}
print_result_message ($contsuccess > 0,
__('Successfully added')."(".$contsuccess."/".$conttotal.")",
__('Could not be added'));
}
$id_group = (int) get_parameter ('id_group', -1);
$id_agents = get_parameter ('id_agents');
$module_names = get_parameter ('module');
$id_alert_template = (int) get_parameter ('id_alert_template');
$add = (bool) get_parameter_post ('add');
if ($add) {
process_manage_add ($id_alert_template, $id_agents);
process_manage_add ($id_alert_template, $id_agents, $module_names);
}
$groups = get_user_groups ();
$table->id = 'delete_table';
$table->id = 'add_table';
$table->width = '95%';
$table->data = array ();
$table->style = array ();
$table->style[0] = 'font-weight: bold; vertical-align:top';
$table->style[2] = 'font-weight: bold';
$table->style[2] = 'font-weight: bold; vertical-align:top';
$table->size = array ();
$table->size[0] = '15%';
$table->size[1] = '85%';
$table->size[1] = '40%';
$table->size[2] = '15%';
$table->size[3] = '40%';
$table->data = array ();
$table->data[0][0] = __('Group');
$table->data[0][1] = print_select_groups(false, "AR", true, 'id_group', $id_group,
$table->data[0][1] = print_select_groups(false, "AR", true, 'id_group', 0,
'', 'Select', -1, true, false, true, '', false);
$table->data[0][2] = '';
$table->data[0][3] = '';
$table->data[1][0] = __('Agent');
$table->data[1][0] = __('Agents');
$table->data[1][0] .= '<span id="agent_loading" class="invisible">';
$table->data[1][0] .= '<img src="images/spinner.png" />';
$table->data[1][0] .= '</span>';
$agents_alerts = get_agents_with_alert_template ($id_alert_template, $id_group,
false, array ('tagente.nombre', 'tagente.id_agente'));
$table->data[1][1] = print_select (index_array ($agents_alerts, 'id_agente', 'nombre'),
$agents = get_agents();
$table->data[1][1] = print_select (index_array ($agents, 'id_agente', 'nombre'),
'id_agents[]', '', '', '', '', true, true, true, '', false);
$table->data[1][2] = __('Modules');
$table->data[1][3] = print_select (array(), 'module[]', '', false, '', '', true, true, false);
$templates = get_alert_templates (false, array ('id', 'name'));
$table->data[2][0] = __('Alert template');
$table->data[2][1] = print_select (index_array ($templates, 'id', 'name'),
'id_alert_template', $id_alert_template, false, __('Select'), 0, true);
$table->data[2][2] = '';
$table->data[2][3] = '';
echo '<form method="post" action="index.php?sec=gagente&sec2=godmode/massive/massive_operations&option=add_alerts" onsubmit="if (! confirm(\''.__('Are you sure?').'\')) return false;">';
print_table ($table);
@ -128,6 +143,9 @@ echo '</form>';
echo '<h3 class="error invisible" id="message"> </h3>';
//Hack to translate text "none" in PHP to javascript
echo '<span id ="none_text" style="display: none;">' . __('None') . '</span>';
require_jquery_file ('form');
require_jquery_file ('pandora.controls');
?>
@ -135,6 +153,8 @@ require_jquery_file ('pandora.controls');
<script type="text/javascript">
/* <![CDATA[ */
$(document).ready (function () {
$("#id_agents").change(agent_changed_by_multiple_agents);
$("#id_group").change (function () {
var $select = $("#id_agents").enable ();
$("#agent_loading").show ();

View File

@ -43,7 +43,7 @@ if (is_ajax ()) {
return;
}
function process_manage_delete ($id_alert_template, $id_agents) {
function process_manage_delete ($id_alert_template, $id_agents, $module_names) {
if (empty ($id_alert_template)) {
echo '<h3 class="error">'.__('No alert selected').'</h3>';
return false;
@ -54,29 +54,44 @@ function process_manage_delete ($id_alert_template, $id_agents) {
return false;
}
process_sql_begin ();
$modules = get_agent_modules ($id_agents, 'id_agente_modulo', false, true);
$success = delete_alert_agent_module (false,
array ('id_agent_module' => $modules,
'id_alert_template' => $id_alert_template));
if (! $success) {
echo '<h3 class="error">'.__('There was an error deleting the alerts, the operation has been cancelled').'</h3>';
echo '<h4>'.__('Could not delete alerts').'</h4>';
process_sql_rollback ();
} else {
echo '<h3 class="suc">'.__('Successfully deleted').'</h3>';
process_sql_commit ();
foreach($module_names as $module){
foreach($id_agents as $id_agent) {
$module_id = get_agentmodule_id($module, $id_agent);
$modules_id[] = $module_id['id_agente_modulo'];
}
}
if(count($module_names) == 1 && $module_names[0] == '0'){
$modules_id = get_agents_common_modules_with_alerts ($id_agents, false, true);
}
$conttotal = 0;
$contsuccess = 0;
foreach($modules_id as $module){
$success = delete_alert_agent_module (false,
array ('id_agent_module' => $module,
'id_alert_template' => $id_alert_template));
if($success)
$contsuccess ++;
$conttotal ++;
}
print_result_message ($contsuccess > 0,
__('Successfully deleted')."(".$contsuccess."/".$conttotal.")",
__('Could not be deleted'));
}
$id_group = (int) get_parameter ('id_group');
$id_agents = get_parameter ('id_agents');
$module_names = get_parameter ('module');
$id_alert_template = (int) get_parameter ('id_alert_template');
$delete = (bool) get_parameter_post ('delete');
if ($delete) {
process_manage_delete ($id_alert_template, $id_agents);
process_manage_delete ($id_alert_template, $id_agents, $module_names);
}
$groups = get_user_groups ();
@ -86,10 +101,12 @@ $table->width = '95%';
$table->data = array ();
$table->style = array ();
$table->style[0] = 'font-weight: bold; vertical-align:top';
$table->style[2] = 'font-weight: bold';
$table->style[2] = 'font-weight: bold; vertical-align:top';
$table->size = array ();
$table->size[0] = '15%';
$table->size[1] = '85%';
$table->size[1] = '40%';
$table->size[2] = '15%';
$table->size[3] = '40%';
$table->data = array ();
@ -97,12 +114,16 @@ $templates = get_alert_templates (false, array ('id', 'name'));
$table->data[0][0] = __('Alert template');
$table->data[0][1] = print_select (index_array ($templates, 'id', 'name'),
'id_alert_template', $id_alert_template, false, __('Select'), 0, true);
$table->data[0][2] = '';
$table->data[0][3] = '';
$table->data[1][0] = __('Group');
$table->data[1][1] = print_select_groups(false, "AR", true, 'id_group', $id_group,
'', '', '', true, false, true, '', $id_alert_template == 0);
$table->data[1][2] = '';
$table->data[1][3] = '';
$table->data[2][0] = __('Agent');
$table->data[2][0] = __('Agents');
$table->data[2][0] .= '<span id="agent_loading" class="invisible">';
$table->data[2][0] .= '<img src="images/spinner.png" />';
$table->data[2][0] .= '</span>';
@ -110,6 +131,8 @@ $agents_alerts = get_agents_with_alert_template ($id_alert_template, $id_group,
false, array ('tagente.nombre', 'tagente.id_agente'));
$table->data[2][1] = print_select (index_array ($agents_alerts, 'id_agente', 'nombre'),
'id_agents[]', '', '', '', '', true, true, true, '', $id_alert_template == 0);
$table->data[2][2] = __('Modules');
$table->data[2][3] = print_select (array(), 'module[]', '', false, '', '', true, true, false);
echo '<form method="post" action="index.php?sec=gmassive&sec2=godmode/massive/massive_operations&option=delete_alerts" onsubmit="if (! confirm(\''.__('Are you sure?').'\')) return false;">';
print_table ($table);
@ -120,15 +143,21 @@ print_submit_button (__('Delete'), 'go', false, 'class="sub delete"');
echo '</div>';
echo '</form>';
//Hack to translate text "none" in PHP to javascript
echo '<span id ="none_text" style="display: none;">' . __('None') . '</span>';
echo '<h3 class="error invisible" id="message"> </h3>';
require_jquery_file ('form');
require_jquery_file ('pandora.controls');
?>
<script type="text/javascript">
/* <![CDATA[ */
$(document).ready (function () {
$("#id_agents").change(agent_changed_by_multiple_agents_with_alerts);
$("#id_alert_template").change (function () {
if (this.value != 0) {
$("#id_agents").enable ();

View File

@ -496,4 +496,146 @@ function getNextAgentContact($idAgent, $maxModules = false) {
else
return false;
}
/**
* Get all the modules common in various agents that have associated alerts. If an empty list is passed it will select all
*
* @param mixed Agent id to get modules. It can also be an array of agent id's.
* @param mixed Array, comma delimited list or singular value of rows to
* select. If nothing is specified, nombre will be selected. A special
* character "*" will select all the values.
* @param mixed Aditional filters to the modules. It can be an indexed array
* (keys would be the field name and value the expected value, and would be
* joined with an AND operator) or a string, including any SQL clause (without
* the WHERE keyword).
* @param bool Wheter to return the modules indexed by the id_agente_modulo or
* not. Default is indexed.
* Example:
<code>
Both are similars:
$modules = get_agent_modules ($id_agent, false, array ('disabled' => 0));
$modules = get_agent_modules ($id_agent, false, 'disabled = 0');
Both are similars:
$modules = get_agent_modules ($id_agent, '*', array ('disabled' => 0, 'history_data' => 0));
$modules = get_agent_modules ($id_agent, '*', 'disabled = 0 AND history_data = 0');
</code>
*
* @return array An array with all modules in the agent.
* If multiple rows are selected, they will be in an array
*/
function get_agents_common_modules_with_alerts ($id_agent, $filter = false, $indexed = true, $get_not_init_modules = true) {
$id_agent = safe_int ($id_agent, 1);
$where = '';
if (! empty ($id_agent)) {
$where = sprintf (' WHERE t2.id_agent_module = t1.id_agente_modulo AND delete_pending = 0 AND id_agente IN (%s) AND (SELECT count(nombre) FROM tagente_modulo t3, talert_template_modules t4 WHERE t4.id_agent_module = t3.id_agente_modulo AND delete_pending = 0 AND t1.nombre = t3.nombre AND id_agente IN (%s)) = (%s)', implode (",", (array) $id_agent), implode (",", (array) $id_agent), count($id_agent));
}
if (! empty ($filter)) {
$where .= ' AND ';
if (is_array ($filter)) {
$fields = array ();
foreach ($filter as $field => $value) {
array_push ($fields, $field.'="'.$value.'"');
}
$where .= implode (' AND ', $fields);
} else {
$where .= $filter;
}
}
$sql = sprintf ('SELECT DISTINCT(t1.id_agente_modulo)
FROM tagente_modulo t1, talert_template_modules t2
%s
ORDER BY nombre',
$where);
$result = get_db_all_rows_sql ($sql);
if (empty ($result)) {
return array ();
}
if (! $indexed)
return $result;
$modules = array ();
foreach ($result as $module) {
if($get_not_init_modules || get_agentmodule_is_init($module['id_agente_modulo'])) {
$modules[$module['id_agente_modulo']] = $module['id_agente_modulo'];
}
}
return $modules;
}
/**
* Get all the modules common in various agents. If an empty list is passed it will select all
*
* @param mixed Agent id to get modules. It can also be an array of agent id's.
* @param mixed Array, comma delimited list or singular value of rows to
* select. If nothing is specified, nombre will be selected. A special
* character "*" will select all the values.
* @param mixed Aditional filters to the modules. It can be an indexed array
* (keys would be the field name and value the expected value, and would be
* joined with an AND operator) or a string, including any SQL clause (without
* the WHERE keyword).
* @param bool Wheter to return the modules indexed by the id_agente_modulo or
* not. Default is indexed.
* Example:
<code>
Both are similars:
$modules = get_agent_modules ($id_agent, false, array ('disabled' => 0));
$modules = get_agent_modules ($id_agent, false, 'disabled = 0');
Both are similars:
$modules = get_agent_modules ($id_agent, '*', array ('disabled' => 0, 'history_data' => 0));
$modules = get_agent_modules ($id_agent, '*', 'disabled = 0 AND history_data = 0');
</code>
*
* @return array An array with all modules in the agent.
* If multiple rows are selected, they will be in an array
*/
function get_agents_common_modules ($id_agent, $filter = false, $indexed = true, $get_not_init_modules = true) {
$id_agent = safe_int ($id_agent, 1);
$where = '';
if (! empty ($id_agent)) {
$where = sprintf (' WHERE delete_pending = 0 AND id_agente IN (%s) AND (SELECT count(nombre) FROM tagente_modulo t2 WHERE delete_pending = 0 AND t1.nombre = t2.nombre AND id_agente IN (%s)) = (%s)', implode (",", (array) $id_agent), implode (",", (array) $id_agent), count($id_agent));
}
if (! empty ($filter)) {
$where .= ' AND ';
if (is_array ($filter)) {
$fields = array ();
foreach ($filter as $field => $value) {
array_push ($fields, $field.'="'.$value.'"');
}
$where .= implode (' AND ', $fields);
} else {
$where .= $filter;
}
}
$sql = sprintf ('SELECT DISTINCT(t1.id_agente_modulo) as id_agente_modulo
FROM tagente_modulo t1, talert_template_modules t2
%s
ORDER BY nombre',
$where);
$result = get_db_all_rows_sql ($sql);
if (empty ($result)) {
return array ();
}
if (! $indexed)
return $result;
$modules = array ();
foreach ($result as $module) {
if($get_not_init_modules || get_agentmodule_is_init($module['id_agente_modulo'])) {
$modules[$module['id_agente_modulo']] = $module['id_agente_modulo'];
}
}
return $modules;
}
?>

View File

@ -164,6 +164,64 @@ function agent_changed_by_multiple_agents (event, id_agent, selected) {
);
}
/**
* Fill up select box with id "module" with modules with alerts of one template
* after agent has been selected, but this not empty the select box.s
*
* @param event that has been triggered
* @param id_agent Agent ID that has been selected
* @param selected Which module(s) have to be selected
*/
function agent_changed_by_multiple_agents_with_alerts (event, id_agent, selected) {
var idAgents = Array();
jQuery.each ($("#id_agents option:selected"), function (i, val) {
//val() because the var is same <option val="NNN"></option>
idAgents.push($(val).val());
});
template = $('#id_alert_template option:selected').attr("value");
$('#module').attr ('disabled', 1);
$('#module').empty ();
$('#module').append ($('<option></option>').html ("Loading...").attr ("value", 0));
jQuery.post ('ajax.php',
{"page": "operation/agentes/ver_agente",
"get_agent_modules_alerts_json_for_multiple_agents": 1,
"template": template,
"id_agent[]": idAgents
},
function (data) {
$('#module').empty ();
if (typeof($(document).data('text_for_module')) != 'undefined') {
$('#module').append ($('<option></option>').html ($(document).data('text_for_module')).attr("value", 0).attr('selected', true));
}
else {
if (typeof(data['any_text']) != 'undefined') {
$('#module').append ($('<option></option>').html (data['any_text']).attr ("value", 0).attr('selected', true));
}
else {
var anyText = $("#any_text").html(); //Trick for catch the translate text.
if (anyText == null) {
anyText = 'Any';
}
$('#module').append ($('<option></option>').html (anyText).attr ("value", 0).attr('selected', true));
}
}
jQuery.each (data, function (i, val) {
s = js_html_entity_decode(val);
$('#module').append ($('<option></option>').html (s).attr ("value", val));
$('#module').fadeIn ('normal');
});
if (selected != undefined)
$('#module').attr ('value', selected);
$('#module').attr ('disabled', 0);
},
"json"
);
}
/**
* Fill up select box with id "agent" with agents after module has been selected, but this not empty the select box.s
*

View File

@ -32,6 +32,7 @@ if (is_ajax ()) {
$get_agent_status_tooltip = (bool) get_parameter ("get_agent_status_tooltip");
$get_agents_group_json = (bool) get_parameter ("get_agents_group_json");
$get_agent_modules_json_for_multiple_agents = (bool) get_parameter("get_agent_modules_json_for_multiple_agents");
$get_agent_modules_alerts_json_for_multiple_agents = (bool) get_parameter("get_agent_modules_alerts_json_for_multiple_agents");
$get_agents_json_for_multiple_modules = (bool) get_parameter("get_agents_json_for_multiple_modules");
$get_agent_modules_json_for_multiple_agents_id = (bool) get_parameter("get_agent_modules_json_for_multiple_agents_id");
$get_agentmodule_status_tooltip = (bool) get_parameter ("get_agentmodule_status_tooltip");
@ -88,6 +89,21 @@ if (is_ajax ()) {
return;
}
if ($get_agent_modules_alerts_json_for_multiple_agents) {
$idAgents = get_parameter('id_agent');
$id_template = get_parameter('template');
$nameModules = get_db_all_rows_sql('SELECT DISTINCT(nombre) FROM tagente_modulo t1, talert_template_modules t2 WHERE t2.id_agent_module = t1.id_agente_modulo AND delete_pending = 0 AND id_agente IN (' . implode(',', $idAgents) . ') AND (SELECT count(nombre) FROM tagente_modulo t3, talert_template_modules t4 WHERE t4.id_agent_module = t3.id_agente_modulo AND delete_pending = 0 AND t1.nombre = t3.nombre AND id_agente IN (' . implode(',', $idAgents) . ')) = (' . count($idAgents) . ')');
$result = array();
foreach($nameModules as $nameModule) {
$result[] = $nameModule['nombre'];
}
echo json_encode($result);
return;
}
if ($get_agent_modules_json_for_multiple_agents) {
$idAgents = get_parameter('id_agent');