mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-30 09:14:09 +02:00
parent
6aed0a2097
commit
142e3f2c43
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\PropertyModifier;
|
||||
|
||||
use Icinga\Module\Director\Forms\ImportRowModifierForm;
|
||||
use Icinga\Module\Director\Hook\PropertyModifierHook;
|
||||
use Icinga\Module\Director\Objects\ImportRowModifier;
|
||||
use Icinga\Module\Director\Objects\ImportSource;
|
||||
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
||||
use Icinga\Module\Director\Web\Form\QuickForm;
|
||||
|
||||
class PropertyModifierGetPropertyFromOtherImportSource extends PropertyModifierHook
|
||||
{
|
||||
protected $importSource;
|
||||
|
||||
private $importedData;
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'Get a property from another Import Source';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* @throws \Zend_Form_Exception
|
||||
*/
|
||||
public static function addSettingsFormFields(QuickForm $form)
|
||||
{
|
||||
if (! $form instanceof DirectorObjectForm) {
|
||||
throw new \RuntimeException('This property modifier works only with a DirectorObjectForm');
|
||||
}
|
||||
$db = $form->getDb();
|
||||
$form->addElement('select', 'import_source_id', [
|
||||
'label' => $form->translate('Import Source'),
|
||||
'description' => $form->translate(
|
||||
'Another Import Source. We\'re going to look up the row with the'
|
||||
. ' key matching the value in the chosen column'
|
||||
),
|
||||
'required' => true,
|
||||
'multiOptions' => $form->optionalEnum($db->enumImportSource()),
|
||||
'class' => 'autosubmit',
|
||||
]);
|
||||
|
||||
if ($form->hasBeenSent()) {
|
||||
$sourceId = $form->getSentValue('import_source_id');
|
||||
} else {
|
||||
$object = $form->getObject();
|
||||
if ($object instanceof ImportRowModifier) {
|
||||
$sourceId = $object->getSetting('import_source_id');
|
||||
} else {
|
||||
$sourceId = null;
|
||||
}
|
||||
}
|
||||
$extra = [];
|
||||
if ($sourceId) {
|
||||
$extra = [
|
||||
'class' => 'director-suggest',
|
||||
'data-suggestion-context' => 'importsourceproperties!' . (int) $sourceId,
|
||||
];
|
||||
}
|
||||
$form->addElement('text', 'foreign_property', [
|
||||
'label' => $form->translate('Property'),
|
||||
'required' => true,
|
||||
'description' => $form->translate(
|
||||
'The property to get from the row we found in the chosen Import Source'
|
||||
),
|
||||
] + $extra);
|
||||
}
|
||||
|
||||
public function transform($value)
|
||||
{
|
||||
$data = $this->getImportedData();
|
||||
|
||||
if (isset($data[$value])) {
|
||||
return $data[$value]->{$this->getSetting('foreign_property')};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function exportSettings()
|
||||
{
|
||||
$settings = parent::exportSettings();
|
||||
$settings->import_source = $this->getImportSource()->getObjectName();
|
||||
unset($settings->import_source_id);
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
protected function & getImportedData()
|
||||
{
|
||||
if ($this->importedData === null) {
|
||||
$this->importedData = $this->getImportSource()
|
||||
->fetchLastRun(true)
|
||||
->fetchRows([$this->getSetting('foreign_property')]);
|
||||
}
|
||||
|
||||
return $this->importedData;
|
||||
}
|
||||
|
||||
protected function getImportSource()
|
||||
{
|
||||
if ($this->importSource === null) {
|
||||
$this->importSource = ImportSource::loadWithAutoIncId(
|
||||
$this->getSetting('import_source_id'),
|
||||
$this->getDb()
|
||||
);
|
||||
}
|
||||
|
||||
return $this->importSource;
|
||||
}
|
||||
}
|
@ -25,6 +25,7 @@ use Icinga\Module\Director\PropertyModifier\PropertyModifierExtractFromDN;
|
||||
use Icinga\Module\Director\PropertyModifier\PropertyModifierFromAdSid;
|
||||
use Icinga\Module\Director\PropertyModifier\PropertyModifierFromLatin1;
|
||||
use Icinga\Module\Director\PropertyModifier\PropertyModifierGetHostByName;
|
||||
use Icinga\Module\Director\PropertyModifier\PropertyModifierGetPropertyFromOtherImportSource;
|
||||
use Icinga\Module\Director\PropertyModifier\PropertyModifierJoin;
|
||||
use Icinga\Module\Director\PropertyModifier\PropertyModifierJsonDecode;
|
||||
use Icinga\Module\Director\PropertyModifier\PropertyModifierLConfCustomVar;
|
||||
@ -83,6 +84,7 @@ $directorHooks = [
|
||||
PropertyModifierFromAdSid::class,
|
||||
PropertyModifierFromLatin1::class,
|
||||
PropertyModifierGetHostByName::class,
|
||||
PropertyModifierGetPropertyFromOtherImportSource::class,
|
||||
PropertyModifierJoin::class,
|
||||
PropertyModifierJsonDecode::class,
|
||||
PropertyModifierLConfCustomVar::class,
|
||||
|
Loading…
x
Reference in New Issue
Block a user