From bdbfeedbd02f3ce79f861e349dcaec7bd80f9c54 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Volker=20Fr=C3=B6hlich?= <volker.froehlich@meduniwien.ac.at>
Date: Tue, 26 Mar 2013 14:35:05 +0100
Subject: [PATCH] ZBXNEXT-1456 -- Filter discovered items

---
 include/forms.inc.php |   44 +++++++++++++++++++++++++++++++++++++++++---
 items.php             |   15 ++++++++++++++-
 2 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/include/forms.inc.php b/include/forms.inc.php
index 8c8c347..6a1e5b1 100644
--- a/include/forms.inc.php
+++ b/include/forms.inc.php
@@ -346,6 +346,7 @@
 		$filter_state				= $_REQUEST['filter_state'];
 		$filter_templated_items		= $_REQUEST['filter_templated_items'];
 		$filter_with_triggers		= $_REQUEST['filter_with_triggers'];
+		$filter_discovery			= $_REQUEST['filter_discovery'];
 		$subfilter_hosts			= $_REQUEST['subfilter_hosts'];
 		$subfilter_apps				= $_REQUEST['subfilter_apps'];
 		$subfilter_types			= $_REQUEST['subfilter_types'];
@@ -354,6 +355,7 @@
 		$subfilter_state			= $_REQUEST['subfilter_state'];
 		$subfilter_templated_items	= $_REQUEST['subfilter_templated_items'];
 		$subfilter_with_triggers	= $_REQUEST['subfilter_with_triggers'];
+		$subfilter_discovery		= $_REQUEST['subfilter_discovery'];
 		$subfilter_history			= $_REQUEST['subfilter_history'];
 		$subfilter_trends			= $_REQUEST['subfilter_trends'];
 		$subfilter_interval			= $_REQUEST['subfilter_interval'];
@@ -369,6 +371,7 @@
 		$form->addVar('subfilter_state', $subfilter_state);
 		$form->addVar('subfilter_templated_items', $subfilter_templated_items);
 		$form->addVar('subfilter_with_triggers', $subfilter_with_triggers);
+		$form->addVar('subfilter_discovery', $subfilter_discovery);
 		$form->addVar('subfilter_history', $subfilter_history);
 		$form->addVar('subfilter_trends', $subfilter_trends);
 		$form->addVar('subfilter_interval', $subfilter_interval);
@@ -610,7 +613,7 @@
 				-1 => _('all'),
 				1 => _('Templated items'),
 				0 => _('Not Templated items'),
-			)))
+			))),
 		), 'item-list-row');
 		// row 5
 		$table->addRow(array(
@@ -620,8 +623,12 @@
 			new CCol($portField),
 			new CCol(null, 'label'),
 			new CCol(),
-			new CCol(null, 'label'),
-			new CCol()
+			new CCol(bold(_('Discovery').NAME_DELIMITER), 'label'),
+			new CCol(new CComboBox('filter_discovery', $filter_discovery, null, array(
+				-1 => _('all'),
+				ZBX_FLAG_DISCOVERY_CREATED => _('Discovered items'),
+				ZBX_FLAG_DISCOVERY_NORMAL => _('Not Discovered items')
+			))),
 		), 'item-list-row');
 
 		$filter = new CButton('filter', _('Filter'),
@@ -653,6 +660,7 @@
 			'state' => array(),
 			'templated_items' => array(),
 			'with_triggers' => array(),
+			'discovery' => array(),
 			'history' => array(),
 			'trends' => array(),
 			'interval' => array()
@@ -839,6 +847,31 @@
 				}
 			}
 
