Kaynağa Gözat

Merge branch '7.x-2.x' of git.drupal.org:sandbox/spficklin/1337878 into 7.x-2.x

Stephen Ficklin 11 yıl önce
ebeveyn
işleme
a1fffe100c

+ 26 - 30
tripal_core/api/tripal_core.chado_nodes.relationships.api.inc

@@ -119,6 +119,8 @@
  *     - 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
+ *     - subject_field_name: the name of the subject field in your relationship table (default: subject_id)
+ *     - object_field_name: the name of the object field in your relationship table (default: object_id)
  *
  * @ingroup tripal_chado_node_api
  */
@@ -131,6 +133,8 @@ function chado_node_relationships_form(&$form, &$form_state, $details) {
   $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';
+  $details['subject_field_name'] = (isset($details['subject_field_name'])) ? $details['subject_field_name'] : 'subject_id';
+  $details['object_field_name'] = (isset($details['object_field_name'])) ? $details['object_field_name'] : 'object_id';
 
   // Some relationship tables don't have a rank
   // thus we need to first check this table has a rank before trying to set it
@@ -224,14 +228,17 @@ function chado_node_relationships_form(&$form, &$form_state, $details) {
     $existing_rels = chado_query(
       "SELECT
           rel.*,
+          rel.".$details['subject_field_name']." as subject_id,
+          rel.".$details['object_field_name']." as object_id,
           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
+        LEFT JOIN {".$details['base_table']."} base1 ON base1.".$details['base_foreign_key']." = rel.".$details['object_field_name']."
+        LEFT JOIN {".$details['base_table']."} base2 ON base2.".$details['base_foreign_key']." = rel.".$details['subject_field_name']."
         LEFT JOIN {cvterm} cvterm ON cvterm.cvterm_id = rel.type_id
-        WHERE rel.object_id = :base_key_value OR rel.subject_id = :base_key_value",
+        WHERE rel.".$details['object_field_name']." = :base_key_value
+            OR rel.".$details['subject_field_name']." = :base_key_value",
         array(':base_key_value' => $details['base_key_value'])
     );
   }
@@ -414,7 +421,6 @@ 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'])) {
@@ -535,21 +541,6 @@ function chado_node_relationships_form_add_button_submit(&$form, &$form_state) {
     );
   }
 
-  // get max rank
-  /**
-  if (isset($form_state['node']->{$details['base_table']}->{$details['base_foreign_key']})) {
-    $rank = tripal_core_get_max_chado_rank(
-      $details['relationship_table'],
-      array(
-        'subject_id' => $relationship['subject_id'],
-        'type_id' => $relationship['type_id'],
-        'object_id' => $relationship['object_id'],
-      )
-    );
-    $relationship['rank'] = strval($rank + 1);
-  }
-  */
-
   $key = $relationship['type_id'] . '-' . $relationship['rank'];
   $form_state['chado_relationships'][$key] = (object) $relationship;
 
@@ -653,7 +644,6 @@ 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 ' . $details['base_name_field']),
@@ -764,8 +754,14 @@ function chado_node_relationships_form_update_relationships($node, $details, $re
     $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));
