From 81b19285a78effe0c98bcbd8f34700673fa06d01 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 8 Jun 2020 14:03:29 +0200 Subject: [PATCH] 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;