DataTypeSqlQuery: add a new crazy data type

This commit is contained in:
Thomas Gelf 2015-07-28 17:58:02 +02:00
parent 00267a3b4f
commit 6db336fc54
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<?php
namespace Icinga\Module\Director\DataType;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Director\Web\Hook\DataTypeHook;
use Icinga\Data\Db\DbConnection;
class DataTypeSqlQuery extends DataTypeHook
{
protected $db;
protected static $cachedResult;
protected static $cacheTime = 0;
public function getFormElement($name, QuickForm $form)
{
$element = $form->createElement('select', $name, array(
'multiOptions' => array(null => '- please choose -') +
$this->fetchData(),
));
return $element;
}
protected function fetchData()
{
if (self::$cachedResult === null || (time() - self::$cacheTime > 3)) {
self::$cachedResult = $this->db()->fetchPairs($this->settings['query']);
self::$cacheTime = time();
}
return self::$cachedResult;
}
public static function addSettingsFormFields(QuickForm $form)
{
$db = $form->getDb();
$form->addElement('text', 'resource', array(
'label' => 'Resource name',
'required' => true,
));
$form->addElement('textarea', 'query', array(
'label' => 'DB Query',
'description' => 'This query should return exactly two columns, value and label',
'required' => true,
));
return $form;
}
protected function db()
{
if ($this->db === null) {
$this->db = DbConnection::fromResourceName($this->settings['resource'])->getDbAdapter();
}
return $this->db;
}
}

View File

@ -7,3 +7,4 @@ $this->registerHook('Director\\DataType', '\\Icinga\\Module\\Director\\DataType\
$this->registerHook('Director\\DataType', '\\Icinga\\Module\\Director\\DataType\\DataTypeNumber', 'number');
$this->registerHook('Director\\DataType', '\\Icinga\\Module\\Director\\DataType\\DataTypeTime', 'time');
$this->registerHook('Director\\DataType', '\\Icinga\\Module\\Director\\DataType\\DataTypeDatalist', 'datalist');
$this->registerHook('Director\\DataType', '\\Icinga\\Module\\Director\\DataType\\DataTypeSqlQuery', 'sqlquery');