Sfoglia il codice sorgente

Updated all modules to set default vocabularies

Stephen Ficklin 11 anni fa
parent
commit
3f6a5fceac

+ 3 - 1
tripal_analysis/includes/tripal_analysis.chado_node.inc

@@ -232,10 +232,12 @@ function chado_analysis_form($node, &$form_state) {
   // Properties Form
   // ----------------------------------
   $instructions = t('To add additional properties to the drop down. ' . l("Add terms to the analysis_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
+  $prop_cv = tripal_get_default_cv('analysisprop', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
   $details = array(
     'property_table' => 'analysisprop',       // the name of the prop table
     'chado_id' => $analysis_id,               // the value of analysis_id for this record
-    'cv_name' => 'analysis_property',         // the cv.name of the cv governing analysisprop.type_id
+    'cv_id' => $cv_id,                        // the cv.cv_id of the cv governing analysisprop.type_id
     'fieldset_title' => 'Properties',
     'additional_instructions' => $instructions
   );

+ 60 - 4
tripal_analysis/tripal_analysis.install

@@ -56,11 +56,17 @@ function tripal_analysis_install() {
   // we may need the analysisfeatureprop table if it doesn't already exist
   tripal_analysis_create_analysisfeatureprop();
 
+  // add vocabularies
+  tripal_analysis_add_cvs();
+  
   // add cvterms
   tripal_analysis_add_cvterms();
 
   // add materialized views
   tripal_analysis_add_mview_analysis_organism();
+  
+  // set the default vocabularies
+  tripal_set_default_cv('analysisprop', 'type_id', 'analysis_property');
 }
 
 /**
@@ -99,6 +105,18 @@ function tripal_analysis_create_analysisfeatureprop() {
   }
 }
 
+/**
+ * Add cvs related to analyses
+ *
+ * @ingroup tripal_analysis
+ */
+function tripal_analysis_add_cvs() {
+
+  // typically here we would add the analysis_property vocabulary
+  // but it already comes with Chado.
+
+}
+
 /**
  * Adds controlled vocabulary terms needed by this module.
  *
@@ -125,9 +143,13 @@ function tripal_analysis_add_cvterms() {
   tripal_cv_add_cvterm($term, 'tripal', 0, 1 , 'tripal');
 
 
-  // the 'analysis_property' vocabulary is for user definable properties.
-  tripal_cv_add_cvterm(array('name' => 'Analysis Type', 'def' => 'The type of analysis that was performed.'),
-     'analysis_property', 0, 1, 'tripal');
+  // the 'analysis_property' vocabulary is for user definable properties wo we
+  // will add an 'Analysis Type' to this vocubulary
+  $term = array(
+    'name' => 'Analysis Type', 
+    'def' => 'The type of analysis that was performed.'
+  );
+  tripal_cv_add_cvterm($term, 'analysis_property', 0, 1, 'tripal');
 }
 
 /**
@@ -256,7 +278,26 @@ function tripal_analysis_add_mview_analysis_organism() {
  *
  */
 function tripal_analysis_update_7200() {
-
+  // We cannot use the Tripal API calls in the 7200 update
+  // because during upgrade the tripal_core should also be disabled
+  
+  // set the analysis_property as default
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'analysis_property'")->fetchField();
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'analysisprop',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add analysis_property vocabulary: '. $error);
+  }
+  
+  
   // During the upgrade from D6 to D7 the vocabulary terms assigned to organisms were
   // copied to the field_data_taxonomyextra table rather than to the correct
   // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
@@ -292,4 +333,19 @@ function tripal_analysis_update_7200() {
       throw new DrupalUpdateException('Could not move organism taxonomy terms: '. $error);
     }
   }
+}
+
+/**
+ * Implementation of hook_update_dependencies().  It specifies a list of
+ * other modules whose updates must be run prior to this one.
+ */
+function tripal_analysis_update_dependencies() {
+  $dependencies = array();
+
+  // the tripal_cv update 7200 must run prior to update 7200 of this module
+  $dependencies['tripal_analysis'][7200] = array(
+    'tripal_cv' => 7200
+  );
+
+  return $dependencies;
 }

+ 60 - 54
tripal_contact/includes/tripal_contact.chado_node.inc

@@ -120,29 +120,37 @@ function chado_contact_form(&$node, $form_state) {
     $description = $form_state['input']['description'];
   }
 
-  // get the contact types. These are those that are part of the tripal_contact
-  // vocabulary and are children of the term 'Contact Type', so we need
-  // to join on the cvtermpath table and select those with a distance of 1
-  $sql = "
-    SELECT CVTS.cvterm_id, CVTS.name
-    FROM {cvtermpath} CVTP
-      INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
-      INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
-      INNER JOIN {cv} CV       ON CVTO.cv_id = CV.cv_id
-    WHERE
-      CV.name = 'tripal_contact' AND
-      CVTO.name = 'Contact Type' AND
-      CVTP.pathdistance = 1
-    ORDER BY CVTS.name ASC
-  ";
-  $results = chado_query($sql);
-  $contact_types = array(3723 => 'Person');
-  while ($contact_type = $results->fetchObject()) {
-    $contact_types[$contact_type->cvterm_id] = $contact_type->name;
-    if (strcmp($contact_type->name, "Person") == 0 and !$type_id) {
-      $type_id = $contact_type->cvterm_id;
+  // get the contact type
+  $type_cv = tripal_get_default_cv('contact', 'type_id');
+  if ($type_cv->name == 'tripal_contact') {
+    // get the contact types. If the default is the 'tripal_contact' vocabulary,
+    // then we want terms that are part of the tripal_contact
+    // vocabulary and are children of the term 'Contact Type', so we need
+    // to join on the cvtermpath table and select those with a distance of 1
+    $sql = "
+      SELECT CVTS.cvterm_id, CVTS.name
+      FROM {cvtermpath} CVTP
+        INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
+        INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
+        INNER JOIN {cv} CV       ON CVTO.cv_id = CV.cv_id
+      WHERE
+        CV.name = 'tripal_contact' AND
+        CVTO.name = 'Contact Type' AND
+        CVTP.pathdistance = 1
+      ORDER BY CVTS.name ASC
+    ";
+    $results = chado_query($sql);
+    while ($contact_type = $results->fetchObject()) {
+      $contact_types[$contact_type->cvterm_id] = $contact_type->name;
+      if (strcmp($contact_type->name, "Person") == 0 and !$type_id) {
+        $type_id = $contact_type->cvterm_id;
+      }
     }
   }
+  else {
+    $contact_types = tripal_get_cvterm_default_select_options('contact', 'type_id', 'contact types');
+    $contact_types[0] = 'Select a Type';
+  }
   $form['type_id'] = array(
     '#type' => 'select',
     '#title' => t('Contact Type'),
@@ -170,25 +178,33 @@ function chado_contact_form(&$node, $form_state) {
 
   // Properties Form
   // ----------------------------------
-  // Need to pass in our own select_options since we use cvtermpath to filter ours
+  $prop_cv = tripal_get_default_cv('contactprop', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
   $select_options = array();
-  $select_options[] = 'Select a Property';
-  $sql = "
-   SELECT CVTS.cvterm_id, CVTS.name
-   FROM {cvtermpath} CVTP
-     INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
-     INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
-     INNER JOIN {cv} CV       ON CVTO.cv_id = CV.cv_id
-   WHERE
-     CV.name = 'tripal_contact' AND
-     NOT CVTO.name = 'Contact Type'
-   ORDER BY CVTS.name ASC";
-  $prop_types = chado_query($sql);
-  while ($prop = $prop_types->fetchObject()) {
-    // add all properties except the Citation. That property is set via the uniquename field
-    if ($prop->name != 'Citation') {
-      if (!isset($select_options[$prop->cvterm_id])) {
-        $select_options[$prop->cvterm_id] = $prop->name;
+  
+  // the Tripal contact vocabulary is heirarchical so if that vocab is default we
+  // want to use the subset of terms not under the type 'Contact Type' for our
+  // properties list.
+  if($prop_cv->name == 'tripal_contact') {
+    // Need to pass in our own select_options since we use cvtermpath to filter ours
+    $select_options[] = 'Select a Property';
+    $sql = "
+     SELECT CVTS.cvterm_id, CVTS.name
+     FROM {cvtermpath} CVTP
+       INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
+       INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
+       INNER JOIN {cv} CV       ON CVTO.cv_id = CV.cv_id
+     WHERE
+       CV.name = 'tripal_contact' AND
+       NOT CVTO.name = 'Contact Type'
+     ORDER BY CVTS.name ASC";
+    $prop_types = chado_query($sql);
+    while ($prop = $prop_types->fetchObject()) {
+      // add all properties except the Citation. That property is set via the uniquename field
+      if ($prop->name != 'Citation') {
+        if (!isset($select_options[$prop->cvterm_id])) {
+          $select_options[$prop->cvterm_id] = $prop->name;
+        }
       }
     }
   }
@@ -196,32 +212,22 @@ function chado_contact_form(&$node, $form_state) {
   $details = array(
     'property_table' => 'contactprop',
     'chado_id' => $contact_id,
-    'cv_name' => 'tripal_contact',
-    'select_options' => $select_options
+    'cv_id' => $cv_id,
+    'select_options' => $select_options,
   );
   chado_add_node_form_properties($form, $form_state, $details);
 
   // RELATIONSHIPS FORM
   //---------------------------------------------
-  // We want to use the contact_relationship_types cv if there are any terms available
-  // and if not, to default to the relationship ontology
-  $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'contact_relationship_types'));
-  $cv_id = $cv_result[0]->cv_id;
-  $select_options = tripal_cv_get_cvterm_options($cv_id);
-  if (empty($select_options)) {
-    $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'relationship'));
-    $cv_id = $cv_result[0]->cv_id;
-    $select_options = tripal_cv_get_cvterm_options($cv_id);
-  }
-  // D7 @TODO: tell tripal admin's about this
-
+  $relationship_cv = tripal_get_default_cv('contact_relationship', 'type_id');
+  $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
   $details = array(
     'relationship_table' => 'contact_relationship', // the name of the _relationship table
     'base_table' => 'contact',                      // the name of your chado base table
     'base_foreign_key' => 'contact_id',             // the name of the key in your base chado table
     'base_key_value' => $contact_id,                // the value of example_id for this record
     'nodetype' => 'contact',                        // the human-readable name of your node type
-    'cv_name' => 'contact_relationship_types',      // the cv.name of the cv governing example_relationship.type_id
+    'cv_id' => $cv_id,                              // the cv.cv_id of the cv governing contact_relationship.type_id
     'base_name_field' => 'name',                    // the base table field you want to be used as the name
     'select_options' => $select_options
   );

+ 136 - 26
tripal_contact/tripal_contact.install

@@ -65,18 +65,11 @@ function tripal_contact_install() {
   // Add cvterms for relationship types
   tripal_contact_add_cvs();
   tripal_contact_add_cvterms();
-
-  /*
-  // Install our custom block visibility settings per node type
-  $query = db_insert('block_node_type')
-    ->fields(array('type', 'module', 'delta'))
-    ->values(array(
-      'type'   => 'chado_contact',
-      'module' => 'tripal_contact', // My module name
-      'delta'  => 'contbase', // Same delta used in hook_block_info
-    )
-  )->execute();
-  */
+  
+  // set the default vocabularies
+  tripal_set_default_cv('contact', 'type_id', 'tripal_contact');
+  tripal_set_default_cv('contactprop', 'type_id', 'tripal_contact');
+  tripal_set_default_cv('contact_relationship', 'type_id', 'contact_relationship');
 }
 
 /**
@@ -100,10 +93,31 @@ function tripal_contact_uninstall() {
  * @ingroup tripal_contact
  */
 function tripal_contact_add_cvs() {
-
+  
+  // Add the cv for contact properties. This is a default vocabulary in the event
+  // that a user does not want to use the tripal_contact vocabulary
+  tripal_cv_add_cv(
+    'contact_property',
+    'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
+  );
+  
+  // add the cv for the contact type. This is a default vocabulary in the event
+  // that a user does not want to use the tripal_contact vocabulary
+  tripal_cv_add_cv(
+    'contact_type',
+    'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
+  );
+  
+  // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
+  tripal_cv_add_cv(
+   'tripal_contact',
+   'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
+  );
+  
+  // add the cv for contact relationships
   tripal_cv_add_cv(
-    'contact_relationship_types',
-    'Contains Types of relationships between contacts.'
+    'contact_relationship',
+    'Contains types of relationships between contacts.'
   );
 
 }
@@ -228,22 +242,118 @@ function tripal_contact_add_custom_tables(){
  *
  */
 function tripal_contact_update_7200() {
+  
+  // We cannot use the Tripal API calls in the 7200 update 
+  // because during upgrade the tripal_core will be disabled
+  
+  // add the contact_type CV
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_contact'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'tripal_contact',
+        'definition' => 'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
+      ))
+      ->execute();
+    }
+    // make this CV the defaults for the contact properties and contact types
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+         'table_name' => 'contact',
+         'field_name' => 'type_id',
+         'cv_id' => $cv_id
+       ))
+      ->execute();
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'contactprop',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add tripal_contact vocabulary: '. $error);
+  }
+  
+  
+  // add the contact_relationship CV
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_relationship'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'contact_relationship',
+        'definition' => 'Contains types of relationships between contacts.'
+      ))
+      ->execute();
+    }
+    // add the default
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'contact_relationship',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+        ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
+  }
 
