Browse Source

Updated Chado Node code to be aware of default vocabs and further developed the default vocab form so that they can be changed

Lacey Sanderson 11 years ago
parent
commit
0aa9fbf9f1

+ 66 - 45
tripal_core/api/tripal_core.chado_nodes.properties.api.inc

@@ -105,12 +105,12 @@
  * @param $form_state
  * @param $form_state
  *   The corresponding form_state array for the form
  *   The corresponding form_state array for the form
  * @param $details
  * @param $details
- *   An array defining details used by this form. 
+ *   An array defining details used by this form.
  *   Required keys that are always required:
  *   Required keys that are always required:
  *     - property_table: the name of the property table (e.g.: featureprop, stockprop, etc.)
  *     - property_table: the name of the property table (e.g.: featureprop, stockprop, etc.)
  *   Required keys for forms that update a record.
  *   Required keys for forms that update a record.
  *     - chado_id: the id of the record to which properties will be associated (e.g.: if a
  *     - chado_id: the id of the record to which properties will be associated (e.g.: if a
- *       feature has a feature_id of 999 and we want to associate properties for that feature 
+ *       feature has a feature_id of 999 and we want to associate properties for that feature
  *       then the chado_id option should be 999)
  *       then the chado_id option should be 999)
  *   Require ONE of the following to identify the controlled vocabulary containing the properties to use:
  *   Require ONE of the following to identify the controlled vocabulary containing the properties to use:
  *     - cv_id: the unique key from the cv table
  *     - cv_id: the unique key from the cv table
@@ -124,7 +124,7 @@
  *       to the default instructions
  *       to the default instructions
  *     - fieldset_title: An alternate name for the fieldset in which the properties
  *     - fieldset_title: An alternate name for the fieldset in which the properties
  *       form is placed. By default the title is 'Properties'.
  *       form is placed. By default the title is 'Properties'.
- *     - default_properties:  An array of properties used to initialize the 
+ *     - default_properties:  An array of properties used to initialize the
  *       properties form.  Each property shoudl be represented as an array with
  *       properties form.  Each property shoudl be represented as an array with
  *       the following keys and values:
  *       the following keys and values:
  *          'cvterm':    The cvterm object for the property type
  *          'cvterm':    The cvterm object for the property type
@@ -132,7 +132,7 @@
  *     - select_options: an array of terms to use for the drop down select box.
  *     - select_options: an array of terms to use for the drop down select box.
  *       this array will be used rather than populating the drop down with terms
  *       this array will be used rather than populating the drop down with terms
  *       from the named vocabulary.  The array must have keys with the cvterm_id
  *       from the named vocabulary.  The array must have keys with the cvterm_id
- *       and values with the cvterm name. 
+ *       and values with the cvterm name.
  *
  *
  * @ingroup tripal_chado_node_api
  * @ingroup tripal_chado_node_api
  */
  */
@@ -156,17 +156,17 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
       array());
       array());
     return;
     return;
   }
   }
-  
+
   // make sure the property table exists before proceeding.
   // make sure the property table exists before proceeding.
   if (!chado_table_exists($details['property_table'])) {
   if (!chado_table_exists($details['property_table'])) {
-    drupal_set_message("Cannot add property elements to the form. The property table, '" . 
+    drupal_set_message("Cannot add property elements to the form. The property table, '" .
       $details['property_table'] . "', does not exists", "error");
       $details['property_table'] . "', does not exists", "error");
-    tripal_report_error('tcprops_form', TRIPAL_ERROR, 
-     "Cannot add property elements to the form. The property table, '%name', cannot be found.", 
+    tripal_report_error('tcprops_form', TRIPAL_ERROR,
+     "Cannot add property elements to the form. The property table, '%name', cannot be found.",
       array('%name' => $details['property_table']));
       array('%name' => $details['property_table']));
     return;
     return;
   }
   }
-  
+
   // if the chado_id_field is not specified then set it using the
   // if the chado_id_field is not specified then set it using the
   // typical chado naming scheme
   // typical chado naming scheme
   if (!array_key_exists('chado_id_field', $details)) {
   if (!array_key_exists('chado_id_field', $details)) {
@@ -174,7 +174,7 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
     $chado_id_field = $chado_id_table . '_id';
     $chado_id_field = $chado_id_table . '_id';
     $details['chado_id_field'] = $chado_id_field;
     $details['chado_id_field'] = $chado_id_field;
   }
   }
