Pārlūkot izejas kodu

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

Lacey Sanderson 11 gadi atpakaļ
vecāks
revīzija
c107f4c6c4

+ 57 - 30
tripal_core/api/tripal_core_chado_nodes.api.inc

@@ -234,17 +234,26 @@ function tripal_core_chado_node_sync_form($form, &$form_state) {
       foreach ($results as $result) {
         $values[$result->id] = $result->value;
       }
-      $form['sync']['ids'] = array(
-        '#title'         => 'Avaliable ' . $args['record_type_title']['plural'],
-        '#type'          => 'checkboxes',
-        '#description'   => t("The above  %title_plural have not been synced. Check those to be synced or leave all unchecked to sync them all.",
-          array(
-            '%title_singular' => strtolower($args['record_type_title']['singular']),
-            '%title_plural'   => strtolower($args['record_type_title']['plural'])
-          )),
-        '#options'       => $values,
-        '#default_value' => (isset($form_state['values']['ids'])) ? $form_state['values']['ids'] : array(),
-      );
+      if (count($values) > 0) {
+        $form['sync']['ids'] = array(
+          '#title'         => 'Avaliable ' . $args['record_type_title']['plural'],
+          '#type'          => 'checkboxes',
+          '#description'   => t("The above  %title_plural have not been synced. Check those to be synced or leave all unchecked to sync them all.",
+            array(
+              '%title_singular' => strtolower($args['record_type_title']['singular']),
+              '%title_plural'   => strtolower($args['record_type_title']['plural'])
+            )),
+          '#options'       => $values,
+          '#default_value' => (isset($form_state['values']['ids'])) ? $form_state['values']['ids'] : array(),
+          '#prefix'        => '<div style="height: 200px; overflow: scroll">',
+          '#suffix'        => '</div><br>',
+        );
+      } 
+      else {
+        $form['sync']['no_ids'] = array(
+          '#markup' => "<p>There are no " .  strtolower($args['record_type_title']['plural']) . " to sync.</p>",
+        );
+      }
     }
   }
   // if we provide a list of checkboxes we shouldn't need a max_sync
@@ -387,17 +396,17 @@ function tripal_core_chado_node_sync_records($base_table, $max_sync = FALSE, $or
     $select[] = 'cvterm.name as cvtname';
     $joins[] = "LEFT JOIN {cvterm} cvterm ON $base_table.type_id = cvterm.cvterm_id";
     foreach ($types as $type) {
-      $where_clauses[] = "cvterm.name = :type_name_$type";
-      $where_args[":type_name_$type"] = $type;
+      $where_clauses['type'][] = "cvterm.name = :type_name_$type";
+      $where_args['type'][":type_name_$type"] = $type;
     }
   }
   
   // if IDs have been supplied
   if ($ids) {
-    $restrictions .= "  Specific Records: " . count($ids) . " recored(s) directory specified.\n";
+    $restrictions .= "  Specific Records: " . count($ids) . " recored(s) specified.\n";
     foreach ($ids as $id) {
-      $where_clauses[] = "$base_table_id = :id_$id";
-      $where_args[":id_$id"] = $id;
+      $where_clauses['id'][] = "$base_table.$base_table_id = :id_$id";
+      $where_args['id'][":id_$id"] = $id;
     }
   }
 
@@ -408,8 +417,8 @@ function tripal_core_chado_node_sync_records($base_table, $max_sync = FALSE, $or
     
     $select[] = 'organism.*';
     $joins[] = "LEFT JOIN {organism} organism ON organism.organism_id = $base_table.organism_id";
-    $where_clauses[] = 'organism.organism_id = :organism_id';
-    $where_args[':organism_id'] = $organism_id;
+    $where_clauses['organism'][] = 'organism.organism_id = :organism_id';
+    $where_args['organism'][':organism_id'] = $organism_id;
   }
 
   // Allow module to add to query
@@ -418,11 +427,34 @@ function tripal_core_chado_node_sync_records($base_table, $max_sync = FALSE, $or
     call_user_func($hook_query_alter, $select, $joins, $where_clauses, $where_args);
   }
 
