function tripal_feature_match_features_page

2.x tripal_feature.module tripal_feature_match_features_page($id)
3.x tripal_chado.module tripal_feature_match_features_page($id)
1.x tripal_feature.module tripal_feature_match_features_page($id)
1 string reference to 'tripal_feature_match_features_page'
tripal_feature_menu in tripal_feature/tripal_feature.module
Implements hook_menu().

File

tripal_feature/tripal_feature.module, line 479
Basic functionality for the tripal module

Code

function tripal_feature_match_features_page($id) {

  // first check to see if the URL (e.g. /feature/$id) is already
  // assigned to a node. If so, then just go there.  Otherwise,
  // try to find the feature.
  $sql = "
    SELECT source
    FROM {url_alias}
    WHERE alias = :alias
  ";
  $match = db_query($sql, array(':alias' => "feature/$id"))->fetchObject();
  if ($match) {
    drupal_goto($match->source);
    return;
  }
  // @todo: re-write to support external chado databases.
  $sql = "
    SELECT
      F.name, F.uniquename, F.feature_id,
      O.genus, O.species, O.organism_id,
      CVT.cvterm_id, CVT.name as type_name,
      CF.nid,
      array_agg(S.name) as synonyms
    FROM {feature} F
      INNER JOIN {organism} O on F.organism_id = O.organism_id
      INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
      LEFT JOIN {feature_synonym} FS on FS.feature_id = F.feature_id
      LEFT JOIN {synonym} S on S.synonym_id = FS.synonym_id
      INNER JOIN [chado_feature] CF on CF.feature_id = F.feature_id
    WHERE
      F.uniquename = :uname or
      F.name = :fname or
      S.name = :sname
    GROUP BY F.name, F.uniquename, F.feature_id, O.genus, O.species,
      O.organism_id, CVT.cvterm_id, CVT.name, CF.nid
  ";

  $args = array(':uname' => $id, ':fname' => $id, ':sname' => $id);
  $results = chado_query($sql, $args);

  $num_matches = 0;

  // iterate through the matches and build the table for showing matches
  $header = array('Uniquename', 'Name', 'Type', 'Species', 'Synonyms');
  $rows = array();
  $curr_match;
  while ($match = $results->fetchObject()) {
    $curr_match = $match;
    $synonyms = $match->synonyms;
    $synonyms = preg_replace('/[\"\{\}]/', '', $synonyms);
    $rows[] = array(
      $match->uniquename,
      "<a href=\"" . url("node/" . $match->nid) . "\">" . $match->name . "</a>",
      $match->type_name,
      '<i>' . $match->genus . ' ' . $match->species . '</i>',
      $synonyms,
    );
    $num_matches++;
  }

  // if we have more than one match then generate the table, otherwise, redirect
  // to the matched feature
  if ($num_matches == 1) {
    drupal_goto("node/" . $curr_match->nid);
  }

  $table_attrs = array('class' => 'tripal-data-table');
  $params = array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => $table_attrs,
    'caption' => '',
    'empty' => "No features matched the given name '$id'",
    'sticky' => FALSE,
    'colgroups' => array(),
  );
  $output = "<p>The following features match the name '$id'.</p>";
  $output .= theme_table($params);
  return $output;
}