DbBackendForm: replace create with createElements and getResources with __construct

refs #5525
This commit is contained in:
Alexander Klimov 2014-07-29 12:22:43 +02:00
parent 5e6ef57590
commit 4e1e845675

View File

@ -5,10 +5,10 @@
namespace Icinga\Form\Config\Authentication; namespace Icinga\Form\Config\Authentication;
use \Exception; use \Exception;
use \Zend_Config;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
use Icinga\Authentication\DbConnection; use Icinga\Authentication\DbConnection;
use Icinga\Authentication\Backend\DbUserBackend; use Icinga\Authentication\Backend\DbUserBackend;
use Icinga\Exception\ConfigurationError;
/** /**
* Form class for adding/modifying database authentication backends * Form class for adding/modifying database authentication backends
@ -16,60 +16,50 @@ use Icinga\Authentication\Backend\DbUserBackend;
class DbBackendForm extends BaseBackendForm class DbBackendForm extends BaseBackendForm
{ {
/** /**
* Return content of the resources.ini or previously set resources * @var array
*
* @return array
*/ */
public function getResources() protected $resources;
public function __construct()
{ {
if ($this->resources === null) { $dbResources = array_keys(
$res = ResourceFactory::getResourceConfigs('db')->toArray(); ResourceFactory::getResourceConfigs('db')->toArray()
);
if (empty($dbResources)) {
throw new ConfigurationError(
t('There are no database resources')
);
}
$this->resources = array_combine($dbResources, $dbResources);
foreach (array_keys($res) as $key) { parent::__construct();
$res[$key] = $key;
} }
return $res; public function createElements(array $formData)
} else {
return $this->resources;
}
}
/**
* Create this form and add all required elements
*
* @see Form::create()
*/
public function create()
{ {
$this->setName('form_modify_backend'); return array(
$name = $this->filterName($this->getBackendName()); $this->createElement(
$this->addElement(
'text', 'text',
'backend_' . $name . '_name', 'name',
array( array(
'required' => true, 'required' => true,
'allowEmpty' => false, 'allowEmpty' => false,
'label' => t('Backend Name'), 'label' => t('Backend Name'),
'helptext' => t('The name of this authentication provider'), 'helptext' => t('The name of this authentication provider'),
'value' => $this->getBackendName()
) )
); ),
$this->createElement(
$this->addElement(
'select', 'select',
'backend_' . $name . '_resource', 'resource',
array( array(
'required' => true, 'required' => true,
'allowEmpty' => false, 'allowEmpty' => false,
'label' => t('Database Connection'), 'label' => t('Database Connection'),
'helptext' => t('The database connection to use for authenticating with this provider'), 'helptext' => t('The database connection to use for authenticating with this provider'),
'value' => $this->getBackend()->get('resource'), 'multiOptions' => $this->resources
'multiOptions' => $this->getResources()
) )
); ),
$this->createElement(
$this->addElement(
'button', 'button',
'btn_submit', 'btn_submit',
array( array(
@ -79,6 +69,15 @@ class DbBackendForm extends BaseBackendForm
'class' => 'btn btn-cta btn-wide', 'class' => 'btn btn-cta btn-wide',
'label' => '<i class="icinga-icon-save"></i> Save Backend' 'label' => '<i class="icinga-icon-save"></i> Save Backend'
) )
),
$this->createElement(
'hidden',
'backend',
array(
'required' => true,
'value' => 'db'
)
)
); );
} }
@ -91,14 +90,12 @@ class DbBackendForm extends BaseBackendForm
*/ */
public function getConfig() public function getConfig()
{ {
$prefix = 'backend_' . $this->filterName($this->getBackendName()) . '_'; return array(
$section = $this->getValue($prefix . 'name'); $this->getValue('name') => array(
$cfg = array(
'backend' => 'db', 'backend' => 'db',
'resource' => $this->getValue($prefix . 'resource'), 'resource' => $this->getValue('resource')
)
); );
return array($section => $cfg);
} }
/** /**
@ -112,7 +109,7 @@ class DbBackendForm extends BaseBackendForm
{ {
try { try {
$testConnection = ResourceFactory::createResource(ResourceFactory::getResourceConfig( $testConnection = ResourceFactory::createResource(ResourceFactory::getResourceConfig(
$this->getValue('backend_' . $this->filterName($this->getBackendName()) . '_resource') $this->getValue('resource')
)); ));
$dbUserBackend = new DbUserBackend($testConnection); $dbUserBackend = new DbUserBackend($testConnection);
if ($dbUserBackend->count() < 1) { if ($dbUserBackend->count() < 1) {