From 0b635ac61b44d7aa760b6151621132be26b1b3b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Volker=20Fr=C3=B6hlich?= <volker.froehlich@meduniwien.ac.at>
Date: Mon, 4 Nov 2013 14:23:21 +0100
Subject: [PATCH] Switch between graphs, simple graphs and screens without loosing temporal settings (ZBXNEXT-1061)

pidzero's ZBXNEXT-1061-2.0.9-2-temporal.patch
---
 charts.php                              |   24 ++++++++++++++++++++++--
 history.php                             |   21 ++++++++++++++++++++-
 host_screen.php                         |   23 +++++++++++++++++++++--
 images/general/bttn/pinset.png          |  Bin 0 -> 402 bytes
 images/general/bttn/pinunset.png        |  Bin 0 -> 252 bytes
 include/classes/screens/CScreenBase.php |    7 +++++++
 include/html.inc.php                    |   19 +++++++++++++++++++
 include/views/monitoring.charts.php     |    2 ++
 include/views/monitoring.history.php    |    1 +
 include/views/monitoring.hostscreen.php |    4 +++-
 include/views/monitoring.screen.php     |    2 ++
 js/main.js                              |   16 ++++++++++++++++
 screens.php                             |   23 +++++++++++++++++++++--
 styles/icon.css                         |    2 ++
 14 files changed, 136 insertions(+), 8 deletions(-)
 create mode 100644 images/general/bttn/pinset.png
 create mode 100644 images/general/bttn/pinunset.png

diff --git a/charts.php b/charts.php
index 2f0ee10..5a1e2cd 100644
--- a/charts.php
+++ b/charts.php
@@ -49,7 +49,8 @@ $fields = array(
 	'favref' =>		array(T_ZBX_STR, O_OPT, P_ACT, NOT_EMPTY,	null),
 	'favid' =>		array(T_ZBX_INT, O_OPT, P_ACT, null,		null),
 	'favstate' =>	array(T_ZBX_INT, O_OPT, P_ACT, NOT_EMPTY,	null),
-	'favaction' =>	array(T_ZBX_STR, O_OPT, P_ACT, IN("'add','remove'"), null)
+	'favaction' =>	array(T_ZBX_STR, O_OPT, P_ACT, IN("'add','remove'"), null),
+	'pinned' =>	array(T_ZBX_INT, O_OPT, P_SYS, NULL, null)
 );
 check_fields($fields);
 
@@ -116,6 +117,24 @@ if (isset($_REQUEST['favobj'])) {
 		}
 	}
 }
