Browse Source

Contact: Added relationship form; Relationship API: fixed bug so works with non-ranked relationship tables

Lacey Sanderson 11 years ago
parent
commit
3df4ac86f3

+ 4 - 4
tripal_analysis/includes/tripal_analysis.chado_node.inc

@@ -224,10 +224,10 @@ function chado_analysis_form($node, &$form_state) {
   // ----------------------------------
   $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") . ".");
   $details = array(
-    'property_table' => 'analysisprop',
-    'base_foreign_key' => 'analysis_id',
-    'base_key_value' => $analysis_id,
-    'cv_name' => 'analysis_property',
+    'property_table' => 'analysisprop',             // the name of the prop table
+    'base_foreign_key' => 'analysis_id',            // the name of the key in your base chado table
+    'base_key_value' => $analysis_id,               // the value of analysis_id for this record
+    'cv_name' => 'analysis_property',               // the cv.name of the cv governing analysisprop.type_id
     'fieldset_title' => 'Properties',
     'additional_instructions' => $instructions
   );

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

@@ -91,7 +91,7 @@ function chado_contact_form(&$node, $form_state) {
     }
     if (!$description) {
       $contactprop = tripal_contact_get_property($contact->contact_id, 'contact_description');
-      $description = $contactprop->value;
+      $description = (isset($contactprop->value)) ? $contactprop->value : '';
     }
 
     // set the contact_id in the form
@@ -188,8 +188,6 @@ function chado_contact_form(&$node, $form_state) {
     }
   }
 
