From a040a2a621ee2a95c694253373bdedcbb3dbfacc Mon Sep 17 00:00:00 2001
From: Esteban Sanchez <esteban.sanchez@gmail.com>
Date: Tue, 17 Mar 2009 09:22:02 +0000
Subject: [PATCH] 2009-03-17  Esteban Sanchez  <estebans@artica.es>

	* godmode/agentes/manage_config.php: Select dependant modules when an
	alert is selected.

	* include/functions_agents.php: Copy selected alerts on
	process_manage_config().



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1540 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     |  10 +-
 .../godmode/agentes/manage_config.php         |  15 +-
 pandora_console/include/functions_agents.php  | 137 +++++++++++-------
 3 files changed, 109 insertions(+), 53 deletions(-)

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index e0dded9d76..d00ee96682 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,11 @@
+2009-03-17  Esteban Sanchez  <estebans@artica.es>
+
+	* godmode/agentes/manage_config.php: Select dependant modules when an
+	alert is selected.
+
+	* include/functions_agents.php: Copy selected alerts on
+	process_manage_config().
+
 2009-03-16  Evi Vanoost  <vanooste@rcbi.rochester.edu>
 
 	* general/logoff.php: Cleaned up invalid html
@@ -16,7 +24,7 @@
 
 	* include/help/en/help_planned_downtime.php,
 	include/help/es/help_planned_downtime.php: Added more information
-    about the scheduled downtime.
+	about the scheduled downtime.
 
 2009-03-16  Esteban Sanchez  <estebans@artica.es>
 
diff --git a/pandora_console/godmode/agentes/manage_config.php b/pandora_console/godmode/agentes/manage_config.php
index 63630846c9..5a481ed57f 100644
--- a/pandora_console/godmode/agentes/manage_config.php
+++ b/pandora_console/godmode/agentes/manage_config.php
@@ -30,7 +30,7 @@ require_once ('include/functions_agents.php');
 require_once ('include/functions_alerts.php');
 require_once ('include/functions_modules.php');
 
-echo '<h2>'.__('Agent configuration'). ' &raquo; '. __('Configuration Management').'</h2>';
+echo '<h3>'.__('Configuration Management').'</h3>';
 
 $source_id_group = (int) get_parameter ('source_id_group');
 $source_id_agent = (int) get_parameter ('source_id_agent');
@@ -69,7 +69,7 @@ $table->data[0][2] .= '</span>';
 $table->data[0][3] = print_select (get_group_agents ($source_id_group, false, "none"),
 	'source_id_agent', $source_id_agent, false, __('Select'), 0, true);
 
-echo '<form id="manage_config_form" method="post" action="index.php?sec=gagente&amp;sec2=godmode/agentes/manage_config">';
+echo '<form id="manage_config_form" method="post">';
 
 echo '<fieldset id="fieldset_source">';
 echo '<legend><span>'.__('Source');
@@ -167,6 +167,7 @@ require_jquery_file ('pandora.controls');
 ?>
 <script type="text/javascript">
 /* <![CDATA[ */
