2015-07-23 11:45:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
use Icinga\Application\Hook;
|
2016-03-06 00:37:45 +01:00
|
|
|
use Icinga\Module\Director\Hook\ImportSourceHook;
|
2016-02-18 23:33:44 +01:00
|
|
|
use Icinga\Module\Director\Objects\ImportSource;
|
2015-07-23 11:45:07 +02:00
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
|
|
|
|
|
|
|
class ImportRowModifierForm extends DirectorObjectForm
|
|
|
|
{
|
2016-02-18 23:33:44 +01:00
|
|
|
protected $source;
|
|
|
|
|
2016-03-06 00:37:45 +01:00
|
|
|
protected $importSource;
|
|
|
|
|
2015-07-23 11:45:07 +02:00
|
|
|
public function setup()
|
|
|
|
{
|
2016-02-18 23:33:44 +01:00
|
|
|
$this->addHidden('source_id', $this->source->id);
|
|
|
|
$this->addHidden('priority', 1);
|
2015-07-23 11:45:07 +02:00
|
|
|
|
2016-03-06 00:37:45 +01:00
|
|
|
$this->addElement('select', 'property_name', array(
|
|
|
|
'label' => $this->translate('Property'),
|
|
|
|
'description' => $this->translate('This must be an import source column (property)'),
|
|
|
|
'multiOptions' => $this->optionalEnum($this->enumSourceColumns()),
|
|
|
|
'required' => true,
|
2015-07-23 11:45:07 +02:00
|
|
|
));
|
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
$error = false;
|
|
|
|
try {
|
|
|
|
$mods = $this->enumModifiers();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$error = $e->getMessage();
|
|
|
|
$mods = $this->optionalEnum(array());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addElement('select', 'provider_class', array(
|
|
|
|
'label' => $this->translate('Modifier'),
|
|
|
|
'required' => true,
|
2016-03-06 00:37:45 +01:00
|
|
|
'description' => $this->translate(
|
|
|
|
'A property modifier allows you to modify a specific property at import time'
|
|
|
|
),
|
2016-02-18 23:33:44 +01:00
|
|
|
'multiOptions' => $this->optionalEnum($mods),
|
|
|
|
'class' => 'autosubmit',
|
2015-07-23 11:45:07 +02:00
|
|
|
));
|
2016-02-18 23:33:44 +01:00
|
|
|
if ($error) {
|
|
|
|
$this->getElement('provider_class')->addError($error);
|
|
|
|
}
|
2015-07-23 11:45:07 +02:00
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
try {
|
|
|
|
if ($class = $this->getSentValue('provider_class')) {
|
|
|
|
if ($class && array_key_exists($class, $mods)) {
|
|
|
|
$this->addSettings($class);
|
|
|
|
}
|
|
|
|
} elseif ($class = $this->object()->provider_class) {
|
|
|
|
$this->addSettings($class);
|
|
|
|
}
|
2015-07-23 11:45:07 +02:00
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
// TODO: next line looks like obsolete duplicate code to me
|
|
|
|
$this->addSettings();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->getElement('provider_class')->addError($e->getMessage());
|
|
|
|
}
|
2015-07-23 11:45:07 +02:00
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
foreach ($this->object()->getSettings() as $key => $val) {
|
|
|
|
if ($el = $this->getElement($key)) {
|
|
|
|
$el->setValue($val);
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 02:00:42 +01:00
|
|
|
|
|
|
|
$this->setButtons();
|
2015-07-23 11:45:07 +02:00
|
|
|
}
|
|
|
|
|
2016-03-06 00:37:45 +01:00
|
|
|
protected function enumSourceColumns()
|
|
|
|
{
|
|
|
|
$columns = $this->getImportSource()->listColumns();
|
|
|
|
$columns = array_combine($columns, $columns);
|
|
|
|
return $columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function getImportSource()
|
|
|
|
{
|
|
|
|
if ($this->importSource === null) {
|
|
|
|
$this->importSource = ImportSourceHook::loadByName($this->source->source_name, $this->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->importSource;
|
|
|
|
}
|
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
protected function enumModifiers()
|
|
|
|
{
|
|
|
|
$hooks = Hook::all('Director\\PropertyModifier');
|
|
|
|
$enum = array();
|
|
|
|
foreach ($hooks as $hook) {
|
|
|
|
$enum[get_class($hook)] = $hook->getName();
|
|
|
|
}
|
|
|
|
|
2016-03-06 00:37:45 +01:00
|
|
|
asort($enum);
|
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
return $enum;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addSettings($class = null)
|
|
|
|
{
|
|
|
|
if ($class === null) {
|
|
|
|
$class = $this->getValue('provider_class');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class !== null) {
|
|
|
|
if (! class_exists($class)) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'The hooked class "%s" for this property modifier does no longer exist',
|
|
|
|
$class
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class::addSettingsFormFields($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSource(ImportSource $source)
|
|
|
|
{
|
|
|
|
$this->source = $source;
|
|
|
|
return $this;
|
|
|
|
}
|
2015-07-23 11:45:07 +02:00
|
|
|
}
|