2015-07-23 11:45:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
2016-11-01 18:28:36 +01:00
|
|
|
use Exception;
|
2016-02-18 23:33:44 +01:00
|
|
|
use Icinga\Application\Hook;
|
2016-11-01 18:28:36 +01:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2016-03-06 00:37:45 +01:00
|
|
|
use Icinga\Module\Director\Hook\ImportSourceHook;
|
2016-11-01 18:28:36 +01:00
|
|
|
use Icinga\Module\Director\Hook\PropertyModifierHook;
|
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;
|
2018-07-13 11:46:25 +02:00
|
|
|
use RuntimeException;
|
2015-07-23 11:45:07 +02:00
|
|
|
|
|
|
|
class ImportRowModifierForm extends DirectorObjectForm
|
|
|
|
{
|
2017-01-12 17:26:06 +01:00
|
|
|
/** @var ImportSource */
|
2016-02-18 23:33:44 +01:00
|
|
|
protected $source;
|
|
|
|
|
2017-01-12 17:26:06 +01:00
|
|
|
/** @var ImportSourceHook */
|
2016-03-06 00:37:45 +01:00
|
|
|
protected $importSource;
|
|
|
|
|
2018-07-13 11:46:25 +02:00
|
|
|
/**
|
|
|
|
* @throws \Zend_Form_Exception
|
|
|
|
*/
|
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);
|
2015-07-23 11:45:07 +02:00
|
|
|
|
2019-08-19 08:46:17 +02:00
|
|
|
$this->addElement('text', 'property_name', array(
|
2016-03-06 00:37:45 +01:00
|
|
|
'label' => $this->translate('Property'),
|
2019-08-19 10:31:06 +02:00
|
|
|
'description' => $this->translate(
|
2019-08-19 11:55:15 +02:00
|
|
|
'Please start typing for a list of suggestions. Dots allow you to access nested'
|
2019-08-19 10:31:06 +02:00
|
|
|
. ' properties: column.some.key. Such nested properties cannot be modified in-place,'
|
|
|
|
. ' but you can store the modified value to a new "target property"'
|
|
|
|
),
|
2016-03-06 00:37:45 +01:00
|
|
|
'required' => true,
|
2019-08-19 08:46:17 +02:00
|
|
|
'class' => 'autosubmit director-suggest',
|
|
|
|
'data-suggestion-context' => 'importsourceproperties!' . $this->source->id,
|
|
|
|
));
|
2015-07-23 11:45:07 +02:00
|
|
|
|
2018-07-13 11:46:25 +02:00
|
|
|
$this->addElement('text', 'target_property', [
|
2016-07-20 13:41:16 +02:00
|
|
|
'label' => $this->translate('Target property'),
|
|
|
|
'description' => $this->translate(
|
2016-08-23 16:23:18 +02:00
|
|
|
'You might want to write the modified value to another (new) property.'
|
2016-07-20 13:41:16 +02:00
|
|
|
. ' This property name can be defined here, the original property would'
|
|
|
|
. ' remain unmodified. Please leave this blank in case you just want to'
|
|
|
|
. ' modify the value of a specific property'
|
|
|
|
),
|
2018-07-13 11:46:25 +02:00
|
|
|
]);
|
2016-07-20 13:41:16 +02:00
|
|
|
|
2018-07-13 11:46:25 +02:00
|
|
|
$this->addElement('textarea', 'description', [
|
2017-07-14 14:45:25 +02:00
|
|
|
'label' => $this->translate('Description'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'An extended description for this Import Row Modifier. This should explain'
|
|
|
|
. " it's purpose and why it has been put in place at all."
|
|
|
|
),
|
|
|
|
'rows' => '3',
|
2018-07-13 11:46:25 +02:00
|
|
|
]);
|
2017-07-14 14:45:25 +02:00
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
$error = false;
|
|
|
|
try {
|
|
|
|
$mods = $this->enumModifiers();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$error = $e->getMessage();
|
2018-07-13 11:46:25 +02:00
|
|
|
$mods = $this->optionalEnum([]);
|
2016-02-18 23:33:44 +01:00
|
|
|
}
|
2023-05-25 15:44:56 +02:00
|
|
|
$this->addElement('YesNo', 'use_filter', [
|
|
|
|
'label' => $this->translate('Set based on filter'),
|
|
|
|
'ignore' => true,
|
|
|
|
'class' => 'autosubmit',
|
|
|
|
'required' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($this->hasBeenSent()) {
|
|
|
|
$useFilter = $this->getSentValue('use_filter');
|
|
|
|
if ($useFilter === null) {
|
|
|
|
$this->setElementValue('use_filter', $useFilter = 'n');
|
|
|
|
}
|
|
|
|
} elseif ($object = $this->getObject()) {
|
|
|
|
$expression = $object->get('filter_expression');
|
|
|
|
$useFilter = ($expression === null || strlen($expression) === 0) ? 'n' : 'y';
|
|
|
|
$this->setElementValue('use_filter', $useFilter);
|
|
|
|
} else {
|
|
|
|
$this->setElementValue('use_filter', $useFilter = 'n');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($useFilter === 'y') {
|
|
|
|
$this->addElement('text', 'filter_expression', [
|
|
|
|
'label' => $this->translate('Filter Expression'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'This allows to filter for specific parts within the given source expression.'
|
|
|
|
. ' You are allowed to refer all imported columns. Examples: host=www* would'
|
|
|
|
. ' set this property only for rows imported with a host property starting'
|
|
|
|
. ' with "www". Complex example: host=www*&!(address=127.*|address6=::1).'
|
|
|
|
. ' Please note, that CIDR notation based matches are also supported: '
|
2023-05-31 07:18:51 +02:00
|
|
|
. ' address=192.0.2.128/25| address=2001:db8::/32| address=::ffff:192.0.2.0/96'
|
2023-05-25 15:44:56 +02:00
|
|
|
),
|
|
|
|
'required' => true,
|
|
|
|
// TODO: validate filter
|
|
|
|
]);
|
|
|
|
}
|
2018-01-25 13:13:42 +01:00
|
|
|
|
2018-07-13 11:46:25 +02:00
|
|
|
$this->addElement('select', 'provider_class', [
|
2016-02-18 23:33:44 +01:00
|
|
|
'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',
|
2018-07-13 11:46:25 +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);
|
|
|
|
}
|
2016-11-01 18:28:36 +01:00
|
|
|
} elseif ($class = $this->object()->get('provider_class')) {
|
2016-02-18 23:33:44 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2020-11-25 02:53:29 +01:00
|
|
|
public function getSetting($name, $default = null)
|
|
|
|
{
|
|
|
|
if ($this->hasBeenSent()) {
|
|
|
|
$value = $this->getSentValue($name);
|
|
|
|
if ($value !== null) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->isNew()) {
|
|
|
|
$value = $this->getElement($name)->getValue();
|
|
|
|
if ($value === null) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->object()->getSetting($name, $default);
|
|
|
|
}
|
|
|
|
|
2018-07-13 11:46:25 +02:00
|
|
|
/**
|
|
|
|
* @return ImportSourceHook
|
|
|
|
* @throws ConfigurationError
|
|
|
|
*/
|
2016-03-06 00:37:45 +01:00
|
|
|
protected function getImportSource()
|
|
|
|
{
|
|
|
|
if ($this->importSource === null) {
|
2017-01-12 17:26:06 +01:00
|
|
|
$this->importSource = ImportSourceHook::loadByName(
|
|
|
|
$this->source->get('source_name'),
|
|
|
|
$this->db
|
|
|
|
);
|
2016-03-06 00:37:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->importSource;
|
|
|
|
}
|
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
protected function enumModifiers()
|
|
|
|
{
|
2016-11-01 18:28:36 +01:00
|
|
|
/** @var PropertyModifierHook[] $hooks */
|
2016-02-18 23:33:44 +01:00
|
|
|
$hooks = Hook::all('Director\\PropertyModifier');
|
2018-07-13 11:46:25 +02:00
|
|
|
$enum = [];
|
2016-02-18 23:33:44 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-13 11:46:25 +02:00
|
|
|
/**
|
|
|
|
* @param null $class
|
|
|
|
*/
|
2016-02-18 23:33:44 +01:00
|
|
|
protected function addSettings($class = null)
|
|
|
|
{
|
|
|
|
if ($class === null) {
|
|
|
|
$class = $this->getValue('provider_class');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class !== null) {
|
|
|
|
if (! class_exists($class)) {
|
2022-04-26 12:58:42 +02:00
|
|
|
throw new RuntimeException(sprintf(
|
2016-02-18 23:33:44 +01:00
|
|
|
'The hooked class "%s" for this property modifier does no longer exist',
|
|
|
|
$class
|
2022-04-26 12:58:42 +02:00
|
|
|
));
|
2016-02-18 23:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$class::addSettingsFormFields($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSource(ImportSource $source)
|
|
|
|
{
|
|
|
|
$this->source = $source;
|
2018-07-13 11:46:25 +02:00
|
|
|
|
2016-02-18 23:33:44 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2015-07-23 11:45:07 +02:00
|
|
|
}
|