Browse Source

Fixed linker_relationship field and worked on prop_adder field

Chun-Huai Cheng 8 years ago
parent
commit
f14cda2e74

+ 70 - 70
tripal_chado/includes/TripalFields/chado_linker__prop_adder.inc

@@ -91,84 +91,84 @@ class chado_linker__prop_adder extends TripalField {
       $form_state['triggering_element']['#name'] == 'kvproperty_adder_button') {
 
       $form_state['rebuild'] = TRUE;
-      $field_name = $element['#field_name'];
-      $entity_type = $element['#entity']->type;
-      $bundle = $element['#entity']->bundle;
+      $field_name = $this->field['field_name'];
+      $bundle = $entity->bundle;
 
       // Get the base table name from the field properties.
-      $field = field_info_field($field_name);
+      $field =$this->field;
       $base_table = $field['settings']['base_table'];
 
       // Get the term for the property
-      //$kvproperty = tripal_chado_get_field_form_values($field_name, $form_state);
-      $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
-
-      if (count($term) == 1) {
-        $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
-
-        // The field name is the table name in this case. We want to get the
-        // primary key as this should be the field that maps th the value.
-        $schema = chado_get_schema($field_name);
-        $pkey = $schema['primary key'][0];
-
-        // Add the field if it doesn't already exists.
-        $field = field_info_field('cvterm');
-        if (!$field) {
-          $create_info = array(
-            'field_name' => 'property-' . $term[0]->cvterm_id,
-            'type' => 'tripal_chado_kvproperty_widget',
-            'cardinality' => FIELD_CARDINALITY_UNLIMITED,
-            'locked' => FALSE,
-            'storage' => array(
-              'type' => 'field_chado_storage',
-            ),
-            'settings' => array(
-              'chado_table' => $field_name,
-              'chado_column' => $pkey,
-              'base_table' => $base_table,
-              'semantic_web' => '',
-            ),
-          );
-          $field = field_create_field($create_info);
-        }
-
-        // Attach the field to the bundle if it isn't already.
-        if (!$field and array_key_exists('bundles', $field) or
-            !array_key_exists('TripalEntity', $field['bundles']) or
-            !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
-
-              $createInstanceInfo = array(
-                'field_name' => 'property-' . $term[0]->cvterm_id,
-                'entity_type' => 'TripalEntity',
-                'bundle' => $this->bundle->name,
-                'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
-                'description' => $term[0]->definition ? $term[0]->definition : '',
-                'required' => FALSE,
-                'settings' => array(),
-                'widget' => array(
-                  'type' => 'tripal_chado_kvproperty_widget',
-                  'settings' => array(
-                    'display_label' => 1,
+      foreach ($items as $delta => $values) {
+        $kvproperty = tripal_get_field_item_keyval($items, $delta, 'value', '');
+        $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
+
+        if (count($term) == 1) {
+          $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
+
+          // The field name is the table name in this case. We want to get the
+          // primary key as this should be the field that maps th the value.
+          $schema = chado_get_schema($field_name);
+          $pkey = $schema['primary key'][0];
+  
+          // Add the field if it doesn't already exists.
+          $field = field_info_field('cvterm');      dpm('property_' . $term[0]->cvterm_id);
+          if (!$field) {
+            $create_info = array(
+              'field_name' => 'property_' . $term[0]->cvterm_id,
+              'type' => 'tripal_chado_kvproperty_widget',
+              'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+              'locked' => FALSE,
+              'storage' => array(
+                'type' => 'field_chado_storage',
+              ),
+              'settings' => array(
+                'chado_table' => $field_name,
+                'chado_column' => $pkey,
+                'base_table' => $base_table,
+                'semantic_web' => '',
+              ),
+            );
+            $field = field_create_field($create_info);
+          }
+
+          // Attach the field to the bundle if it isn't already.
+          if (!$field and array_key_exists('bundles', $field) or
+              !array_key_exists('TripalEntity', $field['bundles']) or
+              !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
+  
+                $createInstanceInfo = array(
+                  'field_name' => 'property_' . $term[0]->cvterm_id,
+                  'entity_type' => 'TripalEntity',
+                  'bundle' => $this->bundle->name,
+                  'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
+                  'description' => $term[0]->definition ? $term[0]->definition : '',
+                  'required' => FALSE,
+                  'settings' => array(),
+                  'widget' => array(
+                    'type' => 'tripal_chado_kvproperty_widget',
+                    'settings' => array(
+                      'display_label' => 1,
+                    ),
                   ),
-                ),
-                'display' => array(
-                  'default' => array(
-                    'label' => 'inline',
-                    'type' => 'tripal_chado_kvproperty_formatter',
-                    'settings' => array(),
+                  'display' => array(
+                    'default' => array(
+                      'label' => 'inline',
+                      'type' => 'tripal_chado_kvproperty_formatter',
+                      'settings' => array(),
+                    ),
                   ),
-                ),
-              );
-              $instance = field_create_instance($createInstanceInfo);
-            }
-      }
-      else if (count($term) > 1) {
-        form_set_error(implode('][', $element ['#parents']) . '][value', t("This term is present in multiple vocabularies. Please select the appropriate one."));
-      }
-      else {
-        form_set_error(implode('][', $element ['#parents']) . '][value', t("Please provide a property type to add."));
+                );
+                //$instance = field_create_instance($createInstanceInfo);
+              }
+        }
+        else if (count($term) > 1) {
+          form_set_error(implode('][', $element ['#parents']) . '][value', t("This term is present in multiple vocabularies. Please select the appropriate one."));
+        }
+        else {
+          form_set_error(implode('][', $element ['#parents']) . '][value', t("Please provide a property type to add."));
+        }
       }
     }
   }
-
 }

+ 60 - 12
tripal_chado/includes/TripalFields/chado_linker__relationship.inc

@@ -156,14 +156,30 @@ class chado_linker__relationship extends TripalField {
     $object_uniquename = '';
     $value = '';
     $rank = '';
+    
+    // Handle special cases
+    $subject_id_key = 'subject_id';
+    $object_id_key = 'object_id';
+    if ($chado_table == 'organism_relationship') {
+      $subject_id_key = 'subject_organism_id';
+      $object_id_key = 'object_organism_id';
+    }
+    else if ($rel_table == 'nd_reagent_relationship') {
+      $subject_id_key = 'subject_reagent_id';
+      $object_id_key = 'object_reagent_id';
+    }
+    else if ($rel_table == 'project_relationship') {
+      $subject_id_key = 'subject_project_id';
+      $object_id_key = 'object_project_id';
+    }
 
     // If the field already has a value then it will come through the $items
     // array.  This happens when editing an existing record.
     if (array_key_exists($delta, $items)) {
       $record_id = isset($items[$delta][$field_table . '__' . $pkey]) ? $items[$delta][$field_table . '__' . $pkey] : '';
-      $subject_id = isset($items[$delta][$field_table . '__subject_id']) ? $items[$delta][$field_table . '__subject_id'] : '';
+      $subject_id = isset($items[$delta][$field_table . '__' . $subject_id_key]) ? $items[$delta][$field_table . '__' . $subject_id_key] : '';
       $type_id = isset($items[$delta][$field_table . '__type_id']) ? $items[$delta][$field_table . '__type_id'] : '';
-      $object_id = isset($items[$delta][$field_table . '__object_id']) ? $items[$delta][$field_table . '__object_id'] : '';
+      $object_id = isset($items[$delta][$field_table . '__' . $object_id_key]) ? $items[$delta][$field_table . '__' . $object_id_key] : '';
 
       if (isset($items[$delta][$field_table . '__value'])) {
         $value = $items[$delta][$field_table . '__value'];
@@ -208,7 +224,7 @@ class chado_linker__relationship extends TripalField {
       '#type' => 'value',
       '#default_value' => $record_id,
     );
-    $widget[$field_table . '__subject_id'] = array(
+    $widget[$field_table . '__' . $subject_id_key] = array(
       '#type' => 'value',
       '#default_value' => $subject_id,
     );
@@ -216,7 +232,7 @@ class chado_linker__relationship extends TripalField {
       '#type' => 'value',
       '#default_value' => $type_id,
     );
-    $widget[$field_table . '__object_id'] = array(
+    $widget[$field_table . '__' . $object_id_key] = array(
       '#type' => 'value',
       '#default_value' => $object_id,
     );
@@ -237,7 +253,7 @@ class chado_linker__relationship extends TripalField {
       '#title' => t('Subject'),
       '#default_value' => $subject_uniquename,
       '#required' => $element['#required'],
-      '#maxlength' => array_key_exists('length', $schema['fields']['subject_id']) ? $schema['fields']['subject_id']['length'] : 255,
+      '#maxlength' => array_key_exists('length', $schema['fields'][$subject_id_key]) ? $schema['fields'][$subject_id_key]['length'] : 255,
       '#size' => 35,
       '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
     );
@@ -400,7 +416,7 @@ class chado_linker__relationship extends TripalField {
       '#title' => t('Object'),
       '#default_value' => $object_uniquename,
       '#required' => $element['#required'],
-      '#maxlength' => array_key_exists('length', $schema['fields']['object_id']) ? $schema['fields']['object_id']['length'] : 255,
+      '#maxlength' => array_key_exists('length', $schema['fields'][$object_id_key]) ? $schema['fields'][$object_id_key]['length'] : 255,
       '#size' => 35,
       '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
     );
@@ -418,9 +434,25 @@ class chado_linker__relationship extends TripalField {
     $schema = chado_get_schema($field_table);
     $fkeys = $schema['foreign keys'];
 
+    // Handle special cases
+    $subject_id_key = 'subject_id';
+    $object_id_key = 'object_id';
+    if ($field_table == 'organism_relationship') {
+      $subject_id_key = 'subject_organism_id';
+      $object_id_key = 'object_organism_id';
+    }
+    else if ($rel_table == 'nd_reagent_relationship') {
+      $subject_id_key = 'subject_reagent_id';
+      $object_id_key = 'object_reagent_id';
+    }
+    else if ($rel_table == 'project_relationship') {
+      $subject_id_key = 'subject_project_id';
+      $object_id_key = 'object_project_id';
+    }
+    
     foreach ($items as $delta => $item) {
-      $subject_id = $item[$field_table . '__subject_id'];
-      $object_id = $item[ $field_table . '__object_id'];
+      $subject_id = $item[$field_table . '__' . $subject_id_key];
+      $object_id = $item[ $field_table . '__' . $object_id_key];
       $type_id = $item[$field_table . '__type_id'];
       $type_id = isset($item['type_id']) ? $item['type_id'] : $type_id;
       $type_name = isset($item['type_name']) ? $item['type_name'] : '';
@@ -462,7 +494,7 @@ class chado_linker__relationship extends TripalField {
       // just with the text value or with an [id: \d+] string embedded.  If the
       // later we will pull it out.
       $subject_id = '';
-      $fkey_rcolumn = $fkeys[$base_table]['columns']['subject_id'];
+      $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
       $matches = array();
       if ($entity) {
         if(preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
@@ -496,7 +528,7 @@ class chado_linker__relationship extends TripalField {
 
       // Now check for a matching object.
       $object_id = '';
-      $fkey_rcolumn = $fkeys[$base_table]['columns']['object_id'];
+      $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
       $matches = array();
       if ($entity) {
         if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
@@ -706,8 +738,24 @@ return;
     if (!$record->$rel_table) {
       return;
     }
-    $srelationships = $record->$rel_table->subject_id;
-    $orelationships = $record->$rel_table->object_id;
+    $srelationships = null;
+    $orelationships = null;
+    if ($rel_table == 'organism_relationship') {
+      $srelationships = $record->$rel_table->subject_organism_id;
+      $orelationships = $record->$rel_table->object_organism_id;
+    }
+    else if ($rel_table == 'nd_reagent_relationship') {
+      $srelationships = $record->$rel_table->subject_reagent_id;
+      $orelationships = $record->$rel_table->object_reagent_id;
+    }
+    else if ($rel_table == 'project_relationship') {
+      $srelationships = $record->$rel_table->subject_project_id;
+      $orelationships = $record->$rel_table->object_project_id;
+    }
+    else {
+      $srelationships = $record->$rel_table->subject_id;
+      $orelationships = $record->$rel_table->object_id;
+    }
 
     $i = 0;
     if ($orelationships) {

+ 1 - 0
tripal_chado/includes/tripal_chado.fields.inc

@@ -510,6 +510,7 @@ function tripal_chado_create_tripalfields_linker(&$info, $details, $entity_type,
         'type' => 'field_chado_storage',
       ),
       'settings' => array(
+        'base_table' => $table_name
       ),
     );
   }

+ 2 - 2
tripal_chado/includes/tripal_chado.migrate.inc

@@ -612,7 +612,7 @@ function tripal_chado_migrate_selected_types($tv3_content_types) {
   foreach ($tv3_content_types AS $tv3_content_type) {
     // Check if the term already exists
     $term = tripal_load_term_entity($tv3_content_type);
-
+dpm($tv3_content_type);
     // If term doesn't exist, create a new bundle for this term
     if (!$term) {
       print("Creating bundle for term '" . $tv3_content_type['term_name'] . "'...\n");
@@ -628,7 +628,7 @@ function tripal_chado_migrate_selected_types($tv3_content_types) {
       'sync_node' => 1,
       'bundle_name' => $bundle_name
     );
-    tripal_chado_publish_records($value);
+    //tripal_chado_publish_records($value);
   }
 }