function chado_node_get_readable_format

2.x tripal_core.chado_nodes.title_and_path.api.inc chado_node_get_readable_format($token)
3.x tripal_core.chado_nodes.title_and_path.inc chado_node_get_readable_format($token)

Generate a Readable but not necessarily unique format based on a given primary key token.

For example, given the token [feature.type_id>cvterm.cvterm_id] you don't want the actual id indexed but instead would want the term name, [feature.type_id>cvterm.name]

1 call to chado_node_get_readable_format()
tripal_core_entity_property_info_alter in tripal_core/includes/tripal_core.search.inc
Implements hook_entity_property_info_alter().

File

tripal_core/api/tripal_core.chado_nodes.title_and_path.api.inc, line 1148
Contains API functions to set titles and paths for all chado nodes

Code

function chado_node_get_readable_format($token) {

  // First, lets break down the token into it's parts.
  // 1. Remove containing brackets.
  $parts = str_replace(array('[', ']'), '', $token);
  // 2. Break into table clauses.
  $parts = explode('>', $parts);
  // 3. Break each table clause into table & field.
  foreach ($parts as $k => $v) {
    $parts[$k] = explode('.', $v);
    if (sizeof($parts[$k]) == 1) {
      $parts[$k] = explode(':', $v);
    }
  }
  $last_k = $k;

  // Now, we want to find readable fields for the last table specified in the token.
  // (ie: for cvterm in [feature.type_id>cvterm.cvterm_id])
  $table = $parts[$last_k][0];
  $format = array();
  if ($table == 'organism') {
    $format[] = preg_replace('/(\w+)\]$/', 'genus]', $token);
    $format[] = preg_replace('/(\w+)\]$/', 'species]', $token);
    $format[] = preg_replace('/(\w+)\]$/', 'common_name]', $token);
    $format = $format[0] . ' ' . $format[1] . ' (' . $format[2] . ')';
  }
  elseif ($table == 'dbxref') {
    $format[] = preg_replace('/(\w+)\]$/', 'accession]', $token);
    $format[] = preg_replace('/(\w+)\]$/', 'db_id>db.name]', $token);
    $format = $format[0] . ' (' . $format[1] . ')';
  }
  else {
    $schema = chado_get_schema($table);
    foreach ($schema['fields'] as $field_name => $details) {
      if (preg_match('/name/', $field_name)) {
        $format[] = preg_replace('/(\w+)\]$/', $field_name . ']', $token);
      }
    }
    $format = implode(', ', $format);
  }
  if (empty($format)) {
    return FALSE;
  }

  return $format;
}