function tripal_pub_importer_setup_form

2.x tripal_pub.pub_importers.inc tripal_pub_importer_setup_form($form, &$form_state = NULL, $pub_import_id = NULL, $action = 'new')
3.x tripal_chado.pub_importers.inc tripal_pub_importer_setup_form($form, &$form_state = NULL, $pub_import_id = NULL, $action = 'new')
1.x pub_importers.inc tripal_pub_importer_setup_form(&$form_state = NULL, $pub_import_id = NULL, $action = 'new')

The form used for creating publication importers.

Parameters

$form: The Drupal form

$form_state: The form state

$pub_import_id: The publication importer ID

$action: The action to perform

Return value

A form array

Related topics

1 string reference to 'tripal_pub_importer_setup_form'
tripal_pub_importer_setup_page in tripal_pub/includes/tripal_pub.pub_importers.inc
Creates the page that contains the publication importer setup form and test results.

File

tripal_pub/includes/tripal_pub.pub_importers.inc, line 239
Management of importers

Code

function tripal_pub_importer_setup_form($form, &$form_state = NULL, $pub_import_id = NULL, $action = 'new') {

  // Default values can come in the following ways:
  //
  // 1) as elements of the $pub_importer object.  This occurs when editing an existing importer
  // 2) in the $form_state['values'] array which occurs on a failed validation or
  //    ajax callbacks from non submit form elements
  // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  //    form elements and the form is being rebuilt
  //
  // set form field defaults

  // Set the default values. If the pub_import_id isn't already defined by the form values
  // and one is provided then look it up in the database
  $criteria = NULL;
  $remote_db = '';
  $days = '';
  $disabled = '';
  $do_contact = '';
  $num_criteria = 1;
  $loader_name = '';

  // if this is an edit the we are pulling an import object from the database
  if ($action == "edit") {
    $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :pub_import_id";
    $importer = db_query($sql, array(':pub_import_id' => $pub_import_id))->fetchObject();

    $criteria = unserialize($importer->criteria);
    $remote_db = $criteria['remote_db'];
    $days = $criteria['days'];
    $disabled = $criteria['disabled'];
    $do_contact = $criteria['do_contact'];
    $num_criteria = $criteria['num_criteria'];
    $loader_name = $criteria['loader_name'];
  }

  // if there are any session variables then use those
  if (array_key_exists('tripal_pub_import', $_SESSION)) {
    $remote_db = $_SESSION['tripal_pub_import']['remote_db'];
    $days = $_SESSION['tripal_pub_import']['days'];
    $disabled = $_SESSION['tripal_pub_import']['disabled'];
    $do_contact = $_SESSION['tripal_pub_import']['do_contact'];
    $num_criteria = $_SESSION['tripal_pub_import']['num_criteria'];
    $loader_name = $_SESSION['tripal_pub_import']['loader_name'];

    // check if the pub_import_id in the session variable is not the same as the one we've been provided
    // if so, then clear the session variable
    if ($pub_import_id and $pub_import_id != $_SESSION['tripal_pub_import']['pub_import_id']) {
      unset($_SESSION['tripal_pub_import']);
    }
  }

  // if we are re constructing the form from a failed validation or ajax callback
  // then use the $form_state['values'] values
  if (array_key_exists('values', $form_state)) {
    $remote_db = $form_state['values']['remote_db'];
    $days = $form_state['values']['days'];
    $disabled = $form_state['values']['disabled'];
    $do_contact = $form_state['values']['do_contact'];
    $num_criteria = $form_state['values']['num_criteria'];
    $loader_name = $form_state['values']['loader_name'];
  }
  // if we are re building the form from after submission (from ajax call) then
  // the values are in the $form_state['input'] array
  if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
    $remote_db = $form_state['input']['remote_db'];
    $days = $form_state['input']['days'];
    $disabled = $form_state['input']['disabled'];
    $do_contact = $form_state['input']['do_contact'];
    $loader_name = $form_state['input']['loader_name'];

    // because the num_criteria is a value and not a visible or hidden form
    // element it is not part of the ['input'] array, so we need to get it from the form
    $num_criteria = $form_state['complete form']['num_criteria']['#value'];
  }

  if (array_key_exists('triggering_element', $form_state) and 
    $form_state['triggering_element']['#name'] == 'add') {
    $num_criteria++;
  }
  if (array_key_exists('triggering_element', $form_state) and 
    $form_state['triggering_element']['#name'] == 'remove') {
    $num_criteria--;
  }



  // set the values we need for later but that should not be shown on the form
  $form['num_criteria'] = array(
    '#type' => 'value',
    '#value' => $num_criteria,
  );
  $form['pub_import_id'] = array(
    '#type' => 'value',
    '#value' => $pub_import_id,
  );
  $form['action'] = array(
    '#type' => 'value',
    '#value' => $action,
  );

  // add in the elements that will be organized via a theme function
  $form['themed_element']['loader_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Loader Name'),
    '#description' => t('Please provide a name for this loader setup.'),
    '#default_value' => $loader_name,
    '#required' => TRUE,
  );

  $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  $remote_dbs = array();
  $values = array(
    'name' => $supported_dbs,
  );
  $dbs = chado_select_record('db', array('*'), $values);
  foreach ($dbs as $index => $db) {
    $remote_dbs[$db->name] = $db->description;
  }
  // use PubMed as the default
  if (!$remote_db) {
    $remote_db = 'PMID';
  }
  $form['themed_element']['remote_db'] = array(
    '#title' => t('Remote Database'),
    '#type' => 'select',
    '#options' => $remote_dbs,
    '#default_value' => $remote_db,
    '#ajax' => array(
      'callback' => "tripal_pubs_setup_form_ajax_update",
      'wrapper' => 'tripal-pubs-importer-setup',
      'effect' => 'fade',
      'method' => 'replace',
    ),
  );
  $form['themed_element']['days'] = array(
    '#type' => 'textfield',
    '#title' => t('Days since record modified'),
    '#description' => t('Limit the search to include pubs that have been added no more than this many days before today.'),
    '#default_value' => $days,
    '#size' => 5,
  );
  $form['themed_element']['disabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disabled'),
    '#description' => t('Check to disable this importer.'),
    '#default_value' => $disabled,
  );
  $form['themed_element']['do_contact'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create Contact'),
    '#description' => t('Check to create an entry in the contact table for each author of a matching publication during import. This allows storage of
       additional information such as affilation, etc. Otherwise, only authors names are retrieved.'),
    '#default_value' => $do_contact,
  );

  // add in the form for the criteria
  tripal_pub_importer_setup_add_criteria_fields($form, $form_state, $num_criteria, $criteria);

  // add in the buttons
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save Importer'),
  );
  $form['test'] = array(
    '#type' => 'submit',
    '#value' => t('Test Importer'),
  );
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete Importer'),
    '#attributes' => array('style' => 'float: right;')
  );

  // add in the section where the test results will appear
  $form['results'] = array(
    '#markup' => '<div id="tripal-pub-importer-test-section"></div>',
  );

  // allow the selected remote database to make changes to the form if needed
  $callback = "tripal_pub_remote_alter_form_$remote_db";
  $form = call_user_func($callback, $form, $form_state, $num_criteria);

  $form['themed_element']['#theme'] = 'tripal_pub_importer_setup_form_elements';

  return $form;
}