From f0b7b9cc876e11772435dc04c5b58a8867739106 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Volker=20Fr=C3=B6hlich?= <volker.froehlich@meduniwien.ac.at>
Date: Wed, 3 Jul 2013 16:48:17 +0200
Subject: [PATCH 1/1] Initial patch for ZBXNEXT-1810

Known issues: Doesn't work if value is set to zero
---
 latest.php |  113 ++++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 69 insertions(+), 44 deletions(-)

diff --git a/latest.php b/latest.php
index 2336689..c04e730 100644
--- a/latest.php
+++ b/latest.php
@@ -52,6 +52,7 @@ $fields=array(
 	'fullscreen'=>			array(T_ZBX_INT, O_OPT,	P_SYS,	IN('0,1'),	NULL),
 // filter
 	'select'=>				array(T_ZBX_STR, O_OPT, NULL,	NULL,		NULL),
+	'select_val'=>			array(T_ZBX_STR, O_OPT, NULL,	NULL,		NULL),
 	'show_without_data'=>	array(T_ZBX_INT, O_OPT, NULL,	IN('0,1'),	NULL),
 	'filter_rst'=>			array(T_ZBX_INT, O_OPT,	P_SYS,	IN('0,1'),	NULL),
 	'filter_set'=>			array(T_ZBX_STR, O_OPT,	P_SYS,	null,		NULL),
@@ -83,15 +84,18 @@ if (!isset($_REQUEST['show_without_data'])) {
 
 if(isset($_REQUEST['filter_rst'])){
 	$_REQUEST['select'] = '';
+	$_REQUEST['select_val'] = '';
 	$_REQUEST['show_without_data'] = 0;
 }
 
 if(isset($_REQUEST['filter_set']) || isset($_REQUEST['filter_rst'])){
 	CProfile::update('web.latest.filter.select',$_REQUEST['select'], PROFILE_TYPE_STR);
+	CProfile::update('web.latest.filter.select_val',$_REQUEST['select_val'], PROFILE_TYPE_STR);
 	CProfile::update('web.latest.filter.show_without_data',$_REQUEST['show_without_data'], PROFILE_TYPE_INT);
 }
 else {
 	$_REQUEST['select'] = CProfile::get('web.latest.filter.select', '');
+	$_REQUEST['select_val'] = CProfile::get('web.latest.filter.select_val', '');
 	$_REQUEST['show_without_data'] = CProfile::get('web.latest.filter.show_without_data', 0);
 }
 // --------------
@@ -134,6 +138,7 @@ $filterForm->setAttribute('name','zbx_filter');
 $filterForm->setAttribute('id','zbx_filter');
 
 $filterForm->addRow(_('Show items with name like'), new CTextBox('select',$_REQUEST['select'],20));
+$filterForm->addRow(_('Show items with values like'), new CTextBox('select_val',$_REQUEST['select_val'],20));
 $filterForm->addRow(_('Show items without data'), new CCheckBox('show_without_data', $_REQUEST['show_without_data'], null, 1));
 
 $reset = new CButton("filter_rst", _('Reset'), 'javascript: var uri = new Curl(location.href); uri.setArgument("filter_rst",1); location.href = uri.getUrl();');
@@ -280,8 +285,8 @@ while($db_item = DBfetch($db_items)){
 
 	$db_app['item_cnt']++;
 
-	if(isset($showAll) && !empty($apps) && !isset($apps[$db_app['applicationid']])) continue;
-	else if(isset($hideAll) && (empty($apps) || isset($apps[$db_app['applicationid']]))) continue;
+	if (empty($_REQUEST['select_val']) && isset($showAll) && !empty($apps) && !isset($apps[$db_app['applicationid']])) continue;
+	else if (empty($_REQUEST['select_val']) && isset($hideAll) && (empty($apps) || isset($apps[$db_app['applicationid']]))) continue;
 
 	if (isset($db_item['lastclock'])) {
 		$lastclock = zbx_date2str(_('d M Y H:i:s'), $db_item['lastclock']);
@@ -292,6 +297,12 @@ while($db_item = DBfetch($db_items)){
 
 	$lastvalue = formatItemValue($db_item);
 
+	// Remove items from application if the value doesn't match the filter
+	if(!empty($_REQUEST['select_val']) && !zbx_stristr($lastvalue, $_REQUEST['select_val']) ) {
+        $db_app['item_cnt']--;
+        continue;
+    }
+
 	$digits = ($db_item['value_type'] == ITEM_VALUE_TYPE_FLOAT) ? 2 : 0;
 	if (isset($db_item['lastvalue']) && isset($db_item['prevvalue'])
 			&& ($db_item['value_type'] == ITEM_VALUE_TYPE_FLOAT || $db_item['value_type'] == ITEM_VALUE_TYPE_UINT64)
@@ -323,16 +334,18 @@ while($db_item = DBfetch($db_items)){
 
 	$item_status = $db_item['status'] == ITEM_STATUS_NOTSUPPORTED ? 'unknown' : null;
 
-	array_push($app_rows, new CRow(array(
-		SPACE,
-		is_show_all_nodes()?SPACE:null,
-		($_REQUEST['hostid']>0)?NULL:SPACE,
-		new CCol(SPACE.SPACE.$description, $item_status),
-		new CCol($lastclock, $item_status),
-		new CCol($lastvalue, $item_status),
-		new CCol($change, $item_status),
-		$actions
-	)));
+	if ((isset($showAll) && (empty($apps) || isset($apps[$db_app['applicationid']]))) || (isset($hideAll) && !isset($apps[$db_app['applicationid']]) && !empty($apps))) {
+		array_push($app_rows, new CRow(array(
+			SPACE,
+			is_show_all_nodes()?SPACE:null,
+			($_REQUEST['hostid']>0)?NULL:SPACE,
+			new CCol(SPACE.SPACE.$description, $item_status),
+			new CCol($lastclock, $item_status),
+			new CCol($lastvalue, $item_status),
+			new CCol($change, $item_status),
+			$actions
+		)));
+	}
 }
 unset($app_rows);
 unset($db_app);
@@ -373,24 +386,26 @@ foreach ($db_apps as $appid => $db_app) {
 	$url.= url_param('groupid').url_param('hostid').url_param('fullscreen').url_param('select');
 	$link = new CLink($img,$url);
 
-	$col = new CCol(array(bold($db_app['name']),SPACE.'('._n('%1$s Item', '%1$s Items', $db_app['item_cnt']).')'));
+	if ($db_app['item_cnt'] != 0) {
+		$col = new CCol(array(bold($db_app['name']),SPACE.'('._n('%1$s Item', '%1$s Items', $db_app['item_cnt']).')'));
 
-	$col->setColSpan(5);
-	// host JS menu link
+		$col->setColSpan(5);
+		// host JS menu link
 
-	$hostSpan = null;
-	if ($_REQUEST['hostid'] == 0) {
-		$hostSpan = new CSpan($host['name'], 'link_menu menu-host');
-		$scripts = $hostScripts[$host['hostid']];
-		$hostSpan->setAttribute('data-menu', hostMenuData($host, $scripts));
-	}
+		$hostSpan = null;
+		if ($_REQUEST['hostid'] == 0) {
+			$hostSpan = new CSpan($host['name'], 'link_menu menu-host');
+			$scripts = $hostScripts[$host['hostid']];
+			$hostSpan->setAttribute('data-menu', hostMenuData($host, $scripts));
+		}
 
-	$table->addRow(array(
-		$link,
-		get_node_name_by_elid($db_app['applicationid']),
-		$hostSpan,
-		$col
-	));
+		$table->addRow(array(
+			$link,
+			get_node_name_by_elid($db_app['applicationid']),
+			$hostSpan,
+			$col
+			));
+	}
 
 	foreach($app_rows as $row)
 		$table->addRow($row);
@@ -447,14 +462,15 @@ while ($db_item = DBfetch($db_items)) {
 		$db_item['unitsLong'] = '';
 
 	$db_host = &$db_hosts[$db_item['hostid']];
+	//TODO: Value "0" doesn't work properly. Same for "0." and "0.0". It's replaced with "0" in the form.
 
 	if (!isset($tab_rows[$db_host['hostid']])) $tab_rows[$db_host['hostid']] = array();
 	$app_rows = &$tab_rows[$db_host['hostid']];
 
 	$db_host['item_cnt']++;
 
-	if (isset($showAll) && !empty($apps) && !isset($apps[0])) continue;
-	else if (isset($hideAll) && (empty($apps) || isset($apps[0]))) continue;
+	if (empty($_REQUEST['select_val']) && isset($showAll) && !empty($apps) && !isset($apps[0])) continue;
+	else if (empty($_REQUEST['select_val']) && isset($hideAll) && (empty($apps) || isset($apps[0]))) continue;
 
 	// column "lastclock"
 	if (isset($db_item['lastclock'])) {
@@ -467,6 +483,12 @@ while ($db_item = DBfetch($db_items)) {
 	// column "lastvalue"
 	$lastvalue = formatItemValue($db_item);
 
+	// Remove items from application if the value doesn't match the filter
+	if(!empty($_REQUEST['select_val']) && !zbx_stristr($lastvalue, $_REQUEST['select_val']) ) {
+		$db_host['item_cnt']--;
+		continue;
+	}
+
 	// column "change"
 	$digits = ($db_item['value_type'] == ITEM_VALUE_TYPE_FLOAT) ? 2 : 0;
 	if (isset($db_item['lastvalue']) && isset($db_item['prevvalue'])
@@ -499,6 +521,7 @@ while ($db_item = DBfetch($db_items)) {
 	}
 
 	$item_status = $db_item['status'] == ITEM_STATUS_NOTSUPPORTED ? 'unknown' : null;
+	if ((isset($showAll) && (empty($apps) || isset($apps[0]))) || (isset($hideAll) && !isset($apps[0]) && !empty($apps))) {
 	array_push($app_rows, new CRow(array(
 		SPACE,
 		is_show_all_nodes() ? ($db_host['item_cnt'] ? SPACE : get_node_name_by_elid($db_item['itemid'])) : null,
@@ -509,6 +532,7 @@ while ($db_item = DBfetch($db_items)) {
 		new CCol($change, $item_status),
 		new CCol($actions, $item_status)
 	)));
+    }
 }
 unset($app_rows);
 unset($db_host);
@@ -548,25 +572,26 @@ foreach ($db_hosts as $hostid => $db_host) {
 	$url.= url_param('groupid').url_param('hostid').url_param('fullscreen').url_param('select');
 	$link = new CLink($img,$url);
 
+    if ($db_host['item_cnt'] != 0) {
+		$col = new CCol(array(bold('- '.('other').' -'), SPACE.'('._n('%1$s Item', '%1$s Items', $db_host['item_cnt']).')'));
+		$col->setColSpan(5);
 
-	$col = new CCol(array(bold('- '.('other').' -'), SPACE.'('._n('%1$s Item', '%1$s Items', $db_host['item_cnt']).')'));
-	$col->setColSpan(5);
+		// host JS menu link
+		$hostSpan = null;
+		if ($_REQUEST['hostid'] == 0) {
+			$hostSpan = new CSpan($host['name'], 'link_menu menu-host');
+			$scripts = $hostScripts[$host['hostid']];
+			$hostSpan->setAttribute('data-menu', hostMenuData($host, $scripts));
+		}
 
-	// host JS menu link
-	$hostSpan = null;
-	if ($_REQUEST['hostid'] == 0) {
-		$hostSpan = new CSpan($host['name'], 'link_menu menu-host');
-		$scripts = $hostScripts[$host['hostid']];
-		$hostSpan->setAttribute('data-menu', hostMenuData($host, $scripts));
+		$table->addRow(array(
+			$link,
+			get_node_name_by_elid($db_host['hostid']),
+			$hostSpan,
+			$col
+		));
 	}
 
-	$table->addRow(array(
-		$link,
-		get_node_name_by_elid($db_host['hostid']),
-		$hostSpan,
-		$col
-	));
-
 	foreach($app_rows as $row)
 		$table->addRow($row);
 }
-- 
1.7.1

