function tripal_pub_importer_setup_add_criteria_fields

2.x tripal_pub.pub_importers.inc tripal_pub_importer_setup_add_criteria_fields(&$form, &$form_state, $num_criteria, $criteria)
3.x tripal_chado.pub_importers.inc tripal_pub_importer_setup_add_criteria_fields(&$form, &$form_state, $num_criteria, $criteria)

A helper function for the importer setup form that adds the criteria to the form that belong to the importer.

Parameters

$form: The form

$form_state: The form state

$num_criteria: The number of criteria that exist for the importer

$criteria: An array containing the criteria

Return value

A form array

Related topics

1 call to tripal_pub_importer_setup_add_criteria_fields()
tripal_pub_importer_setup_form in tripal_pub/includes/tripal_pub.pub_importers.inc
The form used for creating publication importers.

File

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

Code

function tripal_pub_importer_setup_add_criteria_fields(&$form, &$form_state, $num_criteria, $criteria) {

  // choices array
  $scope_choices = array(
    'any' => 'Any Field',
    'abstract' => 'Abstract',
    'author' => 'Author',
    'id' => 'Accession',
    'title' => 'Title',
    'journal' => 'Journal Name'
  );

  $first_op_choices = array(
    '' => '',
    'NOT' => 'NOT'
  );
  $op_choices = array(
    'AND' => 'AND',
    'OR' => 'OR',
    'NOT' => 'NOT'
  );

  for ($i = 1; $i <= $num_criteria; $i++) {
    $is_phrase = 1;

    $search_terms = '';
    $scope = '';
    $is_phrase = '';
    $operation = '';

    // if we have criteria supplied from the database then use that as the initial defaults
    if ($criteria) {
      $search_terms = $criteria['criteria'][$i]['search_terms'];
      $scope = $criteria['criteria'][$i]['scope'];
      $is_phrase = $criteria['criteria'][$i]['is_phrase'];
      $operation = $criteria['criteria'][$i]['operation'];
    }

    // if the criteria comes the session
    if (array_key_exists('tripal_pub_import', $_SESSION)) {
      $search_terms = isset($_SESSION['tripal_pub_import']['criteria'][$i]['search_terms']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['search_terms'] : $search_terms;
      $scope = isset($_SESSION['tripal_pub_import']['criteria'][$i]['scope']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['scope'] : $scope;
      $is_phrase = isset($_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['is_phrase'] : $is_phrase;
      $operation = isset($_SESSION['tripal_pub_import']['criteria'][$i]['operation']) ? $_SESSION['tripal_pub_import']['criteria'][$i]['operation'] : $operation;
    }

    // If the form_state has variables then use those.  This happens when an error occurs on the form or the
    // form is resbumitted using AJAX
    if (array_key_exists('values', $form_state)) {
      $search_terms = $form_state['values']["search_terms-$i"];
      $scope = $form_state['values']["scope-$i"];
      $is_phrase = $form_state['values']["is_phrase-$i"];
      $operation = $form_state['values']["operation-$i"];
    }
    $form['themed_element']['criteria'][$i]["scope-$i"] = array(
      '#type' => 'select',
      '#description' => t('Please select the fields to search for this term.'),
      '#options' => $scope_choices,
      '#default_value' => $scope,
    );
    $form['themed_element']['criteria'][$i]["search_terms-$i"] = array(
      '#type' => 'textfield',
      '#description' => t('<span style="white-space: normal">Please provide a list of words for searching. You may use
        conjunctions such as "AND" or "OR" to separate words if they are expected in
        the same scope, but do not mix ANDs and ORs.  Check the "Is Phrase" checkbox to use conjunctions as part of the text to search</span>'),
      '#default_value' => $search_terms,
      '#required' => TRUE,
      '#maxlength' => 2048,
    );
    $form['themed_element']['criteria'][$i]["is_phrase-$i"] = array(
      '#type' => 'checkbox',
      '#title' => t('Is Phrase?'),
      '#default_value' => $is_phrase,
    );

    if ($i == 1) {
      /*
       $form['criteria'][$i]["operation-$i"] = array(
         '#type'          => 'select',
         '#options'       => $first_op_choices,
         '#default_value' => $operation,
       );*/
    }
    if ($i > 1) {
      $form['themed_element']['criteria'][$i]["operation-$i"] = array(
        '#type' => 'select',
        '#options' => $op_choices,
        '#default_value' => $operation,
      );
    }
    if ($i == $num_criteria) {
      if ($i > 1) {
        $form['themed_element']['criteria'][$i]["remove-$i"] = array(
          '#type' => 'button',
          '#name' => 'remove',
          '#value' => t('Remove'),
          '#ajax' => array(
            'callback' => "tripal_pubs_setup_form_ajax_update",
            'wrapper' => 'tripal-pubs-importer-setup',
            'effect' => 'fade',
            'method' => 'replace',
            'prevent' => 'click'
          ),
          // When this button is clicked, the form will be validated and submitted.
          // Therefore, we set custom submit and validate functions to override the
          // default form submit.  In the validate function we set the form_state
          // to rebuild the form so the submit function never actually gets called,
          // but we need it or Drupal will run the default validate anyway.
          // we also set #limit_validation_errors to empty so fields that
          // are required that don't have values won't generate warnings.
          '#submit' => array('tripal_pub_setup_form_ajax_button_submit'),
          '#validate' => array('tripal_pub_setup_form_ajax_button_validate'),
          '#limit_validation_errors' => array(),
        );
      }
      $form['themed_element']['criteria'][$i]["add-$i"] = array(
        '#type' => 'button',
        '#name' => 'add',
        '#value' => t('Add'),
        '#ajax' => array(
          'callback' => "tripal_pubs_setup_form_ajax_update",
          'wrapper' => 'tripal-pubs-importer-setup',
          'effect' => 'fade',
          'method' => 'replace',
          'prevent' => 'click'
        ),
        // When this button is clicked, the form will be validated and submitted.
        // Therefore, we set custom submit and validate functions to override the
        // default form submit.  In the validate function we set the form_state
        // to rebuild the form so the submit function never actually gets called,
        // but we need it or Drupal will run the default validate anyway.
        // we also set #limit_validation_errors to empty so fields that
        // are required that don't have values won't generate warnings.
        '#submit' => array('tripal_pub_setup_form_ajax_button_submit'),
        '#validate' => array('tripal_pub_setup_form_ajax_button_validate'),
        '#limit_validation_errors' => array(),
      );
    }
  }
}