From e6e993538b382cb63f7c79c5729522594fbeec02 Mon Sep 17 00:00:00 2001
From: enriquecd <enrique.camargo@artica.com>
Date: Mon, 3 Jul 2017 10:38:04 +0200
Subject: [PATCH] Add historical db check to reports template SQL items - #853

---
 .../extras/pandoradb_migrate_6.0_to_7.0.mysql.sql        | 1 +
 pandora_console/include/functions_graph.php              | 7 ++++++-
 pandora_console/include/functions_reporting.php          | 9 ++++++---
 pandora_console/pandoradb.sql                            | 1 +
 4 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
index 588a68da48..6f146f7ca8 100644
--- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
+++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql
@@ -710,6 +710,7 @@ CREATE TABLE IF NOT EXISTS `treport_content_template` (
 	`module_names` TEXT,
 	`module_free_text` TEXT,
 	`each_agent` tinyint(1) default 1,
+	`historical_db` tinyint(1) UNSIGNED NOT NULL default 0,
 	PRIMARY KEY(`id_rc`)
 ) ENGINE = InnoDB DEFAULT CHARSET=utf8;
 
diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php
index 27bcaa491a..8ce80863c1 100644
--- a/pandora_console/include/functions_graph.php
+++ b/pandora_console/include/functions_graph.php
@@ -3419,7 +3419,12 @@ function graph_custom_sql_graph ($id, $width, $height,
 	global $config;
 	
 	$report_content = db_get_row ('treport_content', 'id_rc', $id);
-	$historical_db = db_get_value_sql("SELECT historical_db from treport_content where id_rc =".$id);
+	if($id != null){
+		$historical_db = db_get_value_sql("SELECT historical_db from treport_content where id_rc =".$id);
+	}
+	else{
+		$historical_db = $content['historical_db'];
+	}
 	if ($report_content["external_source"] != "") {
 		$sql = io_safe_output ($report_content["external_source"]);
 	}
diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php
index 3f795eb246..6fd87a01e6 100644
--- a/pandora_console/include/functions_reporting.php
+++ b/pandora_console/include/functions_reporting.php
@@ -3940,9 +3940,12 @@ function reporting_sql($report, $content) {
 			$header = explode('|', $content['header_definition']);
 			$return['header'] = $header;
 		}
-		
-		$historical_db = db_get_value_sql("SELECT historical_db from treport_content where id_rc =".$content['id_rc']);
-		
+		if($content['id_rc'] != null){
+			$historical_db = db_get_value_sql("SELECT historical_db from treport_content where id_rc =".$content['id_rc']);
+		}
+		else{
+			$historical_db = $content['historical_db'];
+		}
 		$result = db_get_all_rows_sql($sql,$historical_db);
 		if ($result !== false) {
 			
diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql
index 1ef3c1f7e8..69c161528a 100644
--- a/pandora_console/pandoradb.sql
+++ b/pandora_console/pandoradb.sql
@@ -2696,6 +2696,7 @@ CREATE TABLE IF NOT EXISTS `treport_content_template` (
 	`module_names` TEXT,
 	`module_free_text` TEXT,
 	`each_agent` tinyint(1) default 1,
+	`historical_db` tinyint(1) UNSIGNED NOT NULL default 0,
 	PRIMARY KEY(`id_rc`)
 ) ENGINE = InnoDB DEFAULT CHARSET=utf8;