From dc56fea72e6f40bbca8c1d11b76592b4dc5cdb72 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Volker=20Fr=C3=B6hlich?= <volker.froehlich@meduniwien.ac.at>
Date: Fri, 4 Oct 2013 11:29:22 +0200
Subject: [PATCH] pidzero's trigger filtering (ZBXNEXT-221)

---
 include/views/configuration.triggers.list.php |   40 ++++++++++++++++++
 triggers.php                                  |   56 ++++++++++++++++++++++++-
 2 files changed, 95 insertions(+), 1 deletions(-)

diff --git a/include/views/configuration.triggers.list.php b/include/views/configuration.triggers.list.php
index e4e668b..67bf1a7 100644
--- a/include/views/configuration.triggers.list.php
+++ b/include/views/configuration.triggers.list.php
@@ -85,6 +85,46 @@ else {
 		),
 		' ]'
 	));
+	// filter
+	$filter_table = new CTable('', 'filter');
+	$filter_table->addRow(array(
+		array(array(bold(_('Description')), SPACE._('like').': '), new CTextBox('filter_description', $_REQUEST['filter_description'], 20)),
+		array(array(bold(_('Type')), ': '), new CComboBox('filter_type', $_REQUEST['filter_type'], null, array(
+			-1 => _('all'),
+			0 => _('Single PROBLEM events'),
+			1 => _('Multiple PROBLEM events')))),
+		array(array(bold(_('Severity')), ': '), new CComboBox('filter_priority', $_REQUEST['filter_priority'], null, array(
+			-1 => _('all'),
+			TRIGGER_SEVERITY_NOT_CLASSIFIED => _('Not classified'),
+			TRIGGER_SEVERITY_INFORMATION => _('Information'),
+			TRIGGER_SEVERITY_WARNING => _('Warning'),
+			TRIGGER_SEVERITY_AVERAGE => _('Average'),
+			TRIGGER_SEVERITY_HIGH => _('High'),
+			TRIGGER_SEVERITY_DISASTER => _('Disaster')))),
+		array(array(bold(_('Discovered')), ': '), new CComboBox('filter_flags', $_REQUEST['filter_flags'], null, array(
+			-1 => _('all'),
+			ZBX_FLAG_DISCOVERY_NORMAL => _('Not Discovered'),
+			ZBX_FLAG_DISCOVERY_CREATED => _('Discovered'))))
+	));
+
+	$filter = new CButton('filter', _('Filter'), "javascript: create_var('zbx_filter', 'filter_set', '1', true);");
+	$filter->useJQueryStyle('main');
+
+	$reset = new CButton('reset', _('Reset'), "javascript: clearAllForm('zbx_filter');");
+	$reset->useJQueryStyle();
+
+	$div_buttons = new CDiv(array($filter, SPACE, $reset));
+	$div_buttons->setAttribute('style', 'padding: 4px 0;');
+
+	$filter_table->addRow(new CCol($div_buttons, 'center', 4));
+
+	$filter_form = new CForm('get');
+	$filter_form->setAttribute('name', 'zbx_filter');
+	$filter_form->setAttribute('id', 'zbx_filter');
+	$filter_form->addItem($filter_table);
+
+	$triggersWidget->addFlicker($filter_form, CProfile::get('web.triggers.filter.state', 0));
+
 }
 
 // create form
