You are here

function _glossary_get_terms in Glossary 7

Same name and namespace in other branches
  1. 5.2 glossary.module \_glossary_get_terms()
  2. 5 glossary.module \_glossary_get_terms()
  3. 6 glossary.module \_glossary_get_terms()

Get taxonomy terms.

1 call to _glossary_get_terms()
glossary_filter_glossary_process in ./glossary.module
Implements hook_filter_FILTER_process().

File

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

Code

function _glossary_get_terms($format) {
  static $got = array();
  $show_all = variable_get('glossary_allow_no_description', FALSE);
  $taxonomy_image_enabled = module_exists('taxonomy_image');
  if (!isset($got[$format])) {
    $got[$format] = array();
  }
  if (!($terms = $got[$format])) {
    $terms = array();
    $vids = variable_get("glossary_vids_{$format}", 0);
    foreach ($vids as $vid) {

      // Get all glossary terms.
      // Omit terms without a description. those are usually container terms.
      // If multilingual taxonomy is enabled only show terms in current or no
      // language.
      $query = db_select('taxonomy_term_data', 'td');

      // We have to use a left join because some people don't use nodes.
      $query
        ->leftJoin('taxonomy_index', 'ti', 'ti.tid = td.tid');
      $query
        ->fields('td', array(
        'tid',
        'name',
        'description',
      ))
        ->addExpression('COUNT(ti.nid)', 'nodes');
      $query
        ->condition('td.vid', $vid)
        ->groupBy('td.tid')
        ->orderBy('LENGTH(td.name)', 'DESC');
      if (module_exists('i18ntaxonomy')) {
        global $language;
        $query
          ->condition('td.language', array(
          '',
          $language->language,
        ), 'IN');
      }

      // $query->addTag('term_access');
      $result = $query
        ->execute();
      while ($term = $result
        ->fetchObject()) {
        if ($term->nodes) {

          // If there were any nodes attached, we need to
          // see if they were unpublished.
          // TODO Please convert this statement to the D7 database API syntax.

          //$unpubs = db_query(db_rewrite_sql("SELECT COUNT(n.nid) FROM {taxonomy_term_node} tn JOIN {node} n USING (nid) WHERE tn.tid=%d AND n.status=0"), $term->tid)->fetchField();
          $query = db_select('taxonomy_index', 'ti')
            ->condition('ti.tid', $term->tid, '=');
          $query
            ->addJoin('INNER', 'node', 'n', 'ti.nid = %alias.nid');
          $query
            ->condition('n.status', 0, '=');
          $query
            ->addExpression('COUNT(n.nid)', 'nodes');
          $unpubs = $query
            ->execute();
          $term->nodes -= $unpubs
            ->fetchField();
        }
        if ($term->description || $show_all) {
          $term->vid = $vid;
          $terms[] = $term;
        }
        if ($taxonomy_image_enabled) {
          $term->image = taxonomy_image_display($term->tid);
        }
        else {
          $term->image = NULL;
        }
      }
    }
    $got[$format] = $terms;
  }
  return $terms;
}