icingaweb2-module-director/application/forms/DirectorDatafieldForm.php

76 lines
2.2 KiB
PHP
Raw Normal View History

2015-07-03 10:49:12 +02:00
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Web\Hook;
2015-07-03 10:49:12 +02:00
class DirectorDatafieldForm extends DirectorObjectForm
2015-07-03 10:49:12 +02:00
{
public function setup()
{
$this->addElement('text', 'varname', array(
2015-07-03 12:05:48 +02:00
'required' => true,
2015-07-03 11:41:47 +02:00
'label' => $this->translate('Field name'),
'description' => $this->translate('The unique name of the field')
2015-07-03 10:49:12 +02:00
));
$this->addElement('text', 'caption', array(
2015-07-03 11:41:47 +02:00
'label' => $this->translate('Caption'),
'description' => $this->translate('The caption which should be displayed')
2015-07-03 10:49:12 +02:00
));
$this->addElement('textarea', 'description', array(
2015-07-03 11:41:47 +02:00
'label' => $this->translate('Description'),
'description' => $this->translate('A description about the field')
2015-07-03 10:49:12 +02:00
));
$this->addElement('select', 'datatype', array(
'label' => $this->translate('Data type'),
'description' => $this->translate('Field type'),
'required' => true,
'multiOptions' => $this->enumDataTypes(),
'class' => 'autosubmit'
2015-07-03 10:49:12 +02:00
));
if ($class = $this->getSentValue('datatype')) {
if ($class && array_key_exists($class, $this->enumDataTypes())) {
$this->addSettings($class);
}
}
}
protected function addSettings($class = null)
{
if ($class === null) {
if ($class = $this->getValue('datatype')) {
$class::addSettingsFormFields($this);
}
} else {
$class::addSettingsFormFields($this);
}
}
2015-07-03 10:49:12 +02:00
public function onSuccess()
{
if ($class = $this->getValue('datatype')) {
if (array_key_exists($class, $this->enumDataTypes())) {
$this->addHidden('format', $class::getFormat());
}
}
parent::onSuccess();
}
protected function enumDataTypes()
{
$hooks = Hook::all('Director\\DataType');
$enum = array(null => '- please choose -');
foreach ($hooks as $hook) {
$enum[get_class($hook)] = $hook->getName();
}
return $enum;
2015-07-03 10:49:12 +02:00
}
}