Importsource: controller, object, list

fixes #9676
This commit is contained in:
Thomas Gelf 2015-07-21 15:16:18 +02:00
parent 1fd97c8606
commit 1c38e4469e
5 changed files with 278 additions and 2 deletions

View File

@ -0,0 +1,53 @@
<?php
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Module\Director\Web\Hook\ImportSourceHook;
class Director_ImportsourceController extends ActionController
{
public function addAction()
{
$this->forward('index', 'importsource', 'director');
}
public function editAction()
{
$this->forward('index', 'importsource', 'director');
}
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);
}
}

View File

@ -25,6 +25,19 @@ class Director_ListController extends ActionController
$this->render('table');
}
public function importsourceAction()
{
$this->view->addLink = $this->view->qlink(
$this->translate('Add import source'),
'director/importsource/add'
);
$this->setConfigTabs()->activate('importsource');
$this->view->title = $this->translate('Import source');
$this->view->table = $this->loadTable('importsource')->setConnection($this->db());
$this->render('table');
}
public function datafieldAction()
{
$this->view->addLink = $this->view->qlink(

View File

@ -0,0 +1,85 @@
<?php
namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Web\Hook;
class ImportSourceForm extends DirectorObjectForm
{
public function setup()
{
$this->addElement('text', 'source_name', array(
'label' => $this->translate('Import source name'),
'required' => true,
));
$this->addElement('text', 'key_column', array(
'label' => $this->translate('Key column name'),
'description' => $this->translate('This must be a column containing unique values like hostnames'),
'required' => true,
));
$this->addElement('select', 'provider_class', array(
'label' => $this->translate('Source Type'),
'required' => true,
'multiOptions' => $this->enumSourceTypes(),
'class' => 'autosubmit'
));
// TODO: Form needs to provide a better way for doing this
if (isset($_POST['provider_class'])) {
$class = $_POST['provider_class'];
if ($class && array_key_exists($class, $this->enumSourceTypes())) {
$this->addSettings($class);
}
}
}
protected function addSettings($class = null)
{
if ($class === null) {
if ($class = $this->getValue('provider_class')) {
$class::addSettingsFormFields($this);
}
} else {
$class::addSettingsFormFields($this);
}
}
public function loadObject($id)
{
parent::loadObject($id);
$this->addSettings();
foreach ($this->object()->getSettings() as $key => $val) {
if ($el = $this->getElement($key)) {
$el->setValue($val);
}
}
$this->moveSubmitToBottom();
return $this;
}
public function onSuccess()
{
/*
$this->getElement('owner')->setValue(
self::username()
);
*/
parent::onSuccess();
}
protected function enumSourceTypes()
{
$hooks = Hook::all('Director\\ImportSource');
$enum = array(null => '- please choose -');
foreach ($hooks as $hook) {
$enum[get_class($hook)] = $hook->getName();
}
return $enum;
}
}

View File

@ -0,0 +1,122 @@
<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
class ImportSource extends DbObject
{
protected $table = 'import_source';
protected $keyName = 'id';
protected $autoincKeyName = 'id';
protected $defaultProperties = array(
'id' => null,
'source_name' => null,
'provider_class' => null,
'key_column' => null
);
protected $settings = array();
public function set($key, $value)
{
if ($this->hasProperty($key)) {
return parent::set($key, $value);
}
if (! array_key_exists($key, $this->settings) || $value !== $this->settings[$key]) {
$this->hasBeenModified = true;
}
$this->settings[$key] = $value;
return $this;
}
public function get($key)
{
if ($this->hasProperty($key)) {
return parent::get($key);
}
if (array_key_exists($key, $this->settings)) {
return $this->settings[$key];
}
return parent::get($key);
}
public function getSettings()
{
return $this->settings;
}
protected function onStore()
{
$old = $this->fetchSettingsFromDb();
$oldKeys = array_keys($old);
$newKeys = array_keys($this->settings);
$add = array();
$mod = array();
$del = array();
foreach ($this->settings as $key => $val) {
if (array_key_exists($key, $old)) {
if ($old[$key] !== $this->settings[$key]) {
$mod[$key] = $this->settings[$key];
}
} else {
$add[$key] = $this->settings[$key];
}
}
foreach (array_diff(array_keys($old), array_keys($this->settings)) as $key) {
$del[$key] = $key;
}
$where = sprintf('source_id = %d AND setting_name = ?', $this->id);
$db = $this->getDb();
foreach ($mod as $key => $val) {
$db->update(
'import_source_setting',
array('setting_value' => $val),
$db->quoteInto($where, $key)
);
}
foreach ($add as $key => $val) {
$db->insert(
'import_source_setting',
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('import_source_setting', array('setting_name', 'setting_value'))
->where('source_id = ?', $this->id)
);
}
protected function onLoadFromDb()
{
$this->settings = $this->fetchSettingsFromDb();
}
}

View File

@ -46,8 +46,11 @@ abstract class ActionController extends Controller
'label' => $this->translate('Data lists'),
'url' => 'director/list/datalist')
)->add('datafield', array(
'label' => $this->translate('Data fields'),
'url' => 'director/list/datafield')
'label' => $this->translate('Data fields'),
'url' => 'director/list/datafield')
)->add('importsource', array(
'label' => $this->translate('Import source'),
'url' => 'director/list/importsource')
);
return $this->view->tabs;
}