Browse Source

Bulk Loader: Bug fix to template add/edit field -now managing regex works

Lacey Sanderson 11 years ago
parent
commit
961b706bdd

+ 23 - 10
tripal_bulk_loader/includes/tripal_bulk_loader.admin.templates.inc

@@ -1604,6 +1604,10 @@ function tripal_bulk_loader_template_field_form_default_values($mode, &$form_sta
   $v['foreign_field'] = (isset($form_state['values']['foreign_field'])) ? $form_state['values']['foreign_field'] : current($ref_chado_fields);
   $v['required'] = (isset($form_state['values']['required'])) ? $form_state['values']['required'] : FALSE;
 
+  if (isset($original_field['regex']) && empty($form_state['storage']['regex']['pattern'])) {
+    $form_state['storage']['regex']['pattern'] = $original_field['regex']['pattern'];
+    $form_state['storage']['regex']['replace'] = $original_field['regex']['replace'];
+  }
   $v['regex_are_set'] = (isset($form_state['storage']['regex']['pattern'])) ? TRUE : FALSE;
   $v['regex-pattern'] = (isset($form_state['storage']['regex']['pattern'])) ? $form_state['storage']['regex']['pattern'] : array();
   $v['regex-replace'] = (isset($form_state['storage']['regex']['replace'])) ? $form_state['storage']['regex']['replace'] : array();
@@ -1660,7 +1664,13 @@ function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
     '#suffix' => '</div>',
   );
 
-  $form['fields']['field_type'] = array(
+  $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(
@@ -1677,7 +1687,7 @@ function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
     ),
   );
 
