SyncRule: Controller Object and fomr
This commit is contained in:
parent
a410fa8f05
commit
3d3dc19d3b
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
use Icinga\Module\Director\Web\Controller\ActionController;
|
||||
use Icinga\Module\Director\Objects\SyncRule;
|
||||
use Icinga\Module\Director\Sync\Sync;
|
||||
use Icinga\Exception\InvalidPropertyException;
|
||||
use Icinga\Web\Notification;
|
||||
|
||||
class Director_SyncruleController extends ActionController
|
||||
{
|
||||
public function addAction()
|
||||
{
|
||||
$this->forward('index', 'syncrule', 'director');
|
||||
}
|
||||
|
||||
public function editAction()
|
||||
{
|
||||
$this->forward('index', 'syncrule', 'director');
|
||||
}
|
||||
|
||||
public function runAction()
|
||||
{
|
||||
if ($runId = Import::run($id = ImportSource::load($this->params->get('id'), $this->db()))) {
|
||||
Notification::success('adf' . $runId);
|
||||
$this->redirectNow('director/list/importrun');
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
public function indexAction()
|
||||
{
|
||||
$edit = false;
|
||||
|
||||
if ($id = $this->params->get('id')) {
|
||||
$edit = true;
|
||||
}
|
||||
|
||||
if ($edit) {
|
||||
$this->view->title = $this->translate('Edit import source');
|
||||
$this->getTabs()->add('edit', array(
|
||||
'url' => 'director/importsource/edit' . '?id=' . $id,
|
||||
'label' => $this->view->title,
|
||||
))->activate('edit');
|
||||
} else {
|
||||
$this->view->title = $this->translate('Add import source');
|
||||
$this->getTabs()->add('add', array(
|
||||
'url' => 'director/importsource/add',
|
||||
'label' => $this->view->title,
|
||||
))->activate('add');
|
||||
}
|
||||
|
||||
$form = $this->view->form = $this->loadForm('importSource')
|
||||
->setSuccessUrl('director/list/importsource')
|
||||
->setDb($this->db());
|
||||
|
||||
if ($edit) {
|
||||
$form->loadObject($id);
|
||||
}
|
||||
|
||||
$form->handleRequest();
|
||||
|
||||
$this->render('object/form', null, true);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Forms;
|
||||
|
||||
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
||||
use Icinga\Web\Hook;
|
||||
|
||||
class SyncRuleForm extends DirectorObjectForm
|
||||
{
|
||||
public function setup()
|
||||
{
|
||||
$this->addElement('text', 'rule_name', array(
|
||||
'label' => $this->translate('Rule name'),
|
||||
'required' => true,
|
||||
));
|
||||
|
||||
$this->addElement('select', 'object_type', array(
|
||||
'label' => $this->translate('Please select a object type'),
|
||||
'description' => $this->translate('This must be a column from the source'),
|
||||
'required' => true,
|
||||
'multiOptions' => array(
|
||||
'null' => '- please choose -',
|
||||
'host' => 'host',
|
||||
'user' => 'user'
|
||||
)
|
||||
));
|
||||
|
||||
$this->addElement('select', 'update_policy', array(
|
||||
'label' => $this->translate('Destination field'),
|
||||
'description' => $this->translate('The value of the source will be transformed to the given attribute'),
|
||||
'required' => true,
|
||||
'multiOptions' => array(
|
||||
'null' => '- please choose -',
|
||||
'merge' => 'merge',
|
||||
'override' => 'override',
|
||||
'ignore' => 'ignore'
|
||||
)
|
||||
));
|
||||
|
||||
$this->addElement('text', 'purge_existing', 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,
|
||||
'multiOptions' => array(
|
||||
'null' => '- please choose -',
|
||||
'y' => 'yes',
|
||||
'n' => 'no'
|
||||
)
|
||||
));
|
||||
|
||||
$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,
|
||||
));
|
||||
}
|
||||
|
||||
public function loadObject($id)
|
||||
{
|
||||
parent::loadObject($id);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function onSuccess()
|
||||
{
|
||||
/*
|
||||
$this->getElement('owner')->setValue(
|
||||
self::username()
|
||||
);
|
||||
*/
|
||||
parent::onSuccess();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,138 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Objects;
|
||||
|
||||
use Icinga\Module\Director\Data\Db\DbObject;
|
||||
|
||||
class SyncRule extends DbObject
|
||||
{
|
||||
protected $table = 'sync_rule';
|
||||
|
||||
protected $keyName = 'id';
|
||||
|
||||
protected $autoincKeyName = 'id';
|
||||
|
||||
protected $defaultProperties = array(
|
||||
'id' => null,
|
||||
'rule_name' => null,
|
||||
'object_type' => null,
|
||||
'update_policy' => null,
|
||||
'purge_existing' => null,
|
||||
'filter_expression' => 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();
|
||||
} */
|
||||
}
|
Loading…
Reference in New Issue