Browse Source

Fixed bugs in contact, featuremap, project, pub and library modules. Also added new tripal_cv_defaults table for keeping track of default CVs for fields

Stephen Ficklin 11 years ago
parent
commit
4b5bf2027a

+ 2 - 2
tripal_contact/includes/tripal_contact.chado_node.inc

@@ -195,7 +195,7 @@ function chado_contact_form(&$node, $form_state) {
 
   $details = array(
     'property_table' => 'contactprop',
-    'chado_id' => 'contact_id',
+    'chado_id' => $contact_id,
     'cv_name' => 'tripal_contact',
     'select_options' => $select_options
   );
@@ -255,7 +255,7 @@ function chado_contact_validate($node, $form, &$form_state) {
   
   
   // Validating for an update
-  if (property_exists($node, 'nid')) {
+  if (!is_null($node->nid)) {
     // get the existing node
     $values = array('contact_id' => $node->contact_id);
     $result = chado_select_record('contact', array('*'), $values);

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

@@ -258,7 +258,7 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
   // Tell tripal administrators how to add terms to the property types drop down.
   if (empty($property_options)) {
     $tripal_message = tripal_set_message(
-      t('There are currently no proeprty types! To add additional properties to the drop
+      t('There are currently no property types! To add properties to the drop
         down list, you need to <a href="@cvtermlink">add a controlled vocabulary term</a>
         to the %cv_name controlled vocabulary.',
         array(
@@ -266,7 +266,7 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
           '@cvtermlink' => url('admin/tripal/chado/tripal_cv/cv/' . $details['cv_id'] . '/cvterm/add')
         )
       ),
-      TRIPAL_WARNING,
+      TRIPAL_NOTICE,
       array('return_html' => TRUE)
     );
   }
@@ -288,10 +288,9 @@ function chado_add_node_form_properties(&$form, &$form_state, $details) {
   $form['properties'] = array(
     '#type' => 'fieldset',
     '#title' => t($details['fieldset_title']),
-    '#description' => t('You may add additional properties by selecting a property type
-      from the dropdown and adding text. You may add as many properties as desired by
-      clicking the add button on the right. To remove a property, click the remove button.'
-      . $details['additional_instructions']),
+    '#description' => t('Add properties by selecting a type
+      from the dropdown, enter a value and click the "Add" button. To 
+      remove a property, click the remove button.' . $details['additional_instructions']),
     '#prefix' => "<div id='properties-fieldset'>",
     '#suffix' => '</div>',
     '#weight'      => 8

+ 122 - 0
tripal_cv/api/tripal_cv.api.inc

@@ -978,4 +978,126 @@ function tripal_associate_cvterm($basetable, $record_id, $cvterm) {
   }
 
   return FALSE;
+}
+/**
+ * This function sets the default vocabulary for a given table and field.
+ *
+ * @param $table
+ *   The name of the table that contains a field with a foreign key
+ *   relationship to the cvterm table
+ * @param $field
+ *   The table field name that has the foreign key relationship to the
+ *   cvterm table for which the default vocabulary will be set
+ * @param $cv_name
+ *   The name of the vocabulary
+ *
+ * @return
+ *   TRUE if set, FALSE if an error occured
+ */
+function tripal_set_default_cv($table, $field, $cv_name) {
+
+  // make sure the cv_id is valid
+  $cv = tripal_get_cv(array('name' => $cv_name));
+
+  if ($cv) {
+    // first delete any entries for this table and field
+    $num_deleted = db_delete('tripal_cv_defaults')
+    ->condition('table_name', $table)
+    ->condition('field_name', $field)
+    ->execute();
+
+    // now add the default value
+    $cv_default_id = db_insert('tripal_cv_defaults')
+    ->fields(array(
+      'table_name' => $table,
+      'field_name' => $field,
+      'cv_id'      => $cv->cv_id,
+    ))
+    ->execute();
+
+    if (!$cv_default_id) {
+      tripal_report_error('tripal_cv', TRIPAL_WARNING,
+      "Cannot set default vocabulary for %table.%field. Check the error logs.",
+      array('%table' => $table, '%field' => $field));
+      return FALSE;
+    }
+  }
+  else {
+    tripal_report_error('tripal_cv', TRIPAL_WARNING,
+    "Cannot set default vocabulary for %table.%field. The vocabulary name, '%cvname', doesn't exist.",
+    array('%table' => $table, '%field' => $field, '%cvname' => $cv_name));
+    return FALSE;
+  }
+}
+
+/**
+ * Retreives the default vocabulary for a given table and field
+ * @param $table
+ *   The name of the table that contains a field with a foreign key
+ *   relationship to the cvterm table
+ * @param $field
+ *   The table field name that has the foreign key relationship to the
+ *   cvterm table for which the default vocabulary will be set
+ *
+ * @return
+ *   The cv array of the default vocabulary or an empty array if not
+ *   available.
+ */
+function tripal_get_default_cv($table, $field) {
+  $sql = "
+    SELECT cv_id
+    FROM {tripal_cv_defaults}
+    WHERE table_name = :table and field_name = :field
+  ";
+  $cv_id = db_query($sql, array(':table' => $table, ':field' => $field))->fetchField();
+
+  return tripal_get_cv(array('cv_id' => $cv_id));
+}
+
+/**
+ * Create an options array to be used in a form element
+ * which provides a list of all chado cvterms. Unlike the
+ * tripal_get_cvterm_select_option, this function retreives the cvterms using
+ * the default vocabulary set for a given table and field.  It will also
+ * notify the administrative user if a default vocabulary is missing for the
+ * field and if the vocabulary is empty.  
+ *   
+ * @param $table
+ *   The name of the table that contains the field with a foreign key
+ *   relationship to the cvterm table
+ * @param $field
+ *   The table field name that has the foreign key relationship to the
+ *   cvterm table for which the default vocabulary will be set
+ * @param $field_desc
+ *   A human readable descriptive name for the field
+ *   
+ * @return
+ *   An array(cvterm_id => name)
+ *   for each cvterm in the chado cvterm table where cv_id=that supplied
+ */
+function tripal_get_cvterm_default_options($table, $field, $field_desc) {
+  
+  $default_cv = tripal_get_default_cv($table, $field);
+  $options = array();
+  
+  if ($default_cv) {
+    $options = tripal_get_cvterm_select_options($default_cv->cv_id);
+  }
+  else {
+    tripal_set_message('There is not a default vocabulary set for ' . $field_desc . '. '. 
+      'Please set one using the ' .
+      l('vocabulary defaults configuration page',
+        'admin/tripal/chado/tripal_cv/defaults',
+        array('attributes' => array('target' => '_blank'))) . '.',
+      TRIPAL_WARNING);
+  }
+  if (count($options) == 0) {
+    tripal_set_message('There are no ' . $field_desc . '. Please ' .
+      l('add terms',
+        'admin/tripal/chado/tripal_cv/cv/' .$default_cv->cv_id. '/cvterm/add',
+         array('attributes' => array('target' => '_blank'))) . ' to the ' .
+      $default_cv->name .' vocabulary.',
+      TRIPAL_WARNING);
+  }
+  return $options;
 }

+ 13 - 0
tripal_cv/includes/tripal_cv.admin.inc

@@ -44,3 +44,16 @@ function tripal_cv_admin_cv_listing() {
 
   return $output;
 }
+
+/**
+ * 
+ */
+function tripal_cv_admin_set_defaults_form() {
+  $form = array();
+  
+  $form['instructions'] = array(
+    '#markup' => 'use the following...'
+  );
+  
+  return $form;
+}

+ 1 - 3
tripal_cv/includes/tripal_cv.cvterm_form.inc

@@ -238,9 +238,7 @@ function tripal_cv_add_cvterm_form_fields(&$form, $form_state, $cv_id = 0, $cvte
   $form['fields']['db_id'] = array(
     '#type'         => 'select',
     '#title'         => t('Database'),
-    '#description'   => t('All terms must be assocated with an external database.
-                          Please select the external database to associate with
-                          this term'),
+    '#description'   => t('All terms must be assocated with a database.'),
     '#options'      => $dbs,
     '#default_value' => $db_id,
     '#required' => TRUE,

+ 62 - 3
tripal_cv/tripal_cv.install

@@ -110,7 +110,16 @@ function tripal_cv_create_tripal_obo_temp() {
  * @ingroup tripal_cv
  */
 function tripal_cv_schema() {
-
+  $schema = array();
+  
+  tripal_cv_get_tripal_cv_obo_table($schema);
+  tripal_cv_get_tripal_cv_defaults_table($schema);
+}
+/**
+ * Table definition for the tripal_cv_obo table
+ * @param $schema
+ */
+function tripal_cv_get_tripal_cv_obo_table(&$schema) {
   $schema['tripal_cv_obo'] = array(
     'fields' => array(
       'obo_id' => array(
@@ -132,9 +141,40 @@ function tripal_cv_schema() {
     ),
     'primary key' => array('obo_id'),
   );
+}
 
-
-  return $schema;
+/**
+ * * Table definition for the tripal_cv_defaults table
+ * @param unknown $schema
+ */
+function tripal_cv_get_tripal_cv_defaults_table(&$schema) {
+  $schema['tripal_cv_defaults'] = array(
+    'fields' => array(
+      'cv_default_id' => array(
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE
+      ),
+      'table_name' => array(
+        'type' => 'varchar',
+        'length' => 128,
+        'not null' => TRUE,
+      ),
+      'field_name' => array(
+        'type' => 'varchar',
+        'length' => 128,
+        'not null' => TRUE,
+      ),
+      'cv_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      )
+    ),
+    'indexes' => array(
+      'tripal_cv_defaults_idx1' => array('table_name', 'field_name'),
+    ),
+    'primary key' => array('cv_default_id')
+  );
 }
 
 /**
@@ -208,3 +248,22 @@ function tripal_cv_add_obo_defaults() {
     db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
   }
 }
+
+
+/**
+ * This is the required update for tripal_cv when upgrading from Drupal core API 6.x.
+ *
+ */
+function tripal_cv_update_7200() {
+
+  // add in the new tripal_cv_defaults table
+  try {
+    $schema = array();
+    tripal_cv_get_tripal_cv_defaults_table($schema);
+    db_create_table('tripal_cv_defaults', $schema['tripal_cv_defaults']);
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to create tripal_cv_defaults table: '. $error);
+  }
+}

+ 10 - 0
tripal_cv/tripal_cv.module

@@ -63,6 +63,16 @@ function tripal_cv_menu() {
     'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
+  
+  $items['admin/tripal/chado/tripal_cv/defaults'] = array(
+    'title' => 'Defaults',
+    'description' => 'Set the default vocabularies for properties and relationships.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_cv_admin_set_defaults_form'),
+    'access arguments' => array('administer controlled vocabularies'),
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 5
+  );
 
   $items['admin/tripal/chado/tripal_cv/help'] = array(
     'title' => 'Help',

+ 4 - 2
tripal_featuremap/includes/tripal_featuremap.chado_node.inc

@@ -140,6 +140,8 @@ function chado_featuremap_form($node, &$form_state) {
     'fieldset_name' => 'Additional Details',
     'additional_instructions' => $instructions
   );
+  // TODO: remove the 'Map Dbxref' from the list as that should now be handled
+  // by the dbxref interface below
   chado_add_node_form_properties($form, $form_state, $details);
 
   // ADDITIONAL DBXREFS FORM
@@ -456,9 +458,9 @@ function chado_featuremap_delete(&$node) {
   // drupal database
   $sql_del = "DELETE FROM {chado_featuremap} WHERE nid = :nid AND vid = :vid";
   db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
-  $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
+  $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
   db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
-  $sql_del = "DELETE FROM {node_revisions} WHERE nid = :nid AND vid = :vid";
+  $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
   db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
 
   // Remove data from map and mapprop tables of chado database as well

+ 1 - 1
tripal_library/theme/tripal_library/tripal_library_base.tpl.php

@@ -42,7 +42,7 @@ $rows[] = array(
 
 // Organism row
 $organism = $library->organism_id->genus ." " . $library->organism_id->species ." (" .$library->organism_id->common_name .")";
-if ($library->organism_id->nid) {
+if (property_exists($library->organism_id, 'nid')) {
   $organism = l("<i>" . $library->organism_id->genus . " " . $library->organism_id->species . "</i> (" .$library->organism_id->common_name .")", "node/".$library->organism_id->nid, array('html' => TRUE));
 } 
 $rows[] = array(

+ 1 - 1
tripal_project/includes/tripal_project.chado_node.inc

@@ -320,7 +320,7 @@ function chado_project_delete($node) {
   // drupal database
   $sql_del = "DELETE FROM {chado_project} WHERE nid = :nid AND vid = :vid";
   db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
-  $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vod";
+  $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
   db_query($sql_del,  array(':nid' => $node->nid, ':vid' => $node->vid));
   $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
   db_query($sql_del,  array(':nid' => $node->nid, ':vid' => $node->vid));

+ 2 - 2
tripal_pub/tripal_pub.install

@@ -227,7 +227,7 @@ function tripal_pub_add_cvs() {
   // Add cv for relationship types
   tripal_cv_add_cv(
     'pub_relationship_types',
-    'Contains Types of relationships between publications.'
+    'Contains types of relationships between publications.'
   );
 }
 
@@ -241,7 +241,7 @@ function tripal_pub_add_cvterms() {
 }
 
 /**
- * This is the required update for tripal_contact when upgrading from Drupal core API 6.x.
+ * This is the required update for tripal_pub when upgrading from Drupal core API 6.x.
  *
  */
 function tripal_pub_update_7200() {

+ 15 - 3
tripal_stock/includes/tripal_stock.chado_node.inc

@@ -66,6 +66,10 @@ function chado_stock_load($nodes) {
     // build the variable with all the stock details
     $values = array('stock_id' => $stock_id);
     $stock = chado_generate_var('stock', $values);
+    
+    // add in the uniquename and the description as these are both text fields
+    $stock = chado_expand_var('stock', 'field', 'stock.uniquename');
+    $stock = chado_expand_var('stock', 'field', 'stock.description');
 
     // by default, the titles are saved using the unique constraint.  We will
     // keep it the same, but remove the duplicate name if the unique name and name
@@ -124,6 +128,7 @@ function chado_stock_load($nodes) {
  */
 function chado_stock_form($node, $form_state) {
 
+  /* I don't think we need this... commenting out but leaving just in case
   // If existing stock then expand all fields needed using the chado API
   if (isset($node->nid)) {
     $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
@@ -136,6 +141,8 @@ function chado_stock_form($node, $form_state) {
       }
     }
   }
+  */
+  //TODO: @lacey can you take a look at the above code?
 
   // Default values can come in the following ways:
   //
@@ -233,8 +240,9 @@ function chado_stock_form($node, $form_state) {
     '#title' =>  t('Stock Details')
   );
 
-  $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
+  $type_options = tripal_get_cvterm_default_options('stock', 'type_id', 'stock types');
   $type_options[0] = 'Select a Type';
+  
   $form['details']['type_id'] = array(
     '#type' => 'select',
     '#title' => t('Type of Stock'),
@@ -297,10 +305,12 @@ function chado_stock_form($node, $form_state) {
 
   // PROPERTIES FORM
   //---------------------------------------------
+  $prop_cv = tripal_get_default_cv('stockprop', 'type_id');
+  $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
   $details = array(
     'property_table' => 'stockprop',
     'chado_id' => $stock_id,
-    'cv_id' => variable_get('chado_stock_prop_types_cv', FALSE)
+    'cv_id' => $cv_id
   );
   chado_add_node_form_properties($form, $form_state, $details);
 
@@ -315,13 +325,15 @@ function chado_stock_form($node, $form_state) {
 
   // RELATIONSHIPS FORM
   //---------------------------------------------
+  $relationship_cv = tripal_get_default_cv('stock_relationship', 'type_id');
+  $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
   $details = array(
     'relationship_table' => 'stock_relationship',
     'base_table' => 'stock',
     'base_foreign_key' => 'stock_id',
     'base_key_value' => $stock_id,
     'nodetype' => 'stock',
-    'cv_id' => variable_get('chado_stock_relationship_cv', 0)
+    'cv_id' => $cv_id
   );
   chado_add_node_form_relationships($form, $form_state, $details);
 

+ 126 - 0
tripal_stock/tripal_stock.install

@@ -49,6 +49,11 @@ function tripal_stock_requirements($phase) {
 function tripal_stock_install() {
   // create the module's data directory
   tripal_create_files_dir('tripal_stock');
+  
+  // set the default vocabularies
+  tripal_set_default_cv('stock', 'type_id', 'stock_type');
+  tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
+  tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
 }
 
 /**
@@ -96,3 +101,124 @@ function tripal_stock_schema() {
 
   return $schema;
 }
+
+/**
+ * Add cvs related to publications
+ *
+ * @ingroup tripal_pub
+ */
+function tripal_stock_add_cvs() {
+
+  // Add cv for relationship types
+  tripal_cv_add_cv(
+    'stock_relationship',
+    'Contains types of relationships between stocks.'
+  );
+  tripal_cv_add_cv(
+    'stock_property',
+    'Contains properties for stocks.'
+  );
+  tripal_cv_add_cv(
+    'stock_type',
+    'Contains a list of types for stocks.'
+  );
+}
+
+/**
+ * Add cvterms related to publications
+ *
+ * @ingroup tripal_pub
+ */
+function tripal_stock_add_cvterms() {
+
+}
+
+/**
+ * This is the required update for tripal_stock when upgrading from Drupal core API 6.x.
+ *
+ */
+function tripal_stock_update_7200() {
+  // add the new CVs.  We can't use the Tripal API because during
+  // an upgrade from D6 to D7 Tripal is disable. So, we have to manually add these
+  // new vocabularies.
+  
+  // add the stock_relationshp CV
+  try {
+    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_relationship'")->fetchObject();
+    if (!$check->cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+        ->fields(array(
+          'name' => 'stock_relationship',
+          'definition' => 'Contains types of relationships between stocks.'
+        ))
+        ->execute();
+      // make this the default cv for the stock_relationships.type_id field
+      db_insert('tripal_cv_defaults')
+        ->fields(array(
+          'table_name' => 'stock_relationship',
+          'field_name' => 'type_id',
+          'cv_id' => $cv_id
+        ))
+        ->execute();
+    }
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add stock_relationship vocabulary: '. $error);
+  }
+ 
+  // add the stock_property CV
+  try {
+    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_property'")->fetchObject();
+    if (!$check->cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+        ->fields(array(
+          'name' => 'stock_property',
+          'definition' => 'Contains properties for stocks.'
+        ))
+        ->execute();
+      // make this the default vocabulary for the stockprop.type_id field
+      db_insert('tripal_cv_defaults')
+        ->fields(array(
+        'table_name' => 'stockprop',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+        ))
+        ->execute();
+    }
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add stock_property vocabulary: '. $error);
+  }
+  
+  
+  // add the stock_type CV
+  try {
+    $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_type'")->fetchObject();
+    if (!$check->cv_id) {
+      // add the vocabulary
+      $cv_id = db_insert('chado.cv')
+      ->fields(array(
+        'name' => 'stock_type',
+        'definition' => 'Contains a list of types for stocks.'
+      ))
+      ->execute();
+      // add this vocabulary as the default cv for stock.type_id
+      db_insert('tripal_cv_defaults')
+        ->fields(array(
+        'table_name' => 'stock',
+        'field_name' => 'type_id',
+        'cv_id' => $cv_id
+        ))
+        ->execute();
+    }
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Failed to add stock_type vocabulary: '. $error);
+  }
+  
+}