You are here

function _lineage_weights in Taxonomy Lineage 7

Same name and namespace in other branches
  1. 6 lineage.module \_lineage_weights()

Determine how many digits to use in weights.

Use db_query() and not db_select() to keep the code small. Apparently you can't chain addExpression() into a SelectQuery :-/

Parameters

vid: The vocabulary vid.

Return value

Array of min weight, max weight, offset to use, and # digits.

1 call to _lineage_weights()
lineage_string in ./lineage.module
Ensure positive weights with enough digits so that all sort properly.

File

./lineage.module, line 374
lineage.module Module code for taxonomy term hierarchy lineage.

Code

function _lineage_weights($vid) {

  // Keep static array with weight info to prevent unneeded queries.
  static $weights = array();
  if (!isset($weights[$vid])) {

    // $weights[$vid]['min'] = db_select('taxonomy_term_data')->addExpression('MIN(weight)')->condition('vid', $vid, '=')->execute()->fetchField();
    $weights[$vid]['min'] = db_query('SELECT MIN(weight) FROM {taxonomy_term_data} WHERE vid = :vid', array(
      ':vid' => $vid,
    ))
      ->fetchField();
    $weights[$vid]['offset'] = $weights[$vid]['min'] < 0 ? abs($weights[$vid]['min']) : 0;

    // $weights[$vid]['max'] = db_select('taxonomy_term_data')->addExpression('MAX(weight)')->condition('vid', $vid, '=')->execute()->fetchField();
    $weights[$vid]['max'] = db_query('SELECT MAX(weight) FROM {taxonomy_term_data} WHERE vid = :vid', array(
      ':vid' => $vid,
    ))
      ->fetchField();
    $weights[$vid]['digits'] = floor(log($weights[$vid]['max'] + $weights[$vid]['offset'] + 1));
    if ($weights[$vid]['digits'] == 0) {
      $weights[$vid]['digits']++;
    }
  }
  return $weights[$vid];
}