Bladeren bron

Issue #2312817: Clean up field add/edit form to make record information more complete and compact.

Lacey Sanderson 9 jaren geleden
bovenliggende
commit
c4d24cee5d
1 gewijzigde bestanden met toevoegingen van 72 en 54 verwijderingen
  1. 72 54
      tripal_bulk_loader/includes/tripal_bulk_loader.admin.templates.inc

+ 72 - 54
tripal_bulk_loader/includes/tripal_bulk_loader.admin.templates.inc

@@ -1629,6 +1629,9 @@ function tripal_bulk_loader_template_field_form_default_values($mode, &$form_sta
   if (!isset($table)) {
     $table = reset($tables);
   }
+  if (!isset($record_name) and isset($form_state['storage']['template_array'][$record_id])) {
+    $record_name = $form_state['storage']['template_array'][$record_id]['record_id'];
+  }
 
   // Chado fields
   $chado_fields = array();
@@ -1688,7 +1691,7 @@ function tripal_bulk_loader_template_field_form_default_values($mode, &$form_sta
   $v['chado_field'] = $field;
   $v['chado_field_options'] = $chado_fields;
 
-  $v['record_name'] = (isset($form_state['values']['record_name'])) ? $form_state['values']['record_name'] : '';
+  $v['record_name'] = (isset($record_name)) ? $record_name : '';
   if (isset($form_state['values']['field_title'])) {
     $v['field_title'] =  $form_state['values']['field_title'];
   }
@@ -1860,30 +1863,17 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
     '#suffix' => '</div>',
   );
 
-  $form['fields']['basic'] = array(
-    '#type' => 'markup',
-    '#prefix' => '<div class="basic">',
-    '#suffix' => '</div>'
-  );
-
-  $form['fields']['basic']['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']['record'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Record'
   );
-
-  $form['fields']['basic']['field_group']  = array(
+  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(
@@ -1899,16 +1889,39 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
     '#required' => TRUE,
   );
 
-  $form['fields']['basic']['record_name'] = array(
-    '#type' => ($values['no_record_id']) ? 'textfield' : 'hidden',
+  $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']['field'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Field',
+  );
 
-  $form['fields']['basic']['field_title'] = array(
+  $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'],
@@ -1916,13 +1929,13 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
 
 
   // Chado Field
-  $form['fields']['chado'] = array(
+  $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']['chado']['chado_table'] = array(
+  $form['fields']['field']['chado']['chado_table'] = array(
     '#type' => 'select',
     '#title' => t('Chado Table'),
     '#options' => $values['chado_table_options'],
@@ -1934,7 +1947,7 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
       ),
   );
 
-  $form['fields']['chado']['chado_field'] = array(
+  $form['fields']['field']['chado']['chado_field'] = array(
     '#type' => 'select',
     '#title' => t('Chado Field/Column'),
     '#options' => $values['chado_field_options'],
@@ -1947,14 +1960,14 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
   );
 
   // loading file data column
-  $form['fields']['columns'] = array(
+  $form['fields']['field']['columns'] = array(
     '#type' => 'fieldset',
     '#title' => t('Data File Column'),
     '#collapsible' => TRUE,
     '#collapsed' => ($values['field_type'] == 'table field')? FALSE : TRUE,
   );
 
-  $form['fields']['columns']['column_number'] = array(
+  $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.'),
@@ -1962,14 +1975,14 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
     '#default_value' => $values['column_number'],
   );
 
-  $form['fields']['columns']['column_exposed'] = array(
+  $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']['columns']['column_exposed_desc'] = array(
+  $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.'),
@@ -1977,28 +1990,28 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
   );
 
   // Global Value
-  $form['fields']['constant'] = array(
+  $form['fields']['field']['constant'] = array(
     '#type' => 'fieldset',
     '#title' => t('Constant'),
     '#collapsible' => TRUE,
     '#collapsed' => ($values['field_type'] == 'constant')? FALSE : TRUE,
   );
 
-  $form['fields']['constant']['constant_value'] = array(
+  $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']['constant']['constant_exposed'] = array(
+  $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']['constant']['constant_validate'] = array(
+  $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.'),
@@ -2006,14 +2019,14 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
   );
 
   // Foreign Key / Referrer
-  $form['fields']['foreign_key'] = array(
+  $form['fields']['field']['foreign_key'] = array(
     '#type' => 'fieldset',
     '#title' => 'Record Referral',
     '#collapsible' => TRUE,
     '#collapsed' => ($values['field_type'] == 'foreign key')? FALSE : TRUE,
   );
 
-  $form['fields']['foreign_key']['show_all_records'] = array(
+  $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'),
@@ -2025,7 +2038,7 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
     ),
   );
 
-  $form['fields']['foreign_key']['foreign_record'] = array(
+  $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.',
@@ -2038,7 +2051,7 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
     '#default_value' => $values['foreign_record'],
   );
 
-  $form['fields']['foreign_key']['foreign_field'] = array(
+  $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.',
@@ -2051,27 +2064,27 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
   if ($values['required'] OR !empty($values['regex-pattern'])) {
     $collapsed = FALSE;
   }
-  $form['fields']['additional'] = array(
+  $form['fields']['field']['additional'] = array(
     '#type' => 'fieldset',
     '#title' => 'Additional Options',
     '#collapsible' => TRUE,
     '#collapsed' => $collapsed
   );
 
-  $form['fields']['additional']['required'] = array(
+  $form['fields']['field']['additional']['required'] = array(
     '#type' => 'checkbox',
     '#title' => 'Make this field required',
     '#default_value' => $values['required'],
   );
 
-  $form['fields']['additional']['regex_transform'] = array(
+  $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']['additional']['regex_transform']['regex_description'] = array(
+  $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 '
@@ -2112,14 +2125,14 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
     );
     
     // Also save the rules to ensure they are kept.
-    $form['fields']['additional']['regex_transform']['old_rules'] = array(
+    $form['fields']['field']['additional']['regex_transform']['old_rules'] = array(
       '#type' => 'hidden',
       '#value' => array(
         'pattern' => $values['regex-pattern'],
         'replace' => $values['regex-replace']
       )
     );
-    $form['fields']['additional']['regex_transform']['old_rules']['#value'] = serialize($form['fields']['additional']['regex_transform']['old_rules']['#value']);
+    $form['fields']['field']['additional']['regex_transform']['old_rules']['#value'] = serialize($form['fields']['additional']['regex_transform']['old_rules']['#value']);
   }
   else {
   
@@ -2131,7 +2144,7 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
 
   }
   
-  $form['fields']['additional']['regex_transform']['regex_pattern'] = array(
+  $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 '
@@ -2143,7 +2156,7 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
     '#default_value' => $pattern_default,
   );
 
-  $form['fields']['additional']['regex_transform']['regex_replace'] = array(
+  $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 '
@@ -2155,7 +2168,7 @@ function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
 
   if ($values['field_type'] == 'table field') {
     $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
-    $form['fields']['additional']['regex_transform']['regex_replace']['#description'] .= '<p>'
+    $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 />'
@@ -2269,12 +2282,15 @@ function tripal_bulk_loader_template_field_form_submit($form, &$form_state) {
     }
 
     // Save old transformation rules if they exist.
-    if ($form_state['values']['old_rules']) {
+    if (isset($form_state['values']['old_rules'])) {
       $field['regex'] = unserialize($form_state['values']['old_rules']);
     }
     // Deal with any additional options
     if (!empty($form_state['values']['regex_pattern']) AND !empty($form_state['values']['regex_replace'])) {
-      $field['regex']['pattern'] = array('/' . $form_state['values']['regex_pattern'] . '/');
+      if (!preg_match('/^\/.*\/$/', $form_state['values']['regex_pattern'])) {
+        $form_state['values']['regex_pattern'] = '/' . $form_state['values']['regex_pattern'] . '/';
+      }
+      $field['regex']['pattern'] = array($form_state['values']['regex_pattern']);
       $field['regex']['replace'] = array($form_state['values']['regex_replace']);
     }
     
@@ -2282,16 +2298,18 @@ function tripal_bulk_loader_template_field_form_submit($form, &$form_state) {
     // Save Template
     if ($form_state['storage']['mode'] == 'create') {
       $template[$priority]['fields'][] = $field;
+      $success_msg = 'Successfully Added Field to Template';
     }
     else {
       $template[$priority]['fields'][$form_state['storage']['field_index']] = $field;
+      $success_msg = 'Successfully Updated Field';
     }
     $form_state['storage']['template']->template_array = serialize($template);
     $form_state['storage']['template']->changed = time();
     $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
 
     if ($success) {
-      drupal_set_message(t('Successfully Added Field to Template'));
+      drupal_set_message(t($success_msg));
       drupal_set_message(t('Template Saved.'));
 
       $form_state['rebuild'] = FALSE;