You are here

function feeds_tamper_ui_list_form in Feeds Tamper 7

Same name and namespace in other branches
  1. 6 feeds_tamper_ui/feeds_tamper_ui.admin.inc \feeds_tamper_ui_list_form()

List of plugins for current importer.

1 string reference to 'feeds_tamper_ui_list_form'
feeds_tamper_ui_menu in feeds_tamper_ui/feeds_tamper_ui.module
Implements hook_menu().

File

feeds_tamper_ui/feeds_tamper_ui.admin.inc, line 12
Forms and their accompanying validation and submit functions for Feeds Tamper UI.

Code

function feeds_tamper_ui_list_form($form, &$form_state, $importer) {
  $mappings = $importer->processor->config['mappings'];
  if (!$mappings) {
    $args = array(
      '@url' => url('admin/structure/feeds/' . $importer->id . '/mapping'),
    );
    drupal_set_message(t('There are no <a href="@url">mappings</a> defined for this importer.', $args), 'warning');
    return $form;
  }
  $sources = $importer->parser
    ->getMappingSources();
  $targets = $importer->processor
    ->getMappingTargets();
  $instances = feeds_tamper_load_by_importer($importer->id, TRUE);

  // Help message at the top. I have a seceret, I added the link back to the
  // mappings because it makes my life a lot easier while testing this.
  $args = array(
    '@url' => url('admin/structure/feeds/' . $importer->id . '/mapping'),
  );
  $message = t('Configure plugins to modify Feeds data before it gets saved. Each <a href="@url">mapping</a> can be manipulated individually.', $args);
  $form['help']['#markup'] = '<div class="help"><p>' . $message . '</p></div>';

  // Helpful shortcut.
  $form['mappings'] = array();
  $map =& $form['mappings'];
  $form['#importer'] = $importer;
  $mapped = array();
  foreach ($mappings as $delta => $mapping) {
    $source = $mapping['source'];
    $target = $mapping['target'];

    // We will skip building the table if it has been built for a source, but we
    // need to add the target label to indicate to the user that more than one
    // field will be affected.
    $target_label = !empty($targets[$target]['name']) ? check_plain($targets[$target]['name']) : check_plain($target);
    $prev = $delta;
    if (isset($mapped[$source])) {
      $prev = $mapped[$source];
    }
    $map[$prev]['#title']['targets'][] = $target_label;

    // We don't need to build the table again for this source.
    if (isset($mapped[$source])) {
      continue;
    }
    $mapped[$source] = $delta;
    $source_label = !empty($sources[$source]['name']) ? check_plain($sources[$source]['name']) : check_plain($source);
    $map[$delta]['#title']['source'] = $source_label;
    $map[$delta]['#tree'] = TRUE;
    $map[$delta]['table'] = array();
    $map[$delta]['#add_link'] = l(t('Add plugin'), 'admin/structure/feeds/' . $importer->id . '/tamper/add/' . bin2hex($source));

    // There are no plugins for this source. We've built all we need to.
    if (!isset($instances[$source])) {
      continue;
    }
    foreach ($instances[$source] as $instance) {

      // Plugin instance description.
      $description = !empty($instance->description) ? check_plain($instance->description) : $instance->id;
      $map[$delta]['table'][$instance->id]['#description'] = $description;

      // Weight field.
      $map[$delta]['table'][$instance->id]['weight'] = array(
        '#type' => 'textfield',
        '#size' => 5,
        '#default_value' => $instance->weight,
        '#attributes' => array(
          'class' => array(
            'weight',
          ),
        ),
      );

      // Name of plugin.
      $plugin = feeds_tamper_get_plugin($instance->plugin_id);
      $map[$delta]['table'][$instance->id]['#name'] = $plugin['name'];

      // Calculate where the plugin lives and it's corresponding operations.
      if ($instance->export_type == EXPORT_IN_CODE) {
        $status = t('Default');
        $edit = t('Override');
        $delete = '';
      }
      elseif ($instance->export_type == EXPORT_IN_DATABASE) {
        $status = t('Normal');
        $edit = t('Edit');
        $delete = t('Delete');
      }
      elseif ($instance->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) {
        $status = t('Overridden');
        $edit = t('Edit');
        $delete = t('Revert');
      }

      // Status column, Default, Normal, Overridden.
      $map[$delta]['table'][$instance->id]['#status'] = $status;

      // Build Edit | Delete, Edit | Revert, Override links.
      $ops = l(t($edit), 'admin/structure/feeds/' . $importer->id . '/tamper/' . $instance->id . '/edit');

      // Plugins provided in code can't be deleted but they can be reverted.
      if (!empty($delete)) {
        $ops .= ' | ';
        $ops .= l(t($delete), 'admin/structure/feeds/' . $importer->id . '/tamper/' . $instance->id . '/delete');
      }
      $map[$delta]['table'][$instance->id]['#edit'] = $ops;

      // Enable/disable checkbox.
      $map[$delta]['table'][$instance->id]['enabled'] = array(
        '#type' => 'checkbox',
        '#default_value' => !$instance->disabled,
      );
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}