You are here

function glossary_get_related in Glossary 6

Same name and namespace in other branches
  1. 5.2 glossary.module \glossary_get_related()
  2. 7 glossary.module \glossary_get_related()

Find all term objects related to a given term ID. Adapted from taxonomy.module.

Parameters

$tid: the term id to look up (int).

$one_way: whether to do one-way or two-way relations (bool).

Return value

an array related-tid => related-name

2 calls to glossary_get_related()
theme_glossary_block_term in ./glossary.module
theme_glossary_overview_item in ./glossary.module

File

./glossary.module, line 1215
Glossary terms will be automatically marked with links to their descriptions.

Code

function glossary_get_related($tid, $key = 'tid', $one_way = FALSE) {
  if ($tid) {
    $related = array();
    $qargs = array_fill(0, 3, $tid);
    if ($one_way) {
      $result = db_query('SELECT r.tid2 FROM {term_relation} r WHERE r.tid1 = %d ORDER BY r.tid2', $qargs);
      while ($term = db_fetch_object($result)) {

        // Hope that taxonomy has this cached to save a query.
        $rel = taxonomy_get_term($term->tid2);
        $related[$rel->{$key}] = $rel;
      }
    }
    else {

      // Two-way (normal taxonomy function).
      $result = db_query('SELECT t.*, tid1, tid2 FROM {term_relation}, {term_data} t WHERE (t.tid = tid1 OR t.tid = tid2) AND (tid1 = %d OR tid2 = %d) AND t.tid != %d ORDER BY weight, name', $qargs);
      while ($term = db_fetch_object($result)) {
        $related[$term->{$key}] = $term;
      }
    }
    return $related;
  }
  else {
    return array();
  }
}