From 061705f52df82330449acd851cc877fd265cccf2 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 5 Sep 2016 19:16:35 +0000 Subject: [PATCH] ConfigController: add settings tab and form --- application/controllers/ConfigController.php | 19 +++ application/forms/SettingsForm.php | 126 +++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 application/forms/SettingsForm.php diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php index 428eb587..e5355bcf 100644 --- a/application/controllers/ConfigController.php +++ b/application/controllers/ConfigController.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Controllers; use Icinga\Module\Director\ConfigDiff; use Icinga\Module\Director\IcingaConfig\IcingaConfig; +use Icinga\Module\Director\Settings; use Icinga\Module\Director\Util; use Icinga\Module\Director\Web\Controller\ActionController; use Icinga\Web\Notification; @@ -94,6 +95,18 @@ class ConfigController extends ActionController $this->render('list/table', null, true); } + public function settingsAction() + { + $this->overviewTabs()->activate('settings'); + $this->view->title = $this->translate('Settings'); + $this->view->form = $this + ->loadForm('Settings') + ->setSettings(new Settings($this->db())) + ->handleRequest(); + + $this->setViewScript('object/form'); + } + // Show all files for a given config public function filesAction() { @@ -248,6 +261,12 @@ class ConfigController extends ActionController 'label' => $this->translate('Deployments'), 'url' => 'director/config/deployments' ) + )->add( + 'settings', + array( + 'label' => $this->translate('Settings'), + 'url' => 'director/config/settings' + ) ); return $this->view->tabs; } diff --git a/application/forms/SettingsForm.php b/application/forms/SettingsForm.php new file mode 100644 index 00000000..af94274e --- /dev/null +++ b/application/forms/SettingsForm.php @@ -0,0 +1,126 @@ +settings; + + $this->addHtmlHint( + $this->translate( + 'Please only change those settings in case you are really sure' + . ' that you are required to do so. Usually the defaults chosen' + . ' by the Icinga Director should make a good fit for your' + . ' environment.' + ) + ); + + $globalZones = array( + null => sprintf( + $this->translate('%s (default)'), + $settings->getDefaultValue('default_global_zone') + ) + ); + + $this->addElement('select', 'default_global_zone', array( + 'label' => $this->translate('Default global zone'), + 'multiOptions' => $globalZones, + 'description' => $this->translate( + 'Icinga Director decides to deploy objects like CheckCommands' + . ' to a global zone. This defaults to "director-global" but' + . ' might be adjusted to a custom Zone name' + ), + 'value' => $settings->getStoredValue('default_global_zone') + )); + + $this->addElement('select', 'disable_all_jobs', array( + 'label' => $this->translate('Disable all Jobs'), + 'multiOptions' => $this->eventuallyConfiguredEnum( + 'disable_all_jobs', + array( + 'n' => $this->translate('No'), + 'y' => $this->translate('Yes'), + ) + ), + 'description' => $this->translate( + 'Whether all configured Jobs should be disabled' + ), + )); + + $this->getElement('disable_all_jobs')->setValue( + $settings->getStoredValue('disable_all_jobs') + ); + + $this->addElement('select', 'config_format', array( + 'label' => $this->translate('Configuration format'), + 'multiOptions' => $this->eventuallyConfiguredEnum( + 'config_format', + array( + 'v2' => $this->translate('Icinga v2.x'), + 'v1' => $this->translate('Icinga v1.x'), + ) + ), + 'description' => $this->translate( + 'Default configuration format. Please note that v1.x is for' + . ' special transitional projects only and completely' + . ' unsupported. There are no plans to make Director a first-' + . 'class configuration backends for Icinga 1.x' + ), + )); + + $this->getElement('config_format')->setValue( + $settings->getStoredValue('config_format') + ); + + $this->setSubmitLabel($this->translate('Store')); + } + + protected function eventuallyConfiguredEnum($name, $enum) + { + return array( + null => sprintf( + $this->translate('%s (default)'), + $enum[$this->settings->getDefaultValue($name)] + ) + ) + $enum; + } + + public function setSettings(Settings $settings) + { + $this->settings = $settings; + return $this; + } + + public function onSuccess() + { + $settings = $this->settings; + + try { + $cnt = 0; + + foreach ($this->getValues() as $key => $value) { + if ($value === '') { + $value = null; + } + + $this->settings->set($key, $value); + } + + $this->setSuccessMessage($this->translate( + 'Settings have been stored' + )); + + parent::onSuccess(); + } catch (Exception $e) { + $this->addError($e->getMessage()); + } + } +}