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

#849840 adityakg, rbayliss, alex_b: Submit full mapping on every submission.

parent b422863d
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
Feeds 6.x 1.0 XXXXXXXXXXXXXXXXXX Feeds 6.x 1.0 XXXXXXXXXXXXXXXXXX
-------------------------------- --------------------------------
- #849840 adityakg, rbayliss, alex_b: Submit full mapping on every submission.
- #849834 rbayliss, alex_b: Generalize feeds_config_form() to feeds_form(). - #849834 rbayliss, alex_b: Generalize feeds_config_form() to feeds_form().
- #907064 alex_b: Track imported terms. - #907064 alex_b: Track imported terms.
- #906720 alex_b: Introduce a hook_feeds_after_clear(). - #906720 alex_b: Introduce a hook_feeds_after_clear().
......
...@@ -531,9 +531,6 @@ function feeds_ui_mapping_form(&$form_state, $importer) { ...@@ -531,9 +531,6 @@ function feeds_ui_mapping_form(&$form_state, $importer) {
if (is_array($mappings)) { if (is_array($mappings)) {
foreach ($mappings as $i => $mapping) { foreach ($mappings as $i => $mapping) {
$mappings[$i]['source'] = isset($source_options) ? $source_options[$mappings[$i]['source']] : $mappings[$i]['source'];
$mappings[$i]['target'] = isset($target_options[$mappings[$i]['target']]) ? $target_options[$mappings[$i]['target']] : $mappings[$i]['target'];
$param = array( $param = array(
'processor' => $importer->processor, 'processor' => $importer->processor,
'mapping' => $mapping, 'mapping' => $mapping,
...@@ -574,7 +571,7 @@ function feeds_ui_mapping_form(&$form_state, $importer) { ...@@ -574,7 +571,7 @@ function feeds_ui_mapping_form(&$form_state, $importer) {
} }
$form['target'] = array( $form['target'] = array(
'#type' => 'select', '#type' => 'select',
'#options' => array('' => t('Select a target')) + _feeds_ui_format_options($targets), '#options' => array('' => t('Select a target')) + $target_options,
); );
$form['add'] = array( $form['add'] = array(
'#type' => 'submit', '#type' => 'submit',
...@@ -595,7 +592,13 @@ function feeds_ui_mapping_form(&$form_state, $importer) { ...@@ -595,7 +592,13 @@ function feeds_ui_mapping_form(&$form_state, $importer) {
function feeds_ui_mapping_form_add_submit($form, &$form_state) { function feeds_ui_mapping_form_add_submit($form, &$form_state) {
$importer = $form['#importer']; $importer = $form['#importer'];
try { try {
$importer->processor->addMapping($form_state['values']['source'], $form_state['values']['target']); $mappings = $form['#mappings'];
$mappings[] = array(
'source' => $form_state['values']['source'],
'target' => $form_state['values']['target'],
'unique' => FALSE,
);
$importer->processor->addConfig(array('mappings' => $mappings));
$importer->processor->save(); $importer->processor->save();
drupal_set_message(t('Mapping has been added.')); drupal_set_message(t('Mapping has been added.'));
} }
...@@ -609,22 +612,25 @@ function feeds_ui_mapping_form_add_submit($form, &$form_state) { ...@@ -609,22 +612,25 @@ function feeds_ui_mapping_form_add_submit($form, &$form_state) {
*/ */
function feeds_ui_mapping_form_submit($form, &$form_state) { function feeds_ui_mapping_form_submit($form, &$form_state) {
$processor = $form['#importer']->processor; $processor = $form['#importer']->processor;
$mappings = $processor->config['mappings'];
// We may set some unique flags to mappings that we remove in the subsequent // We may set some unique flags to mappings that we remove in the subsequent
// step, that's fine. // step, that's fine.
$mappings = $form['#mappings'];
if (isset($form_state['values']['unique_flags'])) { if (isset($form_state['values']['unique_flags'])) {
foreach ($form_state['values']['unique_flags'] as $k => $v) { foreach ($form_state['values']['unique_flags'] as $k => $v) {
$processor->setUnique($mappings[$k]['source'], $mappings[$k]['target'], $v); $mappings[$k]['unique'] = $v;
} }
} }
foreach ($form_state['values']['remove_flags'] as $k => $v) { foreach ($form_state['values']['remove_flags'] as $k => $v) {
if ($v) { if ($v) {
$processor->removeMapping($mappings[$k]['source'], $mappings[$k]['target']); unset($mappings[$k]);
// Keep our keys clean.
$mappings = array_values($mappings);
} }
} }
drupal_set_message(t('Your changes have been saved.')); $processor->addConfig(array('mappings' => $mappings));
$processor->save(); $processor->save();
drupal_set_message(t('Your changes have been saved.'));
} }
/** /**
...@@ -777,9 +783,11 @@ function theme_feeds_ui_mapping_form($form) { ...@@ -777,9 +783,11 @@ function theme_feeds_ui_mapping_form($form) {
$rows = array(); $rows = array();
if (is_array($form['#mappings'])) { if (is_array($form['#mappings'])) {
foreach ($form['#mappings'] as $i => $mapping) { foreach ($form['#mappings'] as $i => $mapping) {
// Some parsers do not define source options.
$source = isset($form['source']['#options'][$mapping['source']]) ? $form['source']['#options'][$mapping['source']] : $mapping['source'];
$rows[] = array( $rows[] = array(
$mapping['source'], $source,
$mapping['target'], $form['target']['#options'][$mapping['target']],
drupal_render($form['unique_flags'][$i]), drupal_render($form['unique_flags'][$i]),
drupal_render($form['remove_flags'][$i]), drupal_render($form['remove_flags'][$i]),
); );
......
...@@ -108,41 +108,6 @@ class FeedsDataProcessor extends FeedsProcessor { ...@@ -108,41 +108,6 @@ class FeedsDataProcessor extends FeedsProcessor {
return $this->config['expire']; return $this->config['expire'];
} }
/**
* Override parent::addMapping() and create a new field for new mapping
* targets.
*
* @see getMappingTargets().
*/
public function addMapping($source, $target, $unique = FALSE) {
if (empty($source) || empty($target)) {
return;
}
// Create a new field with targets that start with "new:"
@list($new, $type) = explode(':', $target);
if ($new == 'new') {
// Build a field name from the source key.
$field_name = data_safe_name($source);
// Get the full schema spec from data.
$type = data_get_field_definition($type);
// Add the field to the table.
$schema = $this->table()->get('table_schema');
if (!isset($schema['fields'][$field_name])) {
$target = $this->table()->addField($field_name, $type);
// Let the user know.
drupal_set_message(t('Created new field "!name".', array('!name' => $field_name)));
}
else {
throw new Exception(t('Field !field_name already exists as a mapping target. Remove it from mapping if you would like to map another source to it. Remove it from !data_table table if you would like to change its definition.', array('!field_name' => $field_name, '!data_table' => l($this->table()->get('name'), 'admin/content/data'))));
}
}
// Let parent populate the mapping configuration.
parent::addMapping($source, $target, $unique);
}
/** /**
* Return available mapping targets. * Return available mapping targets.
*/ */
...@@ -367,4 +332,49 @@ class FeedsDataProcessor extends FeedsProcessor { ...@@ -367,4 +332,49 @@ class FeedsDataProcessor extends FeedsProcessor {
), ),
); );
} }
/**
* Override parent::addConfig().
*/
public function addConfig($config) {
$this->processMappings($config);
return parent::addConfig($config);
}
/**
* Override parent::setConfig().
*/
public function setConfig($config) {
$this->processMappings($config);
return parent::setConfig($config);
}
/**
* Create a new field for each new: mapping.
*/
protected function processMappings(&$config) {
if (!empty($config['mappings'])) {
foreach ($config['mappings'] as &$mapping) {
@list($new, $type) = explode(':', $mapping['target']);
// Create a new field with targets that start with "new:"
if ($new == 'new') {
// Build a field name from the source key.
$field_name = data_safe_name($mapping['source']);
// Get the full schema spec from data.
$type = data_get_field_definition($type);
// Add the field to the table.
$schema = $this->table()->get('table_schema');
if (!isset($schema['fields'][$field_name])) {
$mapping['target'] = $this->table()->addField($field_name, $type);
// Let the user know.
drupal_set_message(t('Created new field "!name".', array('!name' => $field_name)));
}
else {
throw new Exception(t('Field !field_name already exists as a mapping target. Remove it from mapping if you would like to map another source to it. Remove it from !data_table table if you would like to change its definition.', array('!field_name' => $field_name, '!data_table' => l($this->table()->get('name'), 'admin/content/data'))));
}
}
}
}
}
} }
...@@ -168,54 +168,6 @@ abstract class FeedsProcessor extends FeedsPlugin { ...@@ -168,54 +168,6 @@ abstract class FeedsProcessor extends FeedsPlugin {
return array('mappings' => array()); return array('mappings' => array());
} }
/**
* Add a mapping to existing mappings.
*
* @param $source
* A string that identifies a source element.
* @param $target
* A string that identifies a target element.
* @param $unique
* A boolean that defines whether the target value should be unique. If
* TRUE only one item with a given target value can exist on the local
* system. Compare with existingItemId() and uniqueTargets().
*/
public function addMapping($source, $target, $unique = FALSE) {
if (!empty($source) && !empty($target)) {
$this->config['mappings'][] = array(
'source' => $source,
'target' => $target,
'unique' => $unique,
);
}
}
/**
* Set unique state of a mapping target.
*/
public function setUnique($source, $target, $unique) {
if (!empty($source) && !empty($target)) {
foreach ($this->config['mappings'] as $k => $mapping) {
if ($mapping['source'] == $source && $mapping['target'] == $target) {
$this->config['mappings'][$k]['unique'] = $unique;
}
}
}
}
/**
* Remove a mapping.
*/
public function removeMapping($source, $target) {
foreach ($this->config['mappings'] as $k => $mapping) {
if ($mapping['source'] == $source && $mapping['target'] == $target) {
unset($this->config['mappings'][$k]);
}
}
// Keep or keys clean.
$this->config['mappings'] = array_values($this->config['mappings']);
}
/** /**
* Get mappings. * Get 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