From ae69df18397706d96a30cbdf202b7d2edd830ccc Mon Sep 17 00:00:00 2001
From: zarzuelo <zarzuelo@gmail.com>
Date: Fri, 2 Dec 2011 08:52:54 +0000
Subject: [PATCH] 2011-12-02  Sergio Martin <sergio.martin@artica.es>

	* include/functions_modules.php
	include/functions_api.php
	include/functions.php
	extensions/snmp_explorer.php
	godmode/agentes/module_manager_editor_common.php
	godmode/agentes/configurar_agente.php: limited the
	modules creation to not allows to create one if exists
	a module with the same name in the agent to avoid
	recreate a module when go back in the browser.
	Bugfix: 3416129



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@5227 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     | 13 ++++++
 pandora_console/extensions/snmp_explorer.php  | 44 ++++++++++++++++++-
 .../godmode/agentes/configurar_agente.php     | 21 +++++++--
 .../agentes/module_manager_editor_common.php  |  2 +-
 pandora_console/include/functions.php         | 26 +++++++++++
 pandora_console/include/functions_api.php     |  9 ++--
 pandora_console/include/functions_modules.php | 18 +++++---
 7 files changed, 119 insertions(+), 14 deletions(-)

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index 1a95b02a4c..4a61d3b81a 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,16 @@
+2011-12-02  Sergio Martin <sergio.martin@artica.es>
+
+	* include/functions_modules.php
+	include/functions_api.php
+	include/functions.php
+	extensions/snmp_explorer.php
+	godmode/agentes/module_manager_editor_common.php
+	godmode/agentes/configurar_agente.php: limited the 
+	modules creation to not allows to create one if exists
+	a module with the same name in the agent to avoid
+	recreate a module when go back in the browser.
+	Bugfix: 3416129
+
 2011-12-01 Juan Manuel Ramon <juanmanuel.ramon@artica.es>
 
 	* extensions/snmp_explorer.php: Removed trace.
