You are here

function feeds_ui_mapping_form_submit in Feeds 7.2

Same name and namespace in other branches
  1. 8.2 feeds_ui/feeds_ui.admin.inc \feeds_ui_mapping_form_submit()
  2. 6 feeds_ui/feeds_ui.admin.inc \feeds_ui_mapping_form_submit()
  3. 7 feeds_ui/feeds_ui.admin.inc \feeds_ui_mapping_form_submit()

Submission handler for feeds_ui_mapping_form().

File

feeds_ui/feeds_ui.admin.inc, line 784
Contains all page callbacks, forms and theming functions for Feeds administrative pages.

Code

function feeds_ui_mapping_form_submit($form, &$form_state) {
  $importer = feeds_importer($form['#importer']);
  $processor = $importer->processor;
  $form_state += array(
    'mapping_settings' => array(),
    'mapping_settings_edit' => NULL,
  );

  // If an item is in edit mode, prepare it for saving.
  if ($form_state['mapping_settings_edit'] !== NULL) {
    $values = $form_state['values']['config'][$form_state['mapping_settings_edit']]['settings'];
    $form_state['mapping_settings'][$form_state['mapping_settings_edit']] = $values;
  }

  // We may set some settings to mappings that we remove in the subsequent step,
  // that's fine.
  $mappings = $form['#mappings'];
  foreach ($form_state['mapping_settings'] as $k => $v) {
    $mappings[$k] = array(
      'source' => $mappings[$k]['source'],
      'target' => $mappings[$k]['target'],
    ) + $v;
  }
  if (!empty($form_state['values']['remove_flags'])) {
    $remove_flags = array_keys(array_filter($form_state['values']['remove_flags']));
    foreach ($remove_flags as $k) {
      unset($mappings[$k]);
      unset($form_state['values']['mapping_weight'][$k]);
      drupal_set_message(t('Mapping has been removed.'), 'status', FALSE);
    }
  }

  // Keep our keys clean.
  $mappings = array_values($mappings);
  if (!empty($mappings)) {
    array_multisort($form_state['values']['mapping_weight'], $mappings);
  }
  $processor
    ->addConfig(array(
    'mappings' => $mappings,
  ));
  if (strlen($form_state['values']['source']) && strlen($form_state['values']['target'])) {
    try {
      $mappings = $processor
        ->getMappings();
      $mappings[] = array(
        'source' => $form_state['values']['source'],
        'target' => $form_state['values']['target'],
        'unique' => FALSE,
      );
      $processor
        ->addConfig(array(
        'mappings' => $mappings,
      ));
      drupal_set_message(t('Mapping has been added.'));
    } catch (Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
  }
  $importer
    ->save();
  drupal_set_message(t('Your changes have been saved.'));
}