commit 6b1ba98ad3f934fc1c2e59c877f8d986d348a63f
Author: Hasitha Lalinda <hasitha84 at msn dot com>
Date:   Mon Jul 9 16:04:58 2012 +0100

    Resolve macros in graph names

diff --git chart2.php chart2.php
index 7a69d4a..3089b92 100644
--- chart2.php
+++ chart2.php
@@ -76,7 +76,8 @@ $chart_header = '';
 if (id2nodeid($db_data['graphid']) != get_current_nodeid()) {
 	$chart_header = get_node_name_by_elid($db_data['graphid'], true, ': ');
 }
-$chart_header .= $host['name'].': '.$db_data['name'];
+//$chart_header .= $host['name'].': '.$db_data['name']; /** Line commented.(2012-06-09 @blasterblues) **/
+$chart_header = get_resolved_graph_name($host['name'].': '.$db_data['name'],$host['hostid']);  /** Line added.(2012-06-09 @blasterblues) **/
 
 $graph = new CChart($db_data['graphtype']);
 $graph->setHeader($chart_header);
diff --git chart6.php chart6.php
index 9a5f1e6..0177bea 100644
--- chart6.php
+++ chart6.php
@@ -89,7 +89,8 @@ if ($height <= 0) {
 
 $graph->setWidth($width);
 $graph->setHeight($height);
-$graph->setHeader($host['host'].':'.$db_data['name']);
+//$graph->setHeader($host['host'].':'.$db_data['name']); /** Line commented.(2012-06-09 @blasterblues) **/
+$graph->setHeader($host['host'].':'. get_resolved_graph_name($host['name'].': '.$db_data['name'],$host['hostid']));  /** Line added.(2012-06-09 @blasterblues) **/
 
 if ($db_data['show_3d']) {
 	$graph->switchPie3D();
diff --git graphs.php graphs.php
index f149c8d..64ce51f 100644
--- graphs.php
+++ graphs.php
@@ -522,6 +522,7 @@ else {
 	if ($sortfield == 'graphtype') {
 		foreach ($data['graphs'] as $gnum => $graph) {
 			$data['graphs'][$gnum]['graphtype'] = graphType($graph['graphtype']);
+			$data['graphs'][$gnum]['name'] = get_resolved_graph_name($data['graphs'][$gnum]['name'],$data['hostid']);  /** Line added.(2012-06-09 @blasterblues) **/
 		}
 	}
 	order_result($data['graphs'], $sortfield, $sortorder);
diff --git include/blocks.inc.php include/blocks.inc.php
index b73a340..e64ca6d 100644
--- include/blocks.inc.php
+++ include/blocks.inc.php
@@ -77,6 +77,8 @@ function make_favorite_graphs() {
 			}
 			$graph = $graphs[$sourceid];
 			$ghost = reset($graph['hosts']);
+			
+			$graph['name'] = get_resolved_graph_name($graph['name'],$ghost['hostid']); /** Line added.(2012-06-09 @blasterblues) **/
 
 			$link = new CLink(get_node_name_by_elid($sourceid, null, ': ').$ghost['name'].':'.$graph['name'], 'charts.php?graphid='.$sourceid);
 			$link->setTarget('blank');
diff --git include/classes/class.cpagefilter.php include/classes/class.cpagefilter.php
index 61350ae..458d0c1 100644
--- include/classes/class.cpagefilter.php
+++ include/classes/class.cpagefilter.php
@@ -369,7 +369,8 @@ class CPageFilter {
 			order_result($graphs, 'name');
 
 			foreach ($graphs as $graph) {
-				$this->data['graphs'][$graph['graphid']] = $graph['name'];
+				//$this->data['graphs'][$graph['graphid']] = $graph['name']; /** Line commented.(2012-06-09 @blasterblues) **/
+				$this->data['graphs'][$graph['graphid']] = get_resolved_graph_name($graph['name'],$this->hostid); /** Line added.(2012-06-09 @blasterblues) **/
 			}
 
 			// no graphid provided
diff --git include/graphs.inc.php include/graphs.inc.php
index 0a3b76c..74819ae 100644
--- include/graphs.inc.php
+++ include/graphs.inc.php
@@ -18,6 +18,77 @@
 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 **/
 
+/*
+** 2012-06-09 @blasterblues
+**
+** Function: get_resolved_graph_name(mixed $gname,int $hostid)
+** This function searches macros in the graph name and replaces 
+** them with macro's value and returns the modified graph name.
+**/
+function get_resolved_graph_name($gname,$hostid) {
+        if(is_null($hostid) || is_null($gname))
+                return $gname;	
+        if(preg_match_all('/'.ZBX_PREG_EXPRESSION_USER_MACROS.'/', $gname, $arr)) {
+                foreach($arr[0] as $value) {
+                        $search = $value;
+                                if(resolve_host_macro($hostid,$value)) {
+                                        $gname = str_replace($search, $value, $gname);
+                                } else {
+										//Search global macros
+                                        $global_macros = API::UserMacro()->get(array('globalmacro' => 1, 'output' => API_OUTPUT_EXTEND));
+                                        foreach ($global_macros as $gmacros) {
+                                                if(strcmp($gmacros['macro'],$search) == 0) {
+                                                         $gname = str_replace($search, $gmacros['value'], $gname);
+                                                         return $gname;
+                                                }
+                                        }
+                                }
+                }
+        }
+        return $gname;
+}
+
+/*
+** 2012-06-09 @blasterblues
+**
+** Function: resolve_host_macro(int $hostid,mixed &$macro)
+** This function searches for host macros by hostid.
+** If a match found, $macro is replaced by macro's value and returns true. If not, returns fales.
+**/
+function resolve_host_macro($hostid,&$macro) {
+        //Search hosts
+        $host_macros = API::UserMacro()->get(array('hostids' => $hostid, 'output' => API_OUTPUT_EXTEND));
+        if (count($host_macros) > 0) {
+                foreach($host_macros as $value) {
+                     if(strcmp($value['macro'],$macro) == 0) {
+                        $macro = $value['value'];
+                        return true;
+                     }
+                }
+                unset($value);
+        }
+        //Search templates
+        $templates = API::Template()->get(array('hostids' => $hostid,'output' => API_OUTPUT_SHORTEN));
+        if(count($templates) > 0) {
+                foreach ($templates as $value) {
+                        $template_macros = API::UserMacro()->get(array('hostids' => $value['templateid'], 'output' => API_OUTPUT_EXTEND));
+                        if (count($template_macros) > 0) {
+                                foreach($template_macros as $val) {
+                                        if(strcmp($val['macro'],$macro) == 0) {
+                                                $macro = $val['value'];
+                                                return true;
+                                        }
+                                }
+                        }
+                }
+                unset($value);
+				//Search parent templates
+				foreach ($templates as $value) {
+					return resolve_host_macro($value['templateid'],$macro);
+				}
+		}
+		return false;
+}
 
 function graphType($type = null) {
 	$types = array(
