From 1c38e4469e3b21b889f77b169ed507d8f1e1a474 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 21 Jul 2015 15:16:18 +0200 Subject: [PATCH] Importsource: controller, object, list fixes #9676 --- .../controllers/ImportsourceController.php | 53 ++++++++ application/controllers/ListController.php | 13 ++ application/forms/ImportSourceForm.php | 85 ++++++++++++ library/Director/Objects/ImportSource.php | 122 ++++++++++++++++++ .../Web/Controller/ActionController.php | 7 +- 5 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 application/controllers/ImportsourceController.php create mode 100644 application/forms/ImportSourceForm.php create mode 100644 library/Director/Objects/ImportSource.php diff --git a/application/controllers/ImportsourceController.php b/application/controllers/ImportsourceController.php new file mode 100644 index 00000000..2637c985 --- /dev/null +++ b/application/controllers/ImportsourceController.php @@ -0,0 +1,53 @@ +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); + } +} diff --git a/application/controllers/ListController.php b/application/controllers/ListController.php index d45170ce..a182ad9d 100644 --- a/application/controllers/ListController.php +++ b/application/controllers/ListController.php @@ -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( diff --git a/application/forms/ImportSourceForm.php b/application/forms/ImportSourceForm.php new file mode 100644 index 00000000..c2d3171e --- /dev/null +++ b/application/forms/ImportSourceForm.php @@ -0,0 +1,85 @@ +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; + } +} diff --git a/library/Director/Objects/ImportSource.php b/library/Director/Objects/ImportSource.php new file mode 100644 index 00000000..bd26e905 --- /dev/null +++ b/library/Director/Objects/ImportSource.php @@ -0,0 +1,122 @@ + 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(); + } +} diff --git a/library/Director/Web/Controller/ActionController.php b/library/Director/Web/Controller/ActionController.php index 5dbc7b5c..0fe34e69 100644 --- a/library/Director/Web/Controller/ActionController.php +++ b/library/Director/Web/Controller/ActionController.php @@ -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; }