-  $form['fields']['field_group']  = array(
+  $form['fields']['basic']['field_group']  = array(
     '#type' => 'select',
     '#title' => 'Record',
     '#description' => 'This is used to group a set of fields together allowing '
@@ -1692,7 +1702,7 @@ function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
     '#required' => TRUE,
   );
 
-  $form['fields']['record_name'] = array(
+  $form['fields']['basic']['record_name'] = array(
     '#type' => ($values['no_record_id']) ? 'textfield' : 'hidden',
     '#title' => 'Unique Record Name',
     '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
@@ -1701,7 +1711,7 @@ function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
     '#default_value' => $values['record_name'],
   );
 
-  $form['fields']['field_title'] = array(
+  $form['fields']['basic']['field_title'] = array(
     '#type' => 'textfield',
     '#title' => t('Human-readable Title for Field'),
     '#default_value' => $values['field_title'],
@@ -1844,7 +1854,7 @@ function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
     '#type' => 'fieldset',
     '#title' => 'Additional Options',
     '#collapsible' => TRUE,
-    '#collapsed' => TRUE
+    '#collapsed' => FALSE
   );
 
   $form['fields']['additional']['required'] = array(
@@ -1857,7 +1867,7 @@ function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
     '#type' => 'fieldset',
     '#title' => 'Transform Data File Value Rules',
     '#collapsible' => TRUE,
-    '#collapsed' => $values['regex_are_set'],
+    '#collapsed' => FALSE//$values['regex_are_set'],
   );
 
   $form['fields']['additional']['regex_transform']['regex_description'] = array(
@@ -1873,16 +1883,17 @@ function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
 
   $form['fields']['additional']['regex_transform']['regex-data'] = array(
     '#tree' => TRUE,
+    '#theme' => 'tripal_bulk_loader_field_regex_fieldset'
   );
   foreach ($values['regex-pattern'] as $index => $pattern) {
     $data_element = array(
       'pattern' => array(
         '#type' => 'item',
-        '#markup' => $pattern,
+        '#markup' => check_plain($pattern),
       ),
       'replace' => array(
         '#type' => 'item',
-        '#markup' => $values['regex-replace'][$index],
+        '#markup' => check_plain($values['regex-replace'][$index]),
       ),
       'old_index' => array(
         '#type' => 'hidden',
@@ -1892,6 +1903,7 @@ function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
         '#type' => 'select',
         '#options' => range(0, sizeof($values['regex-pattern'])-1),
         '#default_value' => $index,
+        '#attributes' => array('class' => array('rank-weight')), // needed for table dragging
       ),
       'id' => array(
         '#type' => 'hidden',
@@ -1980,12 +1992,12 @@ function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
     '#value' => 'Test Transformation Rules'
   );
 
-  $form['fields']['submit-save'] = array(
+  $form['submit-save'] = array(
       '#type' => 'submit',
       '#value' => 'Save Changes'
   );
 
-  $form['fields']['submit-cancel'] = array(
+  $form['submit-cancel'] = array(
       '#type' => 'submit',
       '#value' => 'Cancel'
   );
@@ -2123,6 +2135,7 @@ function tripal_bulk_loader_template_field_form_submit($form, &$form_state) {
     // Add transformation rule to original field
     $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
     $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
+    $form_state['rebuild'] = TRUE;
     drupal_set_message(t('Successfully Added Transformation Rule'));
 
   }

+ 33 - 0
tripal_bulk_loader/theme/tripal_bulk_loader.theme.inc

@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * Theme the regex data in the tripal_bulk_loader_template_field_form form
+ */
+function tripal_bulk_loader_field_regex_fieldset($variables) {
+  $element = $variables['element'];
+  drupal_add_tabledrag('regex_list_form_id', 'order', 'sibling', 'rank-weight'); // needed for table dragging
+
+  $header = array(
+    'Match Pattern',
+    'Replacement Pattern',
+    'Order',
+    ''
+  );
+
+  $rows = array();
+  foreach (element_children($element) as $key) {
+    $row = array();
+    $row['data'][] = $element[$key]['pattern']['#markup'];
+    $row['data'][] = $element[$key]['replace']['#markup'];
+    $row['data'][] = drupal_render($element[$key]['new_index']) . drupal_render($element[$key]['id']);
+    $row['data'][] = drupal_render($element[$key]['submit-delete']);
+    $row['class'] = array('draggable'); // needed for table dragging
+    $rows[] = $row;
+  }
+
+  return theme('table', array(
+    'header' => $header,
+    'rows' => $rows,
+    'attributes' => array('id' => 'regex_list_form_id'), // needed for table dragging
+  ));
+}

+ 0 - 83
tripal_bulk_loader/theme/tripal_bulk_loader_add_template_field_form.tpl.php

@@ -1,83 +0,0 @@
-
-<?php print $form['add_fields']['#prefix']; ?>
-<fieldset><legend> <?php print $form['add_fields']['#title']; ?> </legend>
-<?php
-  print drupal_render($form['template_name']);
-
-  // Foreach element in the form fieldset 'add_fields'
-  foreach ($form['add_fields'] as $key => $form_array) {
-    if (preg_match('/^#/',$key)) { continue; }
-    
-    // We only care about the additional fieldset
-    if (preg_match('/additional/', $key)) {
-      //print fieldset
-      if ($form_array['#collapsible']) { $class[] = 'collapsible'; }
-      if ($form_array['#collapsed']) { $class[] = 'collapsed'; }
-      if (sizeof($class)) { $class = ' class="'.implode(' ',$class).'"'; }
-      print '<fieldset'.$class.'><legend>'.$form_array['#title'].'</legend>';
-      
-      // Foreach element in the 'additional' fieldset
-      foreach ($form_array as $key => $sub_form_array) {
-        if (preg_match('/^#/',$key)) { continue; }
-        
-        // We only care about the 'regex_transform' fieldset
-        if (preg_match('/regex_transform/', $key)) {
-          
-          // print fieldset
-          if ($sub_form_array['#collapsible']) { $class[] = 'collapsible'; }
-          if ($sub_form_array['#collapsed']) { $class[] = 'collapsed'; }
-          if (sizeof($class)) { $class = ' class="'.implode(' ',$class).'"'; }
-          print '<fieldset'.$class.'><legend>'.$sub_form_array['#title'].'</legend>';
-          
-          // print description
-          print drupal_render($sub_form_array['regex_description']);
-          
-          // Render Draggable Table
-          drupal_add_tabledrag('draggable-table', 'order', 'sibling', 'transform-reorder');
-          $header = array('Match Pattern', 'Replacement Pattern', 'Order', '');
-          $rows = array();
-          foreach ($sub_form_array['regex-data'] as $key => $element) {
-            if (preg_match('/^#/',$key)) { continue; }
-            $element['new_index']['#attributes']['class'] = 'transform-reorder';
-            
-            $row = array();
-            $row[] = drupal_render($element['pattern']);
-            $row[] = drupal_render($element['replace']);
-            $row[] = drupal_render($element['new_index']) . drupal_render($element['id']);
-            $row[] = drupal_render($element['submit-delete']);
-            $rows[] = array('data' => $row, 'class' => 'draggable');
-          }
-          
-          print theme('table', $header, $rows, array('id' => 'draggable-table'));          
-          
-          // render remaining elements
-          foreach ($sub_form_array as $key => $s2_form_array) {
-            if (preg_match('/^#/',$key)) { continue; }
-            if (!preg_match('/regex-data/', $key)) {
-              print drupal_render($s2_form_array);
-            }
-          }
-          
-          print '</fieldset>';
-          
-        } else {
-          // render other elements  in additional fieldset
-          print drupal_render($sub_form_array);
-        }
-      }
-      print '</fieldset>';
-    } else {
-      // render other elements in add_fields fieldset
-      print drupal_render($form_array);
-    }
-  }
-  unset($form['add_fields']);
-?>
-
-</fieldset>
-</div>
-
-<?php
-  //Render remaining -Needed to submit
-  print drupal_render($form);
-?>

+ 0 - 83
tripal_bulk_loader/theme/tripal_bulk_loader_edit_template_field_form.tpl.php

@@ -1,83 +0,0 @@
-
-<?php print $form['edit_fields']['#prefix']; ?>
-<fieldset><legend> <?php print $form['edit_fields']['#title']; ?> </legend>
-<?php
-  print drupal_render($form['template_name']);
-
-  // Foreach element in the form fieldset 'edit_fields'
-  foreach ($form['edit_fields'] as $key => $form_array) {
-    if (preg_match('/^#/',$key)) { continue; }
-    
-    // We only care about the additional fieldset
-    if (preg_match('/additional/', $key)) {
-      //print fieldset
-      if ($form_array['#collapsible']) { $class[] = 'collapsible'; }
-      if ($form_array['#collapsed']) { $class[] = 'collapsed'; }
-      if (sizeof($class)) { $class = ' class="'.implode(' ',$class).'"'; }
-      print '<fieldset'.$class.'><legend>'.$form_array['#title'].'</legend>';
-      
-      // Foreach element in the 'additional' fieldset
-      foreach ($form_array as $key => $sub_form_array) {
-        if (preg_match('/^#/',$key)) { continue; }
-        
-        // We only care about the 'regex_transform' fieldset
-        if (preg_match('/regex_transform/', $key)) {
-          
-          // print fieldset
-          if ($sub_form_array['#collapsible']) { $class[] = 'collapsible'; }
-          if ($sub_form_array['#collapsed']) { $class[] = 'collapsed'; }
-          if (sizeof($class)) { $class = ' class="'.implode(' ',$class).'"'; }
-          print '<fieldset'.$class.'><legend>'.$sub_form_array['#title'].'</legend>';
-          
-          // print description
-          print drupal_render($sub_form_array['regex_description']);
-          
-          // Render Draggable Table
-          drupal_add_tabledrag('draggable-table', 'order', 'sibling', 'transform-reorder');
-          $header = array('Match Pattern', 'Replacement Pattern', 'Order', '');
-          $rows = array();
-          foreach ($sub_form_array['regex-data'] as $key => $element) {
-            if (preg_match('/^#/',$key)) { continue; }
-            $element['new_index']['#attributes']['class'] = 'transform-reorder';
-            
-            $row = array();
-            $row[] = drupal_render($element['pattern']);
-            $row[] = drupal_render($element['replace']);
-            $row[] = drupal_render($element['new_index']) . drupal_render($element['id']);
-            $row[] = drupal_render($element['submit-delete']);
-            $rows[] = array('data' => $row, 'class' => 'draggable');
-          }
-          
-          print theme('table', $header, $rows, array('id' => 'draggable-table'));          
-          
-          // render remaining elements
-          foreach ($sub_form_array as $key => $s2_form_array) {
-            if (preg_match('/^#/',$key)) { continue; }
-            if (!preg_match('/regex-data/', $key)) {
-              print drupal_render($s2_form_array);
-            }
-          }
-          
-          print '</fieldset>';
-          
-        } else {
-          // render other elements  in additional fieldset
-          print drupal_render($sub_form_array);
-        }
-      }
-      print '</fieldset>';
-    } else {
-      // render other elements in edit_fields fieldset
-      print drupal_render($form_array);
-    }
-  }
-  unset($form['edit_fields']);
-?>
-
-</fieldset>
-</div>
-
-<?php
-  //Render remaining -Needed to submit
-  print drupal_render($form);
-?>

+ 4 - 12
tripal_bulk_loader/tripal_bulk_loader.module

@@ -248,23 +248,15 @@ function tripal_bulk_loader_theme() {
       'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
     ),
     'tripal_bulk_loader_modify_template_base_form' => array(
-      //'variables' => array('form' => NULL),
       'template' => 'tripal_bulk_loader_modify_template_base_form',
       'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
       'render element' => 'form'
     ),
-    /**
-    'tripal_bulk_loader_edit_template_field_form' => array(
-      'variables' => array('form' => NULL),
-      'template' => 'tripal_bulk_loader_edit_template_field_form',
-      'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
-    ),
-    'tripal_bulk_loader_add_template_field_form' => array(
-      'variables' => array('form' => NULL),
-      'template' => 'tripal_bulk_loader_add_template_field_form',
-      'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',
+    'tripal_bulk_loader_field_regex_fieldset' => array(
+      'file' => 'theme/tripal_bulk_loader.theme.inc',
+      'function' => 'tripal_bulk_loader_field_regex_fieldset',
+      'render element' => 'element'
     ),
-    */
     'tripal_bulk_loader_admin' => array(
       'template' => 'tripal_bulk_loader_admin',
       'path' => drupal_get_path('module', 'tripal_bulk_loader') . '/theme',