You are here

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

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

Expose source form.

Overrides FeedsHTTPFetcher::sourceForm

File

./FeedsOAIHTTPFetcher.inc, line 196

Class

FeedsOAIHTTPFetcher
Fetcher class for OAI-PMH repository webservices.

Code

public function sourceForm($source_config) {
  ctools_include('dependent');
  $form = parent::sourceForm($source_config);

  // 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'],
      )));
    }
  }
  if (isset($result)) {

    // 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 AHAH event handler.
  $form['source']['#ahah'] = array(
    'path' => 'feeds_oai_pmh/set_ahah',
    'wrapper' => 'ahah-element',
    // ID of div element to update.
    'method' => 'replace',
    'effect' => 'fade',
    'event' => 'change',
  );
  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']);
    }
    else {
      $sets_options = feeds_oai_pmh_sets_options($result['repository']['sets']);
    }
  }
  $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(),
    '#ahah' => array(
      'path' => 'feeds_oai_pmh/set_ahah',
      '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'),
    // Add Ctools processing to make this fieldset dependant on above checkbox
    '#process' => array(
      'ctools_dependent_process',
    ),
    // Form element IDs are edit-feeds-[feeds-object]-[form-element-id]
    '#dependency' => array(
      'edit-feeds-FeedsOAIHTTPFetcher-use-dates' => array(
        1,
      ),
    ),
    // Surrounding fieldsets with a div is necessary for Ctools to work
    '#prefix' => '<div id="edit-feeds-FeedsOAIHTTPFetcher-dates-wrapper">',
    '#suffix' => '</div></div>',
    // This is also needed by Ctools to make fieldsets work
    '#input' => TRUE,
  );
  if (isset($source_config['earliest_timestamp'])) {
    $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.'),
  );
  return $form;
}