mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-21 21:04:26 +02:00
Modifier: initial hook and preperation for generic modifier
This commit is contained in:
parent
8621e3cfdd
commit
3a15cf8f28
53
application/forms/ImportRowModifierForm.php
Normal file
53
application/forms/ImportRowModifierForm.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Forms;
|
||||
|
||||
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
||||
use Icinga\Web\Hook;
|
||||
|
||||
class ImportRowModifierForm extends DirectorObjectForm
|
||||
{
|
||||
public function setup()
|
||||
{
|
||||
$this->addElement('text', 'source_name', array(
|
||||
'label' => $this->translate('Source name'),
|
||||
'required' => true,
|
||||
));
|
||||
|
||||
$this->addElement('text', 'source_field', array(
|
||||
'label' => $this->translate('Source field'),
|
||||
'description' => $this->translate('This must be a column from the source'),
|
||||
'required' => true,
|
||||
));
|
||||
|
||||
$this->addElement('text', 'destination_field', array(
|
||||
'label' => $this->translate('Destination field'),
|
||||
'description' => $this->translate('The value of the source will be transformed to the given attribute'),
|
||||
'required' => true,
|
||||
));
|
||||
|
||||
$this->addElement('text', 'priority', array(
|
||||
'label' => $this->translate('Priority'),
|
||||
'description' => $this->translate('This allows to prioritize the import of a field, synced from different sources for the same object'),
|
||||
'required' => true,
|
||||
));
|
||||
|
||||
$this->addElement('text', 'filter', array(
|
||||
'label' => $this->translate('Filter Expression'),
|
||||
'description' => $this->translate('This allows to filter for specific parts within the given source field'),
|
||||
'required' => true,
|
||||
));
|
||||
|
||||
$this->addElement('select', 'merge', array(
|
||||
'label' => $this->translate('Source Type'),
|
||||
'required' => true,
|
||||
'multiOptions' => array(
|
||||
'null' => '- please choose -',
|
||||
'merge' => 'merge',
|
||||
'override' => 'override'
|
||||
)
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
}
|
135
library/Director/Objects/ImportRowModifier.php
Normal file
135
library/Director/Objects/ImportRowModifier.php
Normal file
@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Objects;
|
||||
|
||||
use Icinga\Module\Director\Data\Db\DbObject;
|
||||
|
||||
class ImportRowModifier extends DbObject
|
||||
{
|
||||
protected $table = 'import_row_modifier';
|
||||
|
||||
protected $keyName = 'id';
|
||||
|
||||
protected $autoincKeyName = 'id';
|
||||
|
||||
protected $defaultProperties = array(
|
||||
'id' => null,
|
||||
'property_id' => null,
|
||||
'provider_class' => null
|
||||
);
|
||||
|
||||
/*
|
||||
protected $properties = array();
|
||||
|
||||
public function set($key, $value)
|
||||
{
|
||||
if ($this->hasProperty($key)) {
|
||||
return parent::set($key, $value);
|
||||
}
|
||||
|
||||
if (! array_key_exists($key, $this->propterties) || $value !== $this->propterties[$key]) {
|
||||
$this->hasBeenModified = true;
|
||||
}
|
||||
$this->properties[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
if ($this->hasProperty($key)) {
|
||||
return parent::get($key);
|
||||
}
|
||||
|
||||
if (array_key_exists($key, $this->properties)) {
|
||||
return $this->properties[$key];
|
||||
}
|
||||
|
||||
return parent::get($key);
|
||||
}
|
||||
|
||||
public function getProperties()
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
protected function onStore()
|
||||
{
|
||||
$old = $this->fetchSettingsFromDb();
|
||||
$oldKeys = array_keys($old);
|
||||
$newKeys = array_keys($this->properties);
|
||||
$add = array();
|
||||
$mod = array();
|
||||
$del = array();
|
||||
|
||||
foreach ($this->properties as $key => $val) {
|
||||
if (array_key_exists($key, $old)) {
|
||||
if ($old[$key] !== $this->properties[$key]) {
|
||||
$mod[$key] = $this->properties[$key];
|
||||
}
|
||||
} else {
|
||||
$add[$key] = $this->properties[$key];
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array_diff(array_keys($old), array_keys($this->settings)) as $key) {
|
||||
$del[$key] = $key;
|
||||
}
|
||||
|
||||
$modifier = $db->fetchRow(
|
||||
$db->select()->from(
|
||||
'sync_modifier',
|
||||
array('id')
|
||||
)->where('property_id = ?', $property_id)
|
||||
)
|
||||
|
||||
$where = sprintf('modifier_id = %d AND param_key = ?', $modifier->id);
|
||||
$db = $this->getDb();
|
||||
foreach ($mod as $key => $val) {
|
||||
$db->update(
|
||||
'sync_modifier_param',
|
||||
array('param_value' => $val),
|
||||
$db->quoteInto($where, $key)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($add as $key => $val) {
|
||||
$db->insert(
|
||||
'sync_modifier',
|
||||
array(
|
||||
'property_id' => $this->id,
|
||||
'
|
||||
)
|
||||
$db->insert(
|
||||
'sync_modifier_param',
|
||||
array(
|
||||
'source_id' => $this->id,
|
||||
'setting_name' => $key,
|
||||
'setting_value' => $val
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($del as $key) {
|
||||
$db->update(
|
||||
'import_source_setting',
|
||||
$db->quoteInto($where, $key)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function fetchSettingsFromDb()
|
||||
{
|
||||
$db = $this->getDb();
|
||||
return $db->fetchPairs(
|
||||
$db->select()
|
||||
->from('sync_modifier_param', array('param_name', 'param_value'))
|
||||
->where('modifier_id = ?', $this->id)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
protected function onLoadFromDb()
|
||||
{
|
||||
$this->settings = $this->fetchSettingsFromDb();
|
||||
} */
|
||||
}
|
135
library/Director/Objects/ImportRowModifierSettings.php
Normal file
135
library/Director/Objects/ImportRowModifierSettings.php
Normal file
@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Objects;
|
||||
|
||||
use Icinga\Module\Director\Data\Db\DbObject;
|
||||
|
||||
class ImportRowModifierSettings extends DbObject
|
||||
{
|
||||
protected $table = 'import_row_modifier_setting';
|
||||
|
||||
protected $keyName = 'id';
|
||||
|
||||
protected $autoincKeyName = 'id';
|
||||
|
||||
protected $defaultProperties = array(
|
||||
'modifier_id' => null,
|
||||
'setting_name' => null,
|
||||
'setting_value' => null,
|
||||
);
|
||||
|
||||
/*
|
||||
protected $properties = array();
|
||||
|
||||
public function set($key, $value)
|
||||
{
|
||||
if ($this->hasProperty($key)) {
|
||||
return parent::set($key, $value);
|
||||
}
|
||||
|
||||
if (! array_key_exists($key, $this->propterties) || $value !== $this->propterties[$key]) {
|
||||
$this->hasBeenModified = true;
|
||||
}
|
||||
$this->properties[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
if ($this->hasProperty($key)) {
|
||||
return parent::get($key);
|
||||
}
|
||||
|
||||
if (array_key_exists($key, $this->properties)) {
|
||||
return $this->properties[$key];
|
||||
}
|
||||
|
||||
return parent::get($key);
|
||||
}
|
||||
|
||||
public function getProperties()
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
protected function onStore()
|
||||
{
|
||||
$old = $this->fetchSettingsFromDb();
|
||||
$oldKeys = array_keys($old);
|
||||
$newKeys = array_keys($this->properties);
|
||||
$add = array();
|
||||
$mod = array();
|
||||
$del = array();
|
||||
|
||||
foreach ($this->properties as $key => $val) {
|
||||
if (array_key_exists($key, $old)) {
|
||||
if ($old[$key] !== $this->properties[$key]) {
|
||||
$mod[$key] = $this->properties[$key];
|
||||
}
|
||||
} else {
|
||||
$add[$key] = $this->properties[$key];
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array_diff(array_keys($old), array_keys($this->settings)) as $key) {
|
||||
$del[$key] = $key;
|
||||
}
|
||||
|
||||
$modifier = $db->fetchRow(
|
||||
$db->select()->from(
|
||||
'sync_modifier',
|
||||
array('id')
|
||||
)->where('property_id = ?', $property_id)
|
||||
)
|
||||
|
||||
$where = sprintf('modifier_id = %d AND param_key = ?', $modifier->id);
|
||||
$db = $this->getDb();
|
||||
foreach ($mod as $key => $val) {
|
||||
$db->update(
|
||||
'sync_modifier_param',
|
||||
array('param_value' => $val),
|
||||
$db->quoteInto($where, $key)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($add as $key => $val) {
|
||||
$db->insert(
|
||||
'sync_modifier',
|
||||
array(
|
||||
'property_id' => $this->id,
|
||||
'
|
||||
)
|
||||
$db->insert(
|
||||
'sync_modifier_param',
|
||||
array(
|
||||
'source_id' => $this->id,
|
||||
'setting_name' => $key,
|
||||
'setting_value' => $val
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($del as $key) {
|
||||
$db->update(
|
||||
'import_source_setting',
|
||||
$db->quoteInto($where, $key)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function fetchSettingsFromDb()
|
||||
{
|
||||
$db = $this->getDb();
|
||||
return $db->fetchPairs(
|
||||
$db->select()
|
||||
->from('sync_modifier_param', array('param_name', 'param_value'))
|
||||
->where('modifier_id = ?', $this->id)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
protected function onLoadFromDb()
|
||||
{
|
||||
$this->settings = $this->fetchSettingsFromDb();
|
||||
} */
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Sync;
|
||||
|
||||
use Icinga\Module\Director\Web\Hook\PropertyModifierHook;
|
||||
|
||||
class PropertyModifierLowercase extends PropertyModifierHook
|
||||
{
|
||||
|
||||
public function transform($value)
|
||||
{
|
||||
return strtolower($value);
|
||||
}
|
||||
|
||||
}
|
80
library/Director/Web/Hook/PropertyModifierHook.php
Normal file
80
library/Director/Web/Hook/PropertyModifierHook.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Web\Hook;
|
||||
|
||||
use Icinga\Module\Director\Web\Form\QuickForm;
|
||||
use Icinga\Module\Director\Db;
|
||||
|
||||
abstract class PropertyModifierHook
|
||||
{
|
||||
protected $settings = array();
|
||||
|
||||
public function getName()
|
||||
{
|
||||
$parts = explode('\\', get_class($this));
|
||||
$class = preg_replace('/ImportRowModifier/', '', array_pop($parts)); // right?
|
||||
|
||||
if (array_shift($parts) === 'Icinga' && array_shift($parts) === 'Module') {
|
||||
$module = array_shift($parts);
|
||||
if ($module !== 'Director') {
|
||||
return sprintf('%s (%s)', $class, $module);
|
||||
}
|
||||
}
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
public static function loadById($property_id, Db $db)
|
||||
{
|
||||
$db = $db->getDbAdapter();
|
||||
$modifier = $db->fetchRow(
|
||||
$db->select()->from(
|
||||
'import_row_modifier',
|
||||
array('id', 'provider_class')
|
||||
)->where('property_id = ?', $property_id)
|
||||
);
|
||||
|
||||
$settings = $db->fetchPairs(
|
||||
$db->select()->from(
|
||||
'import_row_modifier_settings',
|
||||
array('setting_name', 'setting_value')
|
||||
)->where('modifier_id = ?', $modifier->id)
|
||||
);
|
||||
|
||||
$obj = new $modifier->provider_class;
|
||||
$obj->setSettings($settings);
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function setSettings($settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing importable objects
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function fetchData();
|
||||
|
||||
/**
|
||||
* Returns a list of all available columns
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function listColumns();
|
||||
|
||||
/**
|
||||
* Override this method if you want to extend the settings form
|
||||
*
|
||||
* @param QuickForm $form QuickForm that should be extended
|
||||
* @return QuickForm
|
||||
*/
|
||||
public static function addPropertiesFormFields(QuickForm $form)
|
||||
{
|
||||
return $form;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user