-  // Build Query
-  $query = "SELECT " . implode(', ',$select) . ' FROM {' . $base_table . '} ' . $base_table . ' ' . implode(' ', $joins);
-  if (count($where_clauses) > 0) {
-    $query .= " WHERE " . implode(' AND ', $where_clauses);
+  // Build Query, we do a left join on the chado_xxxx table in the Drupal schema
+  // so that if no criteria are specified we only get those items that have not
+  // yet been synced. 
+  $query = "
+    SELECT " . implode(', ',$select) . ' ' .
+    'FROM {' . $base_table . '} ' . $base_table . ' ' . implode(' ', $joins) . ' '.
+    "  LEFT JOIN public.chado_$base_table CT ON CT.$base_table_id = $base_table.$base_table_id " .
+    "WHERE CT.$base_table_id IS NULL AND";
+  
+  // extend the where clause if needed
+  $where = '';
+  $sql_args = array();
+  if (count($where_clauses['type']) > 0) {
+    $where .= '(' . implode(' OR ', $where_clauses['type']) . ') AND';
+    $sql_args = array_merge($sql_args, $where_args['type']);
+  }
+  if (count($where_clauses['organism']) > 0) {
+    $where .= '(' . implode(' OR ', $where_clauses['organism']) . ') AND';
+    $sql_args = array_merge($sql_args, $where_args['organism']);
+  }
+  if (count($where_clauses['id']) > 0) {
+    $where .= '(' . implode(' OR ', $where_clauses['id']) . ') AND';
+    $sql_args = array_merge($sql_args, $where_args['id']);
   }
+  if ($where) {
+    $query .= $where;
+  }
+  $query = substr($query, 0, -4); // remove the trailing 'AND'
   $query .- " ORDER BY " . $base_table_id;
 
   // If Maximum number to Sync is supplied
@@ -437,14 +469,9 @@ function tripal_core_chado_node_sync_records($base_table, $max_sync = FALSE, $or
   else {
     print "\n";
   } 
-    
-  
-  //print "\nQuery: " . preg_replace(array("/FROM/","/LEFT/","/WHERE/"), array("\nFROM","\nLEFT","\nWHERE"), $query) . "\n";
 
-  print_r($query, $where_args);
-  
-  //print "Executing Query...\n";
-  $results = chado_query($query, $where_args);
+  // execute the query
+  $results = chado_query($query, $sql_args);
 
   // Iterate through features that need to be synced
   $count = $results->rowCount();
@@ -460,7 +487,7 @@ function tripal_core_chado_node_sync_records($base_table, $max_sync = FALSE, $or
   try {
     foreach ($results as $record) {
 
-      print "\nLoading $base_table $i of $count ($base_table_id=".$record->{$base_table_id}.")...";
+      print "\nLoading $base_table " . ($i + 1) . " of $count ($base_table_id=".$record->{$base_table_id}.")...";
 
       // update the job status every 1% features
       if ($job_id and $i % $interval == 0) {

+ 156 - 90
tripal_feature/includes/tripal_feature.chado_node.inc

@@ -629,11 +629,11 @@ function tripal_feature_node_presave($node) {
 
   // set the title to ensure it is always unique
   switch ($node->type) {
-  	case 'chado_feature':
-  	  $values = array('organism_id' => $node->organism_id);
-  	  $organism = tripal_core_chado_select('organism', array('genus', 'species'), $values);
-  	  $node->title = $node->fname . ', ' . $node->uniquename . ' (' . $node->feature_type . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
-  	  break;
+    case 'chado_feature':
+      $values = array('organism_id' => $node->organism_id);
+      $organism = tripal_core_chado_select('organism', array('genus', 'species'), $values);
+      $node->title = $node->fname . ', ' . $node->uniquename . ' (' . $node->feature_type . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
+      break;
   }
 }
 
@@ -646,91 +646,157 @@ function tripal_feature_node_insert($node) {
   // set the URL path after inserting.  We do it here because we do not
   // know the feature_id in the presave
   switch ($node->type) {
-  	case 'chado_feature':
-  	  if (!$node->feature_id) {
-  	    $sql = "SELECT * FROM {chado_feature} WHERE nid = :nid";
-  	    $chado_feature = db_query($sql, array(':nid' => $node->nid))->fetchObject();
-  	    $node->feature_id = $chado_feature->feature_id;
-  	  }
-
-  	  // remove any previous alias
-  	  db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
-
-  	  // set the URL for this feature page
-  	  $url_alias = tripal_feature_get_feature_url($node);
-  	  $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
-  	  path_save($path_alias);
-  	  break;
+    case 'chado_feature':
+      if (!$node->feature_id) {
+        $sql = "SELECT * FROM {chado_feature} WHERE nid = :nid";
+        $chado_feature = db_query($sql, array(':nid' => $node->nid))->fetchObject();
+        $node->feature_id = $chado_feature->feature_id;
+      }
+
+      // remove any previous alias
+      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
+
+      // set the URL for this feature page
+      $url_alias = tripal_feature_get_feature_url($node);
+      $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
+      path_save($path_alias);
+      break;
   }
 }
+/**
+ *
+ * @param $node
+ *   A node object containing at least the feature_id and nid
+ * @param $url_alias
+ *   Optional.  This should be the URL alias syntax string that contains
+ *   placeholders such as [id], [genus], [species], [name], [uniquename],
+ *   and [type].  These placeholders will be substituted for actual values.
+ *   If this parameter is not provided then the value of the
+ *   chado_feature_url_string Drupal variable will be used.
+ */
+function tripal_feature_get_feature_url($node, $url_alias = NULL) {
 
+  // get the starting URL alias
+  if(!$url_alias) {
+    $url_alias = variable_get('chado_feature_url_string', '/feature/[genus]/[species]/[type]/[uniquename]');
+    if (!$url_alias) {
+      $url_alias = '/feature/[genus]/[species]/[type]/[uniquename]';
+    }
+    $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
+  }
+
+  // get the feature
+  $values = array('feature_id' => $node->feature_id);
+  $feature = tripal_core_chado_select('feature', array('*'), $values);
+  if (!$feature) {
+    watchdog('trp-seturl', "Cannot find feature when setting URL alias for feature: %id", array('%id' => $node->feature_id), WATCHDOG_ERROR);
+    return FALSE;
+  }
+  $feature = (object) $feature[0];
+
+  // get the organism
+  $values = array('organism_id' => $feature->organism_id);
+  $organism  = tripal_core_chado_select('organism', array('*'), $values);
+  if (!$organism) {
+    watchdog('trp-seturl', "Cannot find organism when setting URL alias for feature: %id", array('%id' => $node->feature_id), WATCHDOG_ERROR);
+    return FALSE;
+  }
+  $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
+  $species = preg_replace('/\s/', '_', strtolower($organism[0]->species));
+
+  // get the type
+  $values = array('cvterm_id' => $feature->type_id);
+  $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
+  if (!$cvterm) {
+    watchdog('trp-seturl', "Cannot find type when setting URL alias for feature: %id", array('%id' => $node->feature_id), WATCHDOG_ERROR);
+    return FALSE;
+  }
+  $type = preg_replace('/\s/', '_', $cvterm[0]->name);
+
+  // now substitute in the values
+  $url_alias = preg_replace('/\[id\]/', $feature->feature_id, $url_alias);
+  $url_alias = preg_replace('/\[genus\]/', $genus, $url_alias);
+  $url_alias = preg_replace('/\[species\]/', $species, $url_alias);
+  $url_alias = preg_replace('/\[type\]/', $type, $url_alias);
+  $url_alias = preg_replace('/\[name\]/', $feature->name, $url_alias);
+  $url_alias = preg_replace('/\[uniquename\]/', $feature->uniquename, $url_alias);
+
+  // the dst field of the url_alias table is only 128 characters long.
+  // if this is the case then simply return the node URL, we can't set this one
+  if (strlen($url_alias) > 128) {
+    watchdog('trp-seturl', "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias), WATCHDOG_ERROR);
+    return "node/" . $node->nid;
+  }
+
+  return $url_alias;
+}
 /**
  *
  * @ingroup tripal_feature
  */
 function tripal_feature_node_view($node, $view_mode, $langcode) {
   switch ($node->type) {
-  	case 'chado_feature':
-  	  // Show feature browser and counts
-  	  if ($view_mode == 'full') {
-  	    $node->content['tripal_feature_alignments'] = array(
-  	      '#value' => theme('tripal_feature_alignments', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_analyses'] = array(
-  	      '#value' => theme('tripal_feature_analyses', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_base'] = array(
-  	      '#value' => theme('tripal_feature_base', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_featurepos'] = array(
-  	      '#value' => theme('tripal_feature_featurepos', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_properties'] = array(
-  	      '#value' => theme('tripal_feature_properties', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_publications'] = array(
-  	      '#value' => theme('tripal_feature_publications', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_references'] = array(
-  	      '#value' => theme('tripal_feature_references', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_relationships'] = array(
-  	      '#value' => theme('tripal_feature_relationships', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_seqence'] = array(
-  	      '#value' => theme('tripal_feature_sequence', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_synonyms'] = array(
-  	      '#value' => theme('tripal_feature_synonyms', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_feature_terms'] = array(
-  	      '#value' => theme('tripal_feature_terms', array('node' => $node)),
-  	    );
-  	  }
-  	  if ($view_mode == 'teaser') {
-  	    $node->content['tripal_feature_teaser'] = array(
-  	      '#value' => theme('tripal_feature_teaser', array('node' => $node)),
-  	    );
-  	  }
-  	  break;
-  	case 'chado_organism':
-  	  // Show feature browser and counts
-  	  if ($view_mode == 'full') {
-  	    $node->content['tripal_organism_feature_counts'] = array(
-  	      '#value' => theme('tripal_organism_feature_counts', array('node' => $node)),
-  	    );
-  	    $node->content['tripal_organism_feature_browser'] = array(
-  	      '#value' => theme('tripal_organism_feature_browser', array('node' => $node)),
-  	    );
-  	  }
-  	  break;
-  	  // TODO: handle these node types. Should we also have a feature browser?
-  	case 'chado_library':
-  	  break;
-  	case 'chado_stock':
-  	  break;
-  	case 'chado_analysis':
-  	  break;
+    case 'chado_feature':
+      // Show feature browser and counts
+      if ($view_mode == 'full') {
+        $node->content['tripal_feature_alignments'] = array(
+          '#value' => theme('tripal_feature_alignments', array('node' => $node)),
+        );
+        $node->content['tripal_feature_analyses'] = array(
+          '#value' => theme('tripal_feature_analyses', array('node' => $node)),
+        );
+        $node->content['tripal_feature_base'] = array(
+          '#value' => theme('tripal_feature_base', array('node' => $node)),
+        );
+        $node->content['tripal_feature_featurepos'] = array(
+          '#value' => theme('tripal_feature_featurepos', array('node' => $node)),
+        );
+        $node->content['tripal_feature_properties'] = array(
+          '#value' => theme('tripal_feature_properties', array('node' => $node)),
+        );
+        $node->content['tripal_feature_publications'] = array(
+          '#value' => theme('tripal_feature_publications', array('node' => $node)),
+        );
+        $node->content['tripal_feature_references'] = array(
+          '#value' => theme('tripal_feature_references', array('node' => $node)),
+        );
+        $node->content['tripal_feature_relationships'] = array(
+          '#value' => theme('tripal_feature_relationships', array('node' => $node)),
+        );
+        $node->content['tripal_feature_seqence'] = array(
+          '#value' => theme('tripal_feature_sequence', array('node' => $node)),
+        );
+        $node->content['tripal_feature_synonyms'] = array(
+          '#value' => theme('tripal_feature_synonyms', array('node' => $node)),
+        );
+        $node->content['tripal_feature_terms'] = array(
+          '#value' => theme('tripal_feature_terms', array('node' => $node)),
+        );
+      }
+      if ($view_mode == 'teaser') {
+        $node->content['tripal_feature_teaser'] = array(
+          '#value' => theme('tripal_feature_teaser', array('node' => $node)),
+        );
+      }
+      break;
+    case 'chado_organism':
+      // Show feature browser and counts
+      if ($view_mode == 'full') {
+        $node->content['tripal_organism_feature_counts'] = array(
+          '#value' => theme('tripal_organism_feature_counts', array('node' => $node)),
+        );
+        $node->content['tripal_organism_feature_browser'] = array(
+          '#value' => theme('tripal_organism_feature_browser', array('node' => $node)),
+        );
+      }
+      break;
+      // TODO: handle these node types. Should we also have a feature browser?
+    case 'chado_library':
+      break;
+    case 'chado_stock':
+      break;
+    case 'chado_analysis':
+      break;
   }
 }
 /**
@@ -741,14 +807,14 @@ function tripal_feature_node_update($node) {
 
   // add items to other nodes, build index and search results
   switch ($node->type) {
-  	case 'chado_feature':
-  	  // remove any previous alias
-  	  db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
-
-  	  // set the URL for this feature page
-  	  $url_alias = tripal_feature_get_feature_url($node);
-  	  $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
-  	  path_save($path_alias);
-  	  break;
+    case 'chado_feature':
+      // remove any previous alias
+      db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
+
+      // set the URL for this feature page
+      $url_alias = tripal_feature_get_feature_url($node);
+      $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
+      path_save($path_alias);
+      break;
   }
 }