-  // add the new contact_relationship_types vocabulary
-  // We cannot use the Tripal API calls in the 7000 update 
-  // because during upgrade the tripal_core should also be disabled
+  // add the contact_type CV
   try {
-    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_relationship_types'")->fetchObject();
-    if (!$check->cv_id) {
-      $sql = "INSERT INTO chado.cv (name, definition) VALUES (
-        'contact_relationship_types',
-        'Contains Types of relationships between contacts.')
-      ";
-      db_query($sql);
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_type'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'contact_type',
+        'definition' => 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
+      ))
+      ->execute();
     }
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
-    throw new DrupalUpdateException('Failed to add contact_relationship_types vocabulary: '. $error);
+    throw new DrupalUpdateException('Failed to add contact_type vocabulary: '. $error);
   }
+  
+  // add the contact_property CV
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'contact_property'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'contact_property',
+        'definition' => 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
+      ))
+      ->execute();
+    }
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add contact_property vocabulary: '. $error);
+  }
+}
+
+/**
+ * Implementation of hook_update_dependencies().  It specifies a list of
+ * other modules whose updates must be run prior to this one.
+ */
+function tripal_contact_update_dependencies() {
+  $dependencies = array();
+
+  // the tripal_cv update 7200 must run prior to update 7200 of this module
+  $dependencies['tripal_contact'][7200] = array(
+    'tripal_cv' => 7200
+  );
+
+  return $dependencies;
 }