+			// discovery
+			if ($filter_discovery == -1) {
+				if ($item['flags'] == ZBX_FLAG_DISCOVERY_NORMAL && !isset($item_params['discovery'][0])) {
+					$item_params['discovery'][0] = array('name' => _('Not discovered'), 'count' => 0);
+				}
+				elseif ($item['flags'] == ZBX_FLAG_DISCOVERY_CREATED && !isset($item_params['discovery'][1])) {
+					$item_params['discovery'][1] = array('name' => _('Discovered'), 'count' => 0);
+				}
+				$show_item = true;
+				foreach ($item['subfilters'] as $name => $value) {
+					if ($name == 'subfilter_discovery') {
+						continue;
+					}
+					$show_item &= $value;
+				}
+				if ($show_item) {
+					if ($item['flags'] == 0) {
+						$item_params['discovery'][0]['count']++;
+					}
+					else {
+						$item_params['discovery'][1]['count']++;
+					}
+				}
+			}
+
 			// trends
 			if (zbx_empty($filter_trends)) {
 				if (!isset($item_params['trends'][$item['trends']])) {
@@ -932,6 +965,11 @@
 			$table_subfilter->addRow(array(_('With triggers'), $with_triggers_output));
 		}
 
+		if ($filter_discovery == -1 && count($item_params['discovery']) > 1) {
+			$discovery_output = prepareSubfilterOutput($item_params['discovery'], $subfilter_discovery, 'subfilter_discovery');
+			$table_subfilter->addRow(array(_('Discovery'), $discovery_output));
+		}
+
 		if (zbx_empty($filter_history) && count($item_params['history']) > 1) {
 			$history_output = prepareSubfilterOutput($item_params['history'], $subfilter_history, 'subfilter_history');
 			$table_subfilter->addRow(array(_('History'), $history_output));
diff --git a/items.php b/items.php
index a651f92..eceb406 100644
--- a/items.php
+++ b/items.php
@@ -155,6 +155,8 @@ $fields = array(
 	'filter_status' =>			array(T_ZBX_INT, O_OPT, null,	IN(array(-1, ITEM_STATUS_ACTIVE, ITEM_STATUS_DISABLED)), null),
 	'filter_state' =>			array(T_ZBX_INT, O_OPT, null,	IN(array(-1, ITEM_STATE_NORMAL, ITEM_STATE_NOTSUPPORTED)), null),
 	'filter_templated_items' => array(T_ZBX_INT, O_OPT, null,	IN('-1,0,1'), null),
+	'filter_discovery' =>		array(T_ZBX_INT, O_OPT, null,
+		IN(array(-1, ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED)), null),
 	'filter_with_triggers' =>	array(T_ZBX_INT, O_OPT, null,	IN('-1,0,1'), null),
 	'filter_ipmi_sensor' =>		array(T_ZBX_STR, O_OPT, null,	null,		null),
 	// subfilters
@@ -164,6 +166,7 @@ $fields = array(
 	'subfilter_value_types' =>	array(T_ZBX_INT, O_OPT, null,	null,		null),
 	'subfilter_status' =>		array(T_ZBX_INT, O_OPT, null,	null,		null),
 	'subfilter_state' =>		array(T_ZBX_INT, O_OPT, null,	null,		null),
+	'subfilter_discovery' =>	array(T_ZBX_INT, O_OPT, null,	null,		null),
 	'subfilter_templated_items' => array(T_ZBX_INT, O_OPT, null, null,		null),
 	'subfilter_with_triggers' => array(T_ZBX_INT, O_OPT, null,	null,		null),
 	'subfilter_hosts' =>		array(T_ZBX_INT, O_OPT, null,	null,		null),
@@ -183,7 +186,7 @@ unset($_REQUEST[$paramsFieldName]);
 
 $subfiltersList = array('subfilter_apps', 'subfilter_types', 'subfilter_value_types', 'subfilter_status',
 	'subfilter_state', 'subfilter_templated_items', 'subfilter_with_triggers', 'subfilter_hosts', 'subfilter_interval',
-	'subfilter_history', 'subfilter_trends'
+	'subfilter_history', 'subfilter_trends', 'subfilter_discovery'
 );
 
 /*
@@ -255,6 +258,7 @@ if (isset($_REQUEST['filter_set'])) {
 	$_REQUEST['filter_status'] = get_request('filter_status', -1);
 	$_REQUEST['filter_state'] = get_request('filter_state', -1);
 	$_REQUEST['filter_templated_items'] = get_request('filter_templated_items', -1);
+	$_REQUEST['filter_discovery'] = get_request('filter_discovery', -1);
 	$_REQUEST['filter_with_triggers'] = get_request('filter_with_triggers', -1);
 	$_REQUEST['filter_ipmi_sensor'] = get_request('filter_ipmi_sensor');
 
@@ -276,6 +280,7 @@ if (isset($_REQUEST['filter_set'])) {
 	CProfile::update('web.items.filter_status', $_REQUEST['filter_status'], PROFILE_TYPE_INT);
 	CProfile::update('web.items.filter_state', $_REQUEST['filter_state'], PROFILE_TYPE_INT);
 	CProfile::update('web.items.filter_templated_items', $_REQUEST['filter_templated_items'], PROFILE_TYPE_INT);
+	CProfile::update('web.items.filter_discovery', $_REQUEST['filter_discovery'], PROFILE_TYPE_INT);
 	CProfile::update('web.items.filter_with_triggers', $_REQUEST['filter_with_triggers'], PROFILE_TYPE_INT);
 	CProfile::update('web.items.filter_ipmi_sensor', $_REQUEST['filter_ipmi_sensor'], PROFILE_TYPE_STR);
 
@@ -304,6 +309,7 @@ else {
 	$_REQUEST['filter_status'] = CProfile::get('web.items.filter_status');
 	$_REQUEST['filter_state'] = CProfile::get('web.items.filter_state');
 	$_REQUEST['filter_templated_items'] = CProfile::get('web.items.filter_templated_items', -1);
+	$_REQUEST['filter_discovery'] = CProfile::get('web.items.filter_discovery', -1);
 	$_REQUEST['filter_with_triggers'] = CProfile::get('web.items.filter_with_triggers', -1);
 	$_REQUEST['filter_ipmi_sensor'] = CProfile::get('web.items.filter_ipmi_sensor');
 
@@ -1004,6 +1010,10 @@ else {
 			&& $_REQUEST['filter_templated_items'] != -1) {
 		$options['inherited'] = $_REQUEST['filter_templated_items'];
 	}
+	if (isset($_REQUEST['filter_discovery']) && !zbx_empty($_REQUEST['filter_discovery'])
+			&& $_REQUEST['filter_discovery'] != -1) {
+		$options['filter']['flags'] = array('flags' => $_REQUEST['filter_discovery']);
+	}
 	if (isset($_REQUEST['filter_with_triggers']) && !zbx_empty($_REQUEST['filter_with_triggers'])
 			&& $_REQUEST['filter_with_triggers'] != -1) {
 		$options['with_triggers'] = $_REQUEST['filter_with_triggers'];
@@ -1061,6 +1071,9 @@ else {
 				'subfilter_templated_items' => empty($_REQUEST['subfilter_templated_items'])
 					|| ($item['templateid'] == 0 && uint_in_array(0, $_REQUEST['subfilter_templated_items'])
 					|| ($item['templateid'] > 0 && uint_in_array(1, $_REQUEST['subfilter_templated_items']))),
+				'subfilter_discovery' => empty($_REQUEST['subfilter_discovery'])
+					|| ($item['flags'] == ZBX_FLAG_DISCOVERY_NORMAL && uint_in_array(0, $_REQUEST['subfilter_discovery'])
+					|| ($item['flags'] == ZBX_FLAG_DISCOVERY_CREATED && uint_in_array(1, $_REQUEST['subfilter_discovery']))),
 				'subfilter_with_triggers' => empty($_REQUEST['subfilter_with_triggers'])
 					|| (count($item['triggers']) == 0 && uint_in_array(0, $_REQUEST['subfilter_with_triggers']))
 					|| (count($item['triggers']) > 0 && uint_in_array(1, $_REQUEST['subfilter_with_triggers'])),
-- 
1.7.1

