Skip to content
Snippets Groups Projects
Commit 0d983562 authored by Alex Barth's avatar Alex Barth
Browse files

Fix storage of mappings.

parent 7ce2c5f8
No related branches found
No related tags found
No related merge requests found
...@@ -159,23 +159,26 @@ function feeds_ui_build_edit_form_submit($form, &$form_state) { ...@@ -159,23 +159,26 @@ function feeds_ui_build_edit_form_submit($form, &$form_state) {
*/ */
function feeds_ui_build_mapping_form(&$form_state, $feed) { function feeds_ui_build_mapping_form(&$form_state, $feed) {
$form = array(); $form = array();
$form['#feed'] = $feed;
// Build a mapping form for each processor configured. // Build a mapping form for each processor configured.
$sources = $feed->parser->getMappingSources(); $sources = $feed->parser->getMappingSources();
foreach ($feed->processors as $class => $processor) { foreach ($feed->processors as $class => $processor) {
// @todo: move actual form building into processors?
$mappings = $processor->getMappings(); $mappings = $processor->getMappings();
$targets = $processor->getMappingTargets(); $targets = $processor->getMappingTargets();
$form['processors'][$class] = array( $form['processors'][$class] = array(
'#type' => 'fieldset', '#type' => 'fieldset',
'#title' => $class, // @todo: human readable title. '#title' => $class, // @todo: human readable title.
'#tree' => TRUE,
); );
$form['processors'][$class]['#mappings'] = $mappings; $form['processors'][$class]['#mappings'] = $mappings;
$form['processors'][$class]['#targets'] = $targets; $form['processors'][$class]['#targets'] = $targets;
$form['processors'][$class]['sources'] = array( $form['processors'][$class]['source'] = array(
'#type' => 'select', '#type' => 'select',
'#options' => array('' => t('Select a source')) + drupal_map_assoc($sources), '#options' => array('' => t('Select a source')) + drupal_map_assoc($sources),
); );
$form['processors'][$class]['targets'] = array( $form['processors'][$class]['target'] = array(
'#type' => 'select', '#type' => 'select',
'#options' => array('' => t('Select a target')) + drupal_map_assoc(array_keys($targets)), '#options' => array('' => t('Select a target')) + drupal_map_assoc(array_keys($targets)),
); );
...@@ -187,6 +190,15 @@ function feeds_ui_build_mapping_form(&$form_state, $feed) { ...@@ -187,6 +190,15 @@ function feeds_ui_build_mapping_form(&$form_state, $feed) {
return $form; return $form;
} }
/**
* Submit handler for feeds_ui_build_mapping_form().
*/
function feeds_ui_build_mapping_form_submit($form, &$form_state) {
foreach ($form['#feed']->processors as $class => $processor) {
$processor->addMapping($form_state['values'][$class]['source'], $form_state['values'][$class]['target']);
}
}
/** /**
* Edit plugin configuration. * Edit plugin configuration.
*/ */
...@@ -250,15 +262,18 @@ function theme_feeds_ui_build_mapping_form($form) { ...@@ -250,15 +262,18 @@ function theme_feeds_ui_build_mapping_form($form) {
t('Source'), t('Source'),
t('Target'), t('Target'),
t('Unique'), t('Unique'),
t('Remove'), ' ',
); );
foreach (element_children($form['processors']) as $processor) { foreach (element_children($form['processors']) as $processor) {
$rows = array(); $rows = array();
if (is_array($form['processors'][$processor]['#mappings'])) { if (is_array($form['processors'][$processor]['#mappings'])) {
foreach ($form['processors'][$processor]['#mappings'] as $mapping) { foreach ($form['processors'][$processor]['#mappings'] as $source => $mapping) {
$rows[] = array( $rows[] = array(
// @todo. $source,
$mapping['target'],
$mapping['unique'] ? t('Yes') : t('No'),
t('Remove'),
); );
} }
} }
...@@ -271,8 +286,8 @@ function theme_feeds_ui_build_mapping_form($form) { ...@@ -271,8 +286,8 @@ function theme_feeds_ui_build_mapping_form($form) {
); );
} }
$rows[] = array( $rows[] = array(
drupal_render($form['processors'][$processor]['sources']), drupal_render($form['processors'][$processor]['source']),
drupal_render($form['processors'][$processor]['targets']), drupal_render($form['processors'][$processor]['target']),
'', '',
drupal_render($form['processors'][$processor]['add']), drupal_render($form['processors'][$processor]['add']),
); );
......
...@@ -209,7 +209,7 @@ class FeedsConfigurable { ...@@ -209,7 +209,7 @@ class FeedsConfigurable {
$save->id = $this->id; $save->id = $this->id;
$save->class = get_class($this); $save->class = get_class($this);
$save->config = $this->config; $save->config = $this->config;
db_query('DELETE FROM {feeds_configuration} WHERE id = "%s"', $save->id); db_query('DELETE FROM {feeds_configuration} WHERE id = "%s" AND class = "%s"', $save->id, $save->class);
drupal_write_record('feeds_configuration', $save); drupal_write_record('feeds_configuration', $save);
} }
...@@ -250,6 +250,8 @@ class FeedsConfigurable { ...@@ -250,6 +250,8 @@ class FeedsConfigurable {
/** /**
* Get configuration. * Get configuration.
* *
* @todo: clean up fallback. Not clear when to use and when not to use getConfig().
*
* @param $fallback * @param $fallback
* Set to false if method should NOT fall back to default configuration. * Set to false if method should NOT fall back to default configuration.
*/ */
...@@ -262,6 +264,7 @@ class FeedsConfigurable { ...@@ -262,6 +264,7 @@ class FeedsConfigurable {
/** /**
* Set configuration. * Set configuration.
* @todo: save() automatically?
* *
* @param $config * @param $config
* Array containing configuration information. Will be filtered by the keys returned by * Array containing configuration information. Will be filtered by the keys returned by
...@@ -393,6 +396,28 @@ class FeedsProcessor extends FeedsConfigurable { ...@@ -393,6 +396,28 @@ class FeedsProcessor extends FeedsConfigurable {
public function process($feed) { public function process($feed) {
} }
/**
* Declare default configuration.
*/
public function getDefaultConfig() {
return array('mappings' => array());
}
/**
* Add a mapping to existing mappings.
*/
public function addMapping($source, $target, $unique = NULL) {
if (!empty($source) && !empty($target)) {
$config = $this->getConfig();
$config['mappings'][$source] = array(
'target' => $target,
'unique' => $unique,
);
}
$this->setConfig($config);
$this->save();
}
/** /**
* Declare possible mapping targets. * Declare possible mapping targets.
* *
...@@ -410,7 +435,7 @@ class FeedsProcessor extends FeedsConfigurable { ...@@ -410,7 +435,7 @@ class FeedsProcessor extends FeedsConfigurable {
* Get mappings. * Get mappings.
*/ */
public function getMappings() { public function getMappings() {
return $this->config->mappings; return $this->config['mappings'];
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment