commit 1043fe5effcb9419eea780a357d95428cc8c2759
Author: Volker Fröhlich <vf@geizhals.at>
Date:   Sat Mar 26 20:29:58 2016 +0100

    Forward-port patch for ZBXNEXT-1456

diff --git a/include/forms.inc.php b/include/forms.inc.php
index 4afd6a7..290fab7 100644
--- a/include/forms.inc.php
+++ b/include/forms.inc.php
@@ -342,6 +342,7 @@ function getItemFilterForm(&$items) {
 	$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'];
@@ -350,6 +351,7 @@ function getItemFilterForm(&$items) {
 	$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'];
@@ -363,6 +365,7 @@ function getItemFilterForm(&$items) {
 		->addVar('subfilter_state', $subfilter_state)
 		->addVar('subfilter_templated_items', $subfilter_templated_items)
 		->addVar('subfilter_with_triggers', $subfilter_with_triggers)
+		->addVar('subfilter_discovery', $subfilter_discovery)
 		->addVar('subfilter_history', $subfilter_history)
 		->addVar('subfilter_trends', $subfilter_trends)
 		->addVar('subfilter_interval', $subfilter_interval);
@@ -580,6 +583,13 @@ function getItemFilterForm(&$items) {
 		(new CNumericBox('filter_port', $filter_port, 5, false, true))->setWidth(ZBX_TEXTAREA_NUMERIC_STANDARD_WIDTH),
 		'filter_port_row'
 	);
+	$filterColumn4->addRow(_('Discovery'),
+		new CComboBox('filter_discovery', $filter_discovery, null, [
+			-1 => _('all'),
+			ZBX_FLAG_DISCOVERY_CREATED => _('Discovered items'),
+			ZBX_FLAG_DISCOVERY_NORMAL => _('Not Discovered items')
+		])
+	);
 
 	$form->addColumn($filterColumn1);
 	$form->addColumn($filterColumn2);
@@ -604,6 +614,7 @@ function getItemFilterForm(&$items) {
 		'state' => [],
 		'templated_items' => [],
 		'with_triggers' => [],
+		'discovery' => [],
 		'history' => [],
 		'trends' => [],
 		'interval' => []
@@ -790,6 +801,31 @@ function getItemFilterForm(&$items) {
 			}
 		}
 
+		// 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']]) && $item['trends'] !== '') {
@@ -889,6 +925,11 @@ function getItemFilterForm(&$items) {
 		$table_subfilter->addRow([$with_triggers_output]);
 	}
 
+	if ($filter_discovery == -1 && count($item_params['discovery']) > 1) {
+		$discovery_output = prepareSubfilterOutput(_('Discovery'), $item_params['discovery'], $subfilter_discovery, 'subfilter_discovery');
+		$table_subfilter->addRow([$discovery_output]);
+	}
+
 	if (zbx_empty($filter_history) && count($item_params['history']) > 1) {
 		$history_output = prepareSubfilterOutput(_('History'), $item_params['history'], $subfilter_history, 'subfilter_history');
 		$table_subfilter->addRow([$history_output]);
diff --git a/items.php b/items.php
index 3f68a74..da633f5 100644
--- a/items.php
+++ b/items.php
@@ -162,6 +162,7 @@ $fields = [
 	'filter_state' =>			[T_ZBX_INT, O_OPT, null,	IN([-1, ITEM_STATE_NORMAL, ITEM_STATE_NOTSUPPORTED]), null],
 	'filter_templated_items' => [T_ZBX_INT, O_OPT, null,	IN('-1,0,1'), null],
 	'filter_with_triggers' =>	[T_ZBX_INT, O_OPT, null,	IN('-1,0,1'), null],
+	'filter_discovery' =>	[T_ZBX_INT, O_OPT, null,	IN('-1, ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED'), null],
 	'filter_ipmi_sensor' =>		[T_ZBX_STR, O_OPT, null,	null,		null],
 	// subfilters
 	'subfilter_set' =>			[T_ZBX_STR, O_OPT, null,	null,		null],
@@ -172,6 +173,7 @@ $fields = [
 	'subfilter_state' =>		[T_ZBX_INT, O_OPT, null,	null,		null],
 	'subfilter_templated_items' => [T_ZBX_INT, O_OPT, null, null,		null],
 	'subfilter_with_triggers' => [T_ZBX_INT, O_OPT, null,	null,		null],
+	'subfilter_discovery' => [T_ZBX_INT, O_OPT, null,	null,		null],
 	'subfilter_hosts' =>		[T_ZBX_INT, O_OPT, null,	null,		null],
 	'subfilter_interval' =>		[T_ZBX_INT, O_OPT, null,	null,		null],
 	'subfilter_history' =>		[T_ZBX_INT, O_OPT, null,	null,		null],
@@ -190,7 +192,7 @@ unset($_REQUEST[$paramsFieldName]);
 
 $subfiltersList = ['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'
 ];
 
 /*
@@ -266,6 +268,7 @@ if (hasRequest('filter_set')) {
 	CProfile::update('web.items.filter_status', getRequest('filter_status', -1), PROFILE_TYPE_INT);
 	CProfile::update('web.items.filter_state', getRequest('filter_state', -1), PROFILE_TYPE_INT);
 	CProfile::update('web.items.filter_templated_items', getRequest('filter_templated_items', -1), PROFILE_TYPE_INT);
+	CProfile::update('web.items.filter_discovery', getRequest('filter_discovery'), PROFILE_TYPE_INT);
 	CProfile::update('web.items.filter_with_triggers', getRequest('filter_with_triggers', -1), PROFILE_TYPE_INT);
 	CProfile::update('web.items.filter_ipmi_sensor', getRequest('filter_ipmi_sensor', ''), PROFILE_TYPE_STR);
 
@@ -317,6 +320,7 @@ $_REQUEST['filter_trends'] = CProfile::get('web.items.filter_trends', '');
 $_REQUEST['filter_status'] = CProfile::get('web.items.filter_status', -1);
 $_REQUEST['filter_state'] = CProfile::get('web.items.filter_state', -1);
 $_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', '');
 
@@ -1206,6 +1210,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'] = $_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'];
@@ -1277,6 +1285,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'])),
