function tripal_bulk_loader_template_field_form

2.x tripal_bulk_loader.admin.templates.inc tripal_bulk_loader_template_field_form($form, &$form_state = NULL)
3.x tripal_bulk_loader.admin.templates.inc tripal_bulk_loader_template_field_form($form, &$form_state = NULL)

Add Field Form

This form is meant to be called from a bulk loader form. Blank Defaults are in place but you can use the following in the query of the path to set defaults for a given template:

  • template_id=\d+: the template to add the field to
  • record_id=\d+: the priority or key in the template array of the record to add the field to

Parameters

$form_state: Contains the values and storage for the form

Return value

A form array to be rendered by drupal_get_form

Related topics

1 string reference to 'tripal_bulk_loader_template_field_form'
tripal_bulk_loader_menu in tripal_bulk_loader/tripal_bulk_loader.module
Implements hook_menu().

File

tripal_bulk_loader/includes/tripal_bulk_loader.admin.templates.inc, line 1815
All functions in this file pertain to administrative management of bulk loader templates

Code

function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
  $form['#cache'] = TRUE; // Make sure the form is cached.

  // if there is no template supplied don't build the form
  if (!isset($form_state['build_info']['args'][1])) {
    return $form;
  }
  // if there is no template array saved then retrieve it
  elseif (!isset($form_state['storage']['template_array'])) {
    $form_state['storage']['template'] = db_select('tripal_bulk_loader_template', 't')
      ->fields('t')
      ->condition('t.template_id', $form_state['build_info']['args'][1], '=')
      ->execute()
      ->fetchObject();
    $form_state['storage']['template_array'] = unserialize($form_state['storage']['template']->template_array);
    $form_state['storage']['template_id'] = $form_state['build_info']['args'][1];

    if (isset($form_state['build_info']['args'][2])) {
      $form_state['storage']['record_id'] = $form_state['build_info']['args'][2];
    }
    if (isset($form_state['build_info']['args'][3])) {
      $form_state['storage']['field_index'] = $form_state['build_info']['args'][3];
    }
  }

  $v['mode'] = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : 'create';
  $form_state['storage']['mode'] = $v['mode'];
  $mode = $v['mode'];

  $values = tripal_bulk_loader_template_field_form_default_values($mode, $form_state);

  // set the breadcrumb
  $breadcrumb = array();
  $breadcrumb[] = l('Home', '<front>');
  $breadcrumb[] = l('Administration', 'admin');
  $breadcrumb[] = l('Tripal', 'admin/tripal');
  $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $values['template_id'] . '/edit');
  drupal_set_breadcrumb($breadcrumb);

  $form['template_name'] = array(
    '#type' => 'item',
    '#title' => 'Template',
    '#markup' => $values['template_name'],
  );

  $form['template_id'] = array(
    '#type' => 'hidden',
    '#value' => $values['template_id'],
  );

  $form['fields'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="tripal_bulk_loader_template-template_fields">',
    '#suffix' => '</div>',
  );

  $form['fields']['record'] = array(
    '#type' => 'fieldset',
    '#title' => 'Record'
  );
  if (!empty($values['record_name'])) {
    $form['fields']['record']['#title'] = 'Record: ' . $values['record_name'];
    $form['fields']['record']['#collapsible'] = TRUE;
    $form['fields']['record']['#collapsed'] = TRUE;
  }

  $form['fields']['record']['field_group'] = array(
    '#type' => 'select',
    '#title' => 'Record',
    '#description' => t(
    'This is used to group a set of fields together allowing ' .
      'multiple records to be inserted into the same table per line of the file'),
    '#options' => $values['field_group_options'],
    '#default_value' => $values['field_group'],
    '#ajax' => array(
      'callback' => 'tripal_bulk_loader_template_fields_ahah',
      'wrapper' => 'tripal_bulk_loader_template-template_fields',
      'effect' => 'fade'
    ),
    '#required' => TRUE,
  );

  $form['fields']['record']['record_name'] = array(
    '#type' => 'textfield',
    '#title' => 'Unique Record Name',
    '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
    '#suffix' => '</div>',
    '#description' => 'A human-readable name for the record; it should be unique.',
    '#disabled' => ($values['no_record_id']) ? FALSE : TRUE,
    '#default_value' => $values['record_name'],
  );

  $form['fields']['record']['record_mode'] = array(
    '#type' => 'radios',
    '#title' => 'Record Type/Action',
    '#options' => array(
      'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
      'select_once' => 'SELECT ONCE: Select the record only once for each constant set.',
      'insert' => 'INSERT: Insert the record',
      'insert_once' => 'INSERT ONCE: Record will be inserted once for each constant set.',
    ),
    '#description' => 'This indicates the action to take when loading a record. There are many additional options available when editing a record such as "Select if Duplicate" which is very important on insert if the record might already exist.',
    '#default_value' => 'insert',
    '#disabled' => ($values['no_record_id']) ? FALSE : TRUE,
  );

  $form['fields']['field'] = array(
    '#type' => 'fieldset',
    '#title' => 'Field',
  );

  $form['fields']['field']['field_type'] = array(
    '#type' => 'radios',
    '#title' => t('Type of Field'),
    '#options' => array(
      'table field' => t('Data: A Field which maps to a column in the supplied file.'),
      'constant' => t('Constant: Field which remains Constant throughout the file'),
      'foreign key' => t('Record Referral: Fields which map to a record in another table'),
    ),
    '#required' => TRUE,
    '#default_value' => $values['field_type'],
    '#ajax' => array(
      'callback' => 'tripal_bulk_loader_template_fields_ahah',
      'wrapper' => 'tripal_bulk_loader_template-template_fields',
      'effect' => 'fade'
    ),
  );

  $form['fields']['field']['field_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Human-readable Title for Field'),
    '#default_value' => $values['field_title'],
  );


  // Chado Field
  $form['fields']['field']['chado'] = array(
    '#type' => 'fieldset',
    '#title' => t('Chado Field/Column Details'),
    '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  );

  $form['fields']['field']['chado']['chado_table'] = array(
    '#type' => 'select',
    '#title' => t('Chado Table'),
    '#options' => $values['chado_table_options'],
    '#default_value' => $values['chado_table'],
    '#ajax' => array(
      'callback' => 'tripal_bulk_loader_template_fields_ahah',
      'wrapper' => 'tripal_bulk_loader_template-template_fields',
      'effect' => 'fade'
    ),
  );

  $form['fields']['field']['chado']['chado_field'] = array(
    '#type' => 'select',
    '#title' => t('Chado Field/Column'),
    '#options' => $values['chado_field_options'],
    '#default_value' => $values['chado_field'],
    '#ajax' => array(
      'callback' => 'tripal_bulk_loader_template_fields_ahah',
      'wrapper' => 'tripal_bulk_loader_template-template_fields',
      'effect' => 'fade'
    ),
  );

  // loading file data column
  $form['fields']['field']['columns'] = array(
    '#type' => 'fieldset',
    '#title' => t('Data File Column'),
    '#collapsible' => TRUE,
    '#collapsed' => ($values['field_type'] == 'table field') ? FALSE : TRUE,
  );

  $form['fields']['field']['columns']['column_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Column'),
    '#description' => t('Specify the column in the data that this field maps to where the first column is 1.'),
    '#size' => 5,
    '#default_value' => $values['column_number'],
  );

  $form['fields']['field']['columns']['column_exposed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow Column to be set for each Bulk Loading Job'),
    '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
    '#default_value' => $values['column_exposed'],
  );

  $form['fields']['field']['columns']['column_exposed_desc'] = array(
    '#type' => 'textfield',
    '#title' => t('Description for exposed field on bulk loading job'),
    '#description' => t('This description should tell the user what column should be entered here.'),
    '#default_value' => $values['column_exposed_desc'],
  );

  // Global Value
  $form['fields']['field']['constant'] = array(
    '#type' => 'fieldset',
    '#title' => t('Constant'),
    '#collapsible' => TRUE,
    '#collapsed' => ($values['field_type'] == 'constant') ? FALSE : TRUE,
  );

  $form['fields']['field']['constant']['constant_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Constant Value'),
    '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
    '#default_value' => $values['constant_value']
  );

  $form['fields']['field']['constant']['constant_exposed'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
    '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
    '#default_value' => $values['constant_exposed'],
  );

  $form['fields']['field']['constant']['constant_validate'] = array(
    '#type' => 'checkbox',
    '#title' => t('Ensure value is in table'),
    '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
    '#default_value' => $values['constant_validate'],
  );

  // Foreign Key / Referrer
  $form['fields']['field']['foreign_key'] = array(
    '#type' => 'fieldset',
    '#title' => 'Record Referral',
    '#collapsible' => TRUE,
    '#collapsed' => ($values['field_type'] == 'foreign key') ? FALSE : TRUE,
  );

  $form['fields']['field']['foreign_key']['show_all_records'] = array(
    '#type' => 'checkbox',
    '#title' => 'Refer to any record',
    '#description' => t('By default, the bulk loader will only allow referral to records in a foreign key relationship.  To allow referral to any previous record, check this box'),
    '#default_value' => $values['show_all_records'],
    '#ajax' => array(
      'callback' => 'tripal_bulk_loader_template_fields_ahah',
      'wrapper' => 'tripal_bulk_loader_template-template_fields',
      'effect' => 'fade'
    ),
  );

  $form['fields']['field']['foreign_key']['foreign_record'] = array(
    '#type' => 'select',
    '#title' => 'Record to refer to',
    '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
    '#options' => $values['foreign_record_options'],
    '#ajax' => array(
      'callback' => 'tripal_bulk_loader_template_fields_ahah',
      'wrapper' => 'tripal_bulk_loader_template-template_fields',
      'effect' => 'fade'
    ),
    '#default_value' => $values['foreign_record'],
  );

  $form['fields']['field']['foreign_key']['foreign_field'] = array(
    '#type' => 'select',
    '#title' => 'Field to refer to',
    '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
    '#options' => $values['foreign_field_options'],
    '#default_value' => $values['foreign_field'],
  );


  $collapsed = TRUE;
  if ($values['required'] OR !empty($values['regex-pattern'])) {
    $collapsed = FALSE;
  }
  $form['fields']['field']['additional'] = array(
    '#type' => 'fieldset',
    '#title' => 'Additional Options',
    '#collapsible' => TRUE,
    '#collapsed' => $collapsed
  );

  $form['fields']['field']['additional']['required'] = array(
    '#type' => 'checkbox',
    '#title' => 'Make this field required',
    '#default_value' => $values['required'],
  );

  $form['fields']['field']['additional']['regex_transform'] = array(
    '#type' => 'fieldset',
    '#title' => 'Transform Data File Value Rules',
    '#collapsible' => TRUE,
    '#collapsed' => (!empty($values['regex-pattern'])) ? FALSE : TRUE,
  );

  $form['fields']['field']['additional']['regex_transform']['regex_description'] = array(
    '#type' => 'item',
    '#markup' => 'A transformation rule allows you to transform the original value '
      . '(usually from a user submitted data file) into the form you would like it stored '
      . 'in the chado database. A rule consists of a match pattern (a php regular expression '
      . 'which captures regions of the original value) and a replacement pattern (a string which may contain capture references '
      . 'that describes what the new value should be).'
  );

  $pattern_default = '';
  $replace_default = '';

  // Check to see if there is more than one regex and if so warn them that 
  // this feature is no longer supported. We don't want people to lose existing
  // transformation rules just by tweaking the field so we will only set defaults
  // if there is a single rule and summarize multiple rules statically.
  if (sizeof($values['regex-pattern']) > 1) {
    $msg = 'This field uses more than one transformation rule. The current rules are:';

    $table_vars['header'] = array('Match Pattern', 'Replacement Pattern');
    $table_vars['rows'] = array();
    foreach ($values['regex-pattern'] as $index => $pattern) {
      $table_vars['rows'][] = array(
        check_plain($pattern),
        check_plain($values['regex-replace'][$index]),
      );
    }
    $msg .= theme('table', $table_vars);

    $msg .= 'Unfortunatly adding multiple transformation rules is no longer supported '
      . 'through the user interface. As long as you don\'t '
      . 'enter new match/replacement patterns below, your current transformation rules will '
      . 'be preserved and will be used during loading. However, if any changes need to be '
      . 'made you will have to switch over to a single transformation rule by entering it below.';

    $form['fields']['additional']['regex_transform']['warning'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="messages warning">' . $msg . '</div>',
    );

    // Also save the rules to ensure they are kept.
    $form['fields']['field']['additional']['regex_transform']['old_rules'] = array(
      '#type' => 'hidden',
      '#value' => array(
        'pattern' => $values['regex-pattern'],
        'replace' => $values['regex-replace']
      )
    );
    $form['fields']['field']['additional']['regex_transform']['old_rules']['#value'] = serialize($form['fields']['additional']['regex_transform']['old_rules']['#value']);
  }
  else {

    // Get current pattern/replacement rule to use as defaults.
    foreach ($values['regex-pattern'] as $index => $pattern) {
      $pattern_default = $pattern;
      $replace_default = $values['regex-replace'][$index];
    }

  }

  $form['fields']['field']['additional']['regex_transform']['regex_pattern'] = array(
    '#type' => 'textfield',
    '#title' => 'Match Pattern',
    '#description' => 'You can use standard php regular expressions in this field to specify a '
      . 'pattern. Only if this pattern matches the value in the data file does the replacement '
      . 'pattern get applied to the value. To capture a section of your value for use in the '
      . 'replacement pattern surround with parentheses. For example, <i>GI:(\d+)</i> will match '
      . ' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
      . ' To match and capture any value use <i>.*</i>',
    '#default_value' => $pattern_default,
  );

  $form['fields']['field']['additional']['regex_transform']['regex_replace'] = array(
    '#type' => 'textfield',
    '#title' => 'Replacement Pattern',
    '#description' => 'This pattern should contain the text you want to replace the match pattern '
      . 'mentioned above. It can include references of the form \n where n is the number of the '
      . 'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
      . 'first set of round brackets.',
    '#default_value' => $replace_default,
  );

  if ($values['field_type'] == 'table field') {
    $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
    $form['fields']['field']['additional']['regex_transform']['regex_replace']['#description'] .= '<p>'
      . 'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
      . 'This allows you to substitute other data file values into the current field. For example, '
      . 'if you had the following line:<br />'
      . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
      . '<br /> and your current field is for column #1 and you\'re inserting into the chado field '
      . 'feature.uniquename then you might want to add in the data to ensure your uniquename is '
      . 'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
      . 'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  }

  $form['submit-save'] = array(
    '#type' => 'submit',
    '#value' => 'Save Changes'
  );

  $form['submit-cancel'] = array(
    '#type' => 'submit',
    '#value' => 'Cancel'
  );

  return $form;
}