+ 12 - 5
tripal_core/api/tripal_core.chado_nodes.properties.api.inc

@@ -113,8 +113,8 @@
  *       feature has a feature_id of 999 and we want to associate properties for that feature 
  *       then the chado_id option should be 999)
  *   Require ONE of the following to identify the controlled vocabulary containing the properties to use:
- *     -cv_id: the unique key from the cv table
- *     -cv_name: the cv.name field uniquely identifying the controlled vocabulary
+ *     - cv_id: the unique key from the cv table
+ *     - cv_name: the cv.name field uniquely identifying the controlled vocabulary
  *   Optional keys include:
  *     - chado_id_field: the foreign key field that links properties to the
  *       chado_id record. If this value is not specified it is determined using the
@@ -129,6 +129,10 @@
  *       the following keys and values:
  *          'cvterm':    The cvterm object for the property type
  *          'value':     The property value
+ *     - select_options: an array of terms to use for the drop down select box.
+ *       this array will be used rather than populating the drop down with terms
+ *       from the named vocabulary.  The array must have keys with the cvterm_id
+ *       and values with the cvterm name. 
  *
  * @ingroup tripal_chado_node_api
  */
@@ -209,8 +213,11 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
     return;
   }
 
-  // Get Property Types for the Select List
-  if (isset($details['select_options'])) {
+  // Get property types for the select list. If the user has provided a set
+  // then use those, otherwise get them from the cvterm table for specified cv. 
+  if (isset($details['select_options']) and 
+      is_array($details['select_options']) and 
+      count($details['select_options']) > 0) {
     $property_options = $details['select_options'];
   }
   // if the select options are not provided then try to get them on our own
@@ -511,7 +518,7 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
   // description
   $type_desc = '';
   if (isset($form_state['input']['property_table']['new']['type'])) {
-    $new_type_id = $form_state['values']['property_table']['new']['type'];
+    $new_type_id = $form_state['input']['property_table']['new']['type'];
     $new_term = tripal_get_cvterm(array('cvterm_id' => $new_type_id));
     $type_desc = $new_term->definition;
   }

+ 3 - 0
tripal_cv/tripal_cv.install

@@ -173,6 +173,9 @@ function tripal_cv_get_tripal_cv_defaults_table(&$schema) {
     'indexes' => array(
       'tripal_cv_defaults_idx1' => array('table_name', 'field_name'),
     ),
+    'unique keys' => array(
+      'tripal_cv_defaults_unq1' => array('table_name', 'field_name', 'cv_id'),
+    ),
     'primary key' => array('cv_default_id')
   );
 }

+ 11 - 3
tripal_example/includes/tripal_example.chado_node.inc

@@ -141,17 +141,23 @@ function chado_example_form($node, &$form_state) {
     // $uniquename   = $form_state['input']['uniquename'];
 
   }
+  
+  // NOTE: if the table has a 'type_id' column, be sure to use the 
+  // tripal_get_default_cv('example', 'type_id') to get the site's 
+  // default vocabulary.
+  
 
-  // add form elements here.
 
   // PROPERTIES FORM
   //---------------------------------------------
   // If there is a exampleprop table and you want to allow users to add/remove entries
   // from it through your node form then add this section to your own node form
