From 81b19285a78effe0c98bcbd8f34700673fa06d01 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 8 Jun 2020 14:03:29 +0200 Subject: [PATCH 1/3] Make auto refresh interval configurable refs #2819 --- .../ApplicationStateController.php | 4 +- application/forms/PreferenceForm.php | 41 +++++++++++++++++++ .../Web/Controller/ActionController.php | 12 +++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/application/controllers/ApplicationStateController.php b/application/controllers/ApplicationStateController.php index 711eea694..e04b70679 100644 --- a/application/controllers/ApplicationStateController.php +++ b/application/controllers/ApplicationStateController.php @@ -64,7 +64,7 @@ class ApplicationStateController extends Controller } } - $this->setAutorefreshInterval(60); + $this->setAutorefreshInterval(60, true); } public function summaryAction() @@ -73,7 +73,7 @@ class ApplicationStateController extends Controller $this->getResponse()->setBody((string) Widget::create('ApplicationStateMessages')); } - $this->setAutorefreshInterval(60); + $this->setAutorefreshInterval(60, true); } public function acknowledgeMessageAction() diff --git a/application/forms/PreferenceForm.php b/application/forms/PreferenceForm.php index 628b81ae4..d6bbd132d 100644 --- a/application/forms/PreferenceForm.php +++ b/application/forms/PreferenceForm.php @@ -90,6 +90,12 @@ class PreferenceForm extends Form */ public function onSuccess() { + if (!($this->getElement('btn_submit_preferences')->isChecked() + || $this->getElement('btn_submit_session')->isChecked() + )) { + return false; + } + $this->preferences = new Preferences($this->store ? $this->store->load() : array()); $oldTheme = $this->preferences->getValue('icingaweb', 'theme'); @@ -148,6 +154,10 @@ class PreferenceForm extends Form $values['timezone'] = 'autodetect'; } + if (! isset($values['auto_refresh'])) { + $values['auto_refresh'] = '1'; + } + $this->populate($values); } @@ -275,6 +285,7 @@ class PreferenceForm extends Form 'auto_refresh', array( 'required' => false, + 'autosubmit' => true, 'label' => $this->translate('Enable auto refresh'), 'description' => $this->translate( 'This option allows you to enable or to disable the global page content auto refresh' @@ -283,6 +294,36 @@ class PreferenceForm extends Form ) ); + if (isset($formData['auto_refresh']) && $formData['auto_refresh']) { + $speeds = [1 => $this->translate('Default')]; + + foreach ([2, 4, 8] as $speed) { + // Using Form#translatePlural() not for $speed==1 and $speed!=1, + // but for different $speed-dependent plural forms, e.g. in Russian + $speeds[$speed] = sprintf($this->translatePlural('%dx slower', '%dx slower', $speed), $speed); + $speeds[rtrim(sprintf('%F', 1.0 / $speed), '0')] = sprintf( + $this->translatePlural('%dx faster', '%dx faster', $speed), + $speed + ); + } + + krsort($speeds); + + $this->addElement( + 'select', + 'auto_refresh_speed', + [ + 'required' => false, + 'label' => $this->translate('Auto refresh speed'), + 'description' => $this->translate( + 'This option allows you to speed up or to slow down the global page content auto refresh' + ), + 'multiOptions' => $speeds, + 'value' => '' + ] + ); + } + $this->addElement( 'number', 'default_page_size', diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php index cc26277b7..ed74af740 100644 --- a/library/Icinga/Web/Controller/ActionController.php +++ b/library/Icinga/Web/Controller/ActionController.php @@ -341,13 +341,23 @@ class ActionController extends Zend_Controller_Action } } - public function setAutorefreshInterval($interval) + public function setAutorefreshInterval($interval, $bypassUserPreferences = false) { if (! is_int($interval) || $interval < 1) { throw new ProgrammingError( 'Setting autorefresh interval smaller than 1 second is not allowed' ); } + + if (! $bypassUserPreferences) { + $user = $this->getRequest()->getUser(); + + if ($user !== null) { + $speed = (float) $user->getPreferences()->getValue('icingaweb', 'auto_refresh_speed', 1.0); + $interval = max(round($interval * $speed), min($interval, 5)); + } + } + $this->autorefreshInterval = $interval; $this->_helper->layout()->autorefreshInterval = $interval; return $this; From 79ad7a4197298a4bba2a9d0b06305e6ffa2957b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Aleksandrovi=C4=8D=20Klimov?= Date: Wed, 25 Nov 2020 13:34:01 +0100 Subject: [PATCH 2/3] Name auto-refresh speeds more human readable Co-authored-by: Johannes Meyer --- application/forms/PreferenceForm.php | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/application/forms/PreferenceForm.php b/application/forms/PreferenceForm.php index d6bbd132d..76e1df680 100644 --- a/application/forms/PreferenceForm.php +++ b/application/forms/PreferenceForm.php @@ -295,20 +295,6 @@ class PreferenceForm extends Form ); if (isset($formData['auto_refresh']) && $formData['auto_refresh']) { - $speeds = [1 => $this->translate('Default')]; - - foreach ([2, 4, 8] as $speed) { - // Using Form#translatePlural() not for $speed==1 and $speed!=1, - // but for different $speed-dependent plural forms, e.g. in Russian - $speeds[$speed] = sprintf($this->translatePlural('%dx slower', '%dx slower', $speed), $speed); - $speeds[rtrim(sprintf('%F', 1.0 / $speed), '0')] = sprintf( - $this->translatePlural('%dx faster', '%dx faster', $speed), - $speed - ); - } - - krsort($speeds); - $this->addElement( 'select', 'auto_refresh_speed', @@ -318,7 +304,12 @@ class PreferenceForm extends Form 'description' => $this->translate( 'This option allows you to speed up or to slow down the global page content auto refresh' ), - 'multiOptions' => $speeds, + 'multiOptions' => [ + '0.5' => $this->translate('Fast', 'refresh_speed'), + '' => $this->translate('Default', 'refresh_speed'), + '2' => $this->translate('Moderate', 'refresh_speed'), + '4' => $this->translate('Slow', 'refresh_speed') + ], 'value' => '' ] ); From 0f400ea4efbe98cb1ae5a245afd057b58f8e3ad0 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 30 Nov 2020 11:41:23 +0100 Subject: [PATCH 3/3] PreferenceForm: Properly process autosubmits Otherwise the default value of element `auto_refresh_speed` is not applied after enabling element `auto_refresh`. --- application/forms/PreferenceForm.php | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/application/forms/PreferenceForm.php b/application/forms/PreferenceForm.php index 76e1df680..64c0ee977 100644 --- a/application/forms/PreferenceForm.php +++ b/application/forms/PreferenceForm.php @@ -44,6 +44,7 @@ class PreferenceForm extends Form public function init() { $this->setName('form_config_preferences'); + $this->setSubmitLabel($this->translate('Save to the Preferences')); } /** @@ -90,12 +91,6 @@ class PreferenceForm extends Form */ public function onSuccess() { - if (!($this->getElement('btn_submit_preferences')->isChecked() - || $this->getElement('btn_submit_session')->isChecked() - )) { - return false; - } - $this->preferences = new Preferences($this->store ? $this->store->load() : array()); $oldTheme = $this->preferences->getValue('icingaweb', 'theme'); @@ -124,7 +119,7 @@ class PreferenceForm extends Form } try { - if ($this->store && $this->getElement('btn_submit_preferences')->isChecked()) { + if ($this->store && $this->getElement('btn_submit')->isChecked()) { $this->save(); Notification::success($this->translate('Preferences successfully saved')); } else { @@ -330,7 +325,7 @@ class PreferenceForm extends Form if ($this->store) { $this->addElement( 'submit', - 'btn_submit_preferences', + 'btn_submit', array( 'ignore' => true, 'label' => $this->translate('Save to the Preferences'), @@ -363,7 +358,7 @@ class PreferenceForm extends Form ); $this->addDisplayGroup( - array('btn_submit_preferences', 'btn_submit_session', 'preferences-progress'), + array('btn_submit', 'btn_submit_session', 'preferences-progress'), 'submit_buttons', array( 'decorators' => array( @@ -374,6 +369,20 @@ class PreferenceForm extends Form ); } + public function addSubmitButton() + { + return $this; + } + + public function isSubmitted() + { + if (parent::isSubmitted()) { + return true; + } + + return $this->getElement('btn_submit_session')->isChecked(); + } + /** * Return the current default timezone *