You are here

public function FeedsOAIHTTPFetcher::sourceForm in Feeds OAI-PMH Fetcher and Parser 7

Same name and namespace in other branches
  1. 6 FeedsOAIHTTPFetcher.inc \FeedsOAIHTTPFetcher::sourceForm()

Expose source form.

File

./FeedsOAIHTTPFetcher.inc, line 198

Class

FeedsOAIHTTPFetcher
Fetcher class for OAI-PMH repository webservices.

Code

public function sourceForm($source_config) {
  $form = parent::sourceForm($source_config);
  $error = FALSE;

  // If earliest_timestamp is not set, and source is, then get info from
  // repository to populate settings.
  if (isset($source_config['source']) && !empty($source_config['source'])) {
    require_once drupal_get_path('module', 'feeds_oai_pmh') . '/feeds_oai_pmh.inc';
    $result = feeds_oai_pmh_identify($source_config['source']);

    #dpm($result);
    if ($result['status'] == 0) {
      $source_config = array_merge($source_config, $result['repository']);
    }
    else {
      drupal_set_message(t('There was a problem fetching repository information: !list', array(
        '!list' => $result['output'],
      )));
      $error = TRUE;
    }
  }
  if (isset($result) && $error == FALSE) {

    // Build options array for sets available in repository.
    $sets_options = feeds_oai_pmh_sets_options($result['repository']['sets']);
  }

  // Override the default "source" element provided by Feeds.
  // Clearer label and description.
  $form['source']['#title'] = t('URL of OAI-PMH endpoint');
  $form['source']['#description'] = t('You can use services like http://www.opendoar.org/ to get a list of repository OAI-PMH endpoints.');

  // Add AJAX event handler.
  $form['source']['#ajax'] = array(
    'callback' => 'feeds_oai_pmh_ajax_callback',
    'wrapper' => 'ahah-element',
    // ID of div element to update.
    'method' => 'replace',
    'effect' => 'fade',
    'event' => 'change',
  );

  // A set wrapper to handle replacement by AJAX callback
  $form['source']['#prefix'] = '<div class="clear-block" id="ahah-element">';
  if ($form['source']['#default_value']) {
    require_once drupal_get_path('module', 'feeds_oai_pmh') . '/feeds_oai_pmh.inc';
    $result = feeds_oai_pmh_identify($form['source']['#default_value']);
    if ($result['status'] == 0) {
      $source_config = array_merge($source_config, $result['repository']);
    }
    elseif (isset($result['repository'])) {
      $sets_options = feeds_oai_pmh_sets_options($result['repository']['sets']);
    }
    else {
      $sets_options = feeds_oai_pmh_sets_options(array());
    }
  }
  $form['set'] = array(
    '#type' => 'select',
    '#title' => t('Set to fetch'),
    '#default_value' => isset($source_config['set']) ? $source_config['set'] : NULL,
    '#options' => isset($sets_options) ? $sets_options : array(),
    '#suffix' => '',
    '#ajax' => array(
      'callback' => 'feeds_oai_pmh_ajax_callback',
      'wrapper' => 'ahah-element',
      // ID of div element to update.
      'method' => 'replace',
      'effect' => 'fade',
      'event' => 'change',
    ),
  );
  if (isset($source_config['source']) && isset($source_config['set'])) {
    $msg = feeds_oai_pmh_current_status_msg($source_config['source'], $source_config['set']);
    if ($msg) {
      $form['status'] = array(
        '#value' => '<div class="messages status">' . $msg . '</div>',
      );
    }
  }
  $form['use_dates'] = array(
    '#type' => 'checkbox',
    '#title' => 'Limit fetch by record creation date',
    '#default_value' => isset($source_config['use_dates']) ? $source_config['use_dates'] : NULL,
  );
  $form['dates'] = array(
    '#type' => 'fieldset',
    '#title' => t('Record creation dates to fetch'),
    // Form element IDs are edit-feeds-[feeds-object]-[form-element-id]
    '#states' => array(
      'visible' => array(
        '#edit-feeds-feedsoaihttpfetcher-use-dates' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  if (isset($source_config['earliest_timestamp']) && $source_config['earliest_timestamp'] > 0) {
    $date = format_date($source_config['earliest_timestamp'], 'custom', 'M d, Y');
    $form['dates']['#description'] = t('Note: earliest record reported by repository is @date', array(
      '@date' => $date,
    ));
  }
  $form['dates']['from'] = array(
    '#type' => 'date',
    '#title' => t('Starting date'),
    '#default_value' => isset($source_config['dates']['from']) ? $source_config['dates']['from'] : NULL,
  );
  $form['dates']['to'] = array(
    '#type' => 'date',
    '#title' => t('Ending date'),
    '#default_value' => isset($source_config['dates']['to']) ? $source_config['dates']['to'] : NULL,
  );
  $form['restart'] = array(
    '#type' => 'checkbox',
    '#title' => t('Reset import for this repository/set to above settings'),
    '#description' => t('This forces any imports that are currently underway
          for the chosen repository/set to start over from the beginning.
          Normally, all imports that have already begun will only try to fetch
          new items until this option is checked, or if the "Delete items"
          option is used.'),
    '#suffix' => '</div>',
  );
  return $form;
}