mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-31 01:34:12 +02:00
parent
313dba50cb
commit
e76665741e
@ -63,6 +63,18 @@ class ImportSourceForm extends DirectorObjectForm
|
||||
}
|
||||
}
|
||||
|
||||
public function hasChangedSetting($name)
|
||||
{
|
||||
if ($this->hasBeenSent() && $this->hasObject()) {
|
||||
/** @var ImportSource $object */
|
||||
$object = $this->getObject();
|
||||
return $object->getStoredSetting($name)
|
||||
!== $this->getSentValue($name);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function addSettings()
|
||||
{
|
||||
if (! ($class = $this->getProviderClass())) {
|
||||
|
@ -56,6 +56,16 @@ abstract class DbObjectWithSettings extends DbObject
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function getStoredSetting($name, $default = null)
|
||||
{
|
||||
$stored = $this->fetchSettingsFromDb();
|
||||
if (array_key_exists($name, $stored)) {
|
||||
return $stored[$name];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function __unset($key)
|
||||
{
|
||||
if ($this->hasProperty($key)) {
|
||||
|
@ -3,9 +3,13 @@
|
||||
namespace Icinga\Module\Director\Import;
|
||||
|
||||
use Icinga\Data\Db\DbConnection;
|
||||
use Icinga\Module\Director\Forms\ImportSourceForm;
|
||||
use Icinga\Module\Director\Hook\ImportSourceHook;
|
||||
use Icinga\Module\Director\Objects\ImportSource;
|
||||
use Icinga\Module\Director\Util;
|
||||
use Icinga\Module\Director\Web\Form\Filter\QueryColumnsFromSql;
|
||||
use Icinga\Module\Director\Web\Form\QuickForm;
|
||||
use ipl\Html\Html;
|
||||
|
||||
class ImportSourceSql extends ImportSourceHook
|
||||
{
|
||||
@ -18,17 +22,43 @@ class ImportSourceSql extends ImportSourceHook
|
||||
|
||||
public function listColumns()
|
||||
{
|
||||
if ($columns = $this->getSetting('column_cache')) {
|
||||
return explode(', ', $columns);
|
||||
} else {
|
||||
return array_keys((array) current($this->fetchData()));
|
||||
}
|
||||
}
|
||||
|
||||
public static function addSettingsFormFields(QuickForm $form)
|
||||
{
|
||||
/** @var ImportSourceForm $form */
|
||||
Util::addDbResourceFormElement($form, 'resource');
|
||||
$form->addElement('textarea', 'query', array(
|
||||
'label' => 'DB Query',
|
||||
/** @var ImportSource $current */
|
||||
$current = $form->getObject();
|
||||
|
||||
$form->addElement('textarea', 'query', [
|
||||
'label' => $form->translate('DB Query'),
|
||||
'required' => true,
|
||||
'rows' => 15,
|
||||
]);
|
||||
$form->addElement('hidden', 'column_cache', [
|
||||
'value' => '',
|
||||
'filters' => [new QueryColumnsFromSql($form)],
|
||||
'required' => true
|
||||
]);
|
||||
if ($current) {
|
||||
if ($columns = $current->getSetting('column_cache')) {
|
||||
$form->addHtmlHint('Columns: ' . $columns);
|
||||
} else {
|
||||
$form->addHtmlHint(Html::tag(
|
||||
'p',
|
||||
['class' => 'warning'],
|
||||
$form->translate(
|
||||
'Please click "Store" once again to determine query columns'
|
||||
)
|
||||
));
|
||||
}
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Web\Form\Filter;
|
||||
|
||||
use Exception;
|
||||
use Icinga\Module\Director\Forms\ImportSourceForm;
|
||||
use Zend_Filter_Interface;
|
||||
|
||||
class QueryColumnsFromSql implements Zend_Filter_Interface
|
||||
{
|
||||
/** @var ImportSourceForm */
|
||||
private $form;
|
||||
|
||||
public function __construct(ImportSourceForm $form)
|
||||
{
|
||||
$this->form = $form;
|
||||
}
|
||||
|
||||
public function filter($value)
|
||||
{
|
||||
$form = $this->form;
|
||||
if (empty($value) || $form->hasChangedSetting('query')) {
|
||||
try {
|
||||
return implode(
|
||||
', ',
|
||||
$this->getQueryColumns($form->getSentOrObjectSetting('query'))
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
$this->form->addUniqueException($e);
|
||||
return '';
|
||||
}
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getQueryColumns($query)
|
||||
{
|
||||
return array_keys((array) current(
|
||||
$this->form->getDb()->getDbAdapter()->fetchAll($query)
|
||||
));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user