+    tripal_core_chado_delete(
+      $relationship_table,
+      array($form_details['subject_field_name'] => $current_id)
+    );
+    tripal_core_chado_delete(
+      $relationship_table,
+      array($form_details['object_field_name'] => $current_id)
+    );
 
     // Add back in relationships as needed
     if ($retrieved_relationships) {
@@ -778,18 +774,18 @@ function chado_node_relationships_form_update_relationships($node, $details, $re
       foreach ($ranks as $rank => $element) {
 
         $values = array(
-          'subject_id' => $element['subject_id'],
+          $form_details['subject_field_name'] => $element['subject_id'],
           'type_id' => $type_id,
-          'object_id' => $element['object_id']
+          $form_details['object_field_name'] => $element['object_id']
         );
 
         // Set the current id if not already
         // this is usually only necessary in an insert
-        if (empty($values['subject_id'])) {
-          $values['subject_id'] = $current_id;
+        if (empty($values[$form_details['subject_field_name']])) {
+          $values[$form_details['subject_field_name']] = $current_id;
         }
-        if (empty($values['object_id'])) {
-          $values['object_id'] = $current_id;
+        if (empty($values[$form_details['object_field_name']])) {
+          $values[$form_details['object_field_name']] = $current_id;
         }
 
         if ($has_rank) {
@@ -797,9 +793,9 @@ function chado_node_relationships_form_update_relationships($node, $details, $re
           $rank_select = tripal_core_get_max_chado_rank(
             $relationship_table,
             array(
-              'subject_id' => $values['subject_id'],
+              $form_details['subject_field_name'] => $values['subject_id'],
               'type_id' => $values['type_id'],
-              'object_id' => $values['object_id'],
+              $form_details['object_field_name'] => $values['object_id'],
             )
           );
           $values['rank'] = $rank_select + 1;

+ 64 - 12
tripal_project/includes/tripal_project.chado_node.inc

@@ -135,22 +135,56 @@ function chado_project_form(&$node, $form_state) {
 
   // Properties Form
   // ----------------------------------
-  // D7 @TODO: Properties API doesn't handle exclude
   // 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.
-  $exclude = array('Project Description');
+  $cv_result = tripal_core_chado_select('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',
     'base_foreign_key' => 'project_id',
     'base_key_value' => $project_id,
     'cv_name' => 'project_property',
-    'additional_instructions' => $instructions
+    'additional_instructions' => $instructions,
+    'select_options' => $select_options
   );
   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' => '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 = 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' => '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
+    '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',
+    'select_options' => $select_options
+  );
+  // Adds the form elements to your current form
+  chado_node_relationships_form($form, $form_state, $details);
+
   return $form;
 
 }
@@ -221,17 +255,27 @@ function chado_project_insert($node) {
     }
     $project_id = $project['project_id'];
 
-    // now add in the properties
+    // * Properties Form *
+    // Add the description property
+    $properties = chado_node_properties_form_retreive($node);
+    $descrip_id = tripal_cv_get_cvterm_by_name('Project Description', NULL, 'project_property');
+    $properties[$descrip_id->cvterm_id][0] = $node->description;
+
     $details = array(
       'property_table' => 'projectprop',
       'base_table' => 'project',
       'foreignkey_name' => 'project_id',
       'foreignkey_value' => $project_id
     );
-    chado_node_properties_form_update_properties($node, $details);
+    chado_node_properties_form_update_properties($node, $details, $properties);
+
+    // * Relationships Form *
+    $details = array(
+      'relationship_table' => 'project_relationship',  // name of the _relationship table
+      'foreignkey_value' => $project_id                // value of the example_id key
+    );
+    chado_node_relationships_form_update_relationships($node, $details);
 
-    // add in the description as a separate property
-    tripal_project_insert_property($project_id, 'Project Description', $node->description, FALSE);
   }
   else {
     $project_id = $node->project_id;
@@ -313,18 +357,26 @@ function chado_project_update($node) {
     array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
   }
 
-  // now add in the properties by first removing any the project
-  // already has and adding the ones we have
+  // * Properties Form *
+  // Add the description property
+  $properties = chado_node_properties_form_retreive($node);
+  $descrip_id = tripal_cv_get_cvterm_by_name('Project Description', NULL, 'project_property');
+  $properties[$descrip_id->cvterm_id][0] = $node->description;
+
   $details = array(
     'property_table' => 'projectprop',
     'base_table' => 'project',
     'foreignkey_name' => 'project_id',
     'foreignkey_value' => $project_id
   );
-  chado_node_properties_form_update_properties($node, $details);
+  chado_node_properties_form_update_properties($node, $details, $properties);
 
-  // add the project description as a property
-  tripal_project_update_property($project_id, 'Project Description', $node->description, 1);
+  // * Relationships Form *
+  $details = array(
+    'relationship_table' => 'project_relationship',  // name of the _relationship table
+    'foreignkey_value' => $project_id                // value of the example_id key
+  );
+  chado_node_relationships_form_update_relationships($node, $details);
 }
 
 /**