+var module_alerts;
 $(document).ready (function () {
 	$("#source_id_group").pandoraSelectGroup ({
 		agentSelect: "select#source_id_agent",
@@ -224,6 +225,7 @@ $(document).ready (function () {
 					"id_agent" : id_agent
 					},
 					function (data, status) {
+						module_alerts = Array ();
 						if (! data) {
 							no_alerts = true;
 						} else {
@@ -236,6 +238,7 @@ $(document).ready (function () {
 									.append (module_name)
 									.append (")");
 								$("#target_alerts").append (option);
+								module_alerts[val["id"]] = val["id_agent_module"];
 							});
 							no_alerts = false;
 						}
@@ -279,6 +282,14 @@ $(document).ready (function () {
 		);
 	});
 	
+	$("#target_alerts").change (function () {
+		jQuery.each ($(this).fieldValue (), function () {
+			if (module_alerts[this] != undefined)
+				$("#target_modules option[value="+module_alerts[this]+"]")
+					.attr ("selected", "selected");
+		});
+	});
+	
 	$("#manage_config_form").submit (function () {
 		$("h3:not([id=message])").remove ();
 		if ($("#source_id_agent").attr ("value") == 0) {
diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php
index 93e6e05e51..b4b46a9e82 100644
--- a/pandora_console/include/functions_agents.php
+++ b/pandora_console/include/functions_agents.php
@@ -127,15 +127,16 @@ function get_agent_alerts ($id_agent, $filter = false, $options = false) {
 /**
  * Copy the agents config from one agent to the other
  *
- * @param int $source_id_agent Agent id
- * @param mixed $destiny_id_agents Agent id or id's (array) to copy to
- * @param bool $modules Whether to copy modules as well (defaults to get_parameter ('copy_modules'))
- * @param bool $alerts Whether to copy alerts as well
- * @param array $target_modules Which modules to copy
+ * @param int Agent id
+ * @param mixed Agent id or id's (array) to copy to
+ * @param bool Whether to copy modules as well (defaults to $_REQUEST['copy_modules'])
+ * @param bool Whether to copy alerts as well
+ * @param array Which modules to copy.
+ * @param array Which alerts to copy. Only will be used if target_modules is empty.
  *
  * @return bool True in case of good, false in case of bad
  */
-function process_manage_config ($source_id_agent, $destiny_id_agents, $copy_modules = false, $copy_alerts = false, $target_modules = false) {
+function process_manage_config ($source_id_agent, $destiny_id_agents, $copy_modules = false, $copy_alerts = false, $target_modules = false, $target_alerts = false) {
 	if (empty ($source_id_agent)) {
 		echo '<h3 class="error">'.__('No source agent to copy').'</h3>';
 		return false;
@@ -149,46 +150,68 @@ function process_manage_config ($source_id_agent, $destiny_id_agents, $copy_modu
 	if ($copy_modules == false) {
 		$copy_modules = (bool) get_parameter ('copy_modules', $copy_modules);
 	}
+	
 	if ($copy_alerts == false) {
 		$copy_alerts = (bool) get_parameter ('copy_alerts', $copy_alerts);
 	}
 	
-	if ($copy_modules) {
-		if (empty ($target_modules)) {
-			$target_modules = (array) get_parameter ('target_modules', $target_modules);
-		}
-		
-		if (empty ($target_modules)) {
+	if (! $copy_modules && ! $copy_alerts)
+		return;
+	
+	if (empty ($target_modules)) {
+		$target_modules = (array) get_parameter ('target_modules', array ());
+	}
+	
+	if (empty ($target_alerts)) {
+		$target_alerts = (array) get_parameter ('target_alerts', array ());
+	}
+	
+	if (empty ($target_modules)) {
+		if (! $copy_alerts) {
 			echo '<h3 class="error">'.__('No modules have been selected').'</h3>';
 			return false;
 		}
+		$target_modules = array ();
 		
-		process_sql ('SET AUTOCOMMIT = 0');
-		process_sql ('START TRANSACTION');
-		$error = false;
-		$alerts = array ();
-		foreach ($destiny_id_agents as $id_destiny_agent) {
-			foreach ($target_modules as $id_agent_module) {
-				$result = copy_agent_module_to_agent ($id_agent_module,
-					$id_destiny_agent);
-				
-				if ($result === false) {
-					$error = true;
-					break;
-				}
-				
-				$id_destiny_module = $result;
-				if (! isset ($alerts[$id_agent_module]))
-					$alerts[$id_agent_module] = get_alerts_agent_module ($id_agent_module,
-						true);
-				
-				if ($alerts[$id_agent_module] === false)
-					continue;
-				
-				if (! $copy_alerts)
-					continue;
-				
-				foreach ($alerts[$id_agent_module] as $alert) {
+		foreach ($target_alerts as $id_alert) {
+			$alert = get_alert_agent_module ($id_alert);
+			if ($alert === false)
+				continue;
+			/* Check if some alerts which doesn't belong to the agent was given */
+			if (get_agentmodule_agent ($alert['id_agent_module']) != $source_id_agent)
+				continue;
+			array_push ($target_modules, $alert['id_agent_module']);
+		}
+	}
+	
+	process_sql ('SET AUTOCOMMIT = 0');
+	process_sql ('START TRANSACTION');
+	$error = false;
+	
+	foreach ($destiny_id_agents as $id_destiny_agent) {
+		foreach ($target_modules as $id_agent_module) {
+			$result = copy_agent_module_to_agent ($id_agent_module,
+				$id_destiny_agent);
+		
+			if ($result === false) {
+				$error = true;
+				break;
+			}
+			
+			$id_destiny_module = $result;
+			
+			if (! $copy_alerts)
+				continue;
+			
+			/* If the alerts were given, copy afterwards. Otherwise, all the
+			alerts for the module will be copied */
+			if (! empty ($target_alerts)) {
+				foreach ($target_alerts as $id_alert) {
+					$alert = get_alert_agent_module ($id_alert);
+					if ($alert === false)
+						continue;
+					if ($alert['id_agent_module'] != $id_agent_module)
+						continue;
 					$result = copy_alert_agent_module_to_agent_module ($alert['id'],
 						$id_destiny_module);
 					if ($result === false) {
@@ -196,20 +219,34 @@ function process_manage_config ($source_id_agent, $destiny_id_agents, $copy_modu
 						break;
 					}
 				}
+				continue;
+			}
+			
+			$alerts = get_alerts_agent_module ($id_agent_module, true);
+			
+			if ($alerts === false)
+				continue;
+			
+			foreach ($alerts as $alert) {
+				$result = copy_alert_agent_module_to_agent_module ($alert['id'],
+					$id_destiny_module);
+				if ($result === false) {
+					$error = true;
+					break;
+				}
 			}
-			if ($error)
-				break;
 		}
-		
-		if ($error) {
-			echo '<h3 class="error">'.__('There was an error copying the agent configuration, the copy has been cancelled').'</h3>';
-			process_sql ('ROLLBACK');
-		} else {
-			echo '<h3 class="suc">'.__('Successfully copied').'</h3>';
-			process_sql ('COMMIT');
-		}
-		process_sql ('SET AUTOCOMMIT = 1');
+		if ($error)
+			break;
 	}
-}
 	
+	if ($error) {
+		echo '<h3 class="error">'.__('There was an error copying the agent configuration, the copy has been cancelled').'</h3>';
+		process_sql ('ROLLBACK');
+	} else {
+		echo '<h3 class="suc">'.__('Successfully copied').'</h3>';
+		process_sql ('COMMIT');
+	}
+	process_sql ('SET AUTOCOMMIT = 1');
+}
 ?>