Skip to content
Snippets Groups Projects
feeds_ui.admin.inc 39.6 KiB
Newer Older
    $output .= '</h4>';
  }

  if (!empty($container['body'])) {
    $output .= '<div class="feeds-container-body">';
    if (is_array($container['body'])) {
      if (isset($container['body']['#type'])) {
        $output .= drupal_render($container['body']);
      }
      else {
        foreach ($container['body'] as $c) {
          $output .= theme('feeds_ui_container', array('container' => $c));
        }
      }
    }
    else {
      $output .= $container['body'];
    }
    $output .= '</div>';
  }

  $output .= '</div>';
  return $output;
}

/**
 * Theme function for feeds_ui_mapping_form().
 */
function theme_feeds_ui_mapping_form($variables) {
  $form = $variables['form'];
  $targets = feeds_importer($form['#importer'])->processor->getMappingTargets();
  $targets = _feeds_ui_format_options($targets, TRUE);

  $sources = feeds_importer($form['#importer'])->parser->getMappingSources();
  // Some parsers do not define source options.
  $sources = $sources ? _feeds_ui_format_options($sources, TRUE) : array();

  // Build the actual mapping table.
Alex Barth's avatar
Alex Barth committed
  $header = array(
    t('Source'),
    t('Target'),
Alex Barth's avatar
Alex Barth committed
    '&nbsp;',
Alex Barth's avatar
Alex Barth committed
  );
  $rows = array();
  if (is_array($form['#mappings'])) {
    foreach ($form['#mappings'] as $i => $mapping) {
      $source = isset($sources[$mapping['source']]) ? check_plain($sources[$mapping['source']]) : check_plain($mapping['source']);
      $target = isset($targets[$mapping['target']]) ? check_plain($targets[$mapping['target']]) : '<em>' . t('Missing') . '</em>';
      // Add indicator to target if target configuration changed.
      if (isset($form['#mapping_settings'][$i])) {
        $target .= '<span class="warning">*</span>';
      }
Alex Barth's avatar
Alex Barth committed
      $rows[] = array(
          $target,
          drupal_render($form['config'][$i]),
          drupal_render($form['remove_flags'][$i]),
          drupal_render($form['mapping_weight'][$i]),
        ),
        'class' => array('draggable', 'tabledrag-leaf'),
Alex Barth's avatar
Alex Barth committed
      );
    }
Alex Barth's avatar
Alex Barth committed
    $rows[] = array(
Alex Barth's avatar
Alex Barth committed
    );
  }
  $rows[] = array(
    drupal_render($form['source']),
    drupal_render($form['target']),
    '',
    drupal_render($form['add']),
  $output = '';
  if (!empty($form['changed'])) {
    // This form element is only available if target configuration changed.
    $output .= drupal_render($form['changed']);
  }
  $output .= '<div class="help feeds-admin-ui">' . drupal_render($form['help']) . '</div>';
  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'feeds-ui-mapping-overview')));

  // Build the help table that explains available sources.
  $rows = array();
  foreach (element_children($form['legendset']['legend']['sources']) as $k) {
    $rows[] = array(
      check_plain(drupal_render($form['legendset']['legend']['sources'][$k]['name'])),
      check_plain(drupal_render($form['legendset']['legend']['sources'][$k]['description'])),
  if (count($rows)) {
    $legend .= '<h4>' . t('Sources') . '</h4>';
    $legend .= theme('table', array('header' => array(t('Name'), t('Description')), 'rows' => $rows));

  // Build the help table that explains available targets.
  $rows = array();
  foreach (element_children($form['legendset']['legend']['targets']) as $k) {
    $rows[] = array(
      check_plain(drupal_render($form['legendset']['legend']['targets'][$k]['name']) . ' (' . $k . ')'),
      check_plain(drupal_render($form['legendset']['legend']['targets'][$k]['description'])),
  $legend .= '<h4>' . t('Targets') . '</h4>';
  $legend .= theme('table', array('header' => array(t('Name'), t('Description')), 'rows' => $rows));

  // Stick tables into collapsible fieldset.
  $form['legendset']['legend'] = array(
    '#markup' => '<div>' . $legend . '</div>',
  );

  $output .= drupal_render($form['legendset']);
  $output .= drupal_render_children($form);

  drupal_add_tabledrag('feeds-ui-mapping-overview', 'order', 'sibling', 'feeds-ui-mapping-weight');
Alex Barth's avatar
Alex Barth committed
  return $output;
 * Page callback to import a Feeds importer.
 */
function feeds_ui_importer_import($form, &$form_state) {
  $form['id'] = array(
    '#type' => 'textfield',
    '#title' => t('Importer id'),
    '#description' => t('Enter the id to use for this importer if it is different from the source importer. Leave blank to use the id of the importer.'),
  );
  $form['id_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Replace an existing importer if one exists with the same id.'),
  );
  $form['bypass_validation'] = array(
    '#type' => 'checkbox',
    '#title' => t('Bypass importer validation'),
    '#description' => t('Bypass the validation of plugins when importing.'),
  );
  $form['importer'] = array(
    '#type' => 'textarea',
    '#rows' => 10,
  );
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
  );
  return $form;
}

/**
 * Form validation handler for feeds_ui_importer_import().
 *
 * @see feeds_ui_importer_import_submit()
 */
function feeds_ui_importer_import_validate($form, &$form_state) {
  $form_state['values']['importer'] = trim($form_state['values']['importer']);
  $form_state['values']['id'] = trim($form_state['values']['id']);

  if (!empty($form_state['values']['id']) && preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['id'])) {
    form_error($form['id'], t('Feeds importer id must be alphanumeric with underscores only.'));
  }

  if (substr($form_state['values']['importer'], 0, 5) == '<?php') {
    $form_state['values']['importer'] = substr($form_state['values']['importer'], 5);
  }

  $feeds_importer = NULL;
  ob_start();
  eval($form_state['values']['importer']);
  ob_end_clean();

  if (!is_object($feeds_importer)) {
    return form_error($form['importer'], t('Unable to interpret Feeds importer code.'));
  }

  if (empty($feeds_importer->api_version) || $feeds_importer->api_version < 1) {
    form_error($form['importer'], t('The importer is not compatible with this version of Feeds.'));
  }
  elseif (version_compare($feeds_importer->api_version, feeds_api_version(), '>')) {
    form_error($form['importer'], t('That importer is created for the version %import_version of Feeds, but you only have version %api_version.', array(
      '%import_version' => $feeds_importer->api_version,
      '%api_version' => feeds_api_version())));
  }

  // Change to user-supplied id.
  if ($form_state['values']['id']) {
    $feeds_importer->id = $form_state['values']['id'];
  }

  $exists = feeds_ui_importer_machine_name_exists($feeds_importer->id);

  if ($exists && !$form_state['values']['id_override']) {
    return form_error($form['id'], t('Feeds importer already exists with that id.'));
  }

  if (!$form_state['values']['bypass_validation']) {
    foreach (array('fetcher', 'parser', 'processor') as $type) {
      $plugin = feeds_plugin($feeds_importer->config[$type]['plugin_key'], $feeds_importer->id);
      if (get_class($plugin) == 'FeedsMissingPlugin') {
        form_error($form['importer'], t('The plugin %plugin is unavailable.', array('%plugin' => $feeds_importer->config[$type]['plugin_key'])));
      }
    }
  }

  $form_state['importer'] = $feeds_importer;
}

/**
 * Form submission handler for feeds_ui_importer_import().
 *
 * @see feeds_ui_importer_import_validate()
 */
function feeds_ui_importer_import_submit($form, &$form_state) {
  $importer = $form_state['importer'];

  // Create a copy of the importer to preserve config.
  $save = feeds_importer($importer->id);
  $save->setConfig($importer->config);
  foreach (array('fetcher', 'parser', 'processor') as $type) {
    $save->setPlugin($importer->config[$type]['plugin_key']);
    $save->$type->setConfig($importer->config[$type]['config']);

  drupal_set_message(t('Successfully imported the %id feeds importer.', array('%id' => $importer->id)));
  $form_state['redirect'] = 'admin/structure/feeds';
}