2015-07-03 10:49:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
2015-11-17 17:27:55 +01:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2015-07-03 13:17:05 +02:00
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
2015-07-24 15:29:27 +02:00
|
|
|
use Icinga\Web\Hook;
|
2015-11-17 17:27:55 +01:00
|
|
|
use Exception;
|
2015-07-03 10:49:12 +02:00
|
|
|
|
2015-07-03 13:17:05 +02:00
|
|
|
class DirectorDatafieldForm extends DirectorObjectForm
|
2015-07-03 10:49:12 +02:00
|
|
|
{
|
2015-07-31 15:51:07 +02:00
|
|
|
protected $objectName = 'Data field';
|
|
|
|
|
2015-07-03 10:49:12 +02:00
|
|
|
public function setup()
|
|
|
|
{
|
2015-07-31 15:49:41 +02:00
|
|
|
$this->addHtmlHint(
|
|
|
|
$this->translate('Data fields allow you to customize input controls your custom variables.')
|
|
|
|
);
|
|
|
|
|
2015-07-03 10:49:12 +02:00
|
|
|
$this->addElement('text', 'varname', array(
|
2015-07-30 09:07:00 +02:00
|
|
|
'label' => $this->translate('Field name'),
|
|
|
|
'description' => $this->translate('The unique name of the field'),
|
|
|
|
'required' => true,
|
2015-07-03 10:49:12 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->addElement('text', 'caption', array(
|
2015-07-30 09:07:00 +02:00
|
|
|
'label' => $this->translate('Caption'),
|
2015-07-31 16:00:25 +02:00
|
|
|
'required' => true,
|
2015-07-03 11:41:47 +02:00
|
|
|
'description' => $this->translate('The caption which should be displayed')
|
2015-07-03 10:49:12 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->addElement('textarea', 'description', array(
|
2015-07-30 09:07:00 +02:00
|
|
|
'label' => $this->translate('Description'),
|
|
|
|
'description' => $this->translate('A description about the field'),
|
|
|
|
'rows' => '3',
|
2015-07-03 10:49:12 +02:00
|
|
|
));
|
|
|
|
|
2015-11-17 17:27:55 +01:00
|
|
|
$error = false;
|
|
|
|
try {
|
|
|
|
$types = $this->enumDataTypes();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$error = $e->getMessage();
|
|
|
|
$types = $this->optionalEnum(array());
|
|
|
|
}
|
|
|
|
|
2015-07-24 15:29:27 +02:00
|
|
|
$this->addElement('select', 'datatype', array(
|
|
|
|
'label' => $this->translate('Data type'),
|
|
|
|
'description' => $this->translate('Field type'),
|
|
|
|
'required' => true,
|
2015-11-17 17:27:55 +01:00
|
|
|
'multiOptions' => $types,
|
2015-07-30 09:07:00 +02:00
|
|
|
'class' => 'autosubmit',
|
2015-07-03 10:49:12 +02:00
|
|
|
));
|
2015-11-17 17:27:55 +01:00
|
|
|
if ($error) {
|
|
|
|
$this->getElement('datatype')->addError($error);
|
|
|
|
}
|
2015-07-27 17:25:09 +02:00
|
|
|
|
2015-11-17 17:27:55 +01:00
|
|
|
try {
|
|
|
|
if ($class = $this->getSentValue('datatype')) {
|
|
|
|
if ($class && array_key_exists($class, $types)) {
|
|
|
|
$this->addSettings($class);
|
|
|
|
}
|
|
|
|
} elseif ($class = $this->object()->datatype) {
|
2015-07-27 17:25:09 +02:00
|
|
|
$this->addSettings($class);
|
|
|
|
}
|
2015-11-17 17:27:55 +01:00
|
|
|
|
|
|
|
// TODO: next line looks like obsolete duplicate code to me
|
|
|
|
$this->addSettings();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->getElement('datatype')->addError($e->getMessage());
|
2015-07-31 16:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->object()->getSettings() as $key => $val) {
|
|
|
|
if ($el = $this->getElement($key)) {
|
|
|
|
$el->setValue($val);
|
|
|
|
}
|
2015-07-27 17:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addSettings($class = null)
|
|
|
|
{
|
|
|
|
if ($class === null) {
|
2015-11-17 17:27:55 +01:00
|
|
|
$class = $this->getValue('datatype');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class !== null) {
|
|
|
|
if (! class_exists($class)) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'The hooked class "%s" for this data field does no longer exist',
|
|
|
|
$class
|
|
|
|
);
|
2015-07-27 17:25:09 +02:00
|
|
|
}
|
2015-11-17 17:27:55 +01:00
|
|
|
|
2015-07-27 17:25:09 +02:00
|
|
|
$class::addSettingsFormFields($this);
|
|
|
|
}
|
2015-07-27 15:52:35 +02:00
|
|
|
}
|
2015-07-03 10:49:12 +02:00
|
|
|
|
2015-07-31 16:00:25 +02:00
|
|
|
protected function clearOutdatedSettings()
|
2015-07-27 15:52:35 +02:00
|
|
|
{
|
2015-07-31 16:00:25 +02:00
|
|
|
$names = array();
|
|
|
|
$object = $this->object();
|
|
|
|
$global = array('varname', 'description', 'caption', 'datatype');
|
|
|
|
|
|
|
|
foreach ($this->getElements() as $el) {
|
|
|
|
if ($el->getIgnore()) continue;
|
|
|
|
$name = $el->getName();
|
|
|
|
if (in_array($name, $global)) continue;
|
|
|
|
$names[$name] = $name;
|
2015-07-24 15:29:27 +02:00
|
|
|
}
|
2015-07-27 15:52:35 +02:00
|
|
|
|
2015-07-31 16:00:25 +02:00
|
|
|
|
|
|
|
foreach ($object->getSettings() as $setting => $value) {
|
|
|
|
if (! array_key_exists($setting, $names)) {
|
|
|
|
unset($object->$setting);
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 15:29:27 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 16:00:25 +02:00
|
|
|
public function onSuccess()
|
2015-07-28 13:20:27 +02:00
|
|
|
{
|
2015-07-31 16:00:25 +02:00
|
|
|
$this->clearOutdatedSettings();
|
2015-07-28 13:20:27 +02:00
|
|
|
|
2015-07-31 16:00:25 +02:00
|
|
|
if ($class = $this->getValue('datatype')) {
|
|
|
|
if (array_key_exists($class, $this->enumDataTypes())) {
|
|
|
|
$this->addHidden('format', $class::getFormat());
|
2015-07-28 13:20:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 16:00:25 +02:00
|
|
|
parent::onSuccess();
|
2015-07-28 13:20:27 +02:00
|
|
|
}
|
|
|
|
|
2015-07-24 15:29:27 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|