Browse Source

Fixed merge conflick in tripal_bulk_loader.loader.inc

laceysanderson 12 years ago
parent
commit
e5843e5264

+ 11 - 4
tripal_analysis/tripal_analysis.views.inc

@@ -35,8 +35,14 @@ function tripal_analysis_views_data()  {
   if (module_exists('tripal_views')) {
     // Base Table: Analysis
     $tablename = 'analysis';
-    if (!tripal_views_is_integrated($tablename, 9)) {
-      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+    // get the setup with the lightest priority. That's the table 
+    // that currently integrated with views.
+    $priority = tripal_views_get_table_lightest_priority($tablename);
+      
+    // check to see if the table is integrated. If it is then integrate it's
+    // corresponding 'chado_[table]' table.
+    if (!tripal_views_is_integrated($tablename, $priority)) {
+      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
       // Make table-specific modifications
       $table_integration_array['fields']['program']['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_string';
@@ -60,8 +66,9 @@ function tripal_analysis_views_data()  {
       'analysisprop'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = tripal_views_get_table_lightest_priority($tablename);     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 111 - 109
tripal_bulk_loader/tripal_bulk_loader.loader.inc

@@ -200,23 +200,23 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
       elseif (preg_match('/foreign key/', $field_array['type'])) {
         $default_data[$priority]['values_array'][$field_array['field']] = array();
         $default_data[$priority]['need_further_processing'] = TRUE;
-        $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['record'] = $field_array['foreign key'];
-
+        $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['record'] = $field_array['foreign key'];        
+        
         // Add in the FK / Referral table
         $fk_priority = $record2priority[$field_array['foreign key']];
-        $fk_table = $template_array[$fk_priority]['table'];
+        $fk_table = $template_array[$fk_priority]['table'];    
         $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['table'] = $fk_table;
-
+        
         // Add in the FK / Referral field
         // for backwards compatibility we need to get the FK relationship to find
-        // out what field we're joining on.  For templates created using a
+        // out what field we're joining on.  For templates created using a 
         // previous version it was assumed that the FK field was always the field to join
         if (!array_key_exists('foreign field', $field_array)) {
           $tbl_description = tripal_core_get_chado_table_schema($record_array['table']);
           foreach ($tbl_description['foreign keys'] as $key_table => $key_array) {
             if ($key_table == $fk_table) {
               foreach ($key_array['columns'] as $left_field => $right_field) {
-                if ($left_field == $field_array['field']) {
+                if ($left_field == $field_array['field']) {                  
                   $field_array['foreign field'] = $right_field;
                 }
               }
@@ -280,8 +280,8 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
     // Open File
     print "\tPreparing to load the current constant set...\n";
     print "\t\tOpen File...\n";
-    $file = new SplFileObject($node->file, 'r');
-    if (!$file) {
+    $file = new SplFileObject($node->file, 'r'); 
+    if (!$file) { 
       watchdog('T_bulk_loader','Could not open file %file', array($node->file), WATCHDOG_ERROR);
       return;
     }
@@ -289,7 +289,7 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
     // Set defaults
     $header = '';
     if (preg_match('/(t|true|1)/', $node->file_has_header)) {
-      $file->next();
+      $file->next();      
       $header = $file->current();
     }
     $num_records = 0;
@@ -341,7 +341,7 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
     tripal_bulk_loader_progress_bar(0, $total_lines);
     while (!$file->eof()) {
       $file->next();
-      $raw_line = $file->current();
+      $raw_line = $file->current();  
       $raw_line = trim($raw_line);
       if (empty($raw_line)) {
         continue;
@@ -351,7 +351,7 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
 
       // update the job status every 1% of lines processed for the current group
       if ($node->job_id and $num_lines % $interval == 0) {
-
+      
         // percentage of lines processed for the current group
         $group_progress = round(($num_lines / $total_lines) * 100);
         tripal_bulk_loader_progress_bar($num_lines, $total_lines);
@@ -385,11 +385,11 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
 
         // execute all records that are not disabled
         $no_errors = FALSE;
-        if (array_key_exists($priority, $data) and
-            array_key_exists('disabled', $data[$priority]) and
+        if (array_key_exists($priority, $data) and 
+            array_key_exists('disabled', $data[$priority]) and 
             $data[$priority]['disabled'] == 0) {
           $no_errors = process_data_array_for_line($priority, $data, $default_data, $options);
-        }
+        } 
         else {
           // set status to true for skipped records
           $no_errors = TRUE;
@@ -404,7 +404,7 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
           }
           $failed = TRUE;
           break;
-        }
+        } 
       } // end of foreach table in default data array
 
       tripal_bulk_loader_progress_file_track_job($job_id, FALSE, TRUE);
@@ -424,7 +424,7 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
             tripal_db_set_savepoint_transaction($savepoint, TRUE);
           }
         }
-      }
+      } 
     } //end of foreach line of file
 
     // END Transaction
@@ -476,13 +476,13 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
   $table = $table_data['table'];
   $values = $table_data['values_array'];
 
-  // populate the values array with real value either from the input data file line
+  // populate the values array with real value either from the input data file line 
   // or from the foreign key / referral record
-  if (array_key_exists('need_further_processing', $table_data) and $table_data['need_further_processing']) {
+  if (array_key_exists('need_further_processing', $table_data) and $table_data['need_further_processing']) {   
     if (array_key_exists($priority, $addt->field2column)) {
       $values = tripal_bulk_loader_add_spreadsheetdata_to_values($values, $addt->line, $addt->field2column[$priority]);
-    }
-    $values = tripal_bulk_loader_add_foreignkey_to_values($table_data, $values, $data, $addt->record2priority, $addt->nid, $priority);
+    } 
+    $values = tripal_bulk_loader_add_foreignkey_to_values($table_data, $values, $data, $addt->record2priority, $addt->nid, $priority);    
   }
 
   $values = tripal_bulk_loader_regex_tranform_values($values, $table_data, $addt->line);
@@ -491,42 +491,21 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
   }
 
   // get the table description
-  $table_desc = tripal_core_get_chado_table_schema($table);
-
-  // for an insert, check that all database required fields are present in the values array
-  // we check for 'optional' in the mode for backwards compatibility. The 'optional'
-  // mode used to be a type of insert
-  if (preg_match('/insert/', $table_data['mode']) or
-      preg_match('/optional/', $table_data['mode'])) {
-    // Check all database table required fields are set
-    $fields = $table_desc['fields'];
-    foreach ($fields as $field => $def) {
-      // a field is considered missing if it cannot be null and there is no default
-      // value for it or it is not of type 'serial'
-      if (array_key_exists('not null', $def) and $def['not null'] == 1 and
-          !array_key_exists($field, $values) and
-          (!array_key_exists('default', $def) or !isset($def['default'])) and
-          strcmp($def['type'], 'serial') != 0) {
-        $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') Missing Database Required Value: ' . $table . '.' . $field;
-        watchdog('T_bulk_loader', $msg, array(), WATCHDOG_NOTICE);
-        $data[$priority]['error'] = TRUE;
-      }
-    }
-  }
-
-  // Check that template required fields are present. if a required field is
+  $table_desc = tripal_core_get_chado_table_schema($table);  
+  
+  // Check that template required fields are present. if a required field is 
   // missing and this
   // is an optional record then just return. otherwise raise an error
   $skip_optional = 0;
   foreach ($table_data['required'] as $field => $required) {
     if ($required) {
       // check if the field has no value (or array is empty)
-      if (!isset($values[$field]) or (is_array($values[$field]) and count($values[$field]) == 0)){
+      if (!isset($values[$field]) or 
+          (is_array($values[$field]) and count($values[$field]) == 0)){
         // check if the record is optional.  For backwards compatiblity we need to
         // check if the 'mode' is set to 'optional'
-        if ($table_data['optional'] or preg_match('/optional/', $table_data['mode']) or
+        if ($table_data['optional'] or preg_match('/optional/', $table_data['mode']) or 
             $table_data['select_optional'])  {
-
           $skip_optional = 1;
           // set the values array to be empty since we all required fields are
           // optional and we can't do a select/insert so we don't want to keep
@@ -534,7 +513,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
           $values = array();
         }
         else {
-          $msg = "\nLine " . $addt->line_num . ' "' . $table_data['record_id'] .
+          $msg = "\nLine " . $addt->line_num . ' "' . $table_data['record_id'] . 
             '" (' . $table_data['mode'] . ') Missing template required value: ' . $table . '.' . $field;
           watchdog('T_bulk_loader', $msg, array(), WATCHDOG_WARNING);
           $data[$priority]['error'] = TRUE;
@@ -543,61 +522,84 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
       }
     }
   }
-
+  
+  // for an insert, check that all database required fields are present in the values array
+  // we check for 'optional' in the mode for backwards compatibility. The 'optional' 
+  // mode used to be a type of insert
+  if (!$skip_optional and (preg_match('/insert/', $table_data['mode']) or 
+       preg_match('/optional/', $table_data['mode']))) {
+    // Check all database table required fields are set
+    $fields = $table_desc['fields'];
+    foreach ($fields as $field => $def) { 	
+      // a field is considered missing if it cannot be null and there is no default
+      // value for it or it is not of type 'serial'
+      if (array_key_exists('not null', $def) and $def['not null'] == 1 and   // field must have a value
+          !array_key_exists($field, $values) and                             // there is not a value for it
+          !array_key_exists('default', $def) and                             // there is no default for it
+          strcmp($def['type'], 'serial') != 0) {                             // it is not a 'serial' type column
+        $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . 
+               ' (' . $table_data['mode'] . ') Missing Database Required Value: ' . $table . '.' . $field;
+        watchdog('T_bulk_loader', $msg, array(), WATCHDOG_ERROR); 
+        $data[$priority]['error'] = TRUE;
+      } 
+    } 
+  } 
+  
   // add updated values array into the data array
   $data[$priority]['values_array'] = $values;
-
+  
   // if there was an error already -> don't insert
   if (array_key_exists('error', $data[$priority]) and $data[$priority]['error']) {
     return $no_errors;
   }
-
-  // skip optional fields
+  
+  // skip optional fields 
   if ($skip_optional) {
     return $no_errors;
   }
-
+  
   // check if it is already inserted
   if (array_key_exists('inserted',$table_data) and $table_data['inserted']) {
     //watchdog('T_bulk_loader','Already Inserted:'.print_r($values,TRUE),array(),WATCHDOG_NOTICE);
     return $no_errors;
   }
-
-  // if "select if duplicate" is enabled then check to ensure unique constraint is not violoated.
+         
+  
+  // if "select if duplicate" is enabled then check to ensure unique constraint is not violoated.  
   // If it is violoated then simply return, the record already exists in the database.
   // We check for insert_unique for backwards compatibilty but that mode no longer exists
-  if (preg_match('/insert_unique/', $table_data['mode']) or
-      (array_key_exists('select_if_duplicate', $table_data) and
+  if (preg_match('/insert_unique/', $table_data['mode']) or 
+      (array_key_exists('select_if_duplicate', $table_data) and 
        $table_data['select_if_duplicate'] == 1)) {
-    $options = array('use_unique' => TRUE);
+    $options = array('is_duplicate' => TRUE);
     $duplicate = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, $options);
     // if this is a duplicate then substitute the values in the table_data array so
-    // that for future records that my depend on this one, they can get the values
+    // that for future records that my depend on this one, they can get the values 
     // needed
     if ($duplicate and is_array($duplicate) and count($duplicate) > 0) {
 
       // if all we have is one field then we will just use the value returned
-      // rather than create an array of values. This way it will prevent
-      // the tripal_core_chado_(select|insert|update) from recursing on
+      // rather than create an array of values. This way it will prevent 
+      // the tripal_core_chado_(select|insert|update) from recursing on 
       // foreign keys and make the loader go faster.
       if (count($duplicate[0]) == 1) {
-        foreach($duplicate[0] as $key => $value){
+        foreach($duplicate[0] as $key => $value){ 
           $data[$priority]['values_array'] = $value;
-        }
+        } 
       }
       // if we have multiple fields returned then we need to set the values
       // the new array.
       else {
         // convert object to array
         $new_values = array();
-        foreach($duplicate[0] as $key => $value){
+        foreach($duplicate[0] as $key => $value){ 
           $new_values[$key] = $value;
-        }
-        $data[$priority]['values_array'] = $new_values;
+        } 
+        $data[$priority]['values_array'] = $new_values;        
       }
       // reset values in data array
       return $no_errors;
-    }
+    } 
   }
 
   if (!preg_match('/select/', $table_data['mode'])) {
@@ -621,21 +623,21 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
       //$record = tripal_core_chado_update($table, $values, $options);
     }
     else {
-      $record = tripal_core_chado_insert($table, $values, $options);
+      $record = tripal_core_chado_insert($table, $values, $options);      
     }
 
     // if the insert was not successful
     if (!$record) {
-      $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' .
-        $table_data['mode'] . ') Unable to insert record into ' . $table .
+      $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . 
+        $table_data['mode'] . ') Unable to insert record into ' . $table . 
         ' where values:' . print_r($values, TRUE);
-
+        
       watchdog('T_bulk_loader', $msg, array(), WATCHDOG_ERROR);
       $data[$priority]['error'] = TRUE;
       $no_errors = FALSE;
     }
     // if the insert was succesful
-    else {
+    else {    
 
       // if mode=insert_once then ensure we only insert it once
       if (preg_match('/insert_once/', $table_data['mode'])) {
@@ -667,7 +669,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
           return $no_errors;
         }//end of if insert record
       }// end of if keeping track of records inserted
-
+    
       // substitute the values array for the primary key if it exists
       // and is a single field
       if(array_key_exists('primary key',$table_desc)){
@@ -688,19 +690,19 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
     if(is_array($values) and count($values) > 0){
       $matches = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values);
     }
-/*$time_end = microtime(true);
-$time = $time_end - $time_start;
-printf("time: %04f seconds\n",$time);
-if($time > 0.05){
-  print "$table\n";
-  print_r(array_keys($table_desc['fields']));
-  print_r($values);
-  exit;
-} */
+//$time_end = microtime(true);
+//$time = $time_end - $time_start;
+//printf("time: %04f seconds\n",$time);
+//if($time > 0.05){
+//  print "$table\n";
+//  print_r(array_keys($table_desc['fields']));
+//  print_r($values);
+//  exit;
+//}
     // if the record doesn't exists and it's not optional then generate an error
     if (count($matches) == 0){
       // No record on select
-      if ($table_data['select_optional'] != 1) {
+      if ($table_data['select_optional'] != 1) {        
         $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') No Matching record in ' . $table . ' where values:' . print_r($values, TRUE);
         watchdog('T_bulk_loader', $msg, array(), WATCHDOG_ERROR);
         $data[$priority]['error'] = TRUE;
@@ -713,19 +715,19 @@ if($time > 0.05){
       }
     }
     // if we have more than one record matching and this select isn't optional then fail
-    if (count($matches) > 1) {
+    if (count($matches) > 1) { 
       if ($table_data['select_optional'] != 1) {
         $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') Too many matching records in ' . $table . ' where values:' . print_r($values, TRUE);
         watchdog('T_bulk_loader', $msg, array(), WATCHDOG_WARNING);
         $data[$priority]['error'] = TRUE;
         $no_errors = FALSE;
-      }
+      } 
       // there are too many matches and this is an optional select so set
       // the values to empty for any records with an FK relationship on this one
       else {
         $data[$priority]['values_array'] = NULL;
       }
-    }
+    } 
   }
   return $no_errors;
 }
@@ -758,7 +760,7 @@ function tripal_bulk_loader_add_spreadsheetdata_to_values($values, $line, $field
     }
     else {
       unset($values[$field]);
-    }
+    }    
   }
 
   return $values;
@@ -779,30 +781,30 @@ function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $dat
   foreach ($values as $field => $value) {
     // if the field value is an array then it is an FK
     if (is_array($value)) {
-
+      
       // get the name and priority of the foreign record
       $foreign_record   = $value['foreign record']['record'];
       $foreign_priority = $record2priority[$foreign_record];
       $foreign_table    = $value['foreign record']['table'];
       $foreign_field    = $value['foreign record']['field'];
-
+      
       // get the values of the foreign record and substitute those for the values
       $foreign_values   = $data[$foreign_priority]['values_array'];
-
+            
       // if the field in the Referral records is in a FK relationship with
       // this field then we can simply keep the value we have
       $tbl_description = tripal_core_get_chado_table_schema($table_array['table']);
       if ($tbl_description and
-          array_key_exists('foreign keys', $tbl_description) and
-          array_key_exists($foreign_table, $tbl_description['foreign keys']) and
+          array_key_exists('foreign keys', $tbl_description) and 
+          array_key_exists($foreign_table, $tbl_description['foreign keys']) and 
           array_key_exists($field, $tbl_description['foreign keys'][$foreign_table]['columns']) and
           $foreign_field == $tbl_description['foreign keys'][$foreign_table]['columns'][$field]){
-         $values[$field] = $foreign_values;
-      }
+         $values[$field] = $foreign_values;      
+      } 
       // if the field in the Referral records is not in an FK relationship
       // with this field then we we have to get the requested value, we must
       // return only a single value
-      else {
+      else {      
         // if the current value of the referral records is a non-array then this
         // is the primary key, we can use it to select the value we need.
         $fk_description = tripal_core_get_chado_table_schema($foreign_table);
@@ -815,7 +817,7 @@ function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $dat
             $record  = tripal_core_chado_select($foreign_table, $columns, $fvalues, $options);
             if ($record) {
               $values[$field] = $record[0]->$foreign_field;
-            }
+            } 
             else {
               unset($values[$field]);
             }
@@ -831,17 +833,17 @@ function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $dat
         else {
           $fvalues  = $foreign_values;
           $columns = array($foreign_field);
-          $options = array('statement_name' => 'blk_' . $nid . $priority . $foreign_table);
+          $options = array('statement_name' => 'blk_' . $nid . $priority . $foreign_table);        
           $record  = tripal_core_chado_select($foreign_table, $columns, $fvalues, $options);
           if ($record){
-            $values[$field] = $record[0]->$foreign_field;
-          }
+            $values[$field] = $record[0]->$foreign_field; 
+          } 
           else {
             unset($values[$field]);
           }
-        } // end else from: if (!is_array($foreign_values) ...
+        } // end else from: if (!is_array($foreign_values) ...         
       } // end else from: if ($tbl_description ...
-    } // end if(is_array($value)) ...
+    } // end if(is_array($value)) ... 
   } // end foreach ($values ...
 
   // return the updated field values
@@ -858,8 +860,8 @@ function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $dat
  */
 function tripal_bulk_loader_regex_tranform_values($values, $table_data, $line) {
 
-  if (!array_key_exists('regex_transform', $table_data) or
-      empty($table_data['regex_transform']) or
+  if (!array_key_exists('regex_transform', $table_data) or  
+      empty($table_data['regex_transform']) or 
       !array_key_exists('regex_transform', $table_data) or
       !is_array($table_data['regex_transform'])) {
     return $values;
@@ -868,7 +870,7 @@ function tripal_bulk_loader_regex_tranform_values($values, $table_data, $line) {
   //watchdog('T_bulk_loader','Regex Transformation:<pre>'.print_r($table_data['regex_transform'], TRUE).'</pre>', array(), WATCHDOG_NOTICE);
 
   foreach ($table_data['regex_transform'] as $field => $regex_array) {
-    if (!array_key_exists('replace', $regex_array) or
+    if (!array_key_exists('replace', $regex_array) or 
         !array_key_exists('pattern', $regex_array) or
         !is_array($regex_array['replace'])) {
       continue;
@@ -933,22 +935,22 @@ function tripal_bulk_loader_flatten_array($values) {
  */
 function tripal_bulk_loader_progress_bar($current=0, $total=100, $size=50) {
   $new_bar = FALSE;
-  $mem = memory_get_usage();
-
+  $mem = memory_get_usage();       
+	
   // First iteration
   if ($current == 0) {
     $new_bar = TRUE;
     fputs(STDOUT, "Progress:\n");
   }
-
+  
   // Percentage round off for a more clean, consistent look
   $percent = sprintf("%.02f", round(($current/$total) * 100, 2));
   // percent indicator must be four characters, if shorter, add some spaces
   for ($i = strlen($percent); $i <= 4; $i++) {
     $percent = ' ' . $percent;
   }
-
-
+ 
+  
   $total_size = $size + $i + 3 + 2;
   $place = 0;
   // if it's not first go, remove the previous bar

+ 10 - 5
tripal_contact/tripal_contact.views.inc

@@ -28,9 +28,13 @@ function tripal_contact_views_data()  {
   if (module_exists('tripal_views')) {
 
     // Base Table: contact
-    $tablename = 'contact';
-    if (!tripal_views_is_integrated($tablename, 9)) {
-      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+    $tablename = 'contact';    
+    $priority = 9;
+      
+    // check to see if the table is integrated. If it is then integrate it's
+    // corresponding 'chado_[table]' table.
+    if (!tripal_views_is_integrated($tablename, $priority)) {
+      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
       $status = tripal_views_integration_add_entry($table_integration_array);
         if (!$status) {
           drupal_set_message(t('Tripal Contact is unable to integrate %table', array('%table' => $tablename)), 'error');
@@ -42,8 +46,9 @@ function tripal_contact_views_data()  {
       'contact_relationship',
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         $status = tripal_views_integration_add_entry($table_integration_array);
         if (!$status) {
           drupal_set_message(t('Tripal Contact is unable to integrate %table', array('%table' => $tablename)), 'error');

+ 65 - 51
tripal_core/api/tripal_core.api.inc

@@ -200,7 +200,10 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
   if ($validate) {
 
     // check for violation of any unique constraints
-    $ukeys = $table_desc['unique keys'];
+    $ukeys = array();
+    if(array_key_exists('unique keys', $table_desc)){
+      $ukeys = $table_desc['unique keys'];
+    }
     $ukselect_cols = array();
     $ukselect_vals = array();
     if ($ukeys) {
@@ -209,9 +212,9 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
           // build the arrays for performing a select that will check the contraint
           $ukselect_cols[] = $field;
           if (!array_key_exists($field, $insert_values)) {
-          	if (array_key_exists('default', $table_desc['fields'][$field])) {
+            if (array_key_exists('default', $table_desc['fields'][$field])) {
               $ukselect_vals[$field] = $table_desc['fields'][$field]['default'];
-          	}
+            }
           }
           else {
             $ukselect_vals[$field] = $insert_values[$field];
@@ -223,19 +226,22 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
           $coptions = array('statement_name' => 'uqsel_' . $table . '_' . $name);
         }
         if (tripal_core_chado_select($table, $ukselect_cols, $ukselect_vals, $coptions)) {
-          watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate record into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
+          watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate record into $table table: " . 
+            print_r($values, 1), array(), 'WATCHDOG_ERROR');
           return FALSE;
         }
       }
     }
 
     // if trying to insert a field that is the primary key, make sure it also is unique
-    $pkey = $table_desc['primary key'][0];
-    if ($insert_values[$pkey]) {
-      $coptions = array('statement_name' => 'pqsel_' . $table . '_' . $pkey);
-      if (tripal_core_chado_select($table, array($pkey), array($pkey => $insert_values[$pkey]), $coptions)) {
-        watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate primary key into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
-        return FALSE;
+    if (array_key_exists('primary key', $table_desc)) {
+      $pkey = $table_desc['primary key'][0];
+      if (array_key_exists($pkey, $insert_values)) {
+        $coptions = array('statement_name' => 'pqsel_' . $table . '_' . $pkey);
+        if (tripal_core_chado_select($table, array($pkey), array($pkey => $insert_values[$pkey]), $coptions)) {
+          watchdog('tripal_core', "tripal_core_chado_insert: Cannot insert duplicate primary key into $table table: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
+          return FALSE;
+        }
       }
     }
 
@@ -247,8 +253,12 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
     foreach ($table_desc['fields'] as $field => $def) {
       // a field is considered missing if it cannot be NULL and there is no default
       // value for it or it is of type 'serial'
-      if ($def['not NULL'] == 1 and !array_key_exists($field, $insert_values) and !isset($def['default']) and strcmp($def['type'], serial) != 0) {
-        watchdog('tripal_core', "tripal_core_chado_insert: Field $table.$field cannot be NULL: " . print_r($values, 1), array(), 'WATCHDOG_ERROR');
+      if (array_key_exists('NOT NULL', $def) and 
+          !array_key_exists($field, $insert_values) and 
+          !array_key_exists('default', $def) and
+          strcmp($def['type'], serial) != 0) {
+        watchdog('tripal_core', "tripal_core_chado_insert: Field $table.$field cannot be NULL: " . 
+          print_r($values, 1), array(), 'WATCHDOG_ERROR');
         return FALSE;
       }
     }
@@ -849,15 +859,9 @@ function tripal_core_chado_delete($table, $match) {
  *     default if the statement is not prepared it will be automatically.
  *     However to avoid this check, which requires a database query you can
  *     set this value to true and the check will not be performed.
- *  - use_unique: TRUE or FALSE.  This will alter the values array provided
- *     to the function and remove fields not in
- *     a unique constraint.  This is useful before an insert to check if
- *     another record exists with the same unique constraint (but possible differences
- *     in the other fields) and cause a duplicate insert error.  If a field
- *     in the unique constraint is missing from the values then the function
- *     will return false.  If the table has a primary key, the resulting
- *     record will only have the pkey field. If there is no primary key the
- *     resulting record will have the unique constraint fields.
+ *  - is_duplicate: TRUE or FALSE.  Checks the values submited to see if
+ *     they violoate any of the unique constraints. If so, the record
+ *     is returned, if not, FALSE is returned.
  *
  *
  * @return
@@ -927,8 +931,8 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
   if (!array_key_exists('statement_name',$options)) {
     $options['statement_name'] = FALSE;
   }
-  if (!array_key_exists('use_unique',$options)) {
-    $options['use_unique'] = FALSE;
+  if (!array_key_exists('is_duplicate',$options)) {
+    $options['is_duplicate'] = FALSE;
   }
 
   // if this is a prepared statement check to see if it has already been prepared
@@ -964,54 +968,65 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
 
   // if the 'use_unique' option is turned on then we want
   // to remove all but unique keys
-  if ($options['use_unique'] and array_key_exists('unique keys', $table_desc)) {
+  if ($options['is_duplicate'] and array_key_exists('unique keys', $table_desc)) {
     $ukeys = $table_desc['unique keys'];
-    $new_values = array();
-    $new_columns = array();
-    $uq_sname = "uq_" . $table . "_";
-
-    // include the primary key in the results returned
-    $has_pkey = 0;
-    if (array_key_exists('primary key', $table_desc)){
-      $has_pkey = 1;
-      $pkeys = $table_desc['primary key'];
-      foreach ($pkeys as $index => $key) {
-        array_push($new_columns, $key);
-      }
-    }
-    // iterate through the unique fields and reset the values and columns
+    $has_results = 0;
+      
+    // iterate through the unique constraints and reset the values and columns
     // arrays to only include these fields
     foreach ($ukeys as $cname => $fields) {
+    	if ($has_results) {
+    	   continue;
+    	}
+      $new_values = array();
+      $new_columns = array();
+      $new_options = array();
+      $uq_sname = "uq_" . $table . "_";
+      $has_pkey = 0;
+      
+      
+      // include the primary key in the results returned
+	    if (array_key_exists('primary key', $table_desc)){
+	      $has_pkey = 1;
+	      $pkeys = $table_desc['primary key'];
+	      foreach ($pkeys as $index => $key) {
+	        array_push($new_columns, $key);
+	      }
+	    }
+	    
       foreach ($fields as $field) {
         if (array_key_exists($field, $values)) {
           $new_values[$field] = $values[$field];
           $uq_sname .= substr($field, 0, 2);
           // if there is no primary key then use the unique contraint fields
           if (!$has_pkey) {
-            array_push($new_columns,$field);
+            array_push($new_columns, $field);
           }
         }
-        elseif (array_key_exists('default',$table_desc['fields'][$field])) {
+        elseif (array_key_exists('default', $table_desc['fields'][$field])) {
           $new_values[$field] = $table_desc['fields'][$field]['default'];
           $uq_sname .= substr($field, 0, 2);
           if (!$has_pkey){
-            array_push($new_columns,$field);
+            array_push($new_columns, $field);
           }
         }
         else {
           return FALSE;
         }
       }
+      $new_options['statement_name'] = $uq_sname;
+      $results = tripal_core_chado_select($table, $new_columns, $new_values, $new_options);
+      // if we have a duplicate record then return the results
+			if (count($results) > 0) {
+        $has_results = 1;
+      } 
+      unset($new_columns);
+      unset($new_values);
+      unset($new_options);
     }
-    // reset the values and columns
-    $values = $new_values;
-    $columns = $new_columns;
-
-    // set the new statement name
-    $options['statement_name'] = $uq_sname;
+    return $results;
   }
 
-
   foreach ($values as $field => $value) {
     $select[] = $field;
     if (is_array($value)) {
@@ -1173,7 +1188,6 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
       $sql = drupal_substr($sql, 0, -2);  // get rid of the trailing ', '
       $psql = drupal_substr($psql, 0, -2);  // get rid of the trailing ', '
     }
-
     // finish constructing the prepared SQL statement
     $psql =  "PREPARE " . $options['statement_name'] . " (" . implode(', ', $idatatypes) . ") AS " . $psql;
 
@@ -1203,8 +1217,8 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
   }
   else {
     $resource = chado_query($sql, $args);
-  }
-
+  } 
+  
   // format results into an array
   $results = array();
   while ($r = db_fetch_object($resource)) {

+ 6 - 5
tripal_core/tripal_core.module

@@ -23,7 +23,8 @@ require_once "api/tripal_core.api.inc";
  * @ingroup tripal_core
  */
 function tripal_core_init() {
-
+  global $base_url;
+  
   // the two lines below are necessary to ensure that the search_path
   // variable is always set.  In the case where a view needs to query the
   // chado schema when it is local to the Drupal database.  Otherwise the
@@ -33,7 +34,7 @@ function tripal_core_init() {
 //  $previous = tripal_db_set_active('chado');
 //  tripal_db_set_active($previous);
   tripal_db_set_chado_search_path('chado');
-
+ 
 
   // create the 'tripal' controlled volcabulary in chado but only if it doesn't already exist, and
   // only if the chado database is present.
@@ -55,10 +56,10 @@ function tripal_core_init() {
   $theme_dir = drupal_get_path('theme', 'tripal');
   $clean_urls  = variable_get('clean_url', 0);
   drupal_add_js(
-    "var baseurl = '$base_url';
+    " var baseurl  = '$base_url';
       var themedir = '$theme_dir';
-      var isClean = $clean_urls;",
-    'inline');
+      var isClean  =  $clean_urls;",
+    'inline', 'header');
 
   // make sure the date time settings are the way Tripal will insert them
   // otherwise PostgreSQL version that may have a different datestyle setting

+ 9 - 4
tripal_cv/tripal_cv.views.inc

@@ -40,9 +40,13 @@ function tripal_cv_views_data() {
       'cvterm'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
+      $priority = 9;
+      
+      // check to see if the table is integrated. If it is then integrate it's
+      // corresponding 'chado_[table]' table.
+      if (!tripal_views_is_integrated($tablename, $priority)) {
         // get default integration array
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
         // add specialty handlers
         if ($tablename == 'cvterm') {
@@ -66,8 +70,9 @@ function tripal_cv_views_data() {
       'cvtermsynonym'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 9 - 4
tripal_db/tripal_db.views.inc

@@ -37,8 +37,12 @@ function tripal_db_views_data()  {
       'dbxref'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+      $priority = 9;
+      
+      // check to see if the table is integrated. If it is then integrate it's
+      // corresponding 'chado_[table]' table.
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
         // add specialty handlers
         if ($tablename == 'db') {
@@ -54,8 +58,9 @@ function tripal_db_views_data()  {
       'dbxrefprop'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 4 - 1
tripal_feature/api/tripal_feature.api.inc

@@ -824,13 +824,16 @@ function tripal_feature_get_formatted_sequence($feature_id, $feature_name,
   // if we are not getting the sequence from the parent sequence then
   // use what comes through from the feature record
   else {
-    $residues = $values->$field; 
+    $sql = "SELECT * FROM feature F WHERE feature_id = %d";
+    $values = db_fetch_object(chado_query($sql,$feature_id));
+    $residues = $values->residues;
     if ($output_format == 'fasta_html') {
        $residues = wordwrap($residues, $num_bases_per_line, "<br>", TRUE);  
     } 
     elseif ($output_format == 'fasta_txt') {
        $residues = wordwrap($residues, $num_bases_per_line, "\n", TRUE);  
     }
+    $residues = ">$feature_name\n$residues\n";
   }
   
   // format the residues for display

+ 18 - 8
tripal_feature/includes/syncFeatures.inc

@@ -127,12 +127,14 @@ function tripal_feature_set_urls($job_id = NULL) {
   $nodes = db_query($sql);
   while ($node = db_fetch_object($nodes)) {
     // now get the feature details
-    $feature_arr = tripal_core_chado_select('feature',
-      array('feature_id', 'name', 'uniquename'),
-      array('feature_id' => $node->feature_id));
-    $feature = $feature_arr[0];
-
-    tripal_feature_set_feature_url($node, $feature);
+    $sql = "SELECT * 
+            FROM feature F
+              INNER JOIN organism O on O.organism_id = F.organism_id
+            WHERE F.feature_id = %d";     
+    $feature = db_fetch_object(chado_query($sql, $node->feature_id));
+    if ($feature) {
+      tripal_feature_set_feature_url($node, $feature);
+    }
   }
 }
 /**
@@ -142,7 +144,9 @@ function tripal_feature_set_feature_url($node, $feature) {
 
   // determine which URL alias to use
   $alias_type = variable_get('chado_feature_url', 'internal ID');
-  $aprefix = variable_get('chado_feature_accession_prefix', 'ID');
+  $aprefix = variable_get('chado_feature_accession_prefix', 'ID');  
+  $genus = preg_replace('/\s/', '_', strtolower($feature->genus));
+  $species = preg_replace('/\s/', '_', strtolower($feature->species));    
   switch ($alias_type) {
     case 'feature name':
       $url_alias = $feature->name;
@@ -150,10 +154,16 @@ function tripal_feature_set_feature_url($node, $feature) {
     case 'feature unique name':
       $url_alias = $feature->uniquename;
       break;
+    case 'genus_species_uqname':
+    	$url_alias = $genus . "/" . $genus . "_" . $species . "/" . $feature->uniquename;
+      break;
+    case 'genus species name':
+      $url_alias = $genus . "/" . $genus . "_" . $species . "/" . $feature->name;
+    	break;
     default:
       $url_alias = "$aprefix$feature->feature_id";
   }
-  print "Setting $alias_type as URL alias for $feature->name: node/$node->nid => $url_alias\n";
+  print "Setting URL alias for $feature->name: node/$node->nid => $url_alias\n";
   // remove any previous alias
   db_query("DELETE FROM {url_alias} WHERE src = '%s'", "node/$node->nid");
   // add the new alias

+ 13 - 7
tripal_feature/includes/tripal_feature.admin.inc

@@ -552,18 +552,24 @@ function get_tripal_feature_admin_form_url_set(&$form) {
 
   );
 
+  $options = array(
+    'internal ID'          => 'Internal ID (Chado feature_id)',
+    'feature unique name'  => 'Feature unique name',
+    'feature name'         => 'Feature name',
+    'genus_species_uqname' => 'Genus + species + unique name (e.g. http://your.site.url/[genus]/[genus]_[species]/[unique_name]',
+    'genus_species_name'   => 'Genus + species + name (e.g. http://your.site.url/[genus]/[genus]_[species]/[name]',
+  );
+  
   $form['url']['chado_feature_url'] = array(
-    '#title'       => t('Unique Identifier'),
-    '#type'        => 'radios',
-    '#description' => t('Choose an identifier type '.
+    '#title'         => t('Unique Identifier'),
+    '#type'          => 'radios',
+    '#description'   => t('Choose an identifier type '.
                         'from the list above that is guaranteed to be unique in your synced '.
                         'dataset. If in doubt it is safest to coose the internal ID. '.
                         'The descrpitor need not be unique amont the total dataset. '.
                         'It only need be unique among the synced dataset.'),
-    '#required'    => FALSE,
-    '#options'     => array('internal ID' => 'internal ID (Chado feature_id)',
-                            'feature unique name' => 'feature unique name',
-                            'feature name' => 'feature name'),
+    '#required'      => FALSE,
+    '#options'       => $options,
     '#default_value' => variable_get('chado_feature_url', 'internal ID'),
   );
 

+ 2 - 2
tripal_feature/tripal_feature.drush.inc

@@ -79,7 +79,7 @@ function drush_tripal_feature_tripal_get_sequence() {
 
   // get the list of features
   $vars = array();
-  $sql  = "SELECT DISTINCT F.feature_id, F.name ".
+  $sql  = "SELECT DISTINCT F.feature_id, F.name, F.uniquename, O.genus, O.species, CVT.name as feature_type ".
           "FROM feature F ".
           "  INNER JOIN organism O on O.organism_id = F.organism_id ".
           "  INNER JOIN cvterm CVT on CVT.cvterm_id = F.type_id ";
@@ -108,7 +108,7 @@ function drush_tripal_feature_tripal_get_sequence() {
   $q = chado_query($sql, $vars);
   while ($feature = db_fetch_object($q)) {
     $feature_id = $feature->feature_id;
-    $feature_name = $feature->name;
+    $feature_name = "$feature->uniquename $feature->name $feature->feature_type ($feature->genus $feature->species)";
       
     $sequence = tripal_feature_get_formatted_sequence($feature_id, $feature_name, 
       $num_bases_per_line, $derive_from_parent, $aggregate, $output_format,

+ 10 - 4
tripal_feature/tripal_feature.views.inc

@@ -36,9 +36,14 @@ function tripal_feature_views_data()  {
     $tables = array(
       'feature',
     );
+        
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+    	$priority = 9;
+    	
+    	// check to see if the table is integrated. If it is then integrate it's
+    	// corresponding 'chado_[table]' table.
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
         // Add in node relationships if chado is in the same db as drupal
         if (tripal_core_chado_schema_exists()) {
@@ -77,8 +82,9 @@ function tripal_feature_views_data()  {
       'featurerange'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 9 - 4
tripal_genetic/tripal_genetic.views.inc

@@ -30,8 +30,12 @@ function tripal_genetic_views_data()  {
       'phenstatement'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+      $priority = 9;
+      
+      // check to see if the table is integrated. If it is then integrate it's
+      // corresponding 'chado_[table]' table.
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
         $status = tripal_views_integration_add_entry($table_integration_array);
         if (!$status) {
           drupal_set_message(t('Tripal Genetic is unable to integrate %table', array('%table' => $tablename)), 'error');
@@ -46,8 +50,9 @@ function tripal_genetic_views_data()  {
       'phenotype_comparison'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         $status = tripal_views_integration_add_entry($table_integration_array);
         if (!$status) {
           drupal_set_message(t('Tripal Genetic is unable to integrate %table', array('%table' => $tablename)), 'error');

+ 9 - 4
tripal_library/tripal_library.views.inc

@@ -35,8 +35,12 @@ function tripal_library_views_data()  {
       'library'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+      $priority = 9;
+      
+      // check to see if the table is integrated. If it is then integrate it's
+      // corresponding 'chado_[table]' table.
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
         // Add in node relationships if chado is in the same db as drupal
         if (tripal_core_chado_schema_exists()) {
@@ -60,8 +64,9 @@ function tripal_library_views_data()  {
       'libraryprop'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 9 - 4
tripal_natural_diversity/tripal_natural_diversity.views.inc

@@ -30,8 +30,12 @@ function tripal_natural_diversity_views_data()  {
       'nd_reagent'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+      $priority = 9;
+      
+      // check to see if the table is integrated. If it is then integrate it's
+      // corresponding 'chado_[table]' table.
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
         // Add specialty handlers
         switch ($tablename) {
@@ -67,8 +71,9 @@ function tripal_natural_diversity_views_data()  {
       'nd_reagentprop'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 9 - 4
tripal_organism/tripal_organism.views.inc

@@ -36,8 +36,12 @@ function tripal_organism_views_data()  {
       'organism'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+      $priority = 9;
+      
+      // check to see if the table is integrated. If it is then integrate it's
+      // corresponding 'chado_[table]' table.
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
         // Add specialty handlers
         $table_integration_array['fields']['common_name']['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_string';
@@ -64,8 +68,9 @@ function tripal_organism_views_data()  {
       'organism_dbxref'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;    	
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 9 - 4
tripal_phenotype/tripal_phenotype.views.inc

@@ -28,8 +28,12 @@ function tripal_phenotype_views_data()  {
       'phenotype'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+	    $priority = 9;
+	      
+	    // check to see if the table is integrated. If it is then integrate it's
+	    // corresponding 'chado_[table]' table.
+	    if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
         // Add specialty handlers
         switch ($tablename) {
@@ -50,8 +54,9 @@ function tripal_phenotype_views_data()  {
       'phenotype_cvterm'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 9 - 4
tripal_project/tripal_project.views.inc

@@ -28,8 +28,12 @@ function tripal_project_views_data()  {
       'project'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+      $priority = 9;
+      
+      // check to see if the table is integrated. If it is then integrate it's
+      // corresponding 'chado_[table]' table.
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
         // Add in node relationships if chado is in the same db as drupal
         if (tripal_core_chado_schema_exists()) {
@@ -47,8 +51,9 @@ function tripal_project_views_data()  {
     $tables = array(
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 12 - 6
tripal_stock/tripal_stock.views.inc

@@ -33,8 +33,12 @@ function tripal_stock_views_data()  {
 
   if (module_exists('tripal_views')) {
     $tablename = 'stock';
-    if (!tripal_views_is_integrated($tablename, 9)) {
-      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+    $priority = 9;
+      
+    // check to see if the table is integrated. If it is then integrate it's
+    // corresponding 'chado_[table]' table.
+    if (!tripal_views_is_integrated($tablename, $priority)) {
+      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
 
       // Add in node relationships if chado is in the same db as drupal
       if (tripal_core_chado_schema_exists()) {
@@ -49,8 +53,9 @@ function tripal_stock_views_data()  {
     }
 
     $tablename = 'stockcollection';
-    if (!tripal_views_is_integrated($tablename, 9)) {
-      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
+    $priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
       tripal_views_integration_add_entry($table_integration_array);
     }
 
@@ -67,8 +72,9 @@ function tripal_stock_views_data()  {
       'stockprop_pub'
     );
     foreach ($tables as $tablename) {
-      if (!tripal_views_is_integrated($tablename, 9)) {
-        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
+    	$priority = 9;     
+      if (!tripal_views_is_integrated($tablename, $priority)) {
+        $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
         tripal_views_integration_add_entry($table_integration_array);
       }
     }

+ 39 - 9
tripal_views/api/tripal_views.api.inc

@@ -5,6 +5,33 @@
  * API functions for Tripal Views Integration
  */
 
+/**
+ * Retrieve the priority of the lightest priority for a given table
+ *
+ * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
+ * and -10 is of highest priority.
+ *
+ * @param $table_name
+ *   The name of the table to retrieve the setup ID for. This can be either a materialized
+ *   view or a chado table
+ *
+ * @return
+ *   returns the lowest priority.  If the table has not been integrated, a priority of 10
+ *   is returned.
+ */
+function tripal_views_get_table_lightest_priority($table_name) {
+
+  $sql = "SELECT priority FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
+  $setup = db_fetch_object(db_query($sql, $table_name));
+  if ($setup) {
+    return $setup->priority;
+  }
+  else {
+  	// default priority is 10
+    return 10;
+  }
+}
+
 /**
  * Retrieve the views integration setup with the lightest priority for a given table
  *
@@ -137,13 +164,13 @@ function tripal_views_integration_add_entry($defn_array) {
     'base_table' => $defn_array['base_table'],
   );
   if ($defn_array['type'] == 'mview') {
-      $mview = db_fetch_object(db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'", $defn_array['table']));
-      $view_record['mview_id'] = $mview->mview_id;
-      if (!$mview->mview_id) {
-        return FALSE;
-      }
+    $mview = db_fetch_object(db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'", $defn_array['table']));
+    $view_record['mview_id'] = $mview->mview_id;
+    if (!$mview->mview_id) {
+      return FALSE;
+    }
   }
-  if ($view_record['name'] && $view_record['comment']) {
+  if ($view_record['name']) { // && $view_record['comment']) {  # SPF: commented out 9/24/2012 .. It's not required on the form
     if ($defn_array['additional_content']) {
       $setup = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",$view_record['table_name'], $view_record['priority']));
       if (empty($setup->setup_id)) {
@@ -386,14 +413,17 @@ function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
 }
 
 /**
- * Integrate all chado tables in the schema api
+ * Integrate all chado tables in the schema api.  This integration only occurs
+ * once and sets all Chado tables to a priority of 10
  */
 function tripal_views_integrate_all_chado_tables() {
 
   $tables = tripal_core_get_chado_tables(TRUE);
   foreach ($tables as $tablename) {
-    if (!tripal_views_is_integrated($tablename, 10)) {
-      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, 10);
+  
+    $priority = 10;
+    if (!tripal_views_is_integrated($tablename, $priority)) {
+      $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
       if ($table_integration_array) {
         tripal_views_integration_add_entry($table_integration_array);
       }

+ 13 - 3
tripal_views/includes/tripal_views_integration_port.inc

@@ -13,13 +13,20 @@ function tripal_views_integration_export_form($form_state, $setup_id) {
   $form = array();
 
   $defn_array = tripal_views_integration_export_entry($setup_id);
+  
+  $t = var_export($defn_array, TRUE);
+  $t = preg_replace("/\n\s+array/","array", $t); // move array( to previous line
+  $t = preg_replace("/true/","TRUE", $t); // upper case true
+  $t = preg_replace("/false/","FALSE", $t); // upper case false
+  $t = preg_replace("/array\(/","array (", $t); // put a space between array and paren   
+        
   $form['export'] = array(
     '#type' => 'textarea',
     '#title' => 'Export',
     '#description' => t('Simply copy the provided export into the Import text area on '
       . 'another tripal views enabled website to port the integration between websites.'),
     '#rows' => 20,
-    '#value' => serialize($defn_array)
+    '#value' => $t,
   );
 
   return $form;
@@ -73,7 +80,10 @@ function tripal_views_integration_import_form() {
  */
 function tripal_views_integration_import_form_submit($form, &$form_state) {
 
-  $defn_array = unserialize($form_state['values']['import']);
+  //$defn_array = unserialize($form_state['values']['import']);
+  // convert the array into a real PHP array
+  $defn_array = array();
+  eval("\$defn_array = " . $form_state['values']['import'] . ";");
 
   // Add optional parameters
   if ($form_state['values']['name']) {
@@ -91,4 +101,4 @@ function tripal_views_integration_import_form_submit($form, &$form_state) {
   else {
     drupal_set_message(t("Unable to import %name Integration", array('%name' => $defn_array['name'])), 'error');
   }
-}
+}

+ 1 - 1
tripal_views/tripal_views.views.inc

@@ -217,7 +217,7 @@ function tripal_views_views_pre_render(&$view) {
 
 function tripal_views_views_data() {
 
-  // Make sure all chado tables are integrated
+	// Make sure all chado tables are integrated
   tripal_views_integrate_all_chado_tables();
 
   // Define Global Fields

+ 3 - 3
tripal_views/views/handlers/tripal_views_handler_filter_select_string.inc

@@ -44,7 +44,7 @@ class tripal_views_handler_filter_select_string extends chado_views_handler_filt
       '#default_value' => (isset($this->options['max_length'])) ? $this->options['max_length'] : 40,
 
     );
-    $form['max_length'] = array(
+    $form['note'] = array(
       '#type' => 'markup',
       '#value' => t('<strong><font color="red">Note:</font></strong> If another filter exists for the same table then '.
                     'the values shown in the drop box will only include those from rows that are not filtered.'),
@@ -185,12 +185,12 @@ class tripal_views_handler_filter_select_string extends chado_views_handler_filt
 
     // filter the aggregates
     if ($this->options['agg']['aggregates_with']) {
-      $this::query_restrict_curr_table_records();
+      $this->query_restrict_curr_table_records();
     }
 
     // filter the base table
     if ($this->options['agg']['records_with']) {
-      $this::query_restrict_base_records();
+      $this->query_restrict_base_records();
     }