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,68 +16,67 @@ 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()
);
foreach (array_keys($res) as $key) { if (empty($dbResources)) {
$res[$key] = $key; throw new ConfigurationError(
} t('There are no database resources')
);
return $res;
} else {
return $this->resources;
} }
$this->resources = array_combine($dbResources, $dbResources);
parent::__construct();
} }
/** public function createElements(array $formData)
* 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', 'name',
'backend_' . $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(
); 'select',
'resource',
$this->addElement( array(
'select', 'required' => true,
'backend_' . $name . '_resource', 'allowEmpty' => false,
array( 'label' => t('Database Connection'),
'required' => true, 'helptext' => t('The database connection to use for authenticating with this provider'),
'allowEmpty' => false, 'multiOptions' => $this->resources
'label' => t('Database Connection'), )
'helptext' => t('The database connection to use for authenticating with this provider'), ),
'value' => $this->getBackend()->get('resource'), $this->createElement(
'multiOptions' => $this->getResources() 'button',
) 'btn_submit',
); array(
'type' => 'submit',
$this->addElement( 'value' => '1',
'button', 'escape' => false,
'btn_submit', 'class' => 'btn btn-cta btn-wide',
array( 'label' => '<i class="icinga-icon-save"></i> Save Backend'
'type' => 'submit', )
'value' => '1', ),
'escape' => false, $this->createElement(
'class' => 'btn btn-cta btn-wide', 'hidden',
'label' => '<i class="icinga-icon-save"></i> Save Backend' '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('resource')
'resource' => $this->getValue($prefix . '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) {