diff --git a/pandora_console/extensions/snmp_explorer.php b/pandora_console/extensions/snmp_explorer.php
index cd28f5738e..67d7abd65d 100755
--- a/pandora_console/extensions/snmp_explorer.php
+++ b/pandora_console/extensions/snmp_explorer.php
@@ -144,6 +144,9 @@ function snmp_explorer() {
 	
 	    $result = false;
 	
+		$errors = array();
+		$done = 0;
+
 	    foreach($id_snmp as $id) {
 			if (isset($interfaces[$id]['ifName']) && $interfaces[$id]['ifName']['value'] != ""){
 					$ifname = $interfaces[$id]['ifName']['value'];
@@ -205,10 +208,47 @@ function snmp_explorer() {
 			    $values['id_modulo'] = 2;
 						
 			    $result = modules_create_agent_module ($id_agent, $name, $values);
+			    
+			    if(is_error($result)) {
+					if(!isset($errors[$result])) {
+						$errors[$result] = 0;
+					}
+					$errors[$result]++;
+				}
+				else {
+					$done++;
+				}
 		    }
 	    }
-	
-	    ui_print_result_message ($result, __('Successfully modules created'), __('Could not be created'));
+	    
+	    if($done > 0) {
+			ui_print_success_message(__('Successfully modules created')." ($done)");
+		}
+		
+		if(!empty($errors)) {
+			$msg = __('Could not be created').':';
+
+		
+			foreach($errors as $code => $number) {
+				switch($code) {
+					case ERR_EXIST:
+						$msg .= '<br>'.__('Another module already exists with the same name')." ($number)";
+						break;
+					case ERR_INCOMPLETE:
+						$msg .= '<br>'.__('Some required fields are missed').': ('.__('name').') '." ($number)";
+						break;
+					case ERR_DB:
+					case ERR_GENERIC:
+					default:
+						$msg .= '<br>'.__('Processing errors')." ($number)";
+						break;
+				}
+				
+			}
+			
+			ui_print_error_message($msg);
+		
+		}
     }
 
     // Create the interface list for the interface
diff --git a/pandora_console/godmode/agentes/configurar_agente.php b/pandora_console/godmode/agentes/configurar_agente.php
index fe86ada233..0dc303eb0d 100644
--- a/pandora_console/godmode/agentes/configurar_agente.php
+++ b/pandora_console/godmode/agentes/configurar_agente.php
@@ -828,9 +828,24 @@ if ($create_module) {
 	else {
 		$id_agent_module = modules_create_agent_module ($id_agente, $name, $values, false, $id_tag);
 	}
-	
-	if ($id_agent_module === false) {
-		echo '<h3 class="error">'.__('There was a problem adding module').'</h3>';
+
+	if (is_error($id_agent_module)) {
+		$msg = __('There was a problem adding module').'. ';
+		switch($id_agent_module) {
+			case ERR_EXIST:
+				$msg .= __('Another module already exists with the same name').'.';
+				break;
+			case ERR_INCOMPLETE:
+				$msg .= __('Some required fields are missed').': ('.__('name').')';
+				break;
+			case ERR_DB:
+			case ERR_GENERIC:
+			default:
+				// No more info
+				break;
+		}
+		$id_agent_module = false;
+		echo '<h3 class="error">'.$msg.'</h3>';
 		$edit_module = true;
 		$moduletype = $id_module;
 		db_pandora_audit("Agent management",
diff --git a/pandora_console/godmode/agentes/module_manager_editor_common.php b/pandora_console/godmode/agentes/module_manager_editor_common.php
index 73080ff3f4..91557122d9 100644
--- a/pandora_console/godmode/agentes/module_manager_editor_common.php
+++ b/pandora_console/godmode/agentes/module_manager_editor_common.php
@@ -244,7 +244,7 @@ $table_advanced->data[6][0] =  __('Tags available');
 if ($__code_from == 'modules') {
 	$__table_modules = 'ttag_module';
 	$__id_where = 'b.id_agente_modulo';
-	$__id = $id_agent_module;
+	$__id = (int)$id_agent_module;
 // Code comes from policy module editor
 }else {
 	global $__id_pol_mod;
diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php
index 928c643409..5a644ffe05 100644
--- a/pandora_console/include/functions.php
+++ b/pandora_console/include/functions.php
@@ -37,6 +37,17 @@ define ('EVENT_PROCESS', 2);
 define ('AGENT_ENABLED',0);
 define ('AGENT_DISABLED',1);
 
+/* Error report codes */
+define ('ERR_GENERIC',-10000);
+
+define ('ERR_EXIST',-20000);
+
+define ('ERR_INCOMPLETE', -30000);
+
+define ('ERR_DB', -40000);
+
+define ('ERR_FILE', -50000);
+
 /* Visual console constants */
 define("MIN_WIDTH",300);
 define("MIN_HEIGHT",120);
@@ -1100,6 +1111,21 @@ function is_ajax () {
 	return defined ('AJAX');
 }
 
+/**
+ * Check if a code is an error code
+ * 
+ * @param int code of an operation. Tipically the id of a module, agent... or a code error
+ * 
+ * @return bool true if a result code is an error or false otherwise
+ */
+function is_error($code) {
+	if($code <= ERR_GENERIC) {
+		return true;
+	}
+	else {
+		return false;
+	}
+}
 /**
  * Transform an array of database result into an indexed array.
  *
diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php
index 66bfcce54d..2efaabe82b 100644
--- a/pandora_console/include/functions_api.php
+++ b/pandora_console/include/functions_api.php
@@ -800,10 +800,13 @@ function set_create_network_module($id, $thrash1, $other, $thrash3) {
 	
 	$idModule = modules_create_agent_module($idAgent, $name, $values, true);
 	
-	if ($idModule === false)
-		returnError('error_create_network_module', 'Error in creation network module.');
-	else
+	if (is_error($idModule)) {
+		// TODO: Improve the error returning more info
+		returnError('error_create_network_module', __('Error in creation network module.'));
+	}
+	else {
 		returnData('string', array('type' => 'string', 'data' => $idModule));
+	}
 }
 
 /**
diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php
index 0f9befff84..54f6e95818 100644
--- a/pandora_console/include/functions_modules.php
+++ b/pandora_console/include/functions_modules.php
@@ -239,17 +239,25 @@ function modules_create_agent_module ($id_agent, $name, $values = false, $disabl
 			return false;
 	}
 
-	if (empty ($name))
-		return false;
+	if (empty ($name)) {
+		return ERR_INCOMPLETE;
+	}
+	
 	if (! is_array ($values))
 		$values = array ();
 	$values['nombre'] = $name;
 	$values['id_agente'] = (int) $id_agent;
+	
+	$exists = (bool)db_get_value_filter('id_agente_modulo', 'tagente_modulo', array('nombre' => $name, 'id_agente' => (int)$id_agent));
 
+	if($exists) {
+		return ERR_EXIST;
+	}
+	
 	$id_agent_module = db_process_sql_insert ('tagente_modulo', $values);
 	
 	if ($id_agent_module === false)
-		return false;
+		return ERR_DB;
 
 	$return_tag = true;
 	if (($tags !== false) || (empty($tags)))
@@ -259,7 +267,7 @@ function modules_create_agent_module ($id_agent, $name, $values = false, $disabl
 		db_process_sql_delete ('tagente_modulo',
 			array ('id_agente_modulo' => $id_agent_module));
 
-		return false;
+		return ERR_DB;
 	}
 
 	switch ($config["dbtype"]) {
@@ -305,7 +313,7 @@ function modules_create_agent_module ($id_agent, $name, $values = false, $disabl
 		db_process_sql_delete ('tagente_modulo',
 			array ('id_agente_modulo' => $id_agent_module));
 		
-		return false;
+		return ERR_DB;
 	}
 	
 	return $id_agent_module;