diff --git a/triggers.php b/triggers.php
index de21cb7..346d622 100644
--- a/triggers.php
+++ b/triggers.php
@@ -75,7 +75,19 @@ $fields = array(
 	'delete' =>				array(T_ZBX_STR, O_OPT, P_SYS|P_ACT, null,	null),
 	'cancel' =>				array(T_ZBX_STR, O_OPT, P_SYS,	null,		null),
 	'form' =>				array(T_ZBX_STR, O_OPT, P_SYS,	null,		null),
-	'form_refresh' =>		array(T_ZBX_INT, O_OPT, null,	null,		null)
+	'form_refresh' =>		array(T_ZBX_INT, O_OPT, null,	null,		null),
+	// filter
+	'filter_set' =>		array(T_ZBX_STR, O_OPT, P_ACT,	null, null),
+	'filter_description' =>	array(T_ZBX_STR, O_OPT, null,	null, null),
+	'filter_priority' =>	array(T_ZBX_INT, O_OPT, null,
+		IN(array(-1, TRIGGER_SEVERITY_NOT_CLASSIFIED, TRIGGER_SEVERITY_INFORMATION, TRIGGER_SEVERITY_WARNING,
+			TRIGGER_SEVERITY_AVERAGE, TRIGGER_SEVERITY_HIGH, TRIGGER_SEVERITY_DISASTER, TRIGGER_SEVERITY_COUNT)), null),
+	'filter_type' =>	array(T_ZBX_INT, O_OPT, null, IN(array(-1, 0, 1)), null),
+	'filter_flags' =>	array(T_ZBX_INT, O_OPT, null, IN(array(-1, ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED)), null),
+	// ajax
+	'favobj' =>	array(T_ZBX_STR, O_OPT, P_ACT, null,      null),
+	'favref' =>	array(T_ZBX_STR, O_OPT, P_ACT, NOT_EMPTY, 'isset({favobj})'),
+	'favstate' =>	array(T_ZBX_INT, O_OPT, P_ACT, NOT_EMPTY, 'isset({favobj})&&("filter"=={favobj})')
 );
 $_REQUEST['showdisabled'] = get_request('showdisabled', CProfile::get('web.triggers.showdisabled', 1));
 
@@ -103,6 +115,36 @@ if (get_request('hostid') && !API::Host()->isWritable(array($_REQUEST['hostid'])
 	access_deny();
 }
 /*
+ * Ajax
+ */
+if (isset($_REQUEST['favobj'])) {
+	if ('filter' == $_REQUEST['favobj']) {
+		CProfile::update('web.triggers.filter.state', $_REQUEST['favstate'], PROFILE_TYPE_INT);
+	}
+}
+
+/*
+ * Filter
+ */
+if (isset($_REQUEST['filter_set'])) {
+	$_REQUEST['filter_description'] = get_request('filter_description');
+	$_REQUEST['filter_priority'] = get_request('filter_priority', -1);
+	$_REQUEST['filter_type'] = get_request('filter_type', -1);
+	$_REQUEST['filter_flags'] = get_request('filter_flags', -1);
+
+	CProfile::update('web.triggers.filter_description', $_REQUEST['filter_description'], PROFILE_TYPE_STR);
+	CProfile::update('web.triggers.filter_priority', $_REQUEST['filter_priority'], PROFILE_TYPE_INT);
+	CProfile::update('web.triggers.filter_type', $_REQUEST['filter_type'], PROFILE_TYPE_INT);
+	CProfile::update('web.triggers.filter_flags', $_REQUEST['filter_flags'], PROFILE_TYPE_INT);
+}
+else {
+	$_REQUEST['filter_description'] = CProfile::get('web.triggers.filter_description');
+	$_REQUEST['filter_priority'] = CProfile::get('web.triggers.filter_priority', -1);
+	$_REQUEST['filter_type'] = CProfile::get('web.triggers.filter_type', -1);
+	$_REQUEST['filter_flags'] = CProfile::get('web.triggers.filter_flags', -1);
+}
+
+/*
  * Actions
  */
 if (isset($_REQUEST['add_expression'])) {
@@ -369,6 +411,18 @@ else {
 		elseif ($data['pageFilter']->groupid > 0) {
 			$options['groupids'] = $data['pageFilter']->groupid;
 		}
+		if (isset($_REQUEST['filter_description']) && !zbx_empty($_REQUEST['filter_description'])) {
+			$options['search']['description'] = empty($_REQUEST['filter_description']) ? null : $_REQUEST['filter_description'];
+		}
+		if ($_REQUEST['filter_type'] > -1) {
+			$options['filter']['type'] = $_REQUEST['filter_type'];
+		}
+		if ($_REQUEST['filter_priority'] > -1) {
+			$options['filter']['priority'] = $_REQUEST['filter_priority'];
+		}
+		if ($_REQUEST['filter_flags'] > -1) {
+			$options['filter']['flags'] = $_REQUEST['filter_flags'];
+		}
 		$data['triggers'] = API::Trigger()->get($options);
 	}
 
-- 
1.7.1