+
+if (isset($_REQUEST['pinned'])) {
+	if ($_REQUEST['pinned'] == 0) {
+		CProfile::update('web.common.pinned.set', $_REQUEST['pinned'], PROFILE_TYPE_INT);
+		echo '$("upd_pin").title = "'._('Pin period').'";'."\n";
+		echo '$("upd_pin").onclick = function() { update_pinned_status(1); }'."\n";
+		echo 'switchElementsClass("upd_pin", "iconpinunset", "iconpinset");';
+	}
+	elseif ($_REQUEST['pinned'] == 1) {
+		CProfile::update('web.common.pinned.set', $_REQUEST['pinned'], PROFILE_TYPE_INT);
+		echo '$("upd_pin").title = "'._('Unpin period').'";'."\n";
+		echo '$("upd_pin").onclick = function() { update_pinned_status(0); }'."\n";
+		echo 'switchElementsClass("upd_pin", "iconpinset", "iconpinunset");';
+	}
+} else {
+	$_REQUEST['pinned'] = CProfile::get('web.common.pinned.set', 0);
+}
+
 if (!empty($_REQUEST['period']) || !empty($_REQUEST['stime'])) {
 	CScreenBase::calculateTime(array(
 		'profileIdx' => 'web.screens',
@@ -146,7 +165,8 @@ if ($page['type'] == PAGE_TYPE_JS || $page['type'] == PAGE_TYPE_HTML_BLOCK) {
 $data = array(
 	'pageFilter' => $pageFilter,
 	'graphid' => $pageFilter->graphid,
-	'fullscreen' => $_REQUEST['fullscreen']
+	'fullscreen' => get_request('fullscreen'),
+	'pinned' => get_request('pinned')
 );
 
 // render view
diff --git a/history.php b/history.php
index 930b6d3..c2d6310 100644
--- a/history.php
+++ b/history.php
@@ -38,7 +38,7 @@ require_once dirname(__FILE__).'/include/page_header.php';
 
 // VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
 $fields = array(
-	'itemid' =>			array(T_ZBX_INT, O_OPT, P_SYS,	DB_ID,	'!isset({favobj})'),
+	'itemid' =>			array(T_ZBX_INT, O_OPT, P_SYS,	DB_ID,	'!isset({favobj})&&!isset({pinned})'),
 	'period' =>			array(T_ZBX_INT, O_OPT, null,	null,	null),
 	'dec' =>			array(T_ZBX_INT, O_OPT, null,	null,	null),
 	'inc' =>			array(T_ZBX_INT, O_OPT, null,	null,	null),
@@ -64,6 +64,7 @@ $fields = array(
 	'form_copy_to' =>	array(T_ZBX_STR, O_OPT, P_SYS,	null,	null),
 	'form_refresh' =>	array(T_ZBX_INT, O_OPT, null,	null,	null),
 	'fullscreen' =>		array(T_ZBX_INT, O_OPT, P_SYS,	IN('0,1'),	null)
+	'pinned' =>		array(T_ZBX_INT, O_OPT, P_SYS, NULL, null)
 );
 check_fields($fields);
 
@@ -108,6 +109,23 @@ if (isset($_REQUEST['favobj'])) {
 	}
 }
 
+if (isset($_REQUEST['pinned'])) {
+	if ($_REQUEST['pinned'] == 0) {
+		CProfile::update('web.common.pinned.set', $_REQUEST['pinned'], PROFILE_TYPE_INT);
+		echo '$("upd_pin").title = "'._('Pin period').'";'."\n";
+		echo '$("upd_pin").onclick = function() { update_pinned_status(1); }'."\n";
+		echo 'switchElementsClass("upd_pin", "iconpinunset", "iconpinset");';
+	}
+	elseif ($_REQUEST['pinned'] == 1) {
+		CProfile::update('web.common.pinned.set', $_REQUEST['pinned'], PROFILE_TYPE_INT);
+		echo '$("upd_pin").title = "'._('Unpin period').'";'."\n";
+		echo '$("upd_pin").onclick = function() { update_pinned_status(0); }'."\n";
+		echo 'switchElementsClass("upd_pin", "iconpinset", "iconpinunset");';
+	}
+} else {
+	$_REQUEST['pinned'] = CProfile::get('web.common.pinned.set', 0);
+}
+
 if ($page['type'] == PAGE_TYPE_JS || $page['type'] == PAGE_TYPE_HTML_BLOCK) {
 	require_once dirname(__FILE__).'/include/page_footer.php';
 	exit();
@@ -152,6 +170,7 @@ $data = array(
 	'iv_string' => array(ITEM_VALUE_TYPE_LOG => 1, ITEM_VALUE_TYPE_TEXT => 1),
 	'iv_numeric' => array(ITEM_VALUE_TYPE_FLOAT => 1, ITEM_VALUE_TYPE_UINT64 => 1),
 	'fullscreen' => $_REQUEST['fullscreen']
+	'pinned' => get_request('pinned')
 );
 
 // render view
diff --git a/host_screen.php b/host_screen.php
index 451ff3b..7cdead1 100644
--- a/host_screen.php
+++ b/host_screen.php
@@ -50,7 +50,8 @@ $fields = array(
 	'favref' =>		array(T_ZBX_STR, O_OPT, P_ACT, NOT_EMPTY,	null),
 	'favid' =>		array(T_ZBX_INT, O_OPT, P_ACT, null,		null),
 	'favaction' =>	array(T_ZBX_STR, O_OPT, P_ACT, IN("'add','remove','flop'"), null),
-	'favstate' =>	array(T_ZBX_INT, O_OPT, P_ACT, NOT_EMPTY,	null)
+	'favstate' =>	array(T_ZBX_INT, O_OPT, P_ACT, NOT_EMPTY,	null),
+	'pinned' =>	array(T_ZBX_INT, O_OPT, P_SYS, NULL, null)
 );
 check_fields($fields);
 
@@ -92,6 +93,23 @@ if (isset($_REQUEST['favobj'])) {
 	}
 }
 
+if (isset($_REQUEST['pinned'])) {
+	if ($_REQUEST['pinned'] == 0) {
+		CProfile::update('web.common.pinned.set', $_REQUEST['pinned'], PROFILE_TYPE_INT);
+		echo '$("upd_pin").title = "'._('Pin period').'";'."\n";
+		echo '$("upd_pin").onclick = function() { update_pinned_status(1); }'."\n";
+		echo 'switchElementsClass("upd_pin", "iconpinunset", "iconpinset");';
+	}
+	elseif ($_REQUEST['pinned'] == 1) {
+		CProfile::update('web.common.pinned.set', $_REQUEST['pinned'], PROFILE_TYPE_INT);
+		echo '$("upd_pin").title = "'._('Unpin period').'";'."\n";
+		echo '$("upd_pin").onclick = function() { update_pinned_status(0); }'."\n";
+		echo 'switchElementsClass("upd_pin", "iconpinset", "iconpinunset");';
+	}
+} else {
+	$_REQUEST['pinned'] = CProfile::get('web.common.pinned.set', 0);
+}
+
 if ($page['type'] == PAGE_TYPE_JS || $page['type'] == PAGE_TYPE_HTML_BLOCK) {
 	require_once dirname(__FILE__).'/include/page_footer.php';
 	exit();
@@ -105,7 +123,8 @@ $data = array(
 	'fullscreen' => $_REQUEST['fullscreen'],
 	'screenid' => get_request('screenid', CProfile::get('web.hostscreen.screenid', null)),
 	'period' => get_request('period'),
-	'stime' => get_request('stime')
+	'stime' => get_request('stime'),
+	'pinned' => get_request('pinned')
 );
 CProfile::update('web.hostscreen.screenid', $data['screenid'], PROFILE_TYPE_ID);
 
diff --git a/images/general/bttn/pinset.png b/images/general/bttn/pinset.png
new file mode 100644
index 0000000000000000000000000000000000000000..85657fda9caf207d9c8c929266ae3d647a61c435
GIT binary patch
diff --git a/images/general/bttn/pinunset.png b/images/general/bttn/pinunset.png
new file mode 100644
index 0000000000000000000000000000000000000000..d39a7f9e1a93054c63ef7b9344b6368113262010
GIT binary patch
diff --git a/include/classes/screens/CScreenBase.php b/include/classes/screens/CScreenBase.php
index a616365..381244f 100644
--- a/include/classes/screens/CScreenBase.php
+++ b/include/classes/screens/CScreenBase.php
@@ -316,6 +316,10 @@ class CScreenBase {
 		if (empty($options['profileIdx2'])) {
 			$options['profileIdx2'] = 0;
 		}
+		if (CProfile::get('web.common.pinned.set')) {
+			$options['profileIdx'] = 'web.common.pinned';
+			$options['profileIdx2'] = 0;
+		}
 
 		// show only latest data without update is set only period
 		if (!empty($options['period']) && empty($options['stime'])) {
@@ -343,6 +347,7 @@ class CScreenBase {
 		}
 		if ($options['updateProfile'] && !empty($options['profileIdx'])) {
 			CProfile::update($options['profileIdx'].'.period', $options['period'], PROFILE_TYPE_INT, $options['profileIdx2']);
+			CProfile::update('web.common.pinned.period', $options['period'], PROFILE_TYPE_INT, 0);
 		}
 
 		// stime
@@ -368,6 +373,8 @@ class CScreenBase {
 			if ($options['updateProfile'] && !empty($options['profileIdx'])) {
 				CProfile::update($options['profileIdx'].'.stime', $options['stime'], PROFILE_TYPE_STR, $options['profileIdx2']);
 				CProfile::update($options['profileIdx'].'.isnow', $isNow, PROFILE_TYPE_INT, $options['profileIdx2']);
+				CProfile::update('web.common.pinned.stime', $options['stime'], PROFILE_TYPE_STR, 0);
+				CProfile::update('web.common.pinned.isnow', $isNow, PROFILE_TYPE_STR, 0);
 			}
 		}
 		else {
diff --git a/include/html.inc.php b/include/html.inc.php
index 7f86c1e..ac006e3 100644
--- a/include/html.inc.php
+++ b/include/html.inc.php
@@ -248,6 +248,25 @@ function get_icon($name, $params = array()) {
 		case 'reset':
 			$icon = new CIcon(_('Reset'), 'iconreset', 'timeControl.objectReset();');
 			break;
+		case 'pinned':
+			$pinned= $params['pinned'] ? 0 : 1;
+
+			if ($pinned) {
+				$title = _('Pin period');
+				$class = 'iconpinunset';
+			}
+			else {
+				$title = _('Unpin period');
+				$class = 'iconpinset';
+			}
+
+			$icon = new CIcon(
+				$title,
+				$class,
+				'update_pinned_status('.$pinned.')'
+			);
+			$icon->setAttribute('id', 'upd_pin');
+			break;
 	}
 	return $icon;
 }
diff --git a/include/views/monitoring.charts.php b/include/views/monitoring.charts.php
index 139b67e..180cbf5 100644
--- a/include/views/monitoring.charts.php
+++ b/include/views/monitoring.charts.php
@@ -32,6 +32,8 @@ if ($this->data['graphid']) {
 	$chartsWidget->addPageHeader(_('Graphs'), array(
 		get_icon('favourite', array('fav' => 'web.favorite.graphids', 'elname' => 'graphid', 'elid' => $this->data['graphid'])),
 		SPACE,
+		get_icon('pinned', array('pinned' => $this->data['pinned'])),
+		SPACE,
 		get_icon('reset', array('id' => $this->data['graphid'])),
 		SPACE,
 		get_icon('fullscreen', array('fullscreen' => $this->data['fullscreen']))
diff --git a/include/views/monitoring.history.php b/include/views/monitoring.history.php
index 85a2ee4..079e782 100644
--- a/include/views/monitoring.history.php
+++ b/include/views/monitoring.history.php
@@ -38,6 +38,7 @@ if (count($this->data['items']) == 1) {
 			'elid' => $this->data['item']['itemid'],
 			'elname' => 'itemid'
 		));
+		$header['right'][] = get_icon('pinned', array('pinned' => $this->data['pinned']));
 	}
 }
 
diff --git a/include/views/monitoring.hostscreen.php b/include/views/monitoring.hostscreen.php
index 2b32ee4..7b68bd0 100644
--- a/include/views/monitoring.hostscreen.php
+++ b/include/views/monitoring.hostscreen.php
@@ -37,7 +37,9 @@ if (empty($this->data['screen']) || empty($this->data['host'])) {
 	));
 }
 else {
-	$screenWidget->addPageHeader(_('SCREENS'), array(get_icon('fullscreen', array('fullscreen' => $this->data['fullscreen']))));
+	$screenWidget->addPageHeader(_('SCREENS'), array(
+		get_icon('pinned', array('pinned' => $this->data['pinned'])),
+		get_icon('fullscreen', array('fullscreen' => $this->data['fullscreen']))));
 	$screenWidget->addItem(BR());
 
 	// host screen list
diff --git a/include/views/monitoring.screen.php b/include/views/monitoring.screen.php
index 92f7523..524590a 100644
--- a/include/views/monitoring.screen.php
+++ b/include/views/monitoring.screen.php
@@ -60,6 +60,8 @@ else {
 		SPACE,
 		get_icon('favourite', array('fav' => 'web.favorite.screenids', 'elname' => 'screenid', 'elid' => $screen['screenid'])),
 		SPACE,
+		get_icon('pinned', array('pinned' => $this->data['pinned'])),
+		SPACE,
 		get_icon('fullscreen', array('fullscreen' => $this->data['fullscreen']))
 	));
 	$screenWidget->addItem(BR());
diff --git a/js/main.js b/js/main.js
index 856d620..b2068c0 100644
--- a/js/main.js
+++ b/js/main.js
@@ -970,6 +970,22 @@ function set_color_by_name(id, color) {
 /*
  * Zabbix ajax requests
  */
+function update_pinned_status(pinned) {
+	if ('undefined' == typeof(Ajax)) {
+		throw('Prototype.js lib is required!');
+	}
+
+	if (typeof(pinned) == 'undefined' || empty(pinned)) {
+		return;
+	}
+
+	var params = {
+		'pinned': pinned
+	};
+
+	send_params(params);
+}
+
 function add2favorites(favobj, favid) {
 	if ('undefined' == typeof(Ajax)) {
 		throw('Prototype.js lib is required!');
diff --git a/screens.php b/screens.php
index 47b434f..f98fb65 100644
--- a/screens.php
+++ b/screens.php
@@ -52,7 +52,8 @@ $fields = array(
 	'favref' =>		array(T_ZBX_STR, O_OPT, P_ACT,	NOT_EMPTY,	null),
 	'favid' =>		array(T_ZBX_INT, O_OPT, P_ACT,	null,		null),
 	'favaction' =>	array(T_ZBX_STR, O_OPT, P_ACT,	IN("'add','remove','flop'"), null),
-	'favstate' =>	array(T_ZBX_INT, O_OPT, P_ACT,	NOT_EMPTY,	null)
+	'favstate' =>	array(T_ZBX_INT, O_OPT, P_ACT,	NOT_EMPTY,	null),
+	'pinned' =>	array(T_ZBX_INT, O_OPT, P_SYS, NULL, null)
 );
 check_fields($fields);
 
@@ -132,6 +133,23 @@ if (isset($_REQUEST['favobj'])) {
 	}
 }
 
+if (isset($_REQUEST['pinned'])) {
+	if ($_REQUEST['pinned'] == 0) {
+		CProfile::update('web.common.pinned.set', $_REQUEST['pinned'], PROFILE_TYPE_INT);
+		echo '$("upd_pin").title = "'._('Pin period').'";'."\n";
+		echo '$("upd_pin").onclick = function() { update_pinned_status(1); }'."\n";
+		echo 'switchElementsClass("upd_pin", "iconpinunset", "iconpinset");';
+	}
+	elseif ($_REQUEST['pinned'] == 1) {
+		CProfile::update('web.common.pinned.set', $_REQUEST['pinned'], PROFILE_TYPE_INT);
+		echo '$("upd_pin").title = "'._('Unpin period').'";'."\n";
+		echo '$("upd_pin").onclick = function() { update_pinned_status(0); }'."\n";
+		echo 'switchElementsClass("upd_pin", "iconpinset", "iconpinunset");';
+	}
+} else {
+	$_REQUEST['pinned'] = CProfile::get('web.common.pinned.set', 0);
+}
+
 if ($page['type'] == PAGE_TYPE_JS || $page['type'] == PAGE_TYPE_HTML_BLOCK) {
 	require_once dirname(__FILE__).'/include/page_footer.php';
 	exit();
@@ -147,7 +165,8 @@ $data = array(
 	'elementid' => get_request('elementid', false),
 
 	// whether we should use screen name to fetch a screen (if this is false, elementid is used)
-	'use_screen_name' => isset($_REQUEST['screenname'])
+	'use_screen_name' => isset($_REQUEST['screenname']),
+	'pinned' => get_request('pinned')
 );
 
 // if none is provided
diff --git a/styles/icon.css b/styles/icon.css
index dc5ba9c..1257c08 100644
--- a/styles/icon.css
+++ b/styles/icon.css
@@ -56,6 +56,8 @@
 .iconsnooze		{ background-image: url('../images/general/bttn/snooze.png'); }
 .iconclose		{ background-image: url('../images/general/bttn/close.png'); }
 .iconmove		{ background-image: url('../images/general/bttn/move.png'); cursor: move; }
+.iconpinset		{ background-image: url('../images/general/bttn/pinset.png'); }
+.iconpinunset		{ background-image: url('../images/general/bttn/pinunset.png'); }
 
 .app-list-toggle, .app-list-toggle-all { margin-left: 3px; }
 
-- 
1.7.1