+  $prop_cv = tripal_get_default_cv('exampleprop', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
   $details = array(
     'property_table' => 'exampleprop',      // the name of the prop table
     'chado_id' => $example_id,              // the value of example_id for this record
-    'cv_name' => 'example_property_types'   // the cv.name of the cv governing exampleprop.type_id
+    'cv_id' => $cv_id                       // the cv.cv_id of the cv governing exampleprop.type_id
   );
   // Adds the form elements to your current form
   chado_add_node_form_properties($form, $form_state, $details);
@@ -172,13 +178,15 @@ function chado_example_form($node, &$form_state) {
   //---------------------------------------------
   // If there is a example_relationship table and you want to allow users to add/remove entries
   // from it through your node form then add this section to your own node form
+  $rels_cv = tripal_get_default_cv('example_relationship', 'type_id');
+  $cv_id = $rels_cv ? $rels_cv->cv_id : NULL;
   $details = array(
     'relationship_table' => 'example_relationship', // the name of the _relationship table
     'base_table' => 'example',                      // the name of your chado base table
     'base_foreign_key' => 'example_id',             // the name of the key in your base chado table
     'base_key_value' => $example_id,                // the value of example_id for this record
     'nodetype' => 'example',                        // the human-readable name of your node type
-    'cv_name' => 'example_relationship_types'         // the cv.name of the cv governing example_relationship.type_id
+    'cv_id' => $cv_id                               // the cv.cv_id of the cv governing example_relationship.type_id
   );
   // Adds the form elements to your current form
   chado_add_node_form_relationships($form, $form_state, $details);

+ 18 - 3
tripal_feature/includes/tripal_feature.chado_node.inc

@@ -212,12 +212,13 @@ function chado_feature_form($node, &$form_state) {
 
   // PROPERTIES FORM
   //---------------------------------------------
+  $prop_cv = tripal_get_default_cv('featureprop', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
   $details = array(
     'property_table' => 'featureprop',      // the name of the prop table
     'chado_id' => $feature_id,              // the value of feature_id for this record
-    'cv_name' => 'feature_property'         // the cv.name of the cv governing featureprop.type_id
+    'cv_id' => $cv_id                       // the cv.cv_id of the cv governing featureprop.type_id
   );
-  // Adds the form elements to your current form
   chado_add_node_form_properties($form, $form_state, $details);
 
   // ADDITIONAL DBXREFS FORM
@@ -227,8 +228,22 @@ function chado_feature_form($node, &$form_state) {
     'base_foreign_key' => 'feature_id',   // the name of the key in your base chado table
     'base_key_value' => $feature_id       // the value of feature_id for this record
   );
-  // Adds the form elements to your current form
   chado_add_node_form_dbxrefs($form, $form_state, $details);
+  
+  
+  // RELATIONSHIPS FORM
+  //---------------------------------------------
+  $relationship_cv = tripal_get_default_cv('feature_relationship', 'type_id');
+  $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
+  $details = array(
+    'relationship_table' => 'feature_relationship',
+    'base_table' => 'feature',
+    'base_foreign_key' => 'feature_id',
+    'base_key_value' => $feature_id,
+    'nodetype' => 'feature',
+    'cv_id' => $cv_id
+  );
+  chado_add_node_form_relationships($form, $form_state, $details);
 
   return $form;
 }

+ 115 - 0
tripal_feature/tripal_feature.install

@@ -56,6 +56,14 @@ function tripal_feature_install() {
 
   // create the temp table we will use for loading GFF files
   tripal_cv_create_tripal_gff_temp();
+  
+  // add the vocabularies used by the feature module:
+  tripal_feature_add_cvs();
+  
+  // set the default vocabularies
+  tripal_set_default_cv('feature', 'type_id', 'sequence');
+  tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
+  tripal_set_default_cv('feature_relationships', 'type_id', 'feature_relationships');
 }
 
 /**
@@ -211,6 +219,29 @@ function tripal_feature_add_organism_count_mview() {
 
   tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
 }
+/**
+ * Add cvs related to publications
+ *
+ * @ingroup tripal_pub
+ */
+function tripal_feature_add_cvs() {
+
+  // Add cv for relationship types
+  tripal_cv_add_cv(
+    'feature_relationship',
+    'Contains types of relationships between features.'
+  );
+  
+  // the feature_property CV already exists... it comes with chado, so no need to add it here
+  
+  // the feature type vocabulary should be the sequence ontology, and even though
+  // this ontology should get loaded we will create it here just so that we can 
+  // set the default vocabulary for the feature.type_id field
+  tripal_cv_add_cv(
+    'sequence',
+    'The Sequence Ontology'
+  );
+}
 
 /**
  * This is the required update for tripal_feature when upgrading from Drupal core API 6.x.
@@ -252,4 +283,88 @@ function tripal_feature_update_7200() {
       throw new DrupalUpdateException('Could not move feature taxonomy terms: '. $error);
     }
   }
+  
+  // set the default feature property vocabulary
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_property'")->fetchField();
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'featureprop',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+        ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to set feature_property vocabulary as default: '. $error);
+  }
+  
+  // add the feature_relationshp CV
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_relationship'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'feature_relationship',
+        'definition' => 'Contains types of relationships between features.'
+      ))
+      ->execute();
+    }
+    // use the new feature_relationship CV we just added
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'feature_relationship',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add feature_relationship vocabulary: '. $error);
+  }
+  
+  // set the feature_type as the 'sequence' ontology
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'sequence'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'sequence',
+        'definition' => 'The Sequence Ontology.'
+      ))
+      ->execute();
+    }
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'feature',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add sequence vocabulary which will be used for the sequence ontology: '. $error);
+  }
+  
+  
+}
+
+/**
+ * Implementation of hook_update_dependencies().  It specifies a list of
+ * other modules whose updates must be run prior to this one.
+ */
+function tripal_feature_update_dependencies() {
+  $dependencies = array();
+
+  // the tripal_cv update 7200 must run prior to update 7200 of this module
+  $dependencies['tripal_feature'][7200] = array(
+    'tripal_cv' => 7200
+  );
+
+  return $dependencies;
 }

+ 8 - 16
tripal_featuremap/includes/tripal_featuremap.chado_node.inc

@@ -107,20 +107,10 @@ function chado_featuremap_form($node, &$form_state) {
     '#default_value' => $description,
   );
 
-  // get the list of unit types
-  $values = array(
-    'cv_id' => array(
-      'name' => 'featuremap_units',
-    )
-  );
-  $columns = array('cvterm_id','name');
-  $options = array('order_by' => array('name' => 'ASC'));
-  $featuremap_units = chado_select_record('cvterm', $columns, $values, $options);
-  $units = array();
-  $units[''] = '';
-  foreach($featuremap_units as $unit) {
-    $units[$unit->cvterm_id] = $unit->name;
-  }
+  // get the list of unit types  
+  $units = tripal_get_cvterm_default_select_options('featuremap', 'unittype_id', 'map unit types');
+  $units[0] = 'Select a Type';
+  
   $form['unittype_id'] = array(
     '#title'       => t('Map Units'),
     '#type'        => t('select'),
@@ -132,11 +122,13 @@ function chado_featuremap_form($node, &$form_state) {
 
   // Properties Form
   // ----------------------------------
+  $prop_cv = tripal_get_default_cv('featuremap_property', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
   $instructions = t('To add additional properties to the drop down. ' . l("Add terms to the featuremap_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
   $details = array(
     'property_table' => 'featuremapprop',
     'chado_id' => $featuremap_id,
-    'cv_name' => 'featuremap_property',
+    'cv_id' => $cv_id,
     'fieldset_name' => 'Additional Details',
     'additional_instructions' => $instructions
   );
@@ -544,4 +536,4 @@ function tripal_featuremap_node_view($node, $view_mode, $langcode) {
       }
       break;
   }
-}
+}

+ 79 - 0
tripal_featuremap/tripal_featuremap.install

@@ -57,6 +57,10 @@ function tripal_featuremap_install() {
   tripal_featuremap_add_cvs();
   tripal_featuremap_add_cvterms();
 
+  // set the default vocabularies
+  tripal_set_default_cv('featuremapprop', 'type_id', 'featuremap_property');
+  tripal_set_default_cv('featureposprop', 'type_id', 'featurepos_property');
+  tripal_set_default_cv('featuremap', 'uinittype_id', 'featuremap_units');
 }
 
 /**
@@ -431,3 +435,78 @@ function tripal_featuremap_add_custom_tables(){
   );
   chado_create_custom_table('featureposprop', $schema, TRUE);
 }
+
+
+/**
+ * This is the required update for tripal_featuremap when upgrading from Drupal core API 6.x.
+ * This update may take some time to complete.
+ */
+function tripal_featuremap_update_7200() {
+  // We can't use the Tripal API during an upgrade from D6 to D7 Tripal  because the tripal_core
+  // module is disabled. So, we have to manually make database additions/changes to chado.
+
+  // set the default vocabularies
+  // featuremap_units
+  try {    
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featuremap_units'")->fetchField();
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'featuremap',
+        'field_name' => 'unittype_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to set featuremap_units vocabulary as default: '. $error);
+  }
+  
+  // featurepos_property
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featurepos_property'")->fetchField();
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'featureposprop',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to set featurepos_property vocabulary as default: '. $error);
+  }
+  
+  // featuremap_property
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'featuremap_property'")->fetchField();
+    db_insert('tripal_cv_defaults')
+     ->fields(array(
+       'table_name' => 'featuremapprop',
+       'field_name' => 'type_id',
+       'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to set featuremap_property vocabulary as default: '. $error);
+  }
+  
+}
+
+/**
+ * Implementation of hook_update_dependencies().  It specifies a list of
+ * other modules whose updates must be run prior to this one.
+ */
+function tripal_featuremap_update_dependencies() {
+  $dependencies = array();
+
+  // the tripal_cv update 7200 must run prior to update 7200 of this module
+  $dependencies['tripal_featuremap'][7200] = array(
+    'tripal_cv' => 7200
+  );
+
+  return $dependencies;
+}

+ 17 - 23
tripal_library/includes/tripal_library.chado_node.inc

@@ -118,20 +118,9 @@ function chado_library_form($node, &$form_state) {
     '#default_value' => $uniquename,
   );
 
-  // get the list of library types
-  $values = array(
-    'cv_id' => array(
-      'name' => 'library_type',
-    )
-  );
-  $columns = array('cvterm_id','name');
-  $options = array('order_by' => array('name' => 'ASC'));
-  $lib_types = chado_select_record('cvterm', $columns, $values, $options);
-  $types = array();
-  $types[''] = '';
-  foreach($lib_types as $type) {
-    $types[$type->cvterm_id] = $type->name;
-  }
+  // get the list of library types  
+  $types = tripal_get_cvterm_default_select_options('library', 'type_id', 'library types');
+  $types[0] = 'Select a Type';
 
   $form['library_type'] = array(
     '#title'       => t('Library Type'),
@@ -172,18 +161,23 @@ function chado_library_form($node, &$form_state) {
 
   // PROPERTIES FORM
   //---------------------------------------------
-  // Generate our own select list so we can desclude the description since it has it's
-  // own form element above
-  $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'library_property'));
-  $cv_id = $cv_result[0]->cv_id;
-  $select_options = tripal_cv_get_cvterm_options($cv_id);
-  $descrip_id = array_search('Library Description', $select_options);
-  unset($select_options[$descrip_id]);
-
+  $select_options = array();
+  $prop_cv = tripal_get_default_cv('stockprop', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
+  // if the default is the 'library_property' vocabulary then we want
+  // to exclude the 'Library Description' term since it has it's own form element above
+  if ($prop_cv->name == 'library_property') {
+    // Generate our own select list so we can exclude the description element
+    $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'library_property'));
+    $cv_id = $cv_result[0]->cv_id;
+    $select_options = tripal_cv_get_cvterm_options($cv_id);
+    $descrip_id = array_search('Library Description', $select_options);
+    unset($select_options[$descrip_id]);
+  }
   $details = array(
     'property_table' => 'libraryprop',      // the name of the prop table
     'chado_id' => $library_id,              // the value of library_id for this record
-    'cv_name' => 'library_property',        // the cv.name of the cv governing libraryprop.type_id
+    'cv_id' => $cv_id,                      // the cv.cv_id of the cv governing libraryprop.type_id
     'select_options' => $select_options
   );
   // Adds the form elements to your current form

+ 52 - 0
tripal_library/tripal_library.install

@@ -57,6 +57,10 @@ function tripal_library_install() {
   // add cvterms
   tripal_library_add_cvs();
   tripal_library_add_cvterms();
+  
+  // set the default vocabularies
+  tripal_set_default_cv('librarypprop', 'type_id', 'library_property');
+  tripal_set_default_cv('library', 'type_id', 'library_type');
 }
 
 /**
@@ -347,4 +351,52 @@ function tripal_library_update_7200() {
       throw new DrupalUpdateException('Could not move library taxonomy terms: '. $error);
     }
   }
+  
+  // set the default vocabularies
+  // library_type
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchField();
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'library',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to set library_type vocabulary as default: '. $error);
+  }
+  // library_property
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchField();
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'libraryprop',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to set the library_property vocabulary as default: '. $error);
+  }
+  
+}
+
+/**
+ * Implementation of hook_update_dependencies().  It specifies a list of
+ * other modules whose updates must be run prior to this one.
+ */
+function tripal_library_update_dependencies() {
+  $dependencies = array();
+
+  // the tripal_cv update 7200 must run prior to update 7200 of this module
+  $dependencies['tripal_library'][7200] = array(
+    'tripal_cv' => 7200
+  );
+
+  return $dependencies;
 }

+ 3 - 1
tripal_organism/includes/tripal_organism.chado_node.inc

@@ -180,10 +180,12 @@ function chado_organism_form($node, $form_state) {
 
   // PROPERTIES FORM
   //---------------------------------------------
+  $prop_cv = tripal_get_default_cv('organismprop', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
   $details = array(
     'property_table' => 'organismprop',      // the name of the prop table
     'chado_id' => $organism_id,              // the value of organism_id for this record
-    'cv_name' => 'organism_property'         // the cv.name of the cv governing organismprop.type_id
+    'cv_id' => $cv_id                        // the cv.cv_id of the cv governing organismprop.type_id
   );
   // Adds the form elements to your current form
   chado_add_node_form_properties($form, $form_state, $details);

+ 33 - 3
tripal_organism/tripal_organism.install

@@ -37,6 +37,10 @@ function tripal_organism_install() {
   // cvs & cvterms
   tripal_organism_add_cvs();
   tripal_organism_add_cvterms();
+  
+  // set the default vocabularies
+  tripal_set_default_cv('organismprop', 'type_id', 'organism_property');
+  
 }
 
 /**
@@ -112,7 +116,10 @@ function tripal_organism_requirements($phase) {
  * @ingroup tripal_organism
  */
 function tripal_organism_add_cvs() {
-  tripal_cv_add_cv('organism_property', 'Contains properties for organisms');
+  tripal_cv_add_cv(
+    'organism_property', 
+    'Contains properties for organisms'
+  );
 }
 
 /**
@@ -134,14 +141,22 @@ function tripal_organism_update_7200() {
   // We cannot use the Tripal API calls in the 7000 update 
   // because during upgrade the tripal_core should also be disabled
   try {
-    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'organism_property'")->fetchObject();
-    if (!$check->cv_id) {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'organism_property'")->fetchField();
+    if (!$cv_id) {
       $sql = "INSERT INTO chado.cv (name, definition) VALUES (
         'organism_property',
         'Contains properties for organisms.')
       ";
       db_query($sql);
     }
+    // add in the default vocabulary
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'organismprop',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
@@ -185,3 +200,18 @@ function tripal_organism_update_7200() {
     }
   }
 }
+
+/**
+ * Implementation of hook_update_dependencies().  It specifies a list of
+ * other modules whose updates must be run prior to this one.
+ */
+function tripal_organism_update_dependencies() {
+  $dependencies = array();
+  
+  // the tripal_cv update 7200 must run prior to update 7200 of this module
+  $dependencies['tripal_organism'][7200] = array(
+    'tripal_cv' => 7200
+  );
+  
+  return $dependencies;
+}

+ 19 - 23
tripal_project/includes/tripal_project.chado_node.inc

@@ -137,21 +137,27 @@ function chado_project_form(&$node, $form_state) {
 
   // Properties Form
   // ----------------------------------
-  // we want to exclude the project description from being loaded as a stored property
-  // because we want to use the property to replace the project.description field as it is
-  // only 255 characters which isn't large enough. We don't want the user to set it
-  // as a property even though it will be stored as a property.
-  $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'project_property'));
-  $cv_id = $cv_result[0]->cv_id;
-  $select_options = tripal_cv_get_cvterm_options($cv_id);
-  $descrip_id = array_search('Project Description', $select_options);
-  unset($select_options[$descrip_id]);
+  $select_options = array();
+  $prop_cv = tripal_get_default_cv('projectprop', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
+  if ($prop_cv = 'project_property') {
+    // if this is the project_property CV then
+    // we want to exclude the project description from being loaded as a stored property
+    // because we want to use the property to replace the project.description field as it is
+    // only 255 characters which isn't large enough. We don't want the user to set it
+    // as a property even though it will be stored as a property.
+    $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'project_property'));
+    $cv_id = $cv_result[0]->cv_id;
+    $select_options = tripal_cv_get_cvterm_options($cv_id);
+    $descrip_id = array_search('Project Description', $select_options);
+    unset($select_options[$descrip_id]);
+  }
 
   $instructions = t('To add properties to the drop down list, you must ' . l("add terms to the project_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
   $details = array(
     'property_table' => 'projectprop',
     'chado_id' => $project_id,
-    'cv_name' => 'project_property',
+    'cv_id' => $cv_id,
     'additional_instructions' => $instructions,
     'select_options' => $select_options
   );
@@ -159,25 +165,15 @@ function chado_project_form(&$node, $form_state) {
 
   // RELATIONSHIPS FORM
   //---------------------------------------------
-  // We want to use the contact_relationship_types cv if there are any terms available
-  // and if not, to default to the relationship ontology
-  $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'project_relationship_types'));
-  $cv_id = $cv_result[0]->cv_id;
-  $select_options = tripal_cv_get_cvterm_options($cv_id);
-  if (empty($select_options)) {
-    $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'relationship'));
-    $cv_id = $cv_result[0]->cv_id;
-    $select_options = tripal_cv_get_cvterm_options($cv_id);
-  }
-  // D7 @TODO: tell tripal admin's about this
-
+  $relationship_cv = tripal_get_default_cv('project_relationship', 'type_id');
+  $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
   $details = array(
     'relationship_table' => 'project_relationship', // the name of the _relationship table
     'base_table' => 'project',                      // the name of your chado base table
     'base_foreign_key' => 'project_id',             // the name of the key in your base chado table
     'base_key_value' => $project_id,                // the value of example_id for this record
     'nodetype' => 'project',                        // the human-readable name of your node type
-    'cv_name' => 'project_relationship_types',      // the cv.name of the cv governing example_relationship.type_id
+    'cv_id' => $cv_id,                              // the cv.cv_id of the cv governing example_relationship.type_id
     'base_name_field' => 'name',                    // the base table field you want to be used as the name
     'subject_field_name' => 'subject_project_id',
     'object_field_name' => 'object_project_id',

+ 69 - 8
tripal_project/tripal_project.install

@@ -53,6 +53,10 @@ function tripal_project_install() {
 
   tripal_project_add_cvs();
   tripal_project_add_cvterms();
+  
+  // set the default vocabularies
+  tripal_set_default_cv('projectprop', 'type_id', 'project_property');
+  tripal_set_default_cv('project_relationship', 'type_id', 'project_relationship');
 }
 
 /**
@@ -98,11 +102,15 @@ function tripal_project_schema() {
  * @ingroup tripal_project
  */
 function tripal_project_add_cvs() {
-  tripal_cv_add_cv('project_property', 'Contains properties for projects');
+  // Add the cv for project properties
+  tripal_cv_add_cv(
+    'project_property', 
+    'Contains properties for projects'
+  );
 
   // Add cv for relationship types
   tripal_cv_add_cv(
-    'project_relationship_types',
+    'project_relationship',
     'Contains Types of relationships between projects.'
   );
 }
@@ -134,18 +142,55 @@ function tripal_project_update_7200() {
   
   // add the project_property CV
   try {
-    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'project_property'")->fetchObject();
-    if (!$check->cv_id) {
-      $sql = "INSERT INTO chado.cv (name, definition) VALUES (
-        'project_property','Contains properties for projects.')
-      ";
-      db_query($sql);
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'project_property'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+        ->fields(array(
+          'name' => 'project_property',
+          'definition' => 'Contains properties for projects.'
+        ))
+        ->execute();
     }
+    // use the new project_property CV we just added
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+      'table_name' => 'projectprop',
+      'field_name' => 'type_id',
+      'cv_id' => $cv_id
+      ))
+      ->execute();
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Failed to add project_property vocabulary: '. $error);
   }
