From 3c5a825f040b3228b303c4a4be65d4b1cd648d48 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 30 Jul 2015 08:53:05 +0200 Subject: [PATCH] SettingsContoller: form should be a form... ...also added a LOT of userfriendly hints, errors and notes --- .../controllers/SettingsController.php | 43 +---- application/forms/ConfigForm.php | 150 ++++++++++++++++++ 2 files changed, 156 insertions(+), 37 deletions(-) create mode 100644 application/forms/ConfigForm.php diff --git a/application/controllers/SettingsController.php b/application/controllers/SettingsController.php index 42e64a9a..f86fb3a6 100644 --- a/application/controllers/SettingsController.php +++ b/application/controllers/SettingsController.php @@ -1,6 +1,5 @@ view->tabs = $this->Module()->getConfigTabs()->activate('config'); + $this->view->tabs = $this->Module() + ->getConfigTabs() + ->activate('config'); - $resource = $this->Config()->get('db', 'resource'); - - $form = new ConfigForm(); - - $form->setIniConfig($this->Config('config')); - $form->addElement('select', 'resource', array( - 'required' => true, - 'label' => $this->translate('DB Resource'), - 'multiOptions' => array(null => $this->translate('- please choose -')) + $this->getResources(), - 'value' => $resource - )); - $form->setSubmitLabel($this->translate('Save')); - - $form->setOnSuccess(function($form) { - /** @var $form ConfigForm */ - $this->Config('config')->setSection('db', array( - 'resource' => $form->getValue('resource') - )); - $form->save(); - }); - - $form->handleRequest(); - - $this->view->form = $form; - } - - public function getResources() - { - $resources = array(); - foreach (ResourceFactory::getResourceConfigs() as $name => $resource) { - if ($resource->type === 'db') { - $resources['ido'][$name] = $name; - } - } - return $resources; + $this->view->form = $this->loadForm('config') + ->setModuleConfig($this->Config()) + ->handleRequest(); } } diff --git a/application/forms/ConfigForm.php b/application/forms/ConfigForm.php new file mode 100644 index 00000000..a5941548 --- /dev/null +++ b/application/forms/ConfigForm.php @@ -0,0 +1,150 @@ +enumResources(); + + $this->addElement('select', 'resource', array( + 'required' => true, + 'label' => $this->translate('DB Resource'), + 'multiOptions' => $this->optionalEnum($resources), + 'class' => 'autosubmit', + 'value' => $this->config()->get('db', 'resource') + )); + + if (empty($resources)) { + $this->getElement('resource')->addError( + $this->translate('This has to be a MySQL or PostgreSQL database') + ); + + $hint = $this->translate('Please click %s to create new DB resources'); + $link = $this->getView()->qlink( + $this->translate('here'), + 'config/resource', + null, + array('data-base-target' => '_main') + ); + $this->addHtmlHint(sprintf($hint, $link)); + } + + $this->setSubmitLabel($this->translate('Store configuration')); + } + + protected function onSetup() + { + if ($this->hasBeenSubmitted()) { + // Do not hinder the form from being stored + return; + } + + if ($this->hasBeenSent() && $this->isValidPartial($this->getRequest()->getPost())) { + $resourceName = $this->getValue('resource'); + } else { + $resourceName = $this->config()->get('db', 'resource'); + } + + if ($resourceName) { + $resource = ResourceFactory::create($resourceName); + $db = $resource->getDbAdapter(); + + try { + $query = $db->select()->from('director_dbversion', 'COUNT(*)'); + $db->fetchOne($query); + + if (! $this->hasBeenSent()) { + $hint = $this->translate( + 'Configuration looks good, you should be ready to %s' + . ' Icinga Director' + ); + $link = $this->getView()->qlink( + $this->translate('start using'), + 'director/welcome', + null, + array('data-base-target' => '_main') + ); + $this->addHtmlHint(sprintf($hint, $link)); + $this->moveSubmitToBottom(); + } + + } catch (Exception $e) { + $this->getElement('resource') + ->addError('Could not fetch: ' . $e->getMessage()) + ->removeDecorator('description'); + + $hint = $this->translate( + 'Please make sure that your database grants enough permissions' + . ' and that you deployed the correct %s.' + ); + $link = $this->getView()->qlink( + $this->translate('database schema'), + 'director/schema/' . $resource->getDbType() + ); + $this->addHtmlHint(sprintf($hint, $link)); + $this->moveSubmitToBottom(); + } + } + } + + public function setModuleConfig(Config $config) + { + $this->config = $config; + return $this; + } + + public function onSuccess() + { + $config = $this->config(); + $value = $this->getValue('resource'); + + $config->setSection('db', array('resource' => $value)); + + try { + $config->saveIni(); + $this->redirectOnSuccess($this->translate('Configuration has been stored')); + } catch (Exception $e) { + $this->getElement('resource')->addError( + sprintf( + $this->translate('Unable to store the configuration to "%s"'), + $config->getConfigFile() + ) + )->removeDecorator('description'); + $this->addHtmlHint( + '
' . $config . '
' + ); + } + } + + protected function config() + { + if ($this->config === null) { + $this->config = Config::module('director'); + } + + return $this->config; + } + + protected function enumResources() + { + $resources = array(); + $allowed = array('mysql', 'pgsql'); + + foreach (ResourceFactory::getResourceConfigs() as $name => $resource) { + if ($resource->type === 'db' && in_array($resource->db, $allowed)) { + $resources[$name] = $name; + } + } + + return $resources; + } +}