-  // D7 @TODO: Properties API doesn't handle Exclude
-  $exclude = array('contact_description');
   $details = array(
     'property_table' => 'contactprop',
     'base_foreign_key' => 'contact_id',
@@ -199,6 +197,33 @@ function chado_contact_form(&$node, $form_state) {
   );
   chado_node_properties_form($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 = tripal_core_chado_select('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 = tripal_core_chado_select('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
+
+  $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
+    'base_name_field' => 'name',                    // the base table field you want to be used as the name
+    'select_options' => $select_options
+  );
+  // Adds the form elements to your current form
+  chado_node_relationships_form($form, $form_state, $details);
+
   return $form;
 }
 
@@ -340,14 +365,26 @@ function chado_contact_insert($node) {
     }
     $contact_id = $contact['contact_id'];
 
-    // now add in the properties
+      // Add the description property
+    $properties = chado_node_properties_form_retreive($node);
+    $contact_descrip_id = tripal_cv_get_cvterm_by_name('contact_description', NULL, 'tripal_contact');
+    $properties[$contact_descrip_id->cvterm_id][0] = $node->description;
+
+    // * Properties Form *
     $details = array(
       'property_table' => 'contactprop',
       'base_table' => 'contact',
       'foreignkey_name' => 'contact_id',
       'foreignkey_value' => $contact_id
     );
-    chado_node_properties_form_update_properties($node, $details);
+    chado_node_properties_form_update_properties($node, $details, $properties);
+
+    // * Relationships Form *
+    $details = array(
+      'relationship_table' => 'contact_relationship',  // name of the _relationship table
+      'foreignkey_value' => $contact_id                // value of the contact_id key
+    );
+    chado_node_relationships_form_update_relationships($node, $details);
   }
   else {
     $contact_id = $node->contact_id;
@@ -402,6 +439,11 @@ function chado_contact_update($node) {
     return;
   }
 
+  // Add the description property
+  $properties = chado_node_properties_form_retreive($node);
+  $contact_descrip_id = tripal_cv_get_cvterm_by_name('contact_description', NULL, 'tripal_contact');
+  $properties[$contact_descrip_id->cvterm_id][0] = $node->description;
+
   // now add in the properties by first removing any the contact
   // already has and adding the ones we have
   $details = array(
@@ -410,10 +452,14 @@ function chado_contact_update($node) {
     'foreignkey_name' => 'contact_id',
     'foreignkey_value' => $contact_id
   );
-  chado_node_properties_form_update_properties($node, $details);
+  chado_node_properties_form_update_properties($node, $details, $properties);
 
-  // add in the description as a separate property
-  tripal_contact_update_property($contact_id, 'contact_description', $node->description, 1);
+  // * Relationships Form *
+  $details = array(
+    'relationship_table' => 'contact_relationship',  // name of the _relationship table
+    'foreignkey_value' => $contact_id                // value of the contact_id key
+  );
+  chado_node_relationships_form_update_relationships($node, $details);
 }
 
 

+ 18 - 1
tripal_contact/tripal_contact.install

@@ -58,7 +58,11 @@ function tripal_contact_install() {
   $obo_path = drupal_realpath(drupal_get_path('module', 'tripal_contact') . '/files/tcontact.obo');
   $obo_id = tripal_cv_add_obo_ref('Tripal Contacts', $obo_path);
   tripal_cv_submit_obo_job($obo_id);
-  
+
+  // 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')
@@ -85,6 +89,19 @@ function tripal_contact_uninstall() {
     */
 }
 
+function tripal_contact_add_cvs() {
+
+  tripal_cv_add_cv(
+    'contact_relationship_types',
+    'Contains Types of relationships between contacts.'
+  );
+
+}
+
+function tripal_contact_add_cvterms() {
+
+}
+
 /**
  * Implementation of hook_schema().
  */

+ 52 - 29
tripal_core/api/tripal_core.chado_nodes.relationships.api.inc

@@ -118,6 +118,7 @@
  *     - nodetype_plural: the non-translated plural title of this node type
  *     - select_options: must be an array where the [key] is a valid cvterm_id and
  *       the [value] is the human-readable name of the option. This is generated from the cv_name/id by default
+ *     - base_name_field: the field in your base table you want to be used as the name of the record
  *
  * @ingroup tripal_chado_node_api
  */
@@ -129,6 +130,12 @@ function chado_node_relationships_form(&$form, &$form_state, $details) {
   $details['fieldset_title'] = (isset($details['fieldset_title'])) ? $details['fieldset_title'] : 'Relationships';
   $details['additional_instructions'] = (isset($details['additional_instructions'])) ? $details['additional_instructions'] : '';
   $details['nodetype_plural']  = (isset($details['nodetype_plural'])) ? $details['nodetype_plural'] : $details['nodetype'] . 's';
+  $details['base_name_field'] = (isset($details['base_name_field'])) ? $details['base_name_field'] : 'uniquename';
+
+  // Some relationship tables don't have a rank
+  // thus we need to first check this table has a rank before trying to set it
+  $table_schema = tripal_core_get_chado_table_schema($details['relationship_table']);
+  $details['table_has_rank'] = (isset($table_schema['fields']['rank'])) ? TRUE : FALSE;
 
   // Get Property Types for the Select List
   if (isset($details['select_options'])) {
@@ -215,7 +222,11 @@ function chado_node_relationships_form(&$form, &$form_state, $details) {
   }
   else {
     $existing_rels = chado_query(
-      "SELECT rel.*, base1.uniquename as object_name, base2.uniquename as subject_name, cvterm.name as type_name
+      "SELECT
+          rel.*,
+          base1.".$details['base_name_field']." as object_name,
+          base2.".$details['base_name_field']." as subject_name,
+          cvterm.name as type_name
         FROM {".$details['relationship_table']."} rel
         LEFT JOIN {".$details['base_table']."} base1 ON base1.".$details['base_foreign_key']." = rel.object_id
         LEFT JOIN {".$details['base_table']."} base2 ON base2.".$details['base_foreign_key']." = rel.subject_id
@@ -263,51 +274,53 @@ function chado_node_relationships_form(&$form, &$form_state, $details) {
   foreach ($existing_rels as $relationship) {
     if (array_key_exists($relationship->type_id, $type_options)) {
 
+      $rank = (isset($relationship->rank)) ? $relationship->rank : 0;
+
       $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = 'markup';
       $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = '';
 
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['#type'] = 'markup';
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['#value'] = '';
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['#type'] = 'markup';
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['#value'] = '';
 
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['object_id'] = array(
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['object_id'] = array(
         '#type' => 'hidden',
         '#value' => $relationship->object_id
       );
 
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['subject_id'] = array(
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['subject_id'] = array(
         '#type' => 'hidden',
         '#value' => $relationship->subject_id
       );
 
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['type_id'] = array(
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['type_id'] = array(
         '#type' => 'hidden',
         '#value' => $relationship->type_id
       );
 
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['object_name'] = array(
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['object_name'] = array(
         '#type' => 'markup',
         '#markup' => $relationship->object_name
       );
 
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['type_name'] = array(
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['type_name'] = array(
         '#type' => 'markup',
         '#markup' => $relationship->type_name
       );
 
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['subject_name'] = array(
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['subject_name'] = array(
         '#type' => 'markup',
         '#markup' => $relationship->subject_name
       );
 
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['rank'] = array(
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['rank'] = array(
         '#type' => 'markup',
-        '#markup' => $relationship->rank
+        '#markup' => $rank
       );
 
-      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['rel_action'] = array(
+      $form['relationships']['relationship_table'][$relationship->type_id][$rank]['rel_action'] = array(
         '#type' => 'submit',
         '#value' => t('Remove'),
-        '#name' => "rel_remove-".$relationship->type_id.'-'.$relationship->rank,
+        '#name' => "rel_remove-".$relationship->type_id.'-'.$rank,
         '#ajax' => array(
           'callback' => 'chado_node_relationships_form_ajax_update',
           'wrapper' => 'tripal-generic-edit-relationships-table',
@@ -401,6 +414,7 @@ function chado_node_relationships_form(&$form, &$form_state, $details) {
 function chado_node_relationships_form_add_button_validate($form, &$form_state) {
 
   $details = unserialize($form_state['values']['relationship_table']['details']);
+  $details['base_name_field'] = (isset($details['base_name_field'])) ? $details['base_name_field'] : 'uniquename';
 
   // At least one of the participants must be the current node
   if (!($form_state['values']['relationship_table']['new']['subject_is_current'] OR $form_state['values']['relationship_table']['new']['object_is_current'])) {
@@ -429,7 +443,7 @@ function chado_node_relationships_form_add_button_validate($form, &$form_state)
     $result = tripal_core_chado_select(
       $details['base_table'],
       array($details['base_foreign_key']),
-      array('uniquename' => $form_state['values']['relationship_table']['new']['subject_name'])
+      array($details['base_name_field'] => $form_state['values']['relationship_table']['new']['subject_name'])
     );
     if (!isset($result[0])) {
       form_set_error('subject_name', 'The subject must be the unique name of an
@@ -445,7 +459,7 @@ function chado_node_relationships_form_add_button_validate($form, &$form_state)
     $result = tripal_core_chado_select(
       $details['base_table'],
       array($details['base_foreign_key']),
-      array('uniquename' => $form_state['values']['relationship_table']['new']['object_name'])
+      array($details['base_name_field'] => $form_state['values']['relationship_table']['new']['object_name'])
     );
     if (!isset($result[0])) {
       form_set_error('object_name', 'The object must be the unique name of an
@@ -638,10 +652,13 @@ function chado_node_relationships_form_create_relationship_formstate_array($form
 function theme_chado_node_relationships_form_table($variables) {
   $element = $variables['element'];
 
+  $details = unserialize($element['details']['#value']);
+  $details['base_name_field'] = (isset($details['base_name_field'])) ? $details['base_name_field'] : 'uniquename';
+
   $header = array(
-    'object_name' => t('Object Unique Name'),
+    'object_name' => t('Object ' . $details['base_name_field']),
     'type_name' => t('Type'),
-    'subject_name' => t('Subject Unique Name'),
+    'subject_name' => t('Subject ' . $details['base_name_field']),
     'rank' => t('Rank'),
     'rel_action' => t('Action')
   );
@@ -741,6 +758,11 @@ function chado_node_relationships_form_update_relationships($node, $details, $re
   $current_id = $details['foreignkey_value'];
 
   if (isset($node->relationship_table) AND ($current_id > 0)) {
+
+    // determine whether there is a rank in this relationship table
+    $form_details = unserialize($node->relationship_table['details']);
+    $has_rank = $form_details['table_has_rank'];
+
     // First remove existing relationships links
     tripal_core_chado_delete($relationship_table, array('subject_id' => $current_id));
     tripal_core_chado_delete($relationship_table, array('object_id' => $current_id));
@@ -758,8 +780,7 @@ function chado_node_relationships_form_update_relationships($node, $details, $re
         $values = array(
           'subject_id' => $element['subject_id'],
           'type_id' => $type_id,
-          'object_id' => $element['object_id'],
-          'rank' => $rank
+          'object_id' => $element['object_id']
         );
 
         // Set the current id if not already
@@ -771,16 +792,18 @@ function chado_node_relationships_form_update_relationships($node, $details, $re
           $values['object_id'] = $current_id;
         }
 
-        // Ensure that the rank is Set & Current
-        $rank_select = tripal_core_get_max_chado_rank(
-          $relationship_table,
-          array(
-            'subject_id' => $values['subject_id'],
-            'type_id' => $values['type_id'],
-            'object_id' => $values['object_id'],
-          )
-        );
-        $values['rank'] = $rank_select + 1;
+        if ($has_rank) {
+          // Ensure that the rank is Set & Current
+          $rank_select = tripal_core_get_max_chado_rank(
+            $relationship_table,
+            array(
+              'subject_id' => $values['subject_id'],
+              'type_id' => $values['type_id'],
+              'object_id' => $values['object_id'],
+            )
+          );
+          $values['rank'] = $rank_select + 1;
+        }
 
         // add relationship
         $success_link = tripal_core_chado_insert(

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

@@ -174,7 +174,7 @@ function chado_example_form($node, &$form_state) {
     '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_id' => 'example_relationship_types'         // the cv.name of the cv governing example_relationship.type_id
+    'cv_name' => 'example_relationship_types'         // the cv.name of the cv governing example_relationship.type_id
   );
   // Adds the form elements to your current form
   chado_node_relationships_form($form, $form_state, $details);