+  
+  // add the project_relationship CV
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'project_relationship'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'project_relationship',
+        'definition' => 'Contains types of relationships between projects.'
+      ))
+      ->execute();
+    }
+    // use the new project_property CV we just added
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'project_relationship',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add project_relationship vocabulary: '. $error);
+  }
 
   // For Tripal in Drupal 6 the project_description cvterm was stored in the
   // 'tripal' CV.  It should be stored in the new project_property CV that
@@ -170,3 +215,19 @@ function tripal_project_update_7200() {
     throw new DrupalUpdateException('Failed to change project_description property type to the project_property CV and update the name: '. $error);
   }
 }
+
+
+/**
+ * Implementation of hook_update_dependencies().  It specifies a list of
+ * other modules whose updates must be run prior to this one.
+ */
+function tripal_project_update_dependencies() {
+  $dependencies = array();
+
+  // the tripal_cv update 7200 must run prior to update 7200 of this module
+  $dependencies['tripal_project'][7200] = array(
+    'tripal_cv' => 7200
+  );
+
+  return $dependencies;
+}

+ 65 - 59
tripal_pub/includes/tripal_pub.chado_node.inc

@@ -157,30 +157,39 @@ function chado_pub_form($node, $form_state) {
     '#default_value' => $title,
     '#required' => TRUE,
   );