-  
+
   // make sure the specified cv exists
   // make sure the specified cv exists
   if (isset($details['cv_name'])) {
   if (isset($details['cv_name'])) {
     // make sure the cv_name is real
     // make sure the cv_name is real
@@ -182,8 +182,8 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
     if (count($result) == 0) {
     if (count($result) == 0) {
       drupal_set_message("Cannot add property elements to the form. The CV name, '" .
       drupal_set_message("Cannot add property elements to the form. The CV name, '" .
         $details['cv_name'] . "', does not exists", "error");
         $details['cv_name'] . "', does not exists", "error");
-      tripal_report_error('tcprops_form', TRIPAL_ERROR, 
-        "Cannot add property elements to the form. The CV named, '%name', cannot be found.", 
+      tripal_report_error('tcprops_form', TRIPAL_ERROR,
+        "Cannot add property elements to the form. The CV named, '%name', cannot be found.",
         array('%name' => $details['cv_name']));
         array('%name' => $details['cv_name']));
       return;
       return;
     }
     }
@@ -196,8 +196,8 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
     if (count($result) == 0) {
     if (count($result) == 0) {
       drupal_set_message("Cannot add property elements to the form. The CV ID, '" .
       drupal_set_message("Cannot add property elements to the form. The CV ID, '" .
         $details['cv_id'] . "', does not exist", "error");
         $details['cv_id'] . "', does not exist", "error");
-      tripal_report_error('tcprops_form', TRIPAL_ERROR, 
-        "Cannot add property elements to the form. The CV ID, '%id', cannot be found.", 
+      tripal_report_error('tcprops_form', TRIPAL_ERROR,
+        "Cannot add property elements to the form. The CV ID, '%id', cannot be found.",
         array('%id' => $details['cv_id']));
         array('%id' => $details['cv_id']));
       return;
       return;
     }
     }
@@ -205,26 +205,47 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
     $details['cv_name'] = $result[0]->name;
     $details['cv_name'] = $result[0]->name;
   }
   }
   else {
   else {
-    drupal_set_message("Please provide either a 'cv_name' or 'cv_id' as an 
-      option for adding properties to the form", "error");
-    tripal_report_error('tcprops_form', TRIPAL_ERROR, 
-      "Please provide either a 'cv_name' or 'cv_id' as an option for adding properties to the form", 
-      array());
+
+    // If we didn't get given a cv identifier, then try retrieving the default one
+    // using the new cv defaults api
+    $default_cv = tripal_get_default_cv($details['property_table'], 'type_id');
+    if (!empty($default_cv)) {
+      $details['cv_id'] = $default_cv->cv_id;
+      $details['cv_name'] = $default_cv->name;
+    }
+    else {
+
+      $default_form_link = l('vocabulary defaults configuration page',
+          'admin/tripal/chado/tripal_cv/defaults',
+          array('attributes' => array('target' => '_blank')));
+      $message = "There is not a default vocabulary set for Property Types. Please set one using the $default_form_link.";
+
+      if (preg_match('/(\w+)_id/',$details['chado_id_field'],$matches)) {
+        $table = $matches[1];
+        $table = ucwords(str_replace('_',' ',$table));
+        $message = "There is not a default vocabulary set for $table Property Types. Please set one using the  $default_form_link.";
+      }
+
+      tripal_set_message($message, TRIPAL_WARNING);
+      tripal_report_error('tcprops_form', TRIPAL_ERROR,
+        "Please provide either a 'cv_name' or 'cv_id' as an option for adding properties to the form",
+        array());
+    }
     return;
     return;
   }
   }
 
 
   // Get property types for the select list. If the user has provided a set
   // Get property types for the select list. If the user has provided a set
-  // then use those, otherwise get them from the cvterm table for specified cv. 
-  if (isset($details['select_options']) and 
-      is_array($details['select_options']) and 
+  // then use those, otherwise get them from the cvterm table for specified cv.
+  if (isset($details['select_options']) and
+      is_array($details['select_options']) and
       count($details['select_options']) > 0) {
       count($details['select_options']) > 0) {
     $property_options = $details['select_options'];
     $property_options = $details['select_options'];
   }
   }
   // if the select options are not provided then try to get them on our own
   // if the select options are not provided then try to get them on our own
   else {
   else {
-    
+
     // if the vocabulary name is provided in the details then use that to
     // if the vocabulary name is provided in the details then use that to
-    // get the terms 
+    // get the terms
     if (isset($details['cv_name'])) {
     if (isset($details['cv_name'])) {
       $property_options = array();
       $property_options = array();
       $property_options[] = 'Select a Property';
       $property_options[] = 'Select a Property';
@@ -241,9 +262,9 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
       while ($prop = $prop_types->fetchObject()) {
       while ($prop = $prop_types->fetchObject()) {
         $property_options[$prop->cvterm_id] = $prop->name;
         $property_options[$prop->cvterm_id] = $prop->name;
       }
       }
-    } 
+    }
     // if the cv_id is set in the $details array then use that to get the terms
     // if the cv_id is set in the $details array then use that to get the terms
-    elseif (isset($details['cv_id'])) {      
+    elseif (isset($details['cv_id'])) {
       $property_options = array();
       $property_options = array();
       $property_options[] = 'Select a Property';
       $property_options[] = 'Select a Property';
       $sql = "
       $sql = "
@@ -296,7 +317,7 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
     '#type' => 'fieldset',
     '#type' => 'fieldset',
     '#title' => t($details['fieldset_title']),
     '#title' => t($details['fieldset_title']),
     '#description' => t('Add properties by selecting a type
     '#description' => t('Add properties by selecting a type
-      from the dropdown, enter a value and click the "Add" button. To 
+      from the dropdown, enter a value and click the "Add" button. To
       remove a property, click the remove button.' . $details['additional_instructions']),
       remove a property, click the remove button.' . $details['additional_instructions']),
     '#prefix' => "<div id='properties-fieldset'>",
     '#prefix' => "<div id='properties-fieldset'>",
     '#suffix' => '</div>',
     '#suffix' => '</div>',
@@ -325,8 +346,8 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
   );
   );
 
 
   /* Properties can come to us in two ways:
   /* Properties can come to us in two ways:
-   * 1) As entries in the $details['default_properties'] option 
-   * 
+   * 1) As entries in the $details['default_properties'] option
+   *
    * 2) In the form state in the $form_state['chado_properties']. Data is in this field
    * 2) In the form state in the $form_state['chado_properties']. Data is in this field
    *    when an AJAX call updates the form state or a validation error.
    *    when an AJAX call updates the form state or a validation error.
    *
    *
@@ -340,34 +361,34 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
   }
   }
   else {
   else {
     $ranks = array(); // a temporary array used for calculating rank
     $ranks = array(); // a temporary array used for calculating rank
-    
+
     // build the SQL for extracting properties already assigned to this record
     // build the SQL for extracting properties already assigned to this record
     $sql_args = array();
     $sql_args = array();
     $sql_args[':chado_id'] = $details['chado_id'];
     $sql_args[':chado_id'] = $details['chado_id'];
     if (array_key_exists('cv_name', $details)) {
     if (array_key_exists('cv_name', $details)) {
       $cv_where = "CV.name = :cvname";
       $cv_where = "CV.name = :cvname";
       $sql_args[':cvname'] = $details['cv_name'];
       $sql_args[':cvname'] = $details['cv_name'];
-    } 
+    }
     elseif (array_key_exists('cv_id', $details)) {
     elseif (array_key_exists('cv_id', $details)) {
       $cv_where = "CV.cv_id = :cvid";
       $cv_where = "CV.cv_id = :cvid";
       $sql_args[':cvid'] = $details['cv_id'];
       $sql_args[':cvid'] = $details['cv_id'];
     }
     }
     $existing_properties = chado_query(
     $existing_properties = chado_query(
-      "SELECT 
-         PP.".$details['property_table']."_id property_id, 
-         CVT.cvterm_id as type_id, 
-         CVT.name as type_name, 
-         CVT.definition, 
-         PP.value, 
+      "SELECT
+         PP.".$details['property_table']."_id property_id,
+         CVT.cvterm_id as type_id,
+         CVT.name as type_name,
+         CVT.definition,
+         PP.value,
          PP.rank
          PP.rank
        FROM {" . $details['property_table'] . "} PP
        FROM {" . $details['property_table'] . "} PP
          INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
          INNER JOIN {cvterm} CVT ON CVT.cvterm_id = PP.type_id
          INNER JOIN {cv} CV      ON CVT.cv_id     = CV.cv_id
          INNER JOIN {cv} CV      ON CVT.cv_id     = CV.cv_id
        WHERE
        WHERE
          PP." . $details['chado_id_field'] . " = :chado_id AND
          PP." . $details['chado_id_field'] . " = :chado_id AND
-         $cv_where 
+         $cv_where
        ORDER BY CVT.name, PP.rank", $sql_args)->fetchAll();
        ORDER BY CVT.name, PP.rank", $sql_args)->fetchAll();
-    
+
     // iterate through the results and get the largest rank for each type
     // iterate through the results and get the largest rank for each type
     foreach ($existing_properties as $property) {
     foreach ($existing_properties as $property) {
       if (array_key_exists($property->type_id, $ranks)) {
       if (array_key_exists($property->type_id, $ranks)) {
@@ -379,10 +400,10 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
         $ranks[$property->type_id] = $property->rank;
         $ranks[$property->type_id] = $property->rank;
       }
       }
     }
     }
-    
+
     // next add in any default properties
     // next add in any default properties
     if (array_key_exists('default_properties', $details)) {
     if (array_key_exists('default_properties', $details)) {
-    
+
       // next iterate through each of the default properties and create a new
       // next iterate through each of the default properties and create a new
       // stdClass array that contains the fields needed.
       // stdClass array that contains the fields needed.
       foreach ($details['default_properties'] as $property) {
       foreach ($details['default_properties'] as $property) {
@@ -393,7 +414,7 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
         $new_prop->value       = $property['value'];
         $new_prop->value       = $property['value'];
         $new_prop->property_id = NULL;
         $new_prop->property_id = NULL;
         // to set the rank for this property, we need to make sure we set
         // to set the rank for this property, we need to make sure we set
-        // it greater than any already existing rank 
+        // it greater than any already existing rank
         if (array_key_exists($property['cvterm']->cvterm_id, $ranks)) {
         if (array_key_exists($property['cvterm']->cvterm_id, $ranks)) {
           $ranks[$property['cvterm']->cvterm_id]++;
           $ranks[$property['cvterm']->cvterm_id]++;
         }
         }
@@ -514,7 +535,7 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
     '#suffix' => '</span>'
     '#suffix' => '</span>'
   );
   );
 
 
-  // get the value selected (only works on AJAX call) and print the 
+  // get the value selected (only works on AJAX call) and print the
   // description
   // description
   $type_desc = '';
   $type_desc = '';
   if (isset($form_state['input']['property_table']['new']['type'])) {
   if (isset($form_state['input']['property_table']['new']['type'])) {
@@ -638,7 +659,7 @@ function chado_add_node_form_properties_add_button_submit(&$form, &$form_state)
 
 
   $key = $property['type_id'] . '-' . $property['rank'];
   $key = $property['type_id'] . '-' . $property['rank'];
   $form_state['chado_properties'][$key] = (object) $property;
   $form_state['chado_properties'][$key] = (object) $property;
-  
+
   // we don't want the new element to pick up the values from the previous element so wipe them out
   // we don't want the new element to pick up the values from the previous element so wipe them out
   unset($form_state['input']['property_table']['new']['type']);
   unset($form_state['input']['property_table']['new']['type']);
   unset($form_state['input']['property_table']['new']['type_name']);
   unset($form_state['input']['property_table']['new']['type_name']);
@@ -774,7 +795,7 @@ function theme_chado_add_node_form_properties($variables) {
       }
       }
     }
     }
   }
   }
-  
+
   return theme('table', array(
   return theme('table', array(
     'header' => $header,
     'header' => $header,
     'rows' => $rows
     'rows' => $rows

+ 27 - 9
tripal_core/api/tripal_core.chado_nodes.relationships.api.inc

@@ -134,7 +134,7 @@ function chado_add_node_form_relationships(&$form, &$form_state, $details) {
       The relationship table, '%name', cannot be found.", array('%name' => $details['relationship_table']));
       The relationship table, '%name', cannot be found.", array('%name' => $details['relationship_table']));
     return;
     return;
   }
   }
-  
+
   // make sure the specified cv exists
   // make sure the specified cv exists
   if (isset($details['cv_name'])) {
   if (isset($details['cv_name'])) {
     // make sure the cv_name is real
     // make sure the cv_name is real
@@ -163,13 +163,31 @@ function chado_add_node_form_relationships(&$form, &$form_state, $details) {
     $details['cv_name'] = $result[0]->name;
     $details['cv_name'] = $result[0]->name;
   }
   }
   else {
   else {
-    drupal_set_message("Please provide either a 'cv_name' or 'cv_id' as an
-        option for adding relationship to the form", "error");
-    tripal_report_error('tcprops_form', TRIPAL_ERROR, "Please provide either a
+
+    // If we didn't get given a cv identifier, then try retrieving the default one
+    // using the new cv defaults api
+    $default_cv = tripal_get_default_cv($details['relationship_table'], 'type_id');
+    if (!empty($default_cv)) {
+      $details['cv_id'] = $default_cv->cv_id;
+      $details['cv_name'] = $default_cv->name;
+    }
+    else {
+
+      $default_form_link = l('vocabulary defaults configuration page',
+          'admin/tripal/chado/tripal_cv/defaults',
+          array('attributes' => array('target' => '_blank')));
+
+      $table = ucwords(str_replace('_',' ',$details['base_table']));
+      $message = "There is not a default vocabulary set for $table Releationship Types. Please set one using the  $default_form_link.";
+
+      tripal_set_message($message, TRIPAL_WARNING);
+      tripal_report_error('tcprops_form', TRIPAL_ERROR, "Please provide either a
         'cv_name' or 'cv_id' as an option for adding relationship to the form", array());
         'cv_name' or 'cv_id' as an option for adding relationship to the form", array());
+    }
+
     return;
     return;
   }
   }
-  
+
   $form_state['rebuild'] = TRUE;
   $form_state['rebuild'] = TRUE;
 
 
   // Set Defaults for optional fields
   // Set Defaults for optional fields
@@ -207,7 +225,7 @@ function chado_add_node_form_relationships(&$form, &$form_state, $details) {
         $type_options[$prop->cvterm_id] = $prop->name;
         $type_options[$prop->cvterm_id] = $prop->name;
         $details['cv_id'] = $prop->cv_id;
         $details['cv_id'] = $prop->cv_id;
       }
       }
-    } 
+    }
     elseif (isset($details['cv_id'])) {
     elseif (isset($details['cv_id'])) {
       $type_options = array();
       $type_options = array();
       $type_options[] = 'Select a Property';
       $type_options[] = 'Select a Property';
@@ -621,7 +639,7 @@ function chado_add_node_form_relationships_add_button_submit(&$form, &$form_stat
 
 
 
 
 
 
-  
+
   // get details for the new relationship
   // get details for the new relationship
   if ($form_state['values']['relationship_table']['new']['subject_is_current']) {
   if ($form_state['values']['relationship_table']['new']['subject_is_current']) {
     $relationship = array(
     $relationship = array(
@@ -651,14 +669,14 @@ function chado_add_node_form_relationships_add_button_submit(&$form, &$form_stat
     unset($form_state['input']['relationship_table']['new']['subject_id']);
     unset($form_state['input']['relationship_table']['new']['subject_id']);
     unset($form_state['input']['relationship_table']['new']['subject_name']);
     unset($form_state['input']['relationship_table']['new']['subject_name']);
   }
   }
-  
+
   $key = $relationship['type_id'] . '-' . $relationship['rank'];
   $key = $relationship['type_id'] . '-' . $relationship['rank'];
   $form_state['chado_relationships'][$key] = (object) $relationship;
   $form_state['chado_relationships'][$key] = (object) $relationship;
 
 
   // we don't want the new element to pick up the values from the previous element so wipe them out
   // we don't want the new element to pick up the values from the previous element so wipe them out
   unset($form_state['input']['relationship_table']['new']['type_id']);
   unset($form_state['input']['relationship_table']['new']['type_id']);
   unset($form_state['input']['relationship_table']['new']['type_name']);
   unset($form_state['input']['relationship_table']['new']['type_name']);
-  
+
   $form_state['rebuild'] = TRUE;
   $form_state['rebuild'] = TRUE;
 }
 }
 
 

+ 19 - 11
tripal_cv/api/tripal_cv.api.inc

@@ -987,10 +987,15 @@ function tripal_associate_cvterm($basetable, $record_id, $cvterm) {
  * @return
  * @return
  *   TRUE if set, FALSE if an error occured
  *   TRUE if set, FALSE if an error occured
  */
  */
-function tripal_set_default_cv($table, $field, $cv_name) {
+function tripal_set_default_cv($table, $field, $cv_name, $cv_id = FALSE) {
 
 
-  // make sure the cv_id is valid
-  $cv = tripal_get_cv(array('name' => $cv_name));
+  // Get the CV object
+  if ($cv_id) {
+    $cv = tripal_get_cv(array('cv_id' => $cv_id));
+  }
+  else {
+    $cv = tripal_get_cv(array('name' => $cv_name));
+  }
 
 
   if ($cv) {
   if ($cv) {
     // first delete any entries for this table and field
     // first delete any entries for this table and field
@@ -1075,6 +1080,16 @@ function tripal_get_cvterm_default_select_options($table, $field, $field_desc) {
 
 
   if ($default_cv) {
   if ($default_cv) {
     $options = tripal_get_cvterm_select_options($default_cv->cv_id);
     $options = tripal_get_cvterm_select_options($default_cv->cv_id);
+
+    if (count($options) == 0) {
+      tripal_set_message('There are no ' . $field_desc . '. Please ' .
+        l('add terms',
+          'admin/tripal/chado/tripal_cv/cv/' .$default_cv->cv_id. '/cvterm/add',
+           array('attributes' => array('target' => '_blank'))) . ' to the ' .
+        $default_cv->name .' vocabulary.',
+        TRIPAL_WARNING);
+    }
+
   }
   }
   else {
   else {
     tripal_set_message('There is not a default vocabulary set for ' . $field_desc . '. '.
     tripal_set_message('There is not a default vocabulary set for ' . $field_desc . '. '.
@@ -1084,13 +1099,6 @@ function tripal_get_cvterm_default_select_options($table, $field, $field_desc) {
         array('attributes' => array('target' => '_blank'))) . '.',
         array('attributes' => array('target' => '_blank'))) . '.',
       TRIPAL_WARNING);
       TRIPAL_WARNING);
   }
   }
-  if (count($options) == 0) {
-    tripal_set_message('There are no ' . $field_desc . '. Please ' .
-      l('add terms',
-        'admin/tripal/chado/tripal_cv/cv/' .$default_cv->cv_id. '/cvterm/add',
-         array('attributes' => array('target' => '_blank'))) . ' to the ' .
-      $default_cv->name .' vocabulary.',
-      TRIPAL_WARNING);
-  }
+
   return $options;
   return $options;
 }
 }

+ 99 - 52
tripal_cv/includes/tripal_cv.admin.inc

@@ -46,11 +46,10 @@ function tripal_cv_admin_cv_listing() {
 }
 }
 
 
 /**
 /**
- * 
+ *
  */
  */
-function tripal_cv_admin_set_defaults_form() {
-  $form = array();
-  
+function tripal_cv_admin_set_defaults_form($form, $form_state) {
+
   $form['instructions'] = array(
   $form['instructions'] = array(
     '#markup' => t('Much of the data housed in Chado is typed, meaning that a ' .
     '#markup' => t('Much of the data housed in Chado is typed, meaning that a ' .
         'controlled vocabulary describes what type of data the record is. For example, '.
         'controlled vocabulary describes what type of data the record is. For example, '.
@@ -60,20 +59,20 @@ function tripal_cv_admin_set_defaults_form() {
         'Chado that requires a type from a vocabulary. By default, autocomplete fields, '.
         'Chado that requires a type from a vocabulary. By default, autocomplete fields, '.
         'type select boxes and type validation occur using the default vocabularies set below. '),
         'type select boxes and type validation occur using the default vocabularies set below. '),
   );
   );
-  
+
   // get the list of all tables that use the cvterm table as an FK
   // get the list of all tables that use the cvterm table as an FK
   $cvterm_schema = chado_get_schema('cvterm');
   $cvterm_schema = chado_get_schema('cvterm');
   $referring_tables = $cvterm_schema['referring_tables'];
   $referring_tables = $cvterm_schema['referring_tables'];
-  
+
   // get the list of tables that already have default set
   // get the list of tables that already have default set
   $cv_defaults = db_select('tripal_cv_defaults', 'TCD')
   $cv_defaults = db_select('tripal_cv_defaults', 'TCD')
     ->fields('TCD', array('cv_default_id', 'table_name', 'field_name', 'cv_id'))
     ->fields('TCD', array('cv_default_id', 'table_name', 'field_name', 'cv_id'))
     ->orderBy('table_name', 'ASC')
     ->orderBy('table_name', 'ASC')
     ->execute();
     ->execute();
-  
+
   // get the list of vocabularies
   // get the list of vocabularies
   $cvs = tripal_get_cv_select_options();
   $cvs = tripal_get_cv_select_options();
-  
+
   $form['settings'] = array(
   $form['settings'] = array(
     '#type' => 'fieldset',
     '#type' => 'fieldset',
     '#title' => t('Configured Defaults'),
     '#title' => t('Configured Defaults'),
@@ -83,83 +82,131 @@ function tripal_cv_admin_set_defaults_form() {
   foreach ($cv_defaults as $cv_default) {
   foreach ($cv_defaults as $cv_default) {
     $cv_default_id = $cv_default->cv_default_id;
     $cv_default_id = $cv_default->cv_default_id;
     $cv = tripal_get_cv(array('cv_id' => $cv_default->cv_id));
     $cv = tripal_get_cv(array('cv_id' => $cv_default->cv_id));
+
     $form['settings']['existing'][$cv_default_id]["id"] = array(
     $form['settings']['existing'][$cv_default_id]["id"] = array(
       '#type' => 'hidden',
       '#type' => 'hidden',
       '#value' => $cv_default_id,
       '#value' => $cv_default_id,
     );
     );
-    $form['settings']['existing'][$cv_default_id]["table_name"] = array(
+
+    // Display
+    $form['settings']['existing'][$cv_default_id]["table_name-display"] = array(
       '#type' => 'markup',
       '#type' => 'markup',
       '#markup' => $cv_default->table_name
       '#markup' => $cv_default->table_name
     );
     );
-    $form['settings']['existing'][$cv_default_id]["field_name"] = array(
+    $form['settings']['existing'][$cv_default_id]["field_name-display"] = array(
       '#type' => 'markup',
       '#type' => 'markup',
       '#markup' => $cv_default->field_name
       '#markup' => $cv_default->field_name
     );
     );
+
+    // Save for use in submit
+    $form['settings']['existing'][$cv_default_id]["table_name"] = array(
+      '#type' => 'hidden',
+      '#value' => $cv_default->table_name
+    );
+    $form['settings']['existing'][$cv_default_id]["field_name"] = array(
+      '#type' => 'hidden',
+      '#value' => $cv_default->field_name
+    );
+
+    // Selectbox to set the vocabulary
+    if (!empty($cv)) {
+      $default_cv = $cv_default->cv_id;
+    }
+    else {
+      $cvs[0] = 'NONE SET';
+      $default_cv = 0;
+    }
     $form['settings']['existing'][$cv_default_id]["vocabulary"] = array(
     $form['settings']['existing'][$cv_default_id]["vocabulary"] = array(
       '#type' => 'select',
       '#type' => 'select',
       '#options' => $cvs,
       '#options' => $cvs,
-      '#default_value' => $cv_default->cv_id,
-      '#suffix' => '<a target="_blank" href="'. url('admin/tripal/chado/tripal_cv/cvterms') . '?cv=' . $cv->name . '">View terms</a>'
+      '#default_value' => $default_cv,
+    );
+
+    // Actions
+    $view_terms = l('New Vocabulary', 'admin/tripal/chado/tripal_cv/cv/add');
+    $add_term = '';
+    if (!empty($cv)) {
+      $view_terms = l(
+        'View Terms',
+        'admin/tripal/chado/tripal_cv/cvterms',
+        array('query' => array('cv' => $cv->name))
+      );
+
+      $add_term = l(
+        'Add Term',
+        'admin/tripal/chado/tripal_cv/cv/' . $cv->cv_id . '/cvterm/add'
+      );
+    }
+    $form['settings']['existing'][$cv_default_id]["view-terms"] = array(
+      '#type' => 'markup',
+      '#markup' => $view_terms
     );
     );
-    // remove button
-    $form['settings']['existing'][$cv_default_id]['remove'] = array(
-      '#type' => 'submit',
-      '#value' => t('Remove'),
-      '#name' => "cv_remove-$cv_default_id",
-      '#ajax' => array(
-        'callback' => "tripal_cv_admin_set_defaults_ajax_update",
-        'wrapper' => 'tripal-generic-edit-properties-table',
-        '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 node form submit.  In the validate function we validate only the
-      // property fields and in the submit we remove the indicated property
-      // from the chado_properties array. In order to keep validate errors
-      // from the node form validate and Drupal required errors for non-property fields
-      // preventing the user from removing properties we set the #limit_validation_errors below
-      '#validate' => array('chado_add_node_form_properties_remove_button_validate'),
-      '#submit' => array('chado_add_node_form_properties_remove_button_submit'),
-      // Limit the validation of the form upon clicking this button to the property_table tree
-      // No other fields will be validated (ie: no fields from the main form or any other api
-      // added form).
-      '#limit_validation_errors' => array(
-        array('property_table')  // Validate all fields within $form_state['values']['property_table']
-      )
+    $form['settings']['existing'][$cv_default_id]["add-new-term"] = array(
+      '#type' => 'markup',
+      '#markup' => $add_term
     );
     );
+
   }
   }
-  
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Update Defaults'
+  );
+
   return $form;
   return $form;
 }
 }
 
 
+function tripal_cv_admin_set_defaults_form_submit($form, $form_state) {
+
+  foreach ($form_state['values']['settings']['existing'] as $default_cv) {
+    if (!empty($default_cv['vocabulary'])) {
+      tripal_set_default_cv(
+        $default_cv['table_name'],
+        $default_cv['field_name'],
+        '', // We are passing in the cv_id so we don't need the name
+        $default_cv['vocabulary']
+      );
+    }
+  }
+
+}
+
 /**
 /**
- * 
+ *
  * @param unknown $variables
  * @param unknown $variables
  */
  */
 function theme_tripal_cv_admin_set_defaults_form($variables) {
 function theme_tripal_cv_admin_set_defaults_form($variables) {
   $element = $variables['element'];
   $element = $variables['element'];
 
 
   $header = array(
   $header = array(
-    'table_name'      => array('data' => t('Table Name'),      'width' => '20%'),
-    'field_name'      => array('data' => t('Field Name'),      'width' => '20%'),
-    'vocabulary'      => array('data' => t('Default Vocabulary'), 'width' => '40%'),
-    'property_action' => array('data' => t('Actions'),    'width' => '20%'),
+    'table_name'      => array('data' => t('Table Name'),         'width' => '20%'),
+    'field_name'      => array('data' => t('Field Name'),         'width' => '20%'),
+    'vocabulary'      => array('data' => t('Default Vocabulary'), 'width' => '30%'),
+    'actions'         => array('data' => t('Actions'),            'width' => '30%'),
   );
   );
 
 
   $rows = array();
   $rows = array();
-  
+
   foreach ($element['settings']['existing'] as $key => $value) {
   foreach ($element['settings']['existing'] as $key => $value) {
     if (is_numeric($key)) {
     if (is_numeric($key)) {
+
+      $action_links = '<ul class="links inline">';
+      if (!empty($value['view-terms'])) {
+        $action_links .= '<li>' . drupal_render($value['view-terms']) . '</li>';
+      }
+      if (!empty($value['add-new-term'])) {
+        $action_links .= '<li>' . drupal_render($value['add-new-term']) . '</li>';
+      }
+      $action_links .= '</li></ul>';
+
       $rows[] = array(
       $rows[] = array(
-        drupal_render($value['table_name']),
-        drupal_render($value['field_name']),
+        drupal_render($value['table_name-display']),
+        drupal_render($value['field_name-display']),
         drupal_render($value['vocabulary']),
         drupal_render($value['vocabulary']),
-        drupal_render($value['remove']),
+        $action_links
       );
       );
     }
     }
-  } 
+  }
   $settings_table = theme('table', array(
   $settings_table = theme('table', array(
     'header' => $header,
     'header' => $header,
     'rows' => $rows
     'rows' => $rows
@@ -168,7 +215,7 @@ function theme_tripal_cv_admin_set_defaults_form($variables) {
     '#type' => 'markup',
     '#type' => 'markup',
     '#markup' => $settings_table,
     '#markup' => $settings_table,
   );
   );
-  
+
   return drupal_render_children($element);
   return drupal_render_children($element);
-  
+
 }
 }