Browse Source

Reorganizging to separate Chado content into new tripal_chado module and created new TripalVocab and TripalTerm entities

Stephen Ficklin 9 years ago
parent
commit
18351daec1
36 changed files with 1746 additions and 1440 deletions
  1. 2 2
      tripal_chado/api/tripal_chado.api.inc
  2. 28 28
      tripal_chado/includes/fields/dbxref_id.inc
  3. 21 21
      tripal_chado/includes/fields/kvproperty.inc
  4. 10 10
      tripal_chado/includes/fields/kvproperty_adder.inc
  5. 10 10
      tripal_chado/includes/fields/md5checksum.inc
  6. 6 6
      tripal_chado/includes/fields/organism_id.inc
  7. 7 7
      tripal_chado/includes/fields/residues.inc
  8. 8 8
      tripal_chado/includes/fields/seqlen.inc
  9. 232 0
      tripal_chado/includes/tripal_chado.entity.inc
  10. 10 10
      tripal_chado/includes/tripal_chado.field_storage.inc
  11. 0 0
      tripal_chado/theme/css/tripal_chado.css
  12. 3 2
      tripal_chado/tripal_chado.info
  13. 152 0
      tripal_chado/tripal_chado.install
  14. 714 0
      tripal_chado/tripal_chado.module
  15. 2 1
      tripal_cv/api/tripal_cv.api.inc
  16. 27 115
      tripal_entities/api/tripal_entities.api.inc
  17. 1 0
      tripal_entities/includes/TripalBundle.inc
  18. 1 14
      tripal_entities/includes/TripalBundleController.inc
  19. 0 30
      tripal_entities/includes/TripalEntity.inc
  20. 1 1
      tripal_entities/includes/TripalEntityController.inc
  21. 1 1
      tripal_entities/includes/TripalEntityUIController.inc
  22. 20 0
      tripal_entities/includes/TripalTerm.inc
  23. 105 0
      tripal_entities/includes/TripalTermController.inc
  24. 19 0
      tripal_entities/includes/TripalVocab.inc
  25. 104 0
      tripal_entities/includes/TripalVocabController.inc
  26. 3 194
      tripal_entities/includes/tripal_entities.admin.inc
  27. 0 143
      tripal_entities/includes/tripal_entities.chado_entity.inc
  28. 0 111
      tripal_entities/includes/tripal_entities.tables.inc
  29. 1 1
      tripal_entities/theme/templates/tripal_data.tpl.php
  30. 3 7
      tripal_entities/tripal_entities.info
  31. 114 165
      tripal_entities/tripal_entities.install
  32. 139 32
      tripal_entities/tripal_entities.module
  33. 0 1
      tripal_fields/tripal_fields.install
  34. 0 518
      tripal_fields/tripal_fields.module
  35. 0 1
      tripal_fields_layout/tripal_fields_layout.info
  36. 2 1
      tripal_ws/includes/tripal_ws.rest.inc

+ 2 - 2
tripal_fields/api/tripal_fields.api.inc → tripal_chado/api/tripal_chado.api.inc

@@ -9,9 +9,9 @@
  *   An associative array containing the field information.  The following
  *   key/value pairs are supported:
  *     'field_type' : a valid field type.  May be any of the Drupal default
- *       fields, one created by the tripal_fields module or another custom module.
+ *       fields, one created by the tripal_chado module or another custom module.
  *     'widget_type' : a valid widget type. May be any of the Drupal default
- *       fields, one created by the tripal_fields module or another custom module.
+ *       fields, one created by the tripal_chado module or another custom module.
  *     'field_settings' : an array of settings that are appropriate for the
  *       selected field type.
  *     'widget_settings' : an array of settings that are appropriate for the

+ 28 - 28
tripal_fields/includes/fields/dbxref_id.inc → tripal_chado/includes/fields/dbxref_id.inc

@@ -10,7 +10,7 @@
  * @param unknown $items
  * @param unknown $display
  */
-function tripal_fields_dbxref_id_formatter(&$element, $entity_type, $entity, $field,
+function tripal_chado_dbxref_id_formatter(&$element, $entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {
 
   foreach ($items as $delta => $item) {
@@ -41,7 +41,7 @@ function tripal_fields_dbxref_id_formatter(&$element, $entity_type, $entity, $fi
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_dbxref_id_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_chado_dbxref_id_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
   $field_name = $field['field_name'];
 
   // Get the field defaults.
@@ -63,11 +63,11 @@ function tripal_fields_dbxref_id_widget(&$widget, $form, $form_state, $field, $i
 
   // Check $form_state['values'] to see if an AJAX call set the values.
   if (array_key_exists('values', $form_state)) {
-    $dbxref_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, $field_name);
-    $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
-    $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
-    $version = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
-    $description = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
+    $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_name);
+    $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
+    $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
+    $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
+    $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
   }
 
   // If we are here because our parent was triggered in a form submit
@@ -85,8 +85,8 @@ function tripal_fields_dbxref_id_widget(&$widget, $form, $form_state, $field, $i
   $schema = chado_get_schema('dbxref');
   $options = tripal_get_db_select_options();
 
-  $widget['#element_validate'] = array('tripal_fields_dbxref_id_widget_validate');
-  $widget['#theme'] = 'tripal_fields_dbxref_id_widget';
+  $widget['#element_validate'] = array('tripal_chado_dbxref_id_widget_validate');
+  $widget['#theme'] = 'tripal_chado_dbxref_id_widget';
   $widget['#prefix'] =  "<span id='$field_name-dbxref--db-id'>";
   $widget['#suffix'] =  "</span>";
 
@@ -95,7 +95,7 @@ function tripal_fields_dbxref_id_widget(&$widget, $form, $form_state, $field, $i
     '#title' => $element['#title'],
     '#description' =>  $element['#description'],
     '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
-    '#theme' => 'tripal_fields_dbxref_id_widget',
+    '#theme' => 'tripal_chado_dbxref_id_widget',
     '#collapsible' => TRUE,
     '#collapsed' => $collapsed,
   );
@@ -112,7 +112,7 @@ function tripal_fields_dbxref_id_widget(&$widget, $form, $form_state, $field, $i
     '#required' => $element['#required'],
     '#default_value' => $db_id,
     '#ajax' => array(
-      'callback' => "tripal_fields_dbxref_id_widget_form_ajax_callback",
+      'callback' => "tripal_chado_dbxref_id_widget_form_ajax_callback",
       'wrapper' => "$field_name-dbxref--db-id",
       'effect' => 'fade',
       'method' => 'replace'
@@ -127,7 +127,7 @@ function tripal_fields_dbxref_id_widget(&$widget, $form, $form_state, $field, $i
     '#size' => 15,
     '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
     '#ajax' => array(
-      'callback' => "tripal_fields_dbxref_id_widget_form_ajax_callback",
+      'callback' => "tripal_chado_dbxref_id_widget_form_ajax_callback",
       'wrapper' => "$field_name-dbxref--db-id",
       'effect' => 'fade',
       'method' => 'replace'
@@ -152,12 +152,12 @@ function tripal_fields_dbxref_id_widget(&$widget, $form, $form_state, $field, $i
   );
 }
 /**
- * An Ajax callback for the tripal_fields_admin_publish_form..
+ * An Ajax callback for the tripal_chado_admin_publish_form..
  */
-function tripal_fields_dbxref_id_widget_form_ajax_callback($form, $form_state) {
+function tripal_chado_dbxref_id_widget_form_ajax_callback($form, $form_state) {
   $field_name = $form_state['triggering_element']['#parents'][0];
-  $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
-  $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, 'dbxref__accession');
+  $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__db_id');
+  $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'dbxref__accession');
   if ($db_id and $accession) {
     $values = array(
       'db_id' => $db_id,
@@ -174,9 +174,9 @@ function tripal_fields_dbxref_id_widget_form_ajax_callback($form, $form_state) {
 }
 
 /**
- * Callback function for validating the tripal_fields_organism_select_widget.
+ * Callback function for validating the tripal_chado_organism_select_widget.
  */
-function tripal_fields_dbxref_id_widget_validate($element, &$form_state) {
+function tripal_chado_dbxref_id_widget_validate($element, &$form_state) {
   $field_name = $element['#parents'][0];
 
   // If the form ID is field_ui_field_edit_form, then the user is editing the
@@ -187,11 +187,11 @@ function tripal_fields_dbxref_id_widget_validate($element, &$form_state) {
   }
 
   // Get the field values.
-  $dbxref_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, $field_name);
-  $db_id = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
-  $accession = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
-  $version = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
-  $description = tripal_fields_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
+  $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_name);
+  $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__db_id");
+  $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__accession");
+  $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__version");
+  $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, "dbxref__description");
 
   // Make sure that if a database ID is provided that an accession is also
   // provided.  Here we use the form_set_error function rather than the
@@ -213,7 +213,7 @@ function tripal_fields_dbxref_id_widget_validate($element, &$form_state) {
   // If user did not select a database, we want to remove dbxref_id from the
   // field.
   if (!$db_id) {
-    tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__');
+    tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
   }
   // If the dbxref_id does not match the db_id + accession then the user
   // has selected a new dbxref record and we need to update the hidden
@@ -221,7 +221,7 @@ function tripal_fields_dbxref_id_widget_validate($element, &$form_state) {
   if ($db_id and $accession) {
     $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
     if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
-      tripal_fields_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id);
+      tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id);
     }
   }
 }
@@ -230,7 +230,7 @@ function tripal_fields_dbxref_id_widget_validate($element, &$form_state) {
  *
  * @param $variables
  */
-function theme_tripal_fields_dbxref_id_widget($variables) {
+function theme_tripal_chado_dbxref_id_widget($variables) {
   $element = $variables['element'];
 
   $layout = "
@@ -271,7 +271,7 @@ function theme_tripal_fields_dbxref_id_widget($variables) {
 /**
  * Loads the field values with appropriate data.
  *
- * This function is called by the tripal_fields_field_storage_load() for
+ * This function is called by the tripal_chado_field_storage_load() for
  * each property managed by the field_chado_storage storage type.  This is
  * an optional hook function that is only needed if the field has
  * multiple form elements.
@@ -281,7 +281,7 @@ function theme_tripal_fields_dbxref_id_widget($variables) {
  * @param $base_table
  * @param $record
  */
-function tripal_fields_dbxref_id_field_load($field, $entity, $base_table, $record) {
+function tripal_chado_dbxref_id_field_load($field, $entity, $base_table, $record) {
 
   $field_name = $field['field_name'];
   $field_type = $field['type'];

+ 21 - 21
tripal_fields/includes/fields/kvproperty.inc → tripal_chado/includes/fields/kvproperty.inc

@@ -10,7 +10,7 @@
  * @param unknown $items
  * @param unknown $display
  */
-function tripal_fields_kvproperty_formatter(&$element, $entity_type, $entity, $field,
+function tripal_chado_kvproperty_formatter(&$element, $entity_type, $entity, $field,
   $instance, $langcode, $items, $display) {
 
   $field_name = $field['field_name'];
@@ -39,7 +39,7 @@ function tripal_fields_kvproperty_formatter(&$element, $entity_type, $entity, $f
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_kvproperty_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_chado_kvproperty_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
   $entity = $form['#entity'];
   $field_name = $field['field_name'];
 
@@ -79,7 +79,7 @@ function tripal_fields_kvproperty_widget(&$widget, $form, $form_state, $field, $
   $widget['#field_name'] = $element['#field_name'];
   $widget['#language'] = $element['#language'];
   $widget['#weight'] = isset($element['#weight']) ? $element['#weight'] : 0;
-  $widget['#element_validate'] = array('tripal_fields_kvproperty_widget_validate');
+  $widget['#element_validate'] = array('tripal_chado_kvproperty_widget_validate');
   $widget['#cardinality'] = 1;
 
   $widget['value'] = array(
@@ -110,14 +110,14 @@ function tripal_fields_kvproperty_widget(&$widget, $form, $form_state, $field, $
  * @param unknown $form
  * @param unknown $form_state
  */
-function tripal_fields_kvproperty_widget_form_ajax_callback($form, $form_state) {
+function tripal_chado_kvproperty_widget_form_ajax_callback($form, $form_state) {
   $field_name = $form_state['triggering_element']['#parents'][0];
   return $form[$field_name];
 }
 /**
- * Callback function for validating the tripal_fields_kvproperty_widget.
+ * Callback function for validating the tripal_chado_kvproperty_widget.
  */
-function tripal_fields_kvproperty_widget_validate($element, &$form_state) {
+function tripal_chado_kvproperty_widget_validate($element, &$form_state) {
   $field_name = $element['#field_name'];
   $delta = $element['#delta'];
   $entity = $element['#entity'];
@@ -137,21 +137,21 @@ function tripal_fields_kvproperty_widget_validate($element, &$form_state) {
   // If we don't have a property value then we need to set all other fields
   // to be empty so that when the module tries to save the field on the
   // entity it won't try to save a partial record.
-  $pkey_val = tripal_fields_get_field_form_values($field_name, $form_state, $delta);
-  $prop_value = tripal_fields_get_field_form_values($field_name, $form_state, $delta, $table_name . "__value");
-  $fk_val = tripal_fields_get_field_form_values($field_name, $form_state, $delta, $table_name . '__' . $lfkey_field);
-  $type_id = tripal_fields_get_field_form_values($field_name, $form_state, $delta, $table_name . '__type_id');
+  $pkey_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta);
+  $prop_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . "__value");
+  $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__' . $lfkey_field);
+  $type_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $table_name . '__type_id');
 
   if (!$prop_value) {
-    tripal_fields_set_field_form_values($field_name, $form_state, '', $delta);
-    tripal_fields_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $lfkey_field);
-    tripal_fields_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__value');
-    tripal_fields_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__type_id');
-    tripal_fields_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__rank');
+    tripal_chado_set_field_form_values($field_name, $form_state, '', $delta);
+    tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $lfkey_field);
+    tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__value');
+    tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__type_id');
+    tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__rank');
   }
   else {
-    $rank = tripal_fields_get_field_form_values($field_name, $form_state, $delta, '_weight');
-    tripal_fields_set_field_form_values($field_name, $form_state, $rank, $delta, $table_name . '__rank');
+    $rank = tripal_chado_get_field_form_values($field_name, $form_state, $delta, '_weight');
+    tripal_chado_set_field_form_values($field_name, $form_state, $rank, $delta, $table_name . '__rank');
   }
   // Remove the properties for this record. We will re-add it. Otherwise,
   // if we change ranks, we wind up with multiple records in the property table.
@@ -163,15 +163,15 @@ function tripal_fields_kvproperty_widget_validate($element, &$form_state) {
   }
 }
 /**
- * Callback function for submitting the tripal_fields_kvproperty_widget.
+ * Callback function for submitting the tripal_chado_kvproperty_widget.
  */
-function tripal_fields_kvproperty_widget_submit($element, &$form_state) {
+function tripal_chado_kvproperty_widget_submit($element, &$form_state) {
 }
 
 /**
  * Loads the field values with appropriate data.
  *
- * This function is called by the tripal_fields_field_storage_load() for
+ * This function is called by the tripal_chado_field_storage_load() for
  * each property managed by the field_chado_storage storage type.  This is
  * an optional hook function that is only needed if the field has
  * multiple form elements.
@@ -181,7 +181,7 @@ function tripal_fields_kvproperty_widget_submit($element, &$form_state) {
  * @param $base_table
  * @param $record
  */
-function tripal_fields_kvproperty_field_load($field, $entity, $base_table, $record) {
+function tripal_chado_kvproperty_field_load($field, $entity, $base_table, $record) {
 
   $field_name = $field['field_name'];
   $field_type = $field['type'];

+ 10 - 10
tripal_fields/includes/fields/kvproperty_adder.inc → tripal_chado/includes/fields/kvproperty_adder.inc

@@ -10,7 +10,7 @@
  * @param unknown $items
  * @param unknown $display
  */
-function tripal_fields_kvproperty_adder_formatter(&$element, $entity_type, $entity, $field,
+function tripal_chado_kvproperty_adder_formatter(&$element, $entity_type, $entity, $field,
   $instance, $langcode, $items, $display) {
 
   foreach ($items as $delta => $item) {
@@ -20,10 +20,10 @@ function tripal_fields_kvproperty_adder_formatter(&$element, $entity_type, $enti
 /**
  *
  */
-function tripal_fields_kvproperty_adder_widget(&$widget, $form, $form_state,
+function tripal_chado_kvproperty_adder_widget(&$widget, $form, $form_state,
     $field, $instance, $langcode, $items, $delta, $element) {
   // This field has no value field.  Just a fieldset for adding new properties.
-  $widget['#element_validate'] = array('tripal_fields_kvproperty_adder_widget_validate');
+  $widget['#element_validate'] = array('tripal_chado_kvproperty_adder_widget_validate');
 
   $widget['#type'] = 'fieldset';
   $widget['#title'] = $element['#title'];
@@ -61,14 +61,14 @@ function tripal_fields_kvproperty_adder_widget(&$widget, $form, $form_state,
     '#value' => t('Add Property'),
     '#type' => 'submit',
     '#name' => 'kvproperty_adder_button',
-    '#submit' => array('tripal_fields_kvproperty_adder_widget_submit'),
+    '#submit' => array('tripal_chado_kvproperty_adder_widget_submit'),
     '#limit_validation_errors' => array(array($field['field_name'])),
   );
 }
 /**
- * Callback function for validating the tripal_fields_kvproperty_adder_widget.
+ * Callback function for validating the tripal_chado_kvproperty_adder_widget.
  */
-function tripal_fields_kvproperty_adder_widget_validate($element, &$form_state) {
+function tripal_chado_kvproperty_adder_widget_validate($element, &$form_state) {
 
    // Add the new field to the entity
    if (array_key_exists('triggering_element', $form_state) and
@@ -80,7 +80,7 @@ function tripal_fields_kvproperty_adder_widget_validate($element, &$form_state)
      $bundle = $element['#entity']->bundle;
 
      // Get the term for the property
-     $kvproperty = tripal_fields_get_field_form_values($field_name, $form_state);
+     $kvproperty = tripal_chado_get_field_form_values($field_name, $form_state);
      $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
      if (count($term) == 1) {
        $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
@@ -92,7 +92,7 @@ function tripal_fields_kvproperty_adder_widget_validate($element, &$form_state)
 
        $field_info = array(
          'field_type' => 'kvproperty',
-         'widget_type' => 'tripal_fields_kvproperty_widget',
+         'widget_type' => 'tripal_chado_kvproperty_widget',
          'field_settings' => array(
            'chado_table' => $field_name,
            'chado_column' => $pkey,
@@ -115,7 +115,7 @@ function tripal_fields_kvproperty_adder_widget_validate($element, &$form_state)
    }
 }
 /**
- * Callback function for submitting the tripal_fields_kvproperty_adder_widget.
+ * Callback function for submitting the tripal_chado_kvproperty_adder_widget.
  */
-function tripal_fields_kvproperty_adder_widget_submit($element, &$form_state) {
+function tripal_chado_kvproperty_adder_widget_submit($element, &$form_state) {
 }

+ 10 - 10
tripal_fields/includes/fields/md5checksum.inc → tripal_chado/includes/fields/md5checksum.inc

@@ -10,7 +10,7 @@
  * @param unknown $items
  * @param unknown $display
  */
-function tripal_fields_md5checksum_checkbox_formatter(&$element, $entity_type, $entity, $field,
+function tripal_chado_md5checksum_checkbox_formatter(&$element, $entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {
   foreach ($items as $delta => $item) {
     $content = key_exists('value', $item) ? $item['value'] : '';
@@ -34,7 +34,7 @@ function tripal_fields_md5checksum_checkbox_formatter(&$element, $entity_type, $
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_md5checksum_checkbox_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_chado_md5checksum_checkbox_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
   $widget['value'] = array(
     '#type' => 'checkbox',
@@ -44,39 +44,39 @@ function tripal_fields_md5checksum_checkbox_widget(&$widget, $form, $form_state,
     '#default_value' => 1,
     '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
     '#delta' => $delta,
-    '#element_validate' => array('tripal_fields_md5checksum_checkbox_widget_validate'),
+    '#element_validate' => array('tripal_chado_md5checksum_checkbox_widget_validate'),
   );
 }
 /**
- * Callback function for validating the tripal_fields_md5checksum_checkbox_widget.
+ * Callback function for validating the tripal_chado_md5checksum_checkbox_widget.
  */
-function tripal_fields_md5checksum_checkbox_widget_validate($element, &$form_state) {
+function tripal_chado_md5checksum_checkbox_widget_validate($element, &$form_state) {
   $field_name = $element['#parents'][0];
 
   // Calculate the md5 checksum for the sequence only if md5 box is checked and
   // the residues exist.
-  $residues = tripal_fields_get_field_form_values('feature__residues', $form_state);
+  $residues = tripal_chado_get_field_form_values('feature__residues', $form_state);
   if ($residues) {
     $residues = preg_replace('/\s/', '', $residues);
-    tripal_fields_set_field_form_values($field_name, $form_state, md5($residues));
+    tripal_chado_set_field_form_values($field_name, $form_state, md5($residues));
   }
   else {
     // Otherwise, remove the md5 value
-    tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__');
+    tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
   }
 }
 
 /**
  * Loads the field values with appropriate data.
  *
- * This function is called by the tripal_fields_field_storage_load() for
+ * This function is called by the tripal_chado_field_storage_load() for
  * each property managed by the field_chado_storage storage type.
  *
  * @param $field
  * @param $entity
  * @param $record
  */
-function tripal_fields_md5checksum_field_load($field, $entity, $record) {
+function tripal_chado_md5checksum_field_load($field, $entity, $record) {
   $field_name = $field['field_name'];
   if ($record and property_exists($record, 'md5checksum')) {
     $entity->{$field_name}['und'][] = array('value' => $record->md5checksum);

+ 6 - 6
tripal_fields/includes/fields/organism_id.inc → tripal_chado/includes/fields/organism_id.inc

@@ -11,7 +11,7 @@
  * @param $display
  * @return string
  */
-function tripal_fields_organism_select_formatter(&$element, $entity_type, $entity, $field,
+function tripal_chado_organism_select_formatter(&$element, $entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {
   foreach ($items as $delta => $item) {
     $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
@@ -35,7 +35,7 @@ function tripal_fields_organism_select_formatter(&$element, $entity_type, $entit
  * @param $delta
  * @param $element
  */
-function tripal_fields_organism_select_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_chado_organism_select_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
   $options = tripal_get_organism_select_options(FALSE);
   $widget['value'] = array(
     '#type' => 'select',
@@ -46,7 +46,7 @@ function tripal_fields_organism_select_widget(&$widget, $form, $form_state, $fie
     '#required' => $element['#required'],
     '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
     '#delta' => $delta,
-    '#element_validate' => array('tripal_fields_organism_select_widget_validate'),
+    '#element_validate' => array('tripal_chado_organism_select_widget_validate'),
   );
   $widget['add_organism'] = array(
     '#type' => 'item',
@@ -54,9 +54,9 @@ function tripal_fields_organism_select_widget(&$widget, $form, $form_state, $fie
   );
 }
 /**
- * Callback function for validating the tripal_fields_organism_select_widget.
+ * Callback function for validating the tripal_chado_organism_select_widget.
  */
-function tripal_fields_organism_select_widget_validate($element, &$form_state) {
+function tripal_chado_organism_select_widget_validate($element, &$form_state) {
   $field_name = $element['#parents'][0];
 
   // If the form ID is field_ui_field_edit_form, then the user is editing the
@@ -65,7 +65,7 @@ function tripal_fields_organism_select_widget_validate($element, &$form_state) {
   if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
     return;
   }
-  $organism_id = tripal_fields_get_field_form_values($field_name, $form_state);
+  $organism_id = tripal_chado_get_field_form_values($field_name, $form_state);
 
   if (!$organism_id) {
     form_error($element, t("Please specify an organism."));

+ 7 - 7
tripal_fields/includes/fields/residues.inc → tripal_chado/includes/fields/residues.inc

@@ -10,7 +10,7 @@
  * @param unknown $items
  * @param unknown $display
  */
-function tripal_fields_residues_textarea_formatter(&$element, $entity_type, $entity, $field,
+function tripal_chado_residues_textarea_formatter(&$element, $entity_type, $entity, $field,
   $instance, $langcode, $items, $display) {
 
   foreach ($items as $delta => $item) {
@@ -37,7 +37,7 @@ function tripal_fields_residues_textarea_formatter(&$element, $entity_type, $ent
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_residues_textarea_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_chado_residues_textarea_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
   $widget['value'] = array(
     '#type' => 'textarea',
@@ -46,19 +46,19 @@ function tripal_fields_residues_textarea_widget(&$widget, $form, $form_state, $f
     '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
     '#default_value' => count($items) > 0 ? $items[0]['value'] : '',
     '#delta' => $delta,
-    '#element_validate' => array('tripal_fields_residues_textarea_widget_validate'),
+    '#element_validate' => array('tripal_chado_residues_textarea_widget_validate'),
   );
 }
 /**
- * Callback function for validating the tripal_fields_residues_textarea_widget.
+ * Callback function for validating the tripal_chado_residues_textarea_widget.
  */
-function tripal_fields_residues_textarea_widget_validate($element, &$form_state) {
+function tripal_chado_residues_textarea_widget_validate($element, &$form_state) {
   $field_name = $element['#parents'][0];
 
   // Remove any white spaces.
-  $residues = tripal_fields_get_field_form_values($field_name, $form_state);
+  $residues = tripal_chado_get_field_form_values($field_name, $form_state);
   if ($residues) {
     $residues = preg_replace('/\s/', '', $residues);
-    tripal_fields_set_field_form_values($field_name, $form_state, $residues);
+    tripal_chado_set_field_form_values($field_name, $form_state, $residues);
   }
 }

+ 8 - 8
tripal_fields/includes/fields/seqlen.inc → tripal_chado/includes/fields/seqlen.inc

@@ -10,7 +10,7 @@
  * @param unknown $items
  * @param unknown $display
  */
-function tripal_fields_seqlen_hidden_formatter(&$element, $entity_type, $entity, $field,
+function tripal_chado_seqlen_hidden_formatter(&$element, $entity_type, $entity, $field,
     $instance, $langcode, $items, $display) {
 
   foreach ($items as $delta => $item) {
@@ -36,7 +36,7 @@ function tripal_fields_seqlen_hidden_formatter(&$element, $entity_type, $entity,
  * @param unknown $delta
  * @param unknown $element
  */
-function tripal_fields_seqlen_hidden_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
+function tripal_chado_seqlen_hidden_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
 
   $widget['value'] =  array(
     '#type' => 'hidden',
@@ -44,25 +44,25 @@ function tripal_fields_seqlen_hidden_widget(&$widget, $form, $form_state, $field
     '#description' => $element['#description'],
     '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
     '#delta' => $delta,
-    '#element_validate' => array('tripal_fields_seqlen_hidden_widget_validate'),
+    '#element_validate' => array('tripal_chado_seqlen_hidden_widget_validate'),
   );
 }
 /**
- * Callback function for validating the tripal_fields_seqlen_hidden_widget.
+ * Callback function for validating the tripal_chado_seqlen_hidden_widget.
  */
-function tripal_fields_seqlen_hidden_widget_validate($element, &$form_state) {
+function tripal_chado_seqlen_hidden_widget_validate($element, &$form_state) {
   $field_name = $element['#parents'][0];
 
   // Get the residues so we can calculate teh length.
-  $residues = tripal_fields_get_field_form_values('feature__residues', $form_state);
+  $residues = tripal_chado_get_field_form_values('feature__residues', $form_state);
 
   // Remove any white spaces.
   if ($residues) {
     $residues = preg_replace('/\s/', '', $residues);
-    tripal_fields_set_field_form_values($field_name, $form_state, strlen($residues));
+    tripal_chado_set_field_form_values($field_name, $form_state, strlen($residues));
   }
   else {
     // Otherwise, remove the md5 value
-    tripal_fields_set_field_form_values($field_name, $form_state, '__NULL__');
+    tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__');
   }
 }

+ 232 - 0
tripal_chado/includes/tripal_chado.entity.inc

@@ -0,0 +1,232 @@
+<?php
+
+/**
+ *
+ * Implements hook_entity_load().
+ */
+function tripal_chado_entity_presave($entity, $type) {
+  // Add the Chado table and column to an entity to which
+  // data of this cvterm will map.
+  if ($type == 'TripalEntity') {
+    $bundle_id = $entity->bundle;
+    $bundle = db_select('tripal_bundle', 'tb')
+      ->fields('tb')
+      ->condition('bundle', $bundle_id)
+      ->execute()
+      ->fetchObject();
+
+    $data = unserialize($bundle->data);
+    $entity->chado_table = $data['data_table'];
+    $entity->chado_field = $data['field'];
+
+    // If we have an ID then this entity has been saved and it will
+    // also therefore have a chado_entity record.  We want to
+    // load this record so it is always part of the entity object.
+    if (property_exists($entity, 'id') and $entity->id) {
+      $details = db_select('chado_entity', 'ce')
+        ->fields('ce')
+        ->condition('entity_id', $entity->id)
+        ->execute()
+        ->fetchObject();
+
+      // Add the chado entity details to the entity in case it's needed
+      // downstream (e.g. in field widget construction).
+      $entity->chado_entity_id = $details->chado_entity_id;
+
+      // Add in the record.
+      $schema = chado_get_schema($entity->chado_table);
+      $pkey = $schema['primary key'][0];
+      $entity->chado_record_id = $details->record_id;
+      $entity->chado_record = chado_generate_var($entity->chado_table, array($pkey =>+ $details->record_id));
+    }
+  }
+}
+/**
+ *
+ * @param $entity
+ * @param $type
+ */
+function tripal_chado_entity_postsave($entity, $type) {
+  // Set the title for this entity using the chado data.
+  $title = chado_get_entity_title($entity);
+  $ec = new TripalEntityController($entity->type);
+  $ec->setTitle($entity, $title);
+}
+/**
+ *
+ * Implements hook_entity_load().
+ */
+function tripal_chado_entity_load($entities, $type) {
+
+}
+
+/**
+ *
+ * Implements hook_tripal_bundle_data_alter().
+ */
+function tripal_chado_tripal_bundle_data_alter(&$bundle_data, $bundle_id, $cvterm) {
+
+  // Get the cvterm for this entity type.
+  $bundle_id = $entity->bundle;
+  $cvterm_id = preg_replace('/bio-data_/', $bundle_id);
+  $cvterm = tripal_get_cv(array('cvterm_id' => $cvterm_id));
+
+  // The organism table does not have a type_id so we won't ever find
+  // a record for it in the tripal_cv_defaults table.
+  if ($cvterm->name == 'organism') {
+    $bundle_data = array(
+      'cv_id' => $cvterm->cv_id->cv_id,
+      'cvterm_id' => $cvterm->cvterm_id,
+      'data_table' => 'organism',
+      'type_table' => 'organism',
+      'field' =>  '',
+    );
+  }
+  // The analysis table does not have a type_id so we won't ever find
+  // a record for it in the tripalcv_defaults table.
+  else if ($cvterm->name == 'analysis') {
+    $bundle_data = array(
+      'cv_id' => $cvterm->cv_id->cv_id,
+      'cvterm_id' => $cvterm->cvterm_id,
+      'data_table' => 'analysis',
+      'type_table' => 'analysis',
+      'field' =>  '',
+    );
+  }
+  else if ($cvterm->name == 'project') {
+    $bundle_data = array(
+      'cv_id' => $cvterm->cv_id->cv_id,
+      'cvterm_id' => $cvterm->cvterm_id,
+      'data_table' => 'project',
+      'type_table' => 'project',
+      'field' =>  '',
+    );
+  }
+  else {
+    // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
+    // TABLES.
+    // Look to see if this vocabulary is used as a default for any table.
+    $default = db_select('tripal_cv_defaults', 't')
+      ->fields('t')
+      ->condition('cv_id', $cvterm->cv_id->cv_id)
+      ->execute()
+      ->fetchObject();
+    if ($default) {
+      $bundle_data = array(
+        'cv_id' => $cvterm->cv_id->cv_id,
+        'cvterm_id' => $cvterm->cvterm_id,
+        'data_table' => $default->table_name,
+        'type_table' => $default->table_name,
+        'field' =>  $default->field_name,
+      );
+    }
+  }
+}
+
+/**
+ *
+ * Implements hook_entity_update().
+ */
+function tripal_chado_entity_update($entity, $type) {
+
+}
+
+/**
+ *
+ * Implements hook_entity_delete().
+ */
+function tripal_chado_entity_delete($entity, $type) {
+  $record = db_select('chado_entity', 'ce')
+    ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
+    ->condition('entity_id', $entity->id)
+    ->execute()
+    ->fetchObject();
+
+  if ($record && property_exists($record, 'chado_entity_id')) {
+    // Delete the corresponding record in Chado
+    $table = $record->data_table;
+    $record_id = $record->record_id;
+    chado_delete_record($table, array($table . '_id' => $record_id));
+
+    //Delete the record in the public.chado_entity table
+    $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
+    db_query($sql, array(':id' => $record->chado_entity_id));
+  }
+}
+
+/**
+ * This theme function is meant to override the data_combo theme.
+ *
+ * @param $variables
+ */
+function theme_tripal_chado_date_combo($variables) {
+  $element = $variables['element'];
+  $field = field_info_field($element['#field_name']);
+  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
+
+  // Group start/end items together in fieldset.
+  $fieldset = array(
+    '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
+    '#value' => '',
+    '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
+    '#attributes' => array(),
+    '#children' => $element['#children'],
+    '#attributes' => array('class' => array('collapsible', 'collapsed')),
+  );
+  return theme('fieldset', array('element' => $fieldset));
+}
+
+
+/**
+ * Determines whether the given user has access to a tripal data entity.
+ *
+ * @param $op
+ *   The operation being performed. One of 'view', 'update', 'create', 'delete'
+ *   or just 'edit' (being the same as 'create' or 'update').
+ * @param $entity
+ *   Optionally a tripal data entity or a tripal data type to check access for.
+ *   If nothing is given, access for all types is determined.
+ * @param $account
+ *   The user to check for. Leave it to NULL to check for the global user.
+ * @return boolean
+ *   Whether access is allowed or not.
+ */
+function tripal_chado_entity_access($op, $entity = NULL, $account = NULL) {
+  if (user_access('administer tripal data', $account)) {
+    return TRUE;
+  }
+  if (isset($entity) && $type_name = $entity->type) {
+    $op = ($op == 'view') ? 'view' : 'edit';
+    if (user_access("$op any $type_name data", $account)) {
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
+
+/**
+ * Menu callback to display an entity.
+ *
+ * As we load the entity for display, we're responsible for invoking a number
+ * of hooks in their proper order.
+ *
+ * @see hook_entity_prepare_view()
+ * @see hook_entity_view()
+ * @see hook_entity_view_alter()
+ */
+function tripal_chado_view_entity($entity, $view_mode = 'full') {
+  $content = '';
+  $controller = entity_get_controller($entity->type);
+  $content = $controller->view(array($entity->id => $entity));
+  drupal_set_title($entity->title);
+  return $content;
+}
+
+/**
+ * Menu title callback for showing individual entities
+ */
+function tripal_chado_entity_title($entity){
+  if ($entity) {
+    return $entity->title;
+  }
+}

+ 10 - 10
tripal_fields/includes/tripal_fields.field_storage.inc → tripal_chado/includes/tripal_chado.field_storage.inc

@@ -2,7 +2,7 @@
 /**
  * Implements hook_field_storage_info().
  */
-function tripal_fields_field_storage_info() {
+function tripal_chado_field_storage_info() {
   return array(
     'field_chado_storage' => array(
       'label' => t('Chado storage'),
@@ -17,13 +17,13 @@ function tripal_fields_field_storage_info() {
  *
  * @param $query
  */
-function tripal_fields_field_storage_query($query) {
+function tripal_chado_field_storage_query($query) {
   // TODO: figure out what this function does.
 }
 /**
  * Implements hook_field_storage_write().
  */
-function tripal_fields_field_storage_write($entity_type, $entity, $op, $fields) {
+function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
 
   // Get the base table, type field and record_id from the entity.
   $base_table = $entity->chado_table;
@@ -31,10 +31,10 @@ function tripal_fields_field_storage_write($entity_type, $entity, $op, $fields)
   $record_id  = property_exists($entity, 'chado_record_id') ? $entity->chado_record_id : NULL;
 
   // Convert the fields into a key/value list of fields and their values.
-  $field_vals = tripal_fields_field_storage_unnest_fields($fields, $entity_type, $entity);
+  $field_vals = tripal_chado_field_storage_unnest_fields($fields, $entity_type, $entity);
 
   // Recursively write fields for the base table.
-  $record_id = tripal_fields_field_storage_write_recursive($entity_type, $entity,
+  $record_id = tripal_chado_field_storage_write_recursive($entity_type, $entity,
       $op, $field_vals, $base_table, $base_table, $type_field, $record_id);
 
   // If this is an insert then add the chado_entity record.
@@ -70,7 +70,7 @@ function tripal_fields_field_storage_write($entity_type, $entity, $op, $fields)
 
         // Iterate through each record.
         foreach ($field_vals[$field_name] as $delta => $fvals) {
-          tripal_fields_field_storage_write_recursive($entity_type, $entity,
+          tripal_chado_field_storage_write_recursive($entity_type, $entity,
               $op, $fvals, $base_table, $field_table);
         }
       }
@@ -92,7 +92,7 @@ function tripal_fields_field_storage_write($entity_type, $entity, $op, $fields)
  * @return
  *   The record_id of the table if a matching record exists.
  */
-function tripal_fields_field_storage_write_recursive($entity_type, $entity,
+function tripal_chado_field_storage_write_recursive($entity_type, $entity,
    $op, $field_vals, $base_table, $tablename, $type_field = NULL,
    $record_id = NULL, $depth = 0) {
 
@@ -153,7 +153,7 @@ function tripal_fields_field_storage_write_recursive($entity_type, $entity,
 
       // Recurse on the FK field.  Pass in the ID for the FK field if one
       // exists in the $field_vals;
-      $fk_val = tripal_fields_field_storage_write_recursive($entity_type,
+      $fk_val = tripal_chado_field_storage_write_recursive($entity_type,
         $entity, $op, $fk_vals, $base_table, $fk_table, NULL, $fk_val, $depth + 1);
       if (isset($fk_val) and $fk_val != '' and $fk_val != 0) {
         $values[$local_id] = $fk_val;
@@ -232,7 +232,7 @@ function tripal_fields_field_storage_write_recursive($entity_type, $entity,
  * Responsible for loading the fields from the Chado database and adding
  * their values to the entity.
  */
-function tripal_fields_field_storage_load($entity_type, $entities, $age,
+function tripal_chado_field_storage_load($entity_type, $entities, $age,
     $fields, $options) {
 
   $load_current = $age == FIELD_LOAD_CURRENT;
@@ -332,7 +332,7 @@ function tripal_fields_field_storage_load($entity_type, $entities, $age,
  *
  * @param $fields
  */
-function tripal_fields_field_storage_unnest_fields($fields, $entity_type, $entity) {
+function tripal_chado_field_storage_unnest_fields($fields, $entity_type, $entity) {
   $new_fields = array();
 
   // Iterate through all of the fields.

+ 0 - 0
tripal_fields/theme/css/tripal_fields.css → tripal_chado/theme/css/tripal_chado.css


+ 3 - 2
tripal_fields/tripal_fields.info → tripal_chado/tripal_chado.info

@@ -1,11 +1,12 @@
-name = Tripal Fields
+name = Tripal Chado
 description = Provides a set of Chado-based fields for the Tripal Entities.
 core = 7.x
 project = tripal
 package = Tripal
 version = 7.x-2.0
 
-stylesheets[all][] = theme/css/tripal_fields.css
+stylesheets[all][] = theme/css/tripal_chado.css
 
 dependencies[] = tripal_core
+dependencies[] = tripal_entities
 dependencies[] = date

+ 152 - 0
tripal_chado/tripal_chado.install

@@ -0,0 +1,152 @@
+<?php
+
+function tripal_chado_install() {
+  // Unfortunately, some Chado base tables do not have a type_id, so we must
+  // take special action for those tables.  These include: organism and
+  // analysis. Until we can find an appropriate controlled vocabulary
+  // that is well supported by the community with types for these tables we
+  // will have to use in-house terms.
+
+  // Add a term to be used for an inherent 'type_id' for the organism table.
+  tripal_insert_cvterm(array(
+    'id' => 'local:organism',
+    'name' => 'organism',
+    'definition' => 'An individual form of life, such as a bacterium, protist, ' .
+    'fungus, plant, or animal, composed of a single cell or a complex of cells  ' .
+    'in which organelles or organs work together to carry out the various  ' .
+    'processes of life. (American Heritage® Dictionary of the English ' .
+    'Language, Fifth Edition. Copyright © 2011 by Houghton Mifflin ' .
+    'Harcourt Publishing Company).',
+    'cv_name' => 'local',
+  ));
+
+  // Add a term to be used for an inherent 'type_id' for the organism table.
+  tripal_insert_cvterm(array(
+    'id' => 'local:analysis',
+    'name' => 'analysis',
+    'definition' => 'A process as a method of studying the nature of something ' .
+    'or of determining its essential features and their relations. ' .
+    '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
+    'Dictionaries Ltd).',
+    'cv_name' => 'local',
+  ));
+
+  tripal_insert_cvterm(array(
+    'id' => 'local:project',
+    'name' => 'project',
+    'definition' => 'A plan or proposal for accomplishing something. ' .
+    '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
+    'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
+    'cv_name' => 'local',
+  ));
+
+  // We want to provide a set of commonly used entity types by default. This
+  // way when a user first installs Tripal there are some commonly used
+  // formats.
+  module_load_include('inc', 'tripal_entities', 'api/tripal_entities.api');
+  module_load_include('inc', 'tripal_entities', 'includes/tripal_entities.admin');
+
+  // Create the 'Organism' entity type. This uses the local:organism term.
+  $error = '';
+  $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
+  $cvterm = chado_generate_var('cvterm', $term);
+  if (!tripal_create_entity_type($cvterm, $error)) {
+    throw new Exception($error);
+  }
+
+  // Create the 'Analysis' entity type. This uses the local:analysis term.
+  $error = '';
+  $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
+  $cvterm = chado_generate_var('cvterm', $term);
+  if (!tripal_create_entity_type($cvterm, $error)) {
+    throw new Exception($error);
+  }
+
+  // Create the 'Project' entity type. This uses the local:project term.
+  $error = '';
+  $term = array('name' => 'project', 'cv_id' => array('name' => 'local'));
+  $cvterm = chado_generate_var('cvterm', $term);
+  if (!tripal_create_entity_type($cvterm, $error)) {
+    throw new Exception($error);
+  }
+}
+
+
+/**
+ * Implements hook_schema().
+ */
+function tripal_chado_schema() {
+
+  // Links TripalEntity entities to the chado record.
+  $schema['chado_entity'] = tripal_chado_chado_entity_schema();
+
+  return $schema;
+}
+
+/**
+ * @section
+ * Schema Definitions.
+ */
+
+
+/**
+ * Links Biological Data Entities to the chado "base" table the data is stored in.
+ * This is where we would specify that a particular gene maps to the record in the
+ * chado.feature table with a feature_id=2432;
+ */
+function tripal_chado_chado_entity_schema() {
+
+  $schema = array(
+    'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
+    'fields' => array(
+      'chado_entity_id' => array(
+        'description' => 'The primary identifier for this table.',
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'entity_id' => array(
+        'description' => 'The unique entity id.',
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'record_id' => array(
+        'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'data_table' => array(
+        'description' => 'Indicates the table in Chado that this term services (e.g. feature, stock, library, etc.)',
+        'type' => 'varchar',
+        'length' => 128,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'type_table' => array(
+        'description' => 'Sometimes the record in the data table doesn’t have a field that specifies  the record type.  For example, an analysis type is stored in the analysisprop table.  If the data_table does have a type field then this value will be the same as the data_table.',
+        'type' => 'varchar',
+        'length' => 128,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'field' => array(
+        'description' => 'The name of the field in the typetable that contains the cvterm record.',
+        'type' => 'varchar',
+        'length' => 128,
+        'not null' => FALSE,
+        'default' => ''
+      ),
+    ),
+    'indexes' => array(
+      'record_id' => array('record_id'),
+      'entity_id' => array('entity_id'),
+      'data_table' => array('data_table'),
+    ),
+    'unique keys' => array(
+      'record' => array('data_table', 'record_id'),
+      'entity_id' => array('entity_id'),
+    ),
+    'primary key' => array('chado_entity_id'),
+  );
+  return $schema;
+}

+ 714 - 0
tripal_chado/tripal_chado.module

@@ -0,0 +1,714 @@
+<?php
+
+require_once "api/tripal_chado.api.inc";
+require_once "includes/tripal_chado.field_storage.inc";
+
+/**
+ * Implements hook_field_info().
+ */
+function tripal_chado_field_info() {
+  $fields = array(
+    'organism_id' => array(
+      'label' => t('Organism'),
+      'description' => t('A field for specifying an organism.'),
+      'default_widget' => 'tripal_chado_organism_select_widget',
+      'default_formatter' => 'tripal_chado_organism_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    ),
+    'dbxref_id' => array(
+      'label' => t('Cross-reference'),
+      'description' => t('This record can be cross-referenced with a record in
+          another online database. This field is intended for the most prominent
+          reference.  At a minimum, the database and accession must be provided.'),
+      'default_widget' => 'tripal_chado_dbxref_id_widget',
+      'default_formatter' => 'tripal_chado_dbxref_id_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    ),
+    'residues' => array(
+      'label' => t('Residues'),
+      'description' => t('A field for managing nucleotide and protein residues.'),
+      'default_widget' => 'tripal_chado_residue_textarea_widget',
+      'default_formatter' => 'tripal_chado_residues_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    ),
+    'md5checksum' => array(
+      'label' => t('MD5 checksum'),
+      'description' => t('A field for generating MD5 checksum for a sequence.'),
+      'default_widget' => 'tripal_chado_md5checksum_checkbox_widget',
+      'default_formatter' => 'tripal_chado_md5checksum_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    ),
+    'seqlen' => array(
+      'label' => t('Sequence length'),
+      'description' => t('A field for calculating the length of a sequence.'),
+      'default_widget' => 'tripal_chado_seqlen_hidden_widget',
+      'default_formatter' => 'tripal_chado_seqlen_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    ),
+    // The field provides a widget for adding new properties
+    // to an entity that is connected to a base table that has a prop table
+    // in Chado.
+    'kvproperty_adder' => array(
+      'label' => t('Add a Property Type'),
+      'description' => t('This record may have any number of properties. Use
+          this field to first add the type.'),
+      'default_widget' => 'tripal_chado_kvproperty_adder_widget',
+      'default_formatter' => 'tripal_chado_kvproperty_adder_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    ),
+    // The field provides form elements for adding a property to an entity
+    // that in turn gets stored in a prop table of Chado (e.g. featureprop,
+    // stockprop, etc).
+    'kvproperty' => array(
+      'label' => t('Add a Property'),
+      'description' => t('Add details about this property.'),
+      'default_widget' => 'tripal_chado_kvproperty_widget',
+      'default_formatter' => 'tripal_chado_kvproperty_formatter',
+      'settings' => array(),
+      'storage' => array(
+        'type' => 'field_chado_storage',
+        'module' => 'tripal_chado',
+        'active' => TRUE
+      ),
+    ),
+  );
+  return $fields;
+}
+
+/**
+ * Implements hook_field_widget_info().
+ */
+function tripal_chado_field_widget_info() {
+  return array(
+    'tripal_chado_organism_select_widget' => array(
+      'label' => t('Organism Select'),
+      'field types' => array('organism_id')
+    ),
+    'tripal_chado_dbxref_id_widget' => array(
+      'label' => t('Cross-reference'),
+      'field types' => array('dbxref_id'),
+      'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference.  At a minimum, the database and accession must be provided.'),
+    ),
+    'tripal_chado_md5checksum_checkbox_widget' => array(
+      'label' => t('MD5 Checksum Checkbox'),
+      'field types' => array('md5checksum'),
+    ),
+    'tripal_chado_residues_textarea_widget' => array(
+      'label' => t('Residues'),
+      'field types' => array('residues'),
+    ),
+    'tripal_chado_seqlen_hidden_widget' => array(
+      'label' => t('Sequence Length'),
+      'field types' => array('seqlen'),
+    ),
+    'tripal_chado_kvproperty_adder_widget' => array(
+      'label' => t('Add a Property'),
+      'field types' => array('kvproperty_adder'),
+    ),
+    'tripal_chado_kvproperty_widget' => array(
+      'label' => t('Property'),
+      'field types' => array('kvproperty'),
+    ),
+  );
+}
+/**
+ * Implements hook_field_formatter_info().
+ */
+function tripal_chado_field_formatter_info() {
+  return array(
+    'tripal_chado_organism_formatter' => array(
+      'label' => t('Organism'),
+      'field types' => array('organism_id')
+    ),
+    'tripal_chado_dbxref_id_formatter' => array(
+      'label' => t('Cross-reference'),
+      'field types' => array('dbxref_id')
+    ),
+    'tripal_chado_md5checksum_formatter' => array(
+      'label' => t('MD5 checksum'),
+      'field types' => array('md5checksum')
+    ),
+    'tripal_chado_residues_formatter' => array(
+      'label' => t('Residues'),
+      'field types' => array('residues')
+    ),
+    'tripal_chado_seqlen_formatter' => array(
+      'label' => t('Sequence length'),
+      'field types' => array('seqlen')
+    ),
+    'tripal_chado_kvproperty_adder_formatter' => array(
+      'label' => t('Add a Property'),
+      'field types' => array('kvproperty_adder')
+    ),
+    'tripal_chado_kvproperty_formatter' => array(
+      'label' => t('Property'),
+      'field types' => array('kvproperty')
+    ),
+  );
+}
+
+/**
+ * Implements hook_chado_field_alter().
+ *
+ * This function is used to change the default field formatter and widget
+ * that are assigned to fields of an Entity. This hook is only used for
+ * those fields that correspond to a column in a Chado table.  An implementation
+ * of this hook can be used to change the default formatters and widgets to
+ * custom formatters and widgets that are created by the module creating
+ * this hook.
+ *
+ * By default, Tripal will provide custom formatters and widgets for many
+ * columns in Chado tables, therefore, this hook will most likely be of use
+ * to extension modules that create custom table inside of Chado.
+ *
+ * @param $field
+ */
+function hook_chado_field_alter(&$field) {
+  // TODO: add example code for how to use this hook.
+}
+
+/**
+ * Implements hook_chado_field_alter().
+ *
+ * This function adds the custom formatters and widgets to many of the Chado
+ * tables.  This way Tripal users get a nice set of already usable fields.
+ */
+function tripal_chado_chado_field_alter(&$field) {
+
+  if (!array_key_exists('field_settings', $field)) {
+    return;
+  }
+  // If the field doesn't list the Chado table or column then just return.
+  if (!array_key_exists('chado_table', $field['field_settings']) or
+      !array_key_exists('chado_column', $field['field_settings'])) {
+    return;
+  }
+  // Here we provide new field types and widgets for FK fields
+  // and fields that need special attention.
+  if ($field['field_settings']['chado_column'] =='organism_id') {
+    $field['field_type'] = 'organism_id';
+    $field['widget_type'] = 'tripal_chado_organism_select_widget';
+    $field['label'] = 'Organism';
+    $field['description'] = 'Select an organism.';
+  }
+  else if ($field['field_settings']['chado_column'] =='dbxref_id') {
+    $field['field_type'] = 'dbxref_id';
+    $field['widget_type'] = 'tripal_chado_primary_dbxref_widget';
+    $field['label'] = 'Primary Cross Reference';;
+    $field['description'] = 'This record can be cross-referenced with a
+    record in another online database. The primary reference is for the
+    most prominent reference.  At a minimum, the database and accession
+    must be provided.  To remove a set reference, change the database
+    field to "Select a Database".';
+  }
+  else if ($field['field_settings']['chado_table'] == 'feature' and
+    $field['field_settings']['chado_column'] == 'md5checksum') {
+    $field['field_type'] = 'md5checksum';
+    $field['widget_type'] = 'tripal_chado_md5checksum_checkbox_widget';
+    $field['label'] = 'MD5 Checksum';
+    $field['description'] = 'Generating MD5 checksum for the sequence.';
+  }
+  else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
+    $field['field_type'] = 'seqlen';
+    $field['widget_type'] = 'tripal_chado_seqlen_hidden_widget';
+    $field['label'] = 'Seqlen';
+    $field['description'] = 'The length of the residues.';
+  }
+  else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
+    $field['field_type'] = 'residues';
+    $field['widget_type'] = 'tripal_chado_residues_textarea_widget';
+    $field['label'] = 'Residues';
+    $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
+  }
+  else if ($field['label'] == 'Timeaccessioned') {
+    $field['label'] = 'Time Accessioned';
+    $field['description'] = 'Please enter the time that this record was first added to the database.';
+  }
+  else if ($field['label'] == 'Timelastmodified') {
+    $field['label'] = 'Time Last Modified';
+    $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
+  }
+}
+
+/**
+ * Implements hook_field_widget_form_alter().
+ */
+function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
+
+  if (array_key_exists('#field_name', $element)) {
+    $field_name = $element['#field_name'];
+    $matches = array();
+
+    if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
+      $tablename = $matches[1];
+      $colname = $matches[2];
+      $schema = chado_get_schema($tablename);
+
+      // The timelastmodified field exists in many Chado tables.  We want
+      // the form element to update to the most recent time rather than the time
+      // in the database.
+      if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
+        // We want the default value for the field to be the current time.
+        $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
+        $element['#date_items']['value'] = $element['#default_value']['value'];
+      }
+      // We want the date combo fieldset to be collaspible so we will
+      // add our own theme_wrapper to replace the one added by the date
+      // module.
+      if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
+        $element['#theme_wrappers'] = array('tripal_chado_date_combo');
+      }
+    }
+  }
+}
+
+/**
+ * Implements hook_field_formatter_view().
+ */
+function tripal_chado_field_formatter_view($entity_type, $entity, $field,
+    $instance, $langcode, $items, $display) {
+
+      $element = array();
+      switch ($display['type']) {
+        case 'tripal_chado_organism_formatter':
+          module_load_include('inc', 'tripal_chado', 'includes/fields/organism_id');
+          tripal_chado_organism_select_formatter($element, $entity_type, $entity, $field,
+              $instance, $langcode, $items, $display);
+          break;
+        case 'tripal_chado_dbxref_id_formatter':
+          module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref_id');
+          tripal_chado_dbxref_id_formatter($element, $entity_type, $entity, $field,
+              $instance, $langcode, $items, $display);
+          break;
+        case 'tripal_chado_md5checksum_formatter':
+          module_load_include('inc', 'tripal_chado', 'includes/fields/md5checksum');
+          tripal_chado_md5checksum_checkbox_formatter($element, $entity_type, $entity, $field,
+              $instance, $langcode, $items, $display);
+          break;
+        case 'tripal_chado_residues_formatter':
+          module_load_include('inc', 'tripal_chado', 'includes/fields/residues');
+          tripal_chado_residues_textarea_formatter($element, $entity_type, $entity, $field,
+              $instance, $langcode, $items, $display);
+          break;
+        case 'tripal_chado_seqlen_formatter':
+          module_load_include('inc', 'tripal_chado', 'includes/fields/seqlen');
+          tripal_chado_seqlen_hidden_formatter($element, $entity_type, $entity, $field,
+              $instance, $langcode, $items, $display);
+          break;
+        case 'tripal_chado_kvproperty_adder_formatter':
+          module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
+          tripal_chado_kvproperty_adder_formatter($element, $entity_type, $entity, $field,
+              $instance, $langcode, $items, $display);
+          break;
+        case 'tripal_chado_kvproperty_formatter':
+          module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty');
+          tripal_chado_kvproperty_formatter($element, $entity_type, $entity, $field,
+              $instance, $langcode, $items, $display);
+          break;
+      }
+      return $element;
+}
+
+/**
+ * Implements hook_field_widget_form().
+ */
+function tripal_chado_field_widget_form(&$form, &$form_state, $field,
+    $instance, $langcode, $items, $delta, $element) {
+
+  $widget = $element;
+  switch ($instance['widget']['type']) {
+    case 'tripal_chado_organism_select_widget':
+      // Make sure the include files get parsed now and for the form submits.
+      form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/organism_id');
+      module_load_include('inc', 'tripal_chado', 'includes/fields/organism_id');
+      // Update the widget with the new field.
+      tripal_chado_organism_select_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      break;
+    case 'tripal_chado_dbxref_id_widget':
+      form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/dbxref_id');
+      module_load_include('inc', 'tripal_chado', 'includes/fields/dbxref_id');
+      tripal_chado_dbxref_id_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      break;
+    case 'tripal_chado_md5checksum_checkbox_widget':
+      form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/md5checksum');
+      module_load_include('inc', 'tripal_chado', 'includes/fields/md5checksum');
+      tripal_chado_md5checksum_checkbox_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      break;
+    case 'tripal_chado_residues_textarea_widget':
+      form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/residues');
+      module_load_include('inc', 'tripal_chado', 'includes/fields/residues');
+      tripal_chado_residues_textarea_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      break;
+    case 'tripal_chado_seqlen_hidden_widget':
+      form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/seqlen');
+      module_load_include('inc', 'tripal_chado', 'includes/fields/seqlen');
+      tripal_chado_seqlen_hidden_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      break;
+    case 'tripal_chado_kvproperty_adder_widget':
+      form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
+      module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty_adder');
+      tripal_chado_kvproperty_adder_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      break;
+    case 'tripal_chado_kvproperty_widget':
+      form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/kvproperty');
+      module_load_include('inc', 'tripal_chado', 'includes/fields/kvproperty');
+      tripal_chado_kvproperty_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+      break;
+  }
+  return $widget;
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * The field_ui_display_overview_form is used for formatting the display
+ * or layout of fields attached to an entity and shown on the entity view page.
+ */
+function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
+  // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
+  // on the add/edit form of an entity for adding new property fields.
+  $fields_names = element_children($form['fields']);
+  foreach ($fields_names as $field_name) {
+    $field_info = field_info_field($field_name);
+    if ($field_info['type'] == 'kvproperty_adder') {
+      unset($form['fields'][$field_name]);
+    }
+  }
+}
+/**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * The field_ui_field_overview_form is used for ordering and configuring the
+ * fields attached to an entity.
+ */
+function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
+  // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
+  // on the add/edit form of an entity for adding new property fields.
+  $fields_names = element_children($form['fields']);
+  foreach ($fields_names as $field_name) {
+    $field_info = field_info_field($field_name);
+    if ($field_info['type'] == 'kvproperty_adder') {
+      unset($form['fields'][$field_name]);
+    }
+  }
+}
+/**
+ * Implements hook_field_is_empty().
+ */
+function tripal_chado_field_is_empty($item, $field) {
+
+  // If there is no value field then the field is empty.
+  if (!array_key_exists('value', $item)) {
+    return TRUE;
+  }
+
+  // Iterate through all of the fields and if at least one has a value
+  // the field is not empty.
+  foreach ($item as $form_field_name => $value) {
+    if (isset($value) and $value != NULL and $value != '') {
+      return FALSE;
+    }
+  }
+
+  // Otherwise, the field is empty.
+  return TRUE;
+}
+
+/**
+ * Returns the values of the field from the $form_state.
+ */
+function tripal_chado_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
+  $value = NULL;
+  // The form_state must have the 'values' key. If not then just return.
+  if (!array_key_exists('values', $form_state)) {
+    return $value;
+  }
+
+  // If the field name is not in the form_state['values'] then return.
+  if (!array_key_exists($field_name, $form_state['values'])) {
+    return $value;
+  }
+
+  // Iterate through the values looking for the field_name provided.
+  foreach ($form_state['values'][$field_name] as $langcode => $items) {
+    $item = $items[$delta];
+    if ($child){
+      if(array_key_exists($child, $item) and $item[$child] != '') {
+        $value = $item[$child];
+      }
+    }
+    else {
+      $value = $item['value'];
+    }
+  }
+  return $value;
+}
+
+/**
+ * Sets the values of the field from the $form_state.
+ */
+function tripal_chado_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
+  // The form_state must have the 'values' key. If not then just return.
+  if (!array_key_exists('values', $form_state)) {
+    return FALSE;
+  }
+
+  // If the field name is not in the form_state['values'] then reutrn.
+  if (!array_key_exists($field_name, $form_state['values'])) {
+    return FALSE;
+  }
+
+  foreach ($form_state['values'][$field_name] as $langcode => $items) {
+    if ($child) {
+      $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
+    }
+    else {
+      $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
+    }
+  }
+  return TRUE;
+}
+
+
+/**
+ * Implements hook_theme().
+ */
+function tripal_chado_theme($existing, $type, $theme, $path) {
+  return array(
+    'tripal_chado_dbxref_id_widget' => array(
+      'render element' => 'element',
+      'file' => 'includes/fields/dbxref_id.inc',
+    ),
+    'tripal_chado_kvproperty_addr_widget' => array(
+      'render element' => 'element',
+      'file' => 'includes/fields/dbxref_id.inc',
+    ),
+    'tripal_chado_date_combo' => array(
+      'render element' => 'element',
+      'file' => 'includes/tripal_chado.entity.inc',
+    ),
+  );
+}
+
+
+/**
+ * Implements hook_add_bundle_fields().
+ *
+ * @param $entity_type_name
+ * @param $bundle_id
+ * @param $cvterm
+ */
+function tripal_chado_add_bundle_fields($entity_type_name, $bundle_id, $cvterm) {
+  // Adds the fields for the base table to the entity.
+  tripal_chado_add_bundle_base_fields($entity_type_name, $bundle_id, $cvterm);
+
+  // Check to see if there are any kv-property tables associated to this
+  // base table. If so, add the fields for that type of table.
+  tripal_chado_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_id, 'featureprop');
+}
+
+/**
+ * Adds the fields for a kv-property table fields
+ *
+ * @param $entity_type_name
+ * @param $bundle_id
+ * @param $kv_table
+ */
+function tripal_chado_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_id, $kv_table) {
+  // First add a generic property field so that users can add new proeprty types.
+  $field_name = $kv_table;
+
+  // Initialize the field array.
+  $field_info = array(
+    'field_type' => 'kvproperty_adder',
+    'widget_type' => 'tripal_fields_kvproperty_adder_widget',
+    'field_settings' => array(),
+    'widget_settings' => array('display_label' => 1),
+    'description' => '',
+    'label' => 'Additional Properties',
+    'is_required' => 0,
+  );
+  tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_id);
+}
+
+/**
+ * Adds the fields for the base table to the entity.
+ */
+function tripal_chado_add_bundle_base_fields($entity_type_name, $bundle_id, $cvterm) {
+
+  // Get the details for this bundle
+  $bundle = db_select('tripal_bundle', 't')
+    ->fields('t')
+    ->condition('type', 'TripalEntity')
+    ->condition('bundle', $bundle_id)
+    ->execute()
+    ->fetchObject();
+  $bundle_data = unserialize($bundle->data);
+
+  $table_name = $bundle_data['data_table'];
+  $type_table = $bundle_data['type_table'];
+  $type_field = $bundle_data['field'];
+
+  // Iterate through the columns of the table and see if fields have been
+  // created for each one. If not, then create them.
+  $schema = chado_get_schema($table_name);
+  $columns = $schema['fields'];
+  foreach ($columns as $column_name => $details) {
+    $field_name = $table_name . '__' . $column_name;
+
+    // Skip the primary key field.
+    if ($column_name == $schema['primary key'][0]) {
+      continue;
+    }
+
+    // Skip the type field.
+    if ($table_name == $type_table and $column_name == $type_field) {
+      continue;
+    }
+
+    // Get the field defaults for this column.
+    $field_info = tripal_chado_get_table_column_field_default($table_name, $schema, $column_name);
+
+
+    // Determine if the field is required.
+    if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
+      $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
+    }
+
+    // If we don't have a field type then we don't need to create a field.
+    if (!$field_info['field_type']) {
+      // If we don't have a field type but it is required and doesn't have
+      // a default value then we are in trouble.
+      if ($field_info['is_required'] and !array_key_exists('default', $details)) {
+        throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
+            array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
+      }
+      continue;
+    }
+
+    // If this field is a foreign key field then we will have a special custom
+    // field provided by Tripal.
+    $is_fk = FALSE;
+    if (array_key_exists('foreign keys', $schema)) {
+      foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
+        if (array_key_exists($column_name, $fk_details['columns'])) {
+          $is_fk = TRUE;
+        }
+      }
+    }
+
+    // Add the field to the bundle.
+    tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_id);
+  }
+}
+
+/**
+ * Returns a $field_info array for a field based on a database column.
+ *
+ */
+function tripal_chado_get_table_column_field_default($table_name, $schema, $column_name) {
+  $details = $schema['fields'][$column_name];
+
+  // Create an array with information about this field.
+  $field_info = array(
+    'field_type' => '',
+    'widget_type' => '',
+    'field_settings' => array(
+      'chado_table' => $table_name,
+      'chado_column' => $column_name,
+    ),
+    'widget_settings' => array('display_label' => 1),
+    'description' => '',
+    'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
+    'is_required' => 0,
+  );
+
+  // Alter the field info array depending on the column details.
+  switch($details['type']) {
+    case 'char':
+      $field_info['field_type'] = 'text';
+      $field_info['widget_type'] = 'text_textfield';
+      $field_info['field_settings']['max_length'] = $details['length'];
+      break;
+    case 'varchar':
+      $field_info['field_type'] = 'text';
+      $field_info['widget_type'] = 'text_textfield';
+      $field_info['field_settings']['max_length'] = $details['length'];
+      break;
+    case 'text':
+      $field_info['field_type'] = 'text';
+      $field_info['widget_type'] = 'text_textarea';
+      $field_info['field_settings']['max_length'] = 17179869184;
+      break;
+    case 'blob':
+      // not sure how to support a blob field.
+      continue;
+      break;
+    case 'int':
+      $field_info['field_type'] = 'number_integer';
+      $field_info['widget_type'] = 'number';
+      break;
+    case 'float':
+      $field_info['field_type'] = 'number_float';
+      $field_info['widget_type'] = 'number';
+      $field_info['field_settings']['precision'] = 10;
+      $field_info['field_settings']['scale'] = 2;
+      $field_info['field_settings']['decimal_separator'] = '.';
+      break;
+    case 'numeric':
+      $field_info['field_type'] = 'number_decimal';
+      $field_info['widget_type'] = 'number';
+      break;
+    case 'serial':
+      // Serial fields are most likely not needed as a field.
+      break;
+    case 'boolean':
+      $field_info['field_type'] = 'list_boolean';
+      $field_info['widget_type'] = 'options_onoff';
+      $field_info['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
+      break;
+    case 'datetime':
+      // Use the Drupal Date and Date API to create the field/widget
+      $field_info['field_type'] = 'datetime';
+      $field_info['widget_type'] = 'date_select';
+      $field_info['widget_settings']['increment'] = 1;
+      $field_info['widget_settings']['tz_handling'] = 'none';
+      $field_info['widget_settings']['collapsible'] = TRUE;
+
+      // TODO: Add settings so that the minutes increment by 1.
+      // And turn off the timezone, as the Chado field doesn't support it.
+      break;
+  }
+
+  return $field_info;
+}

+ 2 - 1
tripal_cv/api/tripal_cv.api.inc

@@ -174,7 +174,7 @@ function tripal_get_cvterm($identifiers, $options = array()) {
   // Set Defaults
   if (!isset($options['include_fk'])) {
     // Tells chado_generate_var to only get the cv
-    $options['include_fk'] = array('cv_id' => TRUE);
+    //$options['include_fk'] = array('cv_id' => TRUE);
   }
 
   // Error Checking of parameters
@@ -236,6 +236,7 @@ function tripal_get_cvterm($identifiers, $options = array()) {
   else {
     // Try to get the cvterm
     $cvterm = chado_generate_var('cvterm', $identifiers, $options);
+    $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
   }
 
   // Ensure the cvterm is singular. If it's an array then it is not singular

+ 27 - 115
tripal_entities/api/tripal_entities.api.inc

@@ -1,62 +1,11 @@
 <?php
 
-/**
- * TODO: The code for creating the title needs to be updated to not
- * use nodes but rather entities.
- *
- * @param unknown $node
- * @return mixed
- */
-function chado_get_entity_title($entity) {
-
-  // Get the base table for the entity
-  $details = db_select('chado_entity', 'ce')
-    ->fields('ce')
-    ->condition('entity_id', $entity->id)
-    ->execute()
-    ->fetchObject();
-
-  $tablename = $details->data_table;
-  $type_field = $details->field;
-  $schema = chado_get_schema($tablename);
-  $pkey_field = $schema['primary key'][0];
-  $record_id = $details->record_id;
-
-  $record = chado_generate_var($tablename, array($pkey_field => $record_id));
-
-  // TODO: fix this so it's native for entities and doesn't expect nodes.
-  // Fake a node
-  $node = new stdClass();
-  $node->$tablename = $record;
-
-  // Get the tokens and format
-  $tokens = array(); // this will be set by chado_node_get_title_format
-  $title = chado_node_get_title_format('chado_' . $tablename, $tokens);
-
-  // Determine which tokens were used in the format string
-  if (preg_match_all('/\[[^]]+\]/', $title, $used_tokens)) {
-
-    // Get the value for each token used
-    foreach ($used_tokens[0] as $token) {
-      $token_info = $tokens[$token];
-      if (!empty($token_info)) {
-        $value = chado_get_token_value($token_info, $node);
-        $title = str_replace($token, $value, $title);
-      }
-    }
-  }
-  else {
-    return $title;
-  }
-
-  return $title;
-}
 
 /**
  * Creates a new Tripal Entity type (i.e. bundle).
  *
  * @param $cvterm
- *  A cvterm object created using the chado_generate_var() function.
+ *  A cvterm object created using the tripal_get_cvterm() function.
  * @param $error
  *  A string, passed by reference, that is filled with the error message
  *  if the function fails.
@@ -66,71 +15,8 @@ function chado_get_entity_title($entity) {
  */
 function tripal_create_entity_type($cvterm, &$error = '') {
 
-  // Create the bundle name and entity type name.  The bundle name is the
-  // cvterm ID.  This isn't very human readable, but the alternative is to
-  // use the accession which may not always be alpha-numeric.
   $bundle_id = 'bio-data_' . $cvterm->cvterm_id;
 
-  // The organism table does not have a type_id so we won't ever find
-  // a record for it in the tripal_cv_defaults table.
-  $bundle_data = array();
-  if ($cvterm->name == 'organism') {
-    $bundle_data = array(
-      'cv_id' => $cvterm->cv_id->cv_id,
-      'cvterm_id' => $cvterm->cvterm_id,
-      'data_table' => 'organism',
-      'type_table' => 'organism',
-      'field' =>  '',
-    );
-  }
-  // The analysis table does not have a type_id so we won't ever find
-  // a record for it in the tripalcv_defaults table.
-  else if ($cvterm->name == 'analysis') {
-    $bundle_data = array(
-      'cv_id' => $cvterm->cv_id->cv_id,
-      'cvterm_id' => $cvterm->cvterm_id,
-      'data_table' => 'analysis',
-      'type_table' => 'analysis',
-      'field' =>  '',
-    );
-  }
-  else if ($cvterm->name == 'project') {
-    $bundle_data = array(
-      'cv_id' => $cvterm->cv_id->cv_id,
-      'cvterm_id' => $cvterm->cvterm_id,
-      'data_table' => 'project',
-      'type_table' => 'project',
-      'field' =>  '',
-    );
-  }
-  else {
-    // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
-    // TABLES.
-    // Look to see if this vocabulary is used as a default for any table.
-    $default = db_select('tripal_cv_defaults', 't')
-      ->fields('t')
-      ->condition('cv_id', $cvterm->cv_id->cv_id)
-      ->execute()
-      ->fetchObject();
-    if ($default) {
-      $bundle_data = array(
-        'cv_id' => $cvterm->cv_id->cv_id,
-        'cvterm_id' => $cvterm->cvterm_id,
-        'data_table' => $default->table_name,
-        'type_table' => $default->table_name,
-        'field' =>  $default->field_name,
-      );
-    }
-    // If there is no default table then we have an error, and we should
-    // set a variable so that the form can help the user deal with the problem.
-    else {
-      $error = t('There is no default mapping of this term\'s ' .
-          'vocabulary to a table in Chado.  Therefore, it is not possible to ' .
-          'determine how to store data of this type.');
-      return FALSE;
-    }
-  }
-
   // Check to see if this bundle exists. If not then create it
   $bundle = db_select('tripal_bundle', 't')
     ->fields('t')
@@ -140,6 +26,7 @@ function tripal_create_entity_type($cvterm, &$error = '') {
     ->fetchObject();
 
   if (!$bundle) {
+    // Inser the bundle.
     db_insert('tripal_bundle')
       ->fields(array(
         'label' => $cvterm->name,
@@ -158,8 +45,33 @@ function tripal_create_entity_type($cvterm, &$error = '') {
   cache_clear_all("entity_info:$langcode", 'cache');
   variable_set('menu_rebuild_needed', TRUE);
 
+
   // Allow modules to now add fields to the bundle
   module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle_id, $cvterm);
 
   return TRUE;
 }
+
+
+/**
+ * Allows a module to add key/value pairs about a bundle.
+ *
+ * If a module needs to associate variables with a particular TripalEntity
+ * type (bundle), it can do so by setting the $bundle_data array passed into
+ * this function.  This hook is called prior to creation of a new entity type.
+ *
+ * @param $bundle_data
+ *   An array for key/value pairs to be associated with a bundle.
+ * @param $bundle_id
+ *   The ID for the bundle.
+ * @param $cvterm
+ *   The CV term object that the bundle represents.
+ */
+function hook_tripal_bundle_data_alter(&$bundle_data, $bundle_id, $cvterm) {
+  // Get the cvterm for this entity type.
+  $bundle_id = $entity->bundle;
+  $cvterm_id = preg_replace('/bio-data_/', $bundle_id);
+  $cvterm = tripal_get_cv(array('cvterm_id' => $cvterm_id));
+
+  // Add any key/value pairs to the $bundle_data array as desired.
+}

+ 1 - 0
tripal_entities/includes/TripalBundle.inc

@@ -8,4 +8,5 @@ class TripalBundle extends Entity {
     parent::__construct($values, $entity_type);
 
   }
+
 }

+ 1 - 14
tripal_entities/includes/TripalBundleController.inc

@@ -5,20 +5,7 @@
 class TripalBundleController extends EntityAPIControllerExportable {
   public function __construct($entityType) {
     parent::__construct($entityType);
-    // The 'bundle of' property is usually set in the hook_entity_info()
-    // function for the "entity type" entity.  This allows the Entity API
-    // to provide the user interface for managing fields attached to the
-    // bundle.  But, we are using the same controller classes for
-    // all entity types and we do not want new links for every
-    // entity type (vocabulary) on the Administration >> Structure menu.
-    // We just want one menu item.  So to support one menu item that
-    // can handle all of the Tripal entity types, we have to set the
-    // 'bundle of' property here rather than in the hook_entity_info() function.
-//     $bundle_of = $entityType;
-//     $bundle_of = preg_replace('/_bundle/', '', $bundle_of);
-//     $info = entity_get_info($bundle_of);
-//     $this->bundleKey = $info['bundle keys']['bundle'];
-//     $this->entityInfo['bundle of'] = $bundle_of;
+
   }
 
   /**

+ 0 - 30
tripal_entities/includes/TripalEntity.inc

@@ -5,37 +5,7 @@
 class TripalEntity extends Entity {
   public function __construct($values = array(), $entity_type) {
     parent::__construct($values, $entity_type);
-    $bundle_id = $this->bundle;
-    $bundle = db_select('tripal_bundle', 'tb')
-      ->fields('tb')
-      ->condition('bundle', $bundle_id)
-      ->execute()
-      ->fetchObject();
 
-    $data = unserialize($bundle->data);
-    $this->chado_table = $data['data_table'];
-    $this->chado_field = $data['field'];
-
-    // If we have an ID then this entity has been saved and it will
-    // also therefore have a chado_entity record.  We want to
-    // load this record so it is always part of the entity object.
-    if (property_exists($this, 'id') and $this->id) {
-      $details = db_select('chado_entity', 'ce')
-        ->fields('ce')
-        ->condition('entity_id', $this->id)
-        ->execute()
-        ->fetchObject();
-
-      // Add the chado entity details to the entity in case it's needed
-      // downstream (e.g. in field widget construction).
-      $this->chado_entity_id = $details->chado_entity_id;
-
-      // Add in the record.
-      $schema = chado_get_schema($this->chado_table);
-      $pkey = $schema['primary key'][0];
-      $this->chado_record_id = $details->record_id;
-      $this->chado_record = chado_generate_var($this->chado_table, array($pkey =>+ $details->record_id));
-    }
   }
 
   protected function defaultLabel() {

+ 1 - 1
tripal_entities/includes/TripalEntityController.inc

@@ -41,7 +41,7 @@ class TripalEntityController extends EntityAPIController {
 
       // Get the CVterm.
       $match = array('cvterm_id' => $cvterm_id);
-      $cvterm = chado_generate_var('cvterm', $match);
+      $cvterm = tripal_get_cvterm($match);
       if ($cvterm) {
         $values['cvterm_id'] = $cvterm->cvterm_id;
       }

+ 1 - 1
tripal_entities/includes/TripalEntityUIController.inc

@@ -41,7 +41,7 @@ class TripalEntityUIController extends EntityDefaultUIController {
       $matches = array();
       preg_match('/^bio-data_(.*?)$/', $bundle, $matches);
       $cvterm_id = $matches[1];
-      $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
+      $cvterm = tripal_get_cvterm(array('cvterm_id' => $cvterm_id));
 
       // Set a custom page for adding new tripal data entities.
       $items['bio-data/add/' . $cvterm_id] = array(

+ 20 - 0
tripal_entities/includes/TripalTerm.inc

@@ -0,0 +1,20 @@
+<?php
+/**
+ * A class the controller will use for instantiating the TripalTerm entity.
+ */
+class TripalTerm extends Entity {
+  public function __construct($values = array()) {
+    parent::__construct($values, $entity_type);
+
+  }
+
+  protected function defaultLabel() {
+    return $this->name;
+  }
+
+  protected function defaultUri() {
+    $vocab = 'TODO';
+    return array('path' => '/vocabulary/' . $vocab . '/term/' . $this->id);
+  }
+
+}

+ 105 - 0
tripal_entities/includes/TripalTermController.inc

@@ -0,0 +1,105 @@
+<?php
+/**
+ * TripalTermController extends DrupalDefaultEntityController.
+ *
+ * Our subclass of DrupalDefaultEntityController lets us add a few
+ * important create, update, and delete methods.
+ */
+class TripalTermController extends EntityAPIController {
+
+  public function __construct($entityType) {
+    parent::__construct($entityType);
+  }
+
+  public function create(array $values = array()) {
+    return parent::create($values);
+  }
+
+  /**
+   * Delete a single entity.
+   *
+   * Really a convenience function for deleteMultiple().
+   */
+  public function delete($entity) {
+    $transaction = db_transaction();
+    try {
+      // Invoke hook_entity_delete().
+      module_invoke_all('entity_delete', $entity, $entity->type);
+      field_attach_delete('TripalTerm', $entity);
+
+      db_delete('tripal_term')
+        ->condition('term_id', $entity->term_id)
+        ->execute();
+    }
+    catch (Exception $e) {
+      $transaction->rollback();
+      watchdog_exception('tripal_entities', $e);
+      throw $e;
+      return FALSE;
+    }
+    return TRUE;
+  }
+
+  /**
+   * Saves the custom fields using drupal_write_record().
+   */
+  public function save($entity) {
+    global $user;
+    $pkeys = array();
+
+    $transaction  = db_transaction();
+    try {
+      // If our entity has no id, then we need to give it a
+      // time of creation.
+      if (empty($entity->id)) {
+        $entity->created = time();
+        $invocation = 'entity_insert';
+      }
+      else {
+        $invocation = 'entity_update';
+        $pkeys = array('id');
+      }
+
+      // Invoke hook_entity_presave().
+      module_invoke_all('entity_presave', $entity, $entity->type);
+
+      // Write out the entity record.
+      $record = array(
+        'vocab_id' => $entity->vocab_id,
+        'term_id' => $entity->term_id,
+        'created' => $entity->created,
+        'changed' => time(),
+      );
+      if ($invocation == 'entity_update') {
+        $record['id'] = $entity->id;
+      }
+      $success = drupal_write_record('tripal_term', $record, $pkeys);
+      if ($success == SAVED_NEW) {
+        $entity->id = $record['id'];
+      }
+
+      // Now we need to either insert or update the fields which are
+      // attached to this entity. We use the same primary_keys logic
+      // to determine whether to update or insert, and which hook we
+      // need to invoke.
+      if ($invocation == 'entity_insert') {
+        field_attach_insert('TripalTerm', $entity);
+      }
+      else {
+        field_attach_update('TripalTerm', $entity);
+      }
+
+      // Invoke either hook_entity_update() or hook_entity_insert().
+      module_invoke_all('entity_postsave', $entity, $entity->type);
+      module_invoke_all($invocation, $entity, $entity->type);
+
+      return $entity;
+    }
+    catch (Exception $e) {
+      $transaction->rollback();
+      watchdog_exception('tripal_entity', $e);
+      drupal_set_message("Could not save the entity:" . $e->getMessage(), "error");
+      return FALSE;
+    }
+  }
+}

+ 19 - 0
tripal_entities/includes/TripalVocab.inc

@@ -0,0 +1,19 @@
+<?php
+/**
+ * A class the controller will use for instantiating the TripalTerm entity.
+ */
+class TripalVocab extends Entity {
+  public function __construct($values = array()) {
+    parent::__construct($values);
+
+  }
+
+  protected function defaultLabel() {
+    return $this->name;
+  }
+
+  protected function defaultUri() {
+    return array('path' => 'vocabulary/' . $this->id);
+  }
+
+}

+ 104 - 0
tripal_entities/includes/TripalVocabController.inc

@@ -0,0 +1,104 @@
+<?php
+/**
+ * TripalVocabController extends DrupalDefaultEntityController.
+ *
+ * Our subclass of DrupalDefaultEntityController lets us add a few
+ * important create, update, and delete methods.
+ */
+class TripalVocabController extends EntityAPIController {
+
+  public function __construct($entityType) {
+    parent::__construct($entityType);
+  }
+
+  public function create(array $values = array()) {
+    return parent::create($values);
+  }
+
+  /**
+   * Delete a single entity.
+   *
+   * Really a convenience function for deleteMultiple().
+   */
+  public function delete($entity) {
+    $transaction = db_transaction();
+    try {
+      // Invoke hook_entity_delete().
+      module_invoke_all('entity_delete', $entity, $entity->type);
+      field_attach_delete('TripalVocab', $entity);
+
+      db_delete('tripal_term')
+        ->condition('term_id', $entity->term_id)
+        ->execute();
+    }
+    catch (Exception $e) {
+      $transaction->rollback();
+      watchdog_exception('tripal_entities', $e);
+      throw $e;
+      return FALSE;
+    }
+    return TRUE;
+  }
+
+  /**
+   * Saves the custom fields using drupal_write_record().
+   */
+  public function save($entity) {
+    global $user;
+    $pkeys = array();
+
+    $transaction  = db_transaction();
+    try {
+      // If our entity has no id, then we need to give it a
+      // time of creation.
+      if (empty($entity->id)) {
+        $entity->created = time();
+        $invocation = 'entity_insert';
+      }
+      else {
+        $invocation = 'entity_update';
+        $pkeys = array('id');
+      }
+
+      // Invoke hook_entity_presave().
+      module_invoke_all('entity_presave', $entity, $entity->type);
+
+      // Write out the entity record.
+      $record = array(
+        'namespace' => $entity->namespace,
+        'created' => $entity->created,
+        'changed' => time(),
+      );
+      if ($invocation == 'entity_update') {
+        $record['id'] = $entity->id;
+      }
+      $success = drupal_write_record('tripal_vocab', $record, $pkeys);
+      if ($success == SAVED_NEW) {
+        $entity->id = $record['id'];
+      }
+
+      // Now we need to either insert or update the fields which are
+      // attached to this entity. We use the same primary_keys logic
+      // to determine whether to update or insert, and which hook we
+      // need to invoke.
+      if ($invocation == 'entity_insert') {
+        field_attach_insert('TripalVocab', $entity);
+      }
+      else {
+        field_attach_update('TripalVocab', $entity);
+      }
+
+      // Invoke either hook_entity_update() or hook_entity_insert().
+      module_invoke_all('entity_postsave', $entity, $entity->type);
+      module_invoke_all($invocation, $entity, $entity->type);
+
+      return $entity;
+    }
+    catch (Exception $e) {
+      $transaction->rollback();
+      watchdog_exception('tripal_entity', $e);
+      drupal_set_message("Could not save the entity:" . $e->getMessage(), "error");
+      return FALSE;
+    }
+  }
+}

+ 3 - 194
tripal_entities/includes/tripal_entities.admin.inc

@@ -52,7 +52,7 @@ function tripal_entities_content_overview_form($form, &$form_state) {
   while ($entity = $entities->fetchObject()) {
 
     // Retrieve details about the term this entity is based on.
-    $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $entity->cvterm_id));
+    $cvterm = tripal_get_cvterm(array('cvterm_id' => $entity->cvterm_id));
 
     // Retrieve details about the user who created this data.
     $author = user_load($entity->uid);
@@ -140,8 +140,7 @@ function tripal_entities_admin_add_type_form($form, &$form_state) {
       );
     }
 
-    $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
-    $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
+    $terms = tripal_get_cvterm($match, array('return_array' => TRUE));
     $num_terms = count($terms);
   }
 
@@ -225,7 +224,7 @@ function tripal_entities_admin_add_type_form_validate($form, &$form_state) {
         'cv_id' => $cv_id,
       );
     }
-    $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
+    $terms = tripal_get_cvterm($match, array('return_array' => TRUE));
     $form_state['storage']['terms'] = $terms;
 
     // If we do not have any terms then the term provided by the user does not
@@ -273,194 +272,4 @@ function tripal_entities_admin_add_type_form_submit($form, &$form_state) {
   }
 }
 
-/**
- * Implements hook_add_bundle_fields().
- *
- * @param $entity_type_name
- * @param $bundle_id
- * @param $cvterm
- */
-function tripal_entities_add_bundle_fields($entity_type_name, $bundle_id, $cvterm) {
-  // Adds the fields for the base table to the entity.
-  tripal_entities_add_bundle_base_fields($entity_type_name, $bundle_id, $cvterm);
-
-  // Check to see if there are any kv-property tables associated to this
-  // base table. If so, add the fields for that type of table.
-  tripal_entities_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_id, 'featureprop');
-}
-
-/**
- * Adds the fields for a kv-property table fields
- *
- * @param $entity_type_name
- * @param $bundle_id
- * @param $kv_table
- */
-function tripal_entities_add_bundle_kvproperty_adder_field($entity_type_name, $bundle_id, $kv_table) {
-  // First add a generic property field so that users can add new proeprty types.
-  $field_name = $kv_table;
-
-  // Initialize the field array.
-  $field_info = array(
-    'field_type' => 'kvproperty_adder',
-    'widget_type' => 'tripal_fields_kvproperty_adder_widget',
-    'field_settings' => array(),
-    'widget_settings' => array('display_label' => 1),
-    'description' => '',
-    'label' => 'Additional Properties',
-    'is_required' => 0,
-  );
-  tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_id);
-}
-
-/**
- * Adds the fields for the base table to the entity.
- */
-function tripal_entities_add_bundle_base_fields($entity_type_name, $bundle_id, $cvterm) {
-
-  // Get the details for this bundle
-  $bundle = db_select('tripal_bundle', 't')
-    ->fields('t')
-    ->condition('type', 'TripalEntity')
-    ->condition('bundle', $bundle_id)
-    ->execute()
-    ->fetchObject();
-  $bundle_data = unserialize($bundle->data);
-
-  $table_name = $bundle_data['data_table'];
-  $type_table = $bundle_data['type_table'];
-  $type_field = $bundle_data['field'];
-
-  // Iterate through the columns of the table and see if fields have been
-  // created for each one. If not, then create them.
-  $schema = chado_get_schema($table_name);
-  $columns = $schema['fields'];
-  foreach ($columns as $column_name => $details) {
-    $field_name = $table_name . '__' . $column_name;
-
-    // Skip the primary key field.
-    if ($column_name == $schema['primary key'][0]) {
-      continue;
-    }
-
-    // Skip the type field.
-    if ($table_name == $type_table and $column_name == $type_field) {
-      continue;
-    }
-
-    // Get the field defaults for this column.
-    $field_info = tripal_entities_get_table_column_field_default($table_name, $schema, $column_name);
-
 
-    // Determine if the field is required.
-    if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
-      $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
-    }
-
-    // If we don't have a field type then we don't need to create a field.
-    if (!$field_info['field_type']) {
-      // If we don't have a field type but it is required and doesn't have
-      // a default value then we are in trouble.
-      if ($field_info['is_required'] and !array_key_exists('default', $details)) {
-        throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
-            array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
-      }
-      continue;
-    }
-
-    // If this field is a foreign key field then we will have a special custom
-    // field provided by Tripal.
-    $is_fk = FALSE;
-    if (array_key_exists('foreign keys', $schema)) {
-      foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
-        if (array_key_exists($column_name, $fk_details['columns'])) {
-          $is_fk = TRUE;
-        }
-      }
-    }
-
-    // Add the field to the bundle.
-    tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_id);
-  }
-}
-
-/**
- * Returns a $field_info array for a field based on a database column.
- *
- */
-function tripal_entities_get_table_column_field_default($table_name, $schema, $column_name) {
-  $details = $schema['fields'][$column_name];
-
-  // Create an array with information about this field.
-  $field_info = array(
-    'field_type' => '',
-    'widget_type' => '',
-    'field_settings' => array(
-      'chado_table' => $table_name,
-      'chado_column' => $column_name,
-    ),
-    'widget_settings' => array('display_label' => 1),
-    'description' => '',
-    'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
-    'is_required' => 0,
-  );
-
-  // Alter the field info array depending on the column details.
-  switch($details['type']) {
-    case 'char':
-      $field_info['field_type'] = 'text';
-      $field_info['widget_type'] = 'text_textfield';
-      $field_info['field_settings']['max_length'] = $details['length'];
-      break;
-    case 'varchar':
-      $field_info['field_type'] = 'text';
-      $field_info['widget_type'] = 'text_textfield';
-      $field_info['field_settings']['max_length'] = $details['length'];
-      break;
-    case 'text':
-      $field_info['field_type'] = 'text';
-      $field_info['widget_type'] = 'text_textarea';
-      $field_info['field_settings']['max_length'] = 17179869184;
-      break;
-    case 'blob':
-      // not sure how to support a blob field.
-      continue;
-      break;
-    case 'int':
-      $field_info['field_type'] = 'number_integer';
-      $field_info['widget_type'] = 'number';
-      break;
-    case 'float':
-      $field_info['field_type'] = 'number_float';
-      $field_info['widget_type'] = 'number';
-      $field_info['field_settings']['precision'] = 10;
-      $field_info['field_settings']['scale'] = 2;
-      $field_info['field_settings']['decimal_separator'] = '.';
-      break;
-    case 'numeric':
-      $field_info['field_type'] = 'number_decimal';
-      $field_info['widget_type'] = 'number';
-      break;
-    case 'serial':
-      // Serial fields are most likely not needed as a field.
-      break;
-    case 'boolean':
-      $field_info['field_type'] = 'list_boolean';
-      $field_info['widget_type'] = 'options_onoff';
-      $field_info['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
-      break;
-    case 'datetime':
-      // Use the Drupal Date and Date API to create the field/widget
-      $field_info['field_type'] = 'datetime';
-      $field_info['widget_type'] = 'date_select';
-      $field_info['widget_settings']['increment'] = 1;
-      $field_info['widget_settings']['tz_handling'] = 'none';
-      $field_info['widget_settings']['collapsible'] = TRUE;
-
-      // TODO: Add settings so that the minutes increment by 1.
-      // And turn off the timezone, as the Chado field doesn't support it.
-      break;
-  }
-
-  return $field_info;
-}

+ 0 - 143
tripal_entities/includes/tripal_entities.chado_entity.inc

@@ -1,143 +0,0 @@
-<?php
-
-/**
- *
- * Implements hook_entity_load().
- */
-function tripal_entities_entity_presave($entity, $type) {
-
-}
-/**
- *
- * @param $entity
- * @param $type
- */
-function tripal_entities_entity_postsave($entity, $type) {
-  // Set the title for this entity using the chado data.
-  $title = chado_get_entity_title($entity);
-  $ec = new TripalEntityController($entity->type);
-  $ec->setTitle($entity, $title);
-}
-/**
- *
- * Implements hook_entity_load().
- */
-function tripal_entities_entity_load($entities, $type) {
-
-}
-
-/**
- *
- * Implements hook_entity_insert().
- */
-function tripal_entities_entity_insert($entity, $type) {
-
-}
-
-/**
- *
- * Implements hook_entity_update().
- */
-function tripal_entities_entity_update($entity, $type) {
-
-}
-
-/**
- *
- * Implements hook_entity_delete().
- */
-function tripal_entities_entity_delete($entity, $type) {
-  $record = db_select('chado_entity', 'ce')
-   ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
-   ->condition('entity_id', $entity->id)
-   ->execute()
-   ->fetchObject();
-
-  if ($record && property_exists($record, 'chado_entity_id')) {
-    // Delete the corresponding record in Chado
-    $table = $record->data_table;
-    $record_id = $record->record_id;
-    chado_delete_record($table, array($table . '_id' => $record_id));
-
-    //Delete the record in the public.chado_entity table
-    $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
-    db_query($sql, array(':id' => $record->chado_entity_id));
-  }
-}
-
-/**
- * This theme function is meant to override the data_combo theme.
- *
- * @param $variables
- */
-function theme_tripal_entities_date_combo($variables) {
-  $element = $variables['element'];
-  $field = field_info_field($element['#field_name']);
-  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
-
-  // Group start/end items together in fieldset.
-  $fieldset = array(
-    '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
-    '#value' => '',
-    '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
-    '#attributes' => array(),
-    '#children' => $element['#children'],
-    '#attributes' => array('class' => array('collapsible', 'collapsed')),
-  );
-  return theme('fieldset', array('element' => $fieldset));
-}
-
-
-/**
- * Determines whether the given user has access to a tripal data entity.
- *
- * @param $op
- *   The operation being performed. One of 'view', 'update', 'create', 'delete'
- *   or just 'edit' (being the same as 'create' or 'update').
- * @param $entity
- *   Optionally a tripal data entity or a tripal data type to check access for.
- *   If nothing is given, access for all types is determined.
- * @param $account
- *   The user to check for. Leave it to NULL to check for the global user.
- * @return boolean
- *   Whether access is allowed or not.
- */
-function tripal_entities_entity_access($op, $entity = NULL, $account = NULL) {
-  if (user_access('administer tripal data', $account)) {
-    return TRUE;
-  }
-  if (isset($entity) && $type_name = $entity->type) {
-    $op = ($op == 'view') ? 'view' : 'edit';
-    if (user_access("$op any $type_name data", $account)) {
-      return TRUE;
-    }
-  }
-  return FALSE;
-}
-
-/**
- * Menu callback to display an entity.
- *
- * As we load the entity for display, we're responsible for invoking a number
- * of hooks in their proper order.
- *
- * @see hook_entity_prepare_view()
- * @see hook_entity_view()
- * @see hook_entity_view_alter()
- */
-function tripal_entities_view_entity($entity, $view_mode = 'full') {
-  $content = '';
-  $controller = entity_get_controller($entity->type);
-  $content = $controller->view(array($entity->id => $entity));
-  drupal_set_title($entity->title);
-  return $content;
-}
-
-/**
- * Menu title callback for showing individual entities
- */
-function tripal_entities_entity_title($entity){
-  if ($entity) {
-    return $entity->title;
-  }
-}

+ 0 - 111
tripal_entities/includes/tripal_entities.tables.inc

@@ -1,111 +0,0 @@
-<?php
-
-/**
- * This function populates the Tripal entity tables using existing
- * data in the database.
- */
-function tripal_entities_populate_entity_tables() {
-  // Get the cvterm table and look for all of the tables that link to it.
-  $schema = chado_get_schema('cvterm');
-  $referring = $schema['referring_tables'];
-
-  // Perform this action in a transaction
-  $transaction = db_transaction();
-  print "\nNOTE: Populating of tripal entity tables is performed using a database transaction. \n" .
-      "If the load fails or is terminated prematurely then the entire set of \n" .
-      "insertions/updates is rolled back and will not be found in the database\n\n";
-  try {
-
-    // Iterate through the referring tables to see what records are there.
-    foreach ($referring as $tablename) {
-
-      // We only want to look at base tables.
-      if ($tablename == 'cvterm_dbxref' || $tablename == 'cvterm_relationship' ||
-          $tablename == 'cvtermpath' || $tablename == 'cvtermprop' || $tablename == 'chadoprop' ||
-          $tablename == 'cvtermsynonym' || preg_match('/_relationship$/', $tablename) ||
-          preg_match('/_cvterm$/', $tablename)) {
-        continue;
-      }
-
-      print "Examining $tablename...\n";
-      $ref_schema = chado_get_schema($tablename);
-      $fkeys = $ref_schema['foreign keys'];
-      foreach ($fkeys['cvterm']['columns'] as $local_id => $remote_id) {
-
-        // Get the list of cvterm_ids from existing records in the table.
-        $sql = "
-          SELECT $local_id
-          FROM { " . $tablename . "}
-          GROUP BY $local_id
-        ";
-        $results = chado_query($sql);
-        while ($cvterm_id = $results->fetchField()) {
-
-          // Get the CV term details.
-          $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
-
-          // First add a record to the tripal_vocabulary table.
-          $values = array(
-            'cv_id' => $cvterm->cv_id->cv_id,
-            'db_id' => $cvterm->dbxref_id->db_id->db_id,
-            'publish' => 0,
-          );
-          $vocabulary_id = 0;
-          $vocabulary = chado_select_record('tripal_vocabulary', array('vocabulary_id'), $values);
-          if (count($vocabulary) == 0) {
-            $vocabulary = chado_insert_record('tripal_vocabulary', $values);
-            $vocabulary_id = $vocabulary['vocabulary_id'];
-          }
-          else {
-            $vocabulary_id = $vocabulary[0]->vocabulary_id;
-          }
-
-          // Next add a record to the tripal_term table.
-          $values = array(
-            'vocabulary_id' => $vocabulary_id,
-            'cvterm_id' => $cvterm_id,
-            'publish' => 0
-          );
-          $term_id = 0;
-          $bundle = chado_select_record('tripal_term', array('term_id'), $values);
-          if (count($bundle) == 0) {
-            $bundle = chado_insert_record('tripal_term', $values);
-            $term_id = $bundle['term_id'];
-          }
-          else {
-            $term_id = $bundle[0]->term_id;
-          }
-
-          // Add the table where the records are found.
-          $values = array(
-            'term_id' => $term_id,
-            'data_table' => $tablename,
-            'type_table' => $tablename,
-            'field' => $local_id
-          );
-          if (!chado_select_record('tripal_term_usage', array('term_usage_id'), $values, array('has_record' => TRUE))) {
-            chado_insert_record('tripal_term_usage', $values);
-          }
-
-          // Add the table where the records are found.
-          $values = array(
-            'vocabulary_id' => $vocabulary_id,
-            'data_table' => $tablename,
-            'type_table' => $tablename,
-            'field' => $local_id
-          );
-          if (!chado_select_record('tripal_vocabulary_usage', array('vocabulary_id'), $values, array('has_record' => TRUE))) {
-            chado_insert_record('tripal_vocabulary_usage', $values);
-          }
-        }
-      }
-    }
-  }
-  catch (Exception $e) {
-    print "\n"; // make sure we start errors on new line
-    $transaction->rollback();
-    watchdog_exception('tripal_ws', $e);
-    print "FAILED: Rolling back database changes...\n";
-  }
-  print "\nDone.\n";
-}

+ 1 - 1
tripal_entities/theme/templates/tripal_data.tpl.php

@@ -8,7 +8,7 @@
  *   print a subset such as render($content['field_example']). Use
  *   hide($content['field_example']) to temporarily suppress the printing of a
  *   given element.
- * - $title: The name of the chado data object
+ * - $title: The title of the entity
  * - $url: The standard URL for viewing the object
  * - $page: TRUE if this is the main view page $url points too.
  * - $classes: String of classes that can be used to style contextually through

+ 3 - 7
tripal_entities/tripal_entities.info

@@ -1,16 +1,12 @@
 name = Tripal Entities
-description = Creates Drupal entities using Controlled Vocabulary terms and integrtates those with Chado.
+description = Provides a framework for Entities based on controlled vocabularies.
 core = 7.x
 project = tripal
 package = Tripal
 version = 7.x-2.0
-configure = admin/tripal/chado/tripal_entities
 
+dependencies[] = date
+dependencies[] = entity
 dependencies[] = tripal_core
-dependencies[] = tripal_views
 dependencies[] = tripal_db
 dependencies[] = tripal_cv
-dependencies[] = tripal_fields
-
-dependencies[] = date
-dependencies[] = entity

+ 114 - 165
tripal_entities/tripal_entities.install

@@ -4,80 +4,6 @@
  * Install for a tripal data entity - creates the base table for our entity.
  */
 
-/**
- * Implements hook_install().
- */
-function tripal_entities_install() {
-
-  // Unfortunately, some Chado base tables do not have a type_id, so we must
-  // take special action for those tables.  These include: organism and
-  // analysis. Until we can find an appropriate controlled vocabulary
-  // that is well supported by the community with types for these tables we
-  // will have to use in-house terms.
-
-  // Add a term to be used for an inherent 'type_id' for the organism table.
-  tripal_insert_cvterm(array(
-    'id' => 'local:organism',
-    'name' => 'organism',
-    'definition' => 'An individual form of life, such as a bacterium, protist, ' .
-      'fungus, plant, or animal, composed of a single cell or a complex of cells  ' .
-      'in which organelles or organs work together to carry out the various  ' .
-      'processes of life. (American Heritage® Dictionary of the English ' .
-      'Language, Fifth Edition. Copyright © 2011 by Houghton Mifflin ' .
-      'Harcourt Publishing Company).',
-    'cv_name' => 'local',
-  ));
-
-  // Add a term to be used for an inherent 'type_id' for the organism table.
-  tripal_insert_cvterm(array(
-    'id' => 'local:analysis',
-    'name' => 'analysis',
-    'definition' => 'A process as a method of studying the nature of something ' .
-      'or of determining its essential features and their relations. ' .
-      '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
-      'Dictionaries Ltd).',
-    'cv_name' => 'local',
-  ));
-
-  tripal_insert_cvterm(array(
-    'id' => 'local:project',
-    'name' => 'project',
-    'definition' => 'A plan or proposal for accomplishing something. ' .
-      '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
-      'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
-    'cv_name' => 'local',
-  ));
-
-  // We want to provide a set of commonly used entity types by default. This
-  // way when a user first installs Tripal there are some commonly used
-  // formats.
-  module_load_include('inc', 'tripal_entities', 'api/tripal_entities.api');
-  module_load_include('inc', 'tripal_entities', 'includes/tripal_entities.admin');
-
-  // Create the 'Organism' entity type. This uses the local:organism term.
-  $error = '';
-  $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
-  $cvterm = chado_generate_var('cvterm', $term);
-  if (!tripal_create_entity_type($cvterm, $error)) {
-    throw new Exception($error);
-  }
-
-  // Create the 'Analysis' entity type. This uses the local:analysis term.
-  $error = '';
-  $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
-  $cvterm = chado_generate_var('cvterm', $term);
-  if (!tripal_create_entity_type($cvterm, $error)) {
-    throw new Exception($error);
-  }
-
-  // Create the 'Project' entity type. This uses the local:project term.
-  $error = '';
-  $term = array('name' => 'project', 'cv_id' => array('name' => 'local'));
-  $cvterm = chado_generate_var('cvterm', $term);
-  if (!tripal_create_entity_type($cvterm, $error)) {
-    throw new Exception($error);
-  }
-}
 
 /**
  * Implements hook_schema().
@@ -85,31 +11,14 @@ function tripal_entities_install() {
 function tripal_entities_schema() {
 
   // Adds a table for managing TripalEntity entities.
+  $schema['tripal_vocab'] = tripal_entities_tripal_vocab_schema();
+  $schema['tripal_term'] = tripal_entities_tripal_term_schema();
   $schema['tripal_entity'] = tripal_entities_tripal_entity_schema();
-
-  // Adds a table for managing the TripalEntity entity types (bundles).
   $schema['tripal_bundle'] = tripal_entities_tripal_bundle_schema();
 
-  // Links TripalEntity entities to the chado record.
-  $schema['chado_entity'] = tripal_entities_chado_entity_schema();
-
   return $schema;
 }
 
-/**
- * Implements hook_uninstall().
- *
- * At uninstall time we'll notify field.module that the entity was deleted
- * so that attached fields can be cleaned up.
- */
-function tripal_entities_uninstall() {
-//   $terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
-//   foreach ($terms as $term) {
-//     $bundle_id = 'bio-data_' . $term->cvterm_id;
-//     field_attach_delete_bundle('TripalEntity', $bundle_id);
-//   }
-}
-
 /**
  * @section
  * Schema Definitions.
@@ -146,8 +55,8 @@ function tripal_entities_tripal_entity_schema() {
         'not null' => TRUE,
         'default' => '',
       ),
-      'cvterm_id' => array(
-        'description' => 'The cvterm_id for the type of entity. This cvterm_id should match a record in the Chado cvterm table.',
+      'term_id' => array(
+        'description' => 'The term_id for the type of entity. This term_id corresponds to a TripalTerm record.',
         'type' => 'int',
         'not null' => TRUE,
       ),
@@ -183,137 +92,177 @@ function tripal_entities_tripal_entity_schema() {
       ),
     ),
     'indexes' => array(
-      'cvterm_id' => array('cvterm_id'),
+      'term_id' => array('term_id'),
       'entity_changed' => array('changed'),
       'entity_created' => array('created'),
       'type' => array('type'),
       'uid' => array('uid'),
     ),
-    'unique keys' => array(
-    ),
+    'unique keys' => array(),
     'primary key' => array('id'),
   );
   return $schema;
 }
 
 /**
- * The base table for Biological Data Type Entites.
- * This table contains a list of Biological Data Types.
- * For the example above (5 genes and 10 mRNAs), there would only be two records in
- * this table one for "gene" and another for "mRNA".
+ * The base table for TripalVocab schema.
+ *
+ * This contains the actual data. For example, if you have a 5 genes and 10 mRNA then
+ * this table will have 15 records and include both genes and mRNA's.
  */
-function tripal_entities_tripal_bundle_schema() {
+function tripal_entities_tripal_vocab_schema() {
 
+  // This schema only provides enough information to assign a unique ID
+  // to the vocabulary. Any additonal information is added to the Entity object
+  // by the selected database back-end.
   $schema = array(
-    'description' => 'Stores information about defined tripal data types.',
+    'description' => 'The base table for TripalVocab entities.',
     'fields' => array(
       'id' => array(
+        'description' => 'The primary identifier for a vocab entity.',
         'type' => 'serial',
+        'unsigned' => TRUE,
         'not null' => TRUE,
-        'description' => 'Primary Key: Unique Chado data type identifier.',
-      ),
-      'type' => array(
-        'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
-        'type' => 'varchar',
-        'length' => 64,
-        'not null' => TRUE,
-        'default' => '',
       ),
-      'bundle' => array(
-        'description' => 'The type of bundle. This should be an official vocabulary ID (e.g. SO, RO, GO) followed by an underscore and the term accession.',
+      'namespace' => array(
+        'description' => 'The namespace for the vocabulary (e.g. SO, PATO, etc.).',
         'type' => 'varchar',
-        'length' => 1024,
+        'length' => 10,
         'not null' => TRUE,
-        'default' => '',
       ),
-      'label' => array(
-        'description' => 'The human-readable name of this bundle.',
-        'type' => 'varchar',
-        'length' => 255,
+      'created' => array(
+        'description' => 'The Unix timestamp when the entity was created.',
+        'type' => 'int',
         'not null' => TRUE,
-        'default' => '',
+        'default' => 0,
       ),
-      'weight' => array(
+      'changed' => array(
+        'description' => 'The Unix timestamp when the entity was most recently saved.',
         'type' => 'int',
         'not null' => TRUE,
         'default' => 0,
-        'size' => 'tiny',
-        'description' => 'The weight of this tripal data type in relation to others.',
       ),
-      'data' => array(
-        'type' => 'text',
-        'not null' => FALSE,
-        'size' => 'big',
-        'serialize' => TRUE,
-        'description' => 'A serialized array of additional data related to this tripal data type.',
-      ),
-    ) + entity_exportable_schema_fields(),
-    'primary key' => array('id'),
-    'unique keys' => array(
-      'bundle' => array('bundle'),
     ),
+    'indexes' => array(
+      'namespace' => array('namespace'),
+      'entity_changed' => array('changed'),
+      'entity_created' => array('created'),
+    ),
+    'unique keys' => array('namespace' => array('namespace')),
+    'primary key' => array('id'),
   );
   return $schema;
 }
 
 /**
- * Links Biological Data Entities to the chado "base" table the data is stored in.
- * This is where we would specify that a particular gene maps to the record in the
- * chado.feature table with a feature_id=2432;
+ * The base table for TripalTerm entities.
+ *
+ * This contains the actual data. For example, if you have a 5 genes and 10 mRNA then
+ * this table will have 15 records and include both genes and mRNA's.
  */
-function tripal_entities_chado_entity_schema() {
+function tripal_entities_tripal_term_schema() {
 
+  // This schema only provides enough information to assign a unique ID
+  // to the term and associate it to it's vocabulary. Any additonal information
+  // is added to the Entity object by the selected database back-end.
   $schema = array(
-    'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
+    'description' => 'The base table for TripalTerm entities.',
     'fields' => array(
-      'chado_entity_id' => array(
-        'description' => 'The primary identifier for this table.',
+      'id' => array(
+        'description' => 'The primary identifier for a term entity.',
         'type' => 'serial',
         'unsigned' => TRUE,
         'not null' => TRUE,
       ),
-      'entity_id' => array(
-        'description' => 'The unique entity id.',
+      'vocab_id' => array(
+        'description' => 'The vocabulary_id of the TripalVocab entity to which this term belongs.',
         'type' => 'int',
         'not null' => TRUE,
       ),
-      'record_id' => array(
-        'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
+      'term_id' => array(
+        'description' => 'The id (or accession) of this term in the vocabulary.',
+        'type' => 'varchar',
+        'length' => 1024,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'created' => array(
+        'description' => 'The Unix timestamp when the entity was created.',
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'changed' => array(
+        'description' => 'The Unix timestamp when the entity was most recently saved.',
         'type' => 'int',
         'not null' => TRUE,
+        'default' => 0,
+      ),
+    ),
+    'indexes' => array(
+      'vocab_id' => array('vocab_id'),
+      'term_id' => array('term_id'),
+      'entity_changed' => array('changed'),
+      'entity_created' => array('created'),
+
+    ),
+    'foreign keys' => array(
+      'tripal_vocab' => array(
+        'table' => 'tripal_vocab',
+        'columns' => array(
+          'vocab_id' => 'vocab_id',
+        ),
       ),
-      'data_table' => array(
-        'description' => 'Indicates the table in Chado that this term services (e.g. feature, stock, library, etc.)',
+    ),
+    'unique keys' => array('vocab_term' => array('vocab_id', 'term_id')),
+    'primary key' => array('id'),
+  );
+  return $schema;
+}
+
+/**
+ * The base table for TripalEntity entities.
+ *
+ * This table contains a list of Biological Data Types.
+ * For the example above (5 genes and 10 mRNAs), there would only be two records in
+ * this table one for "gene" and another for "mRNA".
+ */
+function tripal_entities_tripal_bundle_schema() {
+
+  $schema = array(
+    'description' => 'Stores information about defined tripal data types.',
+    'fields' => array(
+      'id' => array(
+        'type' => 'serial',
+        'not null' => TRUE,
+        'description' => 'Primary Key: Unique numeric ID.',
+      ),
+      'type' => array(
+        'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
         'type' => 'varchar',
-        'length' => 128,
+        'length' => 64,
         'not null' => TRUE,
         'default' => '',
       ),
-      'type_table' => array(
-        'description' => 'Sometimes the record in the data table doesn’t have a field that specifies  the record type.  For example, an analysis type is stored in the analysisprop table.  If the data_table does have a type field then this value will be the same as the data_table.',
+      'bundle' => array(
+        'description' => 'The type of bundle. This should be an official vocabulary ID (e.g. SO, RO, GO) followed by an underscore and the term accession.',
         'type' => 'varchar',
-        'length' => 128,
+        'length' => 1024,
         'not null' => TRUE,
         'default' => '',
       ),
-      'field' => array(
-        'description' => 'The name of the field in the typetable that contains the cvterm record.',
+      'label' => array(
+        'description' => 'The human-readable name of this bundle.',
         'type' => 'varchar',
-        'length' => 128,
-        'not null' => FALSE,
-        'default' => ''
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
       ),
     ),
-    'indexes' => array(
-      'record_id' => array('record_id'),
-      'entity_id' => array('entity_id'),
-      'data_table' => array('data_table'),
-    ),
+    'primary key' => array('id'),
     'unique keys' => array(
-      'record' => array('data_table', 'record_id'),
-      'entity_id' => array('entity_id'),
+      'bundle' => array('bundle'),
     ),
-    'primary key' => array('chado_entity_id'),
   );
   return $schema;
 }

+ 139 - 32
tripal_entities/tripal_entities.module

@@ -1,9 +1,11 @@
 <?php
 
 require_once "api/tripal_entities.api.inc";
-require_once "includes/tripal_entities.chado_entity.inc";
 require_once "includes/tripal_entities.entity_form.inc";
-require_once "includes/tripal_entities.tables.inc";
+require_once "includes/TripalVocab.inc";
+require_once "includes/TripalVocabController.inc";
+require_once "includes/TripalTerm.inc";
+require_once "includes/TripalTermController.inc";
 require_once "includes/TripalEntity.inc";
 require_once "includes/TripalEntityController.inc";
 require_once "includes/TripalEntityUIController.inc";
@@ -156,10 +158,6 @@ function tripal_entities_theme($existing, $type, $theme, $path) {
       'template' => 'tripal_entity',
       'path' => "$path/theme/templates"
     ),
-    'tripal_entities_date_combo' => array(
-      'render element' => 'element',
-      'file' => 'includes/tripal_entities.chado_entity.inc',
-    ),
     'tripal_entities_add_list' => array(
       'variables' => array('content' => NULL),
       'file' => 'includes/tripal_entities.entity_form.inc',
@@ -206,10 +204,112 @@ function tripal_entities_rdf_mapping() {
  */
 function tripal_entities_entity_info() {
   $entities = array();
+  //
+  // The TripalVocab entity is meant to house vocabularies.  It is these
+  // vocabs that are used by the TripalTerm entities.  The storage backend
+  // is responsible for setting the values of this entity.
+  //
+  $entities['TripalVocab'] = array(
+    // A human readable label to identify our entity.
+    'label' => 'Controlled Vocabulary',
+    'plural label' => 'Controlled Vocabularies',
+
+    // The entity class and controller class extend the classes provided by the
+    // Entity API.
+    'entity class' => 'TripalVocab',
+    'controller class' => 'TripalVocabController',
+
+    // The table for this entity defined in hook_schema()
+    'base table' => 'tripal_vocabulary',
+
+    // If fieldable == FALSE, we can't attach fields.
+    'fieldable' => TRUE,
+
+    // entity_keys tells the controller what database fields are used for key
+    // functions. It is not required if we don't have bundles or revisions.
+    // Here we do not support a revision, so that entity key is omitted.
+    'entity keys' => array (
+      'id' => 'id',
+    ),
+
+    // Callback function for access to this entity.
+    'access callback' => 'tripal_entity_access',
+
+    // FALSE disables caching. Caching functionality is handled by Drupal core.
+    'static cache' => FALSE,
+
+    // This entity doesn't support bundles.
+    'bundles' => array (),
+
+    'view modes' => array (
+      'full' => array (
+        'label' => t ('Full content'),
+        'custom settings' => FALSE
+       ),
+       'teaser' => array (
+         'label' => t ('Teaser'),
+         'custom settings' => TRUE
+       ),
+     ),
+  );
+
+  //
+  // The TripalTerm entity is meant to house vocabulary terms.  It is these
+  // terms that are used by the TripalEntity entities.  The storage backend
+  // is responsible for setting the values of this entity.
+  //
+  $entities['TripalTerm'] = array(
+    // A human readable label to identify our entity.
+    'label' => 'Controlled Vocabulary Term',
+    'plural label' => 'Controlled Vocabulary Terms',
+
+    // The entity class and controller class extend the classes provided by the
+    // Entity API.
+    'entity class' => 'TripalTerm',
+    'controller class' => 'TripalTermController',
+
+    // The table for this entity defined in hook_schema()
+    'base table' => 'tripal_term',
+
+    // If fieldable == FALSE, we can't attach fields.
+    'fieldable' => TRUE,
+
+    // entity_keys tells the controller what database fields are used for key
+    // functions. It is not required if we don't have bundles or revisions.
+    // Here we do not support a revision, so that entity key is omitted.
+    'entity keys' => array (
+      'id' => 'id',
+    ),
+
+    // Callback function for access to this entity.
+    'access callback' => 'tripal_entity_access',
+
+    // FALSE disables caching. Caching functionality is handled by Drupal core.
+    'static cache' => FALSE,
+
+    // This entity doesn't support bundles.
+    'bundles' => array (),
+
+    'view modes' => array (
+      'full' => array (
+        'label' => t ('Full content'),
+        'custom settings' => FALSE
+      ),
+      'teaser' => array (
+        'label' => t ('Teaser'),
+        'custom settings' => TRUE
+      ),
+    ),
+  );
+
+  //
+  // The TripalEntity is used for all data. It links data from a storage
+  // back-end to a TripalTerm entity.
+  //
   $entities['TripalEntity'] = array (
     // A human readable label to identify our entity.
-    'label' => 'Biological Content',
-    'plural label' => 'Biological Content',
+    'label' => 'Tripal Content',
+    'plural label' => 'Tripal Content',
 
     // The entity class and controller class extend the classes provided by the
     // Entity API.
@@ -269,7 +369,11 @@ function tripal_entities_entity_info() {
     )
   );
 
-  // The entity that holds information about the entity types.
+  //
+  // The TripalBundle entity is used manage the bundle types.  The 'bundle of'
+  // attribute links this to the TripalEntity and allows the UI provided
+  // by the entity module to work for each TripalEntity bundle.
+  //
   $entities['TripalBundle'] = array (
     'label' => 'Tripal Content Type',
     'entity class' => 'TripalBundle',
@@ -303,30 +407,33 @@ function tripal_entities_entity_info() {
  * Add in the bundles (entity types) to the TripalEntity entity.
  */
 function tripal_entities_entity_info_alter(&$entity_info){
-  // Dynamically add in the bundles. Bundles are alternative groups of fields
-  // or configuration associated with an entity type .We want to dynamically
-  // add the bundles to the entity.
-  $bundles = db_select('tripal_bundle', 'tb')
-    ->fields('tb')
-    ->execute();
-
-  while ($bundle = $bundles->fetchObject()) {
-    $bundle_id = $bundle->bundle;
-    $cvterm_id = preg_replace('/bio-data_/', '', $bundle_id);
-    $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
-    $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
-
-    $entity_info['TripalEntity']['bundles'][$bundle_id] = array (
-      'label' => $label,
-      'admin' => array (
-        'path' => 'admin/structure/bio-data/manage/%tripal_bundle',
-        'real path' => 'admin/structure/bio-data/manage/' . $bundle_id,
-        'bundle argument' => 4,
-        'access arguments' => array (
-          'administer tripal data types'
+
+  if (array_key_exists('TripalEntity', $entity_info)) {
+    // Dynamically add in the bundles. Bundles are alternative groups of fields
+    // or configuration associated with an entity type .We want to dynamically
+    // add the bundles to the entity.
+    $bundles = db_select('tripal_bundle', 'tb')
+      ->fields('tb')
+      ->execute();
+
+    while ($bundle = $bundles->fetchObject()) {
+      $bundle_id = $bundle->bundle;
+      $cvterm_id = preg_replace('/bio-data_/', '', $bundle_id);
+      $cvterm = tripal_get_cvterm(array('cvterm_id' => $cvterm_id));
+      $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
+
+      $entity_info['TripalEntity']['bundles'][$bundle_id] = array (
+        'label' => $label,
+        'admin' => array (
+          'path' => 'admin/structure/bio-data/manage/%tripal_bundle',
+          'real path' => 'admin/structure/bio-data/manage/' . $bundle_id,
+          'bundle argument' => 4,
+          'access arguments' => array (
+            'administer tripal data types'
+          )
         )
-      )
-    );
+      );
+    }
   }
 }
 

+ 0 - 1
tripal_fields/tripal_fields.install

@@ -1 +0,0 @@
-<?php

+ 0 - 518
tripal_fields/tripal_fields.module

@@ -1,518 +0,0 @@
-<?php
-
-require_once "api/tripal_fields.api.inc";
-require_once "includes/tripal_fields.field_storage.inc";
-
-/**
- * Implements hook_field_info().
- */
-function tripal_fields_field_info() {
-  $fields = array(
-    'organism_id' => array(
-      'label' => t('Organism'),
-      'description' => t('A field for specifying an organism.'),
-      'default_widget' => 'tripal_fields_organism_select_widget',
-      'default_formatter' => 'tripal_fields_organism_formatter',
-      'settings' => array(),
-      'storage' => array(
-        'type' => 'field_chado_storage',
-        'module' => 'tripal_fields',
-        'active' => TRUE
-      ),
-    ),
-    'dbxref_id' => array(
-      'label' => t('Cross-reference'),
-      'description' => t('This record can be cross-referenced with a record in
-          another online database. This field is intended for the most prominent
-          reference.  At a minimum, the database and accession must be provided.'),
-      'default_widget' => 'tripal_fields_dbxref_id_widget',
-      'default_formatter' => 'tripal_fields_dbxref_id_formatter',
-      'settings' => array(),
-      'storage' => array(
-        'type' => 'field_chado_storage',
-        'module' => 'tripal_fields',
-        'active' => TRUE
-      ),
-    ),
-    'residues' => array(
-      'label' => t('Residues'),
-      'description' => t('A field for managing nucleotide and protein residues.'),
-      'default_widget' => 'tripal_fields_residue_textarea_widget',
-      'default_formatter' => 'tripal_fields_residues_formatter',
-      'settings' => array(),
-      'storage' => array(
-        'type' => 'field_chado_storage',
-        'module' => 'tripal_fields',
-        'active' => TRUE
-      ),
-    ),
-    'md5checksum' => array(
-      'label' => t('MD5 checksum'),
-      'description' => t('A field for generating MD5 checksum for a sequence.'),
-      'default_widget' => 'tripal_fields_md5checksum_checkbox_widget',
-      'default_formatter' => 'tripal_fields_md5checksum_formatter',
-      'settings' => array(),
-      'storage' => array(
-        'type' => 'field_chado_storage',
-        'module' => 'tripal_fields',
-        'active' => TRUE
-      ),
-    ),
-    'seqlen' => array(
-      'label' => t('Sequence length'),
-      'description' => t('A field for calculating the length of a sequence.'),
-      'default_widget' => 'tripal_fields_seqlen_hidden_widget',
-      'default_formatter' => 'tripal_fields_seqlen_formatter',
-      'settings' => array(),
-      'storage' => array(
-        'type' => 'field_chado_storage',
-        'module' => 'tripal_fields',
-        'active' => TRUE
-      ),
-    ),
-    // The field provides a widget for adding new properties
-    // to an entity that is connected to a base table that has a prop table
-    // in Chado.
-    'kvproperty_adder' => array(
-      'label' => t('Add a Property Type'),
-      'description' => t('This record may have any number of properties. Use
-          this field to first add the type.'),
-      'default_widget' => 'tripal_fields_kvproperty_adder_widget',
-      'default_formatter' => 'tripal_fields_kvproperty_adder_formatter',
-      'settings' => array(),
-      'storage' => array(
-        'type' => 'field_chado_storage',
-        'module' => 'tripal_fields',
-        'active' => TRUE
-      ),
-    ),
-    // The field provides form elements for adding a property to an entity
-    // that in turn gets stored in a prop table of Chado (e.g. featureprop,
-    // stockprop, etc).
-    'kvproperty' => array(
-      'label' => t('Add a Property'),
-      'description' => t('Add details about this property.'),
-      'default_widget' => 'tripal_fields_kvproperty_widget',
-      'default_formatter' => 'tripal_fields_kvproperty_formatter',
-      'settings' => array(),
-      'storage' => array(
-        'type' => 'field_chado_storage',
-        'module' => 'tripal_fields',
-        'active' => TRUE
-      ),
-    ),
-  );
-  return $fields;
-}
-
-/**
- * Implements hook_field_widget_info().
- */
-function tripal_fields_field_widget_info() {
-  return array(
-    'tripal_fields_organism_select_widget' => array(
-      'label' => t('Organism Select'),
-      'field types' => array('organism_id')
-    ),
-    'tripal_fields_dbxref_id_widget' => array(
-      'label' => t('Cross-reference'),
-      'field types' => array('dbxref_id'),
-      'description' => t('This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference.  At a minimum, the database and accession must be provided.'),
-    ),
-    'tripal_fields_md5checksum_checkbox_widget' => array(
-      'label' => t('MD5 Checksum Checkbox'),
-      'field types' => array('md5checksum'),
-    ),
-    'tripal_fields_residues_textarea_widget' => array(
-      'label' => t('Residues'),
-      'field types' => array('residues'),
-    ),
-    'tripal_fields_seqlen_hidden_widget' => array(
-      'label' => t('Sequence Length'),
-      'field types' => array('seqlen'),
-    ),
-    'tripal_fields_kvproperty_adder_widget' => array(
-      'label' => t('Add a Property'),
-      'field types' => array('kvproperty_adder'),
-    ),
-    'tripal_fields_kvproperty_widget' => array(
-      'label' => t('Property'),
-      'field types' => array('kvproperty'),
-    ),
-  );
-}
-/**
- * Implements hook_field_formatter_info().
- */
-function tripal_fields_field_formatter_info() {
-  return array(
-    'tripal_fields_organism_formatter' => array(
-      'label' => t('Organism'),
-      'field types' => array('organism_id')
-    ),
-    'tripal_fields_dbxref_id_formatter' => array(
-      'label' => t('Cross-reference'),
-      'field types' => array('dbxref_id')
-    ),
-    'tripal_fields_md5checksum_formatter' => array(
-      'label' => t('MD5 checksum'),
-      'field types' => array('md5checksum')
-    ),
-    'tripal_fields_residues_formatter' => array(
-      'label' => t('Residues'),
-      'field types' => array('residues')
-    ),
-    'tripal_fields_seqlen_formatter' => array(
-      'label' => t('Sequence length'),
-      'field types' => array('seqlen')
-    ),
-    'tripal_fields_kvproperty_adder_formatter' => array(
-      'label' => t('Add a Property'),
-      'field types' => array('kvproperty_adder')
-    ),
-    'tripal_fields_kvproperty_formatter' => array(
-      'label' => t('Property'),
-      'field types' => array('kvproperty')
-    ),
-  );
-}
-
-/**
- * Implements hook_chado_field_alter().
- *
- * This function is used to change the default field formatter and widget
- * that are assigned to fields of an Entity. This hook is only used for
- * those fields that correspond to a column in a Chado table.  An implementation
- * of this hook can be used to change the default formatters and widgets to
- * custom formatters and widgets that are created by the module creating
- * this hook.
- *
- * By default, Tripal will provide custom formatters and widgets for many
- * columns in Chado tables, therefore, this hook will most likely be of use
- * to extension modules that create custom table inside of Chado.
- *
- * @param $field
- */
-function hook_chado_field_alter(&$field) {
-  // TODO: add example code for how to use this hook.
-}
-
-/**
- * Implements hook_chado_field_alter().
- *
- * This function adds the custom formatters and widgets to many of the Chado
- * tables.  This way Tripal users get a nice set of already usable fields.
- */
-function tripal_fields_chado_field_alter(&$field) {
-
-  if (!array_key_exists('field_settings', $field)) {
-    return;
-  }
-  // If the field doesn't list the Chado table or column then just return.
-  if (!array_key_exists('chado_table', $field['field_settings']) or
-      !array_key_exists('chado_column', $field['field_settings'])) {
-    return;
-  }
-  // Here we provide new field types and widgets for FK fields
-  // and fields that need special attention.
-  if ($field['field_settings']['chado_column'] =='organism_id') {
-    $field['field_type'] = 'organism_id';
-    $field['widget_type'] = 'tripal_fields_organism_select_widget';
-    $field['label'] = 'Organism';
-    $field['description'] = 'Select an organism.';
-  }
-  else if ($field['field_settings']['chado_column'] =='dbxref_id') {
-    $field['field_type'] = 'dbxref_id';
-    $field['widget_type'] = 'tripal_fields_primary_dbxref_widget';
-    $field['label'] = 'Primary Cross Reference';;
-    $field['description'] = 'This record can be cross-referenced with a
-    record in another online database. The primary reference is for the
-    most prominent reference.  At a minimum, the database and accession
-    must be provided.  To remove a set reference, change the database
-    field to "Select a Database".';
-  }
-  else if ($field['field_settings']['chado_table'] == 'feature' and
-    $field['field_settings']['chado_column'] == 'md5checksum') {
-    $field['field_type'] = 'md5checksum';
-    $field['widget_type'] = 'tripal_fields_md5checksum_checkbox_widget';
-    $field['label'] = 'MD5 Checksum';
-    $field['description'] = 'Generating MD5 checksum for the sequence.';
-  }
-  else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
-    $field['field_type'] = 'seqlen';
-    $field['widget_type'] = 'tripal_fields_seqlen_hidden_widget';
-    $field['label'] = 'Seqlen';
-    $field['description'] = 'The length of the residues.';
-  }
-  else if ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
-    $field['field_type'] = 'residues';
-    $field['widget_type'] = 'tripal_fields_residues_textarea_widget';
-    $field['label'] = 'Residues';
-    $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
-  }
-  else if ($field['label'] == 'Timeaccessioned') {
-    $field['label'] = 'Time Accessioned';
-    $field['description'] = 'Please enter the time that this record was first added to the database.';
-  }
-  else if ($field['label'] == 'Timelastmodified') {
-    $field['label'] = 'Time Last Modified';
-    $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
-  }
-}
-
-/**
- * Implements hook_field_widget_form_alter().
- */
-function tripal_fields_field_widget_form_alter(&$element, &$form_state, $context) {
-
-  if (array_key_exists('#field_name', $element)) {
-    $field_name = $element['#field_name'];
-    $matches = array();
-
-    if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
-      $tablename = $matches[1];
-      $colname = $matches[2];
-      $schema = chado_get_schema($tablename);
-
-      // The timelastmodified field exists in many Chado tables.  We want
-      // the form element to update to the most recent time rather than the time
-      // in the database.
-      if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
-        // We want the default value for the field to be the current time.
-        $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
-        $element['#date_items']['value'] = $element['#default_value']['value'];
-      }
-      // We want the date combo fieldset to be collaspible so we will
-      // add our own theme_wrapper to replace the one added by the date
-      // module.
-      if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
-        $element['#theme_wrappers'] = array('tripal_entities_date_combo');
-      }
-    }
-  }
-}
-
-/**
- * Implements hook_field_formatter_view().
- */
-function tripal_fields_field_formatter_view($entity_type, $entity, $field,
-    $instance, $langcode, $items, $display) {
-
-      $element = array();
-      switch ($display['type']) {
-        case 'tripal_fields_organism_formatter':
-          module_load_include('inc', 'tripal_fields', 'includes/fields/organism_id');
-          tripal_fields_organism_select_formatter($element, $entity_type, $entity, $field,
-              $instance, $langcode, $items, $display);
-          break;
-        case 'tripal_fields_dbxref_id_formatter':
-          module_load_include('inc', 'tripal_fields', 'includes/fields/dbxref_id');
-          tripal_fields_dbxref_id_formatter($element, $entity_type, $entity, $field,
-              $instance, $langcode, $items, $display);
-          break;
-        case 'tripal_fields_md5checksum_formatter':
-          module_load_include('inc', 'tripal_fields', 'includes/fields/md5checksum');
-          tripal_fields_md5checksum_checkbox_formatter($element, $entity_type, $entity, $field,
-              $instance, $langcode, $items, $display);
-          break;
-        case 'tripal_fields_residues_formatter':
-          module_load_include('inc', 'tripal_fields', 'includes/fields/residues');
-          tripal_fields_residues_textarea_formatter($element, $entity_type, $entity, $field,
-              $instance, $langcode, $items, $display);
-          break;
-        case 'tripal_fields_seqlen_formatter':
-          module_load_include('inc', 'tripal_fields', 'includes/fields/seqlen');
-          tripal_fields_seqlen_hidden_formatter($element, $entity_type, $entity, $field,
-              $instance, $langcode, $items, $display);
-          break;
-        case 'tripal_fields_kvproperty_adder_formatter':
-          module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty_adder');
-          tripal_fields_kvproperty_adder_formatter($element, $entity_type, $entity, $field,
-              $instance, $langcode, $items, $display);
-          break;
-        case 'tripal_fields_kvproperty_formatter':
-          module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty');
-          tripal_fields_kvproperty_formatter($element, $entity_type, $entity, $field,
-              $instance, $langcode, $items, $display);
-          break;
-      }
-      return $element;
-}
-
-/**
- * Implements hook_field_widget_form().
- */
-function tripal_fields_field_widget_form(&$form, &$form_state, $field,
-    $instance, $langcode, $items, $delta, $element) {
-
-  $widget = $element;
-  switch ($instance['widget']['type']) {
-    case 'tripal_fields_organism_select_widget':
-      // Make sure the include files get parsed now and for the form submits.
-      form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/organism_id');
-      module_load_include('inc', 'tripal_fields', 'includes/fields/organism_id');
-      // Update the widget with the new field.
-      tripal_fields_organism_select_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
-      break;
-    case 'tripal_fields_dbxref_id_widget':
-      form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/dbxref_id');
-      module_load_include('inc', 'tripal_fields', 'includes/fields/dbxref_id');
-      tripal_fields_dbxref_id_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
-      break;
-    case 'tripal_fields_md5checksum_checkbox_widget':
-      form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/md5checksum');
-      module_load_include('inc', 'tripal_fields', 'includes/fields/md5checksum');
-      tripal_fields_md5checksum_checkbox_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
-      break;
-    case 'tripal_fields_residues_textarea_widget':
-      form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/residues');
-      module_load_include('inc', 'tripal_fields', 'includes/fields/residues');
-      tripal_fields_residues_textarea_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
-      break;
-    case 'tripal_fields_seqlen_hidden_widget':
-      form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/seqlen');
-      module_load_include('inc', 'tripal_fields', 'includes/fields/seqlen');
-      tripal_fields_seqlen_hidden_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
-      break;
-    case 'tripal_fields_kvproperty_adder_widget':
-      form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/kvproperty_adder');
-      module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty_adder');
-      tripal_fields_kvproperty_adder_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
-      break;
-    case 'tripal_fields_kvproperty_widget':
-      form_load_include($form_state, 'inc', 'tripal_fields', 'includes/fields/kvproperty');
-      module_load_include('inc', 'tripal_fields', 'includes/fields/kvproperty');
-      tripal_fields_kvproperty_widget($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
-      break;
-  }
-  return $widget;
-}
-
-/**
- * Implements hook_form_FORM_ID_alter().
- *
- * The field_ui_display_overview_form is used for formatting the display
- * or layout of fields attached to an entity and shown on the entity view page.
- */
-function tripal_fields_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
-  // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
-  // on the add/edit form of an entity for adding new property fields.
-  $fields_names = element_children($form['fields']);
-  foreach ($fields_names as $field_name) {
-    $field_info = field_info_field($field_name);
-    if ($field_info['type'] == 'kvproperty_adder') {
-      unset($form['fields'][$field_name]);
-    }
-  }
-}
-/**
- * Implements hook_form_FORM_ID_alter().
- *
- * The field_ui_field_overview_form is used for ordering and configuring the
- * fields attached to an entity.
- */
-function tripal_fields_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
-  // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
-  // on the add/edit form of an entity for adding new property fields.
-  $fields_names = element_children($form['fields']);
-  foreach ($fields_names as $field_name) {
-    $field_info = field_info_field($field_name);
-    if ($field_info['type'] == 'kvproperty_adder') {
-      unset($form['fields'][$field_name]);
-    }
-  }
-}
-/**
- * Implements hook_field_is_empty().
- */
-function tripal_fields_field_is_empty($item, $field) {
-
-  // If there is no value field then the field is empty.
-  if (!array_key_exists('value', $item)) {
-    return TRUE;
-  }
-
-  // Iterate through all of the fields and if at least one has a value
-  // the field is not empty.
-  foreach ($item as $form_field_name => $value) {
-    if (isset($value) and $value != NULL and $value != '') {
-      return FALSE;
-    }
-  }
-
-  // Otherwise, the field is empty.
-  return TRUE;
-}
-
-/**
- * Returns the values of the field from the $form_state.
- */
-function tripal_fields_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
-  $value = NULL;
-  // The form_state must have the 'values' key. If not then just return.
-  if (!array_key_exists('values', $form_state)) {
-    return $value;
-  }
-
-  // If the field name is not in the form_state['values'] then return.
-  if (!array_key_exists($field_name, $form_state['values'])) {
-    return $value;
-  }
-
-  // Iterate through the values looking for the field_name provided.
-  foreach ($form_state['values'][$field_name] as $langcode => $items) {
-    $item = $items[$delta];
-    if ($child){
-      if(array_key_exists($child, $item) and $item[$child] != '') {
-        $value = $item[$child];
-      }
-    }
-    else {
-      $value = $item['value'];
-    }
-  }
-  return $value;
-}
-
-/**
- * Sets the values of the field from the $form_state.
- */
-function tripal_fields_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
-  // The form_state must have the 'values' key. If not then just return.
-  if (!array_key_exists('values', $form_state)) {
-    return FALSE;
-  }
-
-  // If the field name is not in the form_state['values'] then reutrn.
-  if (!array_key_exists($field_name, $form_state['values'])) {
-    return FALSE;
-  }
-
-  foreach ($form_state['values'][$field_name] as $langcode => $items) {
-    if ($child) {
-      $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
-    }
-    else {
-      $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
-    }
-  }
-  return TRUE;
-}
-
-
-/**
- * Implements hook_theme().
- */
-function tripal_fields_theme($existing, $type, $theme, $path) {
-  return array(
-    'tripal_fields_dbxref_id_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/fields/dbxref_id.inc',
-    ),
-    'tripal_fields_kvproperty_addr_widget' => array(
-      'render element' => 'element',
-      'file' => 'includes/fields/dbxref_id.inc',
-    ),
-  );
-}
-

+ 0 - 1
tripal_fields_layout/tripal_fields_layout.info

@@ -6,5 +6,4 @@ package = Tripal
 version = 7.x-2.0
 
 dependencies[] = tripal_core
-dependencies[] = tripal_fields
 dependencies[] = tripal_entities

+ 2 - 1
tripal_ws/includes/tripal_ws.rest.inc

@@ -17,6 +17,7 @@ function tripal_ws_rest() {
   // Lump everything ito a try block so that if there is a problem we can
   // throw an error and have that returned in the response.
   try {
+
     // Get the list of published terms (these are the bundle IDs)
     $bundles = db_select('tripal_bundle', 'tb')
       ->fields('tb')
@@ -29,7 +30,7 @@ function tripal_ws_rest() {
         $terms[$cvterm->name] = $cvterm;
       }
     }
-    ksort($terms);
+    ksort($terms, SORT_STRING|SORT_FLAG_CASE);
     $result['content_types'] = array();
     foreach ($terms as $name => $cvterm) {
       $result['content_types'][$name] = array(