-  // get the list of publication types.  In the Tripal publication
-  // ontologies these are all grouped under the term 'Publication Type'
-  // we want the default to be 'Journal Article'
-  $sql = "
-    SELECT
-      CVTS.cvterm_id, CVTS.name
-    FROM {cvtermpath} CVTP
-      INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
-      INNER JOIN {cvterm} CVTO ON CVTP.object_id  = CVTO.cvterm_id
-      INNER JOIN {cv}          ON CVTO.cv_id      = CV.cv_id
-    WHERE
-      CV.name = 'tripal_pub' AND CVTO.name = 'Publication Type' AND
-      NOT CVTS.is_obsolete = 1
-    ORDER BY CVTS.name ASC
-  ";
-  $results = chado_query($sql);
-  $pub_types = array();
-  while ($pub_type = $results->fetchObject()) {
-    $pub_types[$pub_type->cvterm_id] = $pub_type->name;
-    // if we don't have a default type then set the default to be 'Journal Article'
-    if (strcmp($pub_type->name,"Journal Article") == 0 and !$type_id) {
-      $type_id = $pub_type->cvterm_id;
+  
+  $type_cv = tripal_get_default_cv('pub', 'type_id');
+  if ($type_cv->name == 'tripal_pub') {
+   
+    // get the list of publication types.  In the Tripal publication
+    // ontologies these are all grouped under the term 'Publication Type'
+    // we want the default to be 'Journal Article'
+    $sql = "
+      SELECT
+        CVTS.cvterm_id, CVTS.name
+      FROM {cvtermpath} CVTP
+        INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
+        INNER JOIN {cvterm} CVTO ON CVTP.object_id  = CVTO.cvterm_id
+        INNER JOIN {cv}          ON CVTO.cv_id      = CV.cv_id
+      WHERE
+        CV.name = 'tripal_pub' AND CVTO.name = 'Publication Type' AND
+        NOT CVTS.is_obsolete = 1
+      ORDER BY CVTS.name ASC
+    ";
+    $results = chado_query($sql);
+    $pub_types = array();
+    while ($pub_type = $results->fetchObject()) {
+      $pub_types[$pub_type->cvterm_id] = $pub_type->name;
+      // if we don't have a default type then set the default to be 'Journal Article'
+      if (strcmp($pub_type->name,"Journal Article") == 0 and !$type_id) {
+        $type_id = $pub_type->cvterm_id;
+      }
     }
   }
+  else {
+    $pub_types = tripal_get_cvterm_default_select_options('pub', 'type_id', 'publication types');
+    $pub_types[0] = 'Select a Type'; 
+  }
 
   $form['type_id'] = array(
     '#type' => 'select',
@@ -217,38 +226,43 @@ function chado_pub_form($node, $form_state) {
 
   // Properties Form
   // ----------------------------------
-  // Need to pass in our own select_options since we use cvtermpath to filter ours
   $select_options = array();
-  $select_options[] = 'Select a Property';
-  $sql = "
-    SELECT
-      DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
-    FROM {cvtermpath} CVTP
-      INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
-      INNER JOIN {cvterm} CVTO ON CVTP.object_id  = CVTO.cvterm_id
-      INNER JOIN {cv}          ON CVTO.cv_id      = CV.cv_id
-    WHERE CV.name = 'tripal_pub' and
-      (CVTO.name = 'Publication Details' OR CVTS.name = 'Publication Type') AND
-      NOT CVTS.is_obsolete = 1
-    ORDER BY CVTS.name ASC
-  ";
-  $prop_types = chado_query($sql);
-  while ($prop = $prop_types->fetchObject()) {
-    // add all properties except the Citation. That property is set via the uniquename field
-    if ($prop->name == 'Citation') {
-      continue;
-    }
-    // Publication Dbxref's are handled by the dbxref form addition below
-    if ($prop->name == 'Publication Dbxref') {
-      continue;
+  $prop_cv = tripal_get_default_cv('pubprop', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
+  // if the poperty cv is 'tripal_pub' then we need to pass in our own select_options 
+  // for only a subset of the vocabulary  
+  if ($prop_cv->name == 'tripal_pub') {
+    $select_options[] = 'Select a Property';
+    $sql = "
+      SELECT
+        DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
+      FROM {cvtermpath} CVTP
+        INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
+        INNER JOIN {cvterm} CVTO ON CVTP.object_id  = CVTO.cvterm_id
+        INNER JOIN {cv}          ON CVTO.cv_id      = CV.cv_id
+      WHERE CV.name = 'tripal_pub' and
+        (CVTO.name = 'Publication Details' OR CVTS.name = 'Publication Type') AND
+        NOT CVTS.is_obsolete = 1
+      ORDER BY CVTS.name ASC
+    ";
+    $prop_types = chado_query($sql);
+    while ($prop = $prop_types->fetchObject()) {
+      // add all properties except the Citation. That property is set via the uniquename field
+      if ($prop->name == 'Citation') {
+        continue;
+      }
+      // Publication Dbxref's are handled by the dbxref form addition below
+      if ($prop->name == 'Publication Dbxref') {
+        continue;
+      }
+      $select_options[$prop->cvterm_id] = $prop->name;
     }
-    $select_options[$prop->cvterm_id] = $prop->name;
   }
 
   $details = array(
     'property_table' => 'pubprop',
     'chado_id' => $pub_id,
-    'cv_name' => 'tripal_pub',
+    'cv_id' => $cv_id,
     'select_options' => $select_options,
     'default_properties' => $more_props,
   );
@@ -256,23 +270,15 @@ function chado_pub_form($node, $form_state) {
 
   // RELATIONSHIPS FORM
   //---------------------------------------------
-  // We want to use the contact_relationship_types cv if there are any terms available
-  // and if not, then default to the relationship ontology
-  $cv_result = chado_select_record('cv',array('cv_id'),array('name' => 'pub_relationship_types'));
-  if (count($cv_result) > 0) {
-    $cv_id = $cv_result[0]->cv_id;
-    $cv_name = 'pub_relationship_types';
-    $select_options = tripal_cv_get_cvterm_options($cv_id);
-  }
-  // D7 @TODO: tell tripal admin's about this
-
+  $relationship_cv = tripal_get_default_cv('pub_relationship', 'type_id');
+  $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
   $details = array(
     'relationship_table' => 'pub_relationship', // the name of the _relationship table
     'base_table' => 'pub',                      // the name of your chado base table
     'base_foreign_key' => 'pub_id',             // the name of the key in your base chado table
     'base_key_value' => $pub_id,                // the value of example_id for this record
     'nodetype' => 'pub',                        // the human-readable name of your node type
-    'cv_name' => $cv_name,                      // the cv.name of the cv containing the properties
+    'cv_id' => $cv_id,                          // the cv.cv_id of the cv containing the properties
     'base_name_field' => 'uniquename',          // the base table field you want to be used as the name
     'select_options' => $select_options
   );
@@ -1134,7 +1140,7 @@ function tripal_pub_node_update($node) {
 /**
  * Implements hook_node_presave(). Acts on all content types.
  *
- * @ingroup tripal_stock
+ * @ingroup tripal_pub
  */
 function tripal_pub_node_presave($node) {
   switch ($node->type) {

+ 130 - 12
tripal_pub/tripal_pub.install

@@ -62,6 +62,11 @@ function tripal_pub_install() {
 
   // add the custom tables
   tripal_pub_add_custom_tables();
+  
+  // set the default vocabularies
+  tripal_set_default_cv('pub', 'type_id', 'tripal_pub');
+  tripal_set_default_cv('pubprop', 'type_id', 'tripal_pub');
+  tripal_set_default_cv('pub_relationship', 'type_id', 'pub_relationship');
 
 }
 
@@ -224,9 +229,28 @@ function tripal_pub_add_custom_tables() {
  */
 function tripal_pub_add_cvs() {
 
+  
+  // Add the cv for pub properties
+  tripal_cv_add_cv(
+    'tripal_pub',
+    'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
+  );
+  
+  // Add the cv for pub types
+  tripal_cv_add_cv(
+    'pub_type',
+    'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
+  );
+  
+  // Add the cv for pub properties
+  tripal_cv_add_cv(
+    'pub_property',
+    'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
+  );
+  
   // Add cv for relationship types
   tripal_cv_add_cv(
-    'pub_relationship_types',
+    'pub_relationship',
     'Contains types of relationships between publications.'
   );
 }
@@ -246,21 +270,115 @@ function tripal_pub_add_cvterms() {
  */
 function tripal_pub_update_7200() {
 
-  // add the new pub_relationship_types vocabulary
-  // We cannot use the Tripal API calls in the 7000 update 
-  // because during upgrade the tripal_core should also be disabled
+  
+  // add the tripal_pub CV and set it to be the default for pub types and pub properties
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_pub'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+        ->fields(array(
+          'name' => 'tripal_pub',
+          'definition' => 'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
+        ))
+        ->execute();
+    }
+    // use the new pub_property CV we just added
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'pub',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+    // use the new pub_property CV we just added
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'pubprop',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
+  }
+  
+  // add the pub_property CV
   try {
-    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_relationship_types'")->fetchObject();
-    if (!$check->cv_id) {
-      $sql = "INSERT INTO chado.cv (name, definition) VALUES (
-        'pub_relationship_types',
-        'Contains Types of relationships between publications.')
-      ";
-      db_query($sql);
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_property'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+        ->fields(array(
+          'name' => 'pub_property',
+          'definition' => 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
+        ))
+        ->execute();
     }
   }
   catch (\PDOException $e) {
     $error = $e->getMessage();
-    throw new DrupalUpdateException('Failed to add pub_relationship_types vocabulary: '. $error);
+    throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
   }
+  
+  // add the pub_type CV
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_type'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'pub_type',
+        'definition' => 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
+      ))
+      ->execute();
+    }
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add pub_type vocabulary: '. $error);
+  }
+  
+  // add the pub_relationship CV
+  try {
+    $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_relationship'")->fetchField();
+    if (!$cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'pub_relationship',
+        'definition' => 'Contains types of relationships between publications.'
+      ))
+      ->execute();
+    }
+    // use the new pub_property CV we just added
+    db_insert('tripal_cv_defaults')
+      ->fields(array(
+        'table_name' => 'pub_relationship',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+      ))
+      ->execute();
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add pub_relationship vocabulary: '. $error);
+  }
+}
+
+/**
+ * Implementation of hook_update_dependencies().  It specifies a list of
+ * other modules whose updates must be run prior to this one.
+ */
+function tripal_pub_update_dependencies() {
+  $dependencies = array();
+
+  // the tripal_cv update 7200 must run prior to update 7200 of this module
+  $dependencies['tripal_pub'][7200] = array(
+    'tripal_cv' => 7200
+  );
+
+  return $dependencies;
 }

+ 15 - 2
tripal_stock/tripal_stock.install

@@ -144,8 +144,6 @@ function tripal_stock_update_7200() {
   
   // add the stock_relationshp CV
   try {
-
-    
     $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_relationship'")->fetchField();
     if (!$cv_id) {
       // add the vocabulary
@@ -229,4 +227,19 @@ function tripal_stock_update_7200() {
     throw new DrupalUpdateException('Failed to add stock_type vocabulary: '. $error);
   }
   
+}
+
+/**
+ * Implementation of hook_update_dependencies().  It specifies a list of
+ * other modules whose updates must be run prior to this one.
+ */
+function tripal_stock_update_dependencies() {
+  $dependencies = array();
+
+  // the tripal_cv update 7200 must run prior to update 7200 of this module
+  $dependencies['tripal_stock'][7200] = array(
+    'tripal_cv' => 7200
+  );
+
+  return $dependencies;
 }