You are here

function theme_range_formatter_generic in Range 6

Proxy theme function for number field formatters.

1 string reference to 'theme_range_formatter_generic'
range_theme in ./range.module
Implementation of hook_theme().

File

./range.module, line 304
Defines numeric fields within a range of possible values and displays them colored accordingly..

Code

function theme_range_formatter_generic($element) {
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $value = $element['#item']['value'];
  if ($allowed_values = content_allowed_values($field)) {
    if (isset($allowed_values[$value]) && $allowed_values[$value] != $value) {
      return $allowed_values[$value];
    }
  }
  if (empty($value) && $value !== '0') {
    return '';
  }
  switch ($element['#formatter']) {
    case 'us_0':
      $output = number_format($value, 0, '.', ',');
      break;
    case 'us_1':
      $output = number_format($value, 1, '.', ',');
      break;
    case 'us_2':
      $output = number_format($value, 2, '.', ',');
      break;
    case 'be_0':
      $output = number_format($value, 0, ',', '.');
      break;
    case 'be_1':
      $output = number_format($value, 1, ',', '.');
      break;
    case 'be_2':
      $output = number_format($value, 2, ',', '.');
      break;
    case 'fr_0':
      $output = number_format($value, 0, ', ', ' ');
      break;
    case 'fr_1':
      $output = number_format($value, 1, ', ', ' ');
      break;
    case 'fr_2':
      $output = number_format($value, 2, ', ', ' ');
      break;
    default:
      $output = $value;
      break;
  }
  $prefixes = isset($field['prefix']) ? explode('|', check_plain($field['prefix'])) : array(
    0 => '',
  );
  $suffixes = isset($field['suffix']) ? explode('|', check_plain($field['suffix'])) : array(
    0 => '',
  );
  $prefix = count($prefixes) > 1 ? format_plural($value, $prefixes[0], $prefixes[1]) : $prefixes[0];
  $suffix = count($suffixes) > 1 ? format_plural($value, $suffixes[0], $suffixes[1]) : $suffixes[0];
  $field_settings = unserialize(db_result(db_query("SELECT global_settings from {" . content_field_tablename() . "} where field_name = '%s'", $element['#field_name'])));
  if ($field_settings['min'] <= $value && $value <= $field_settings['max'] || $field_settings['min'] == '<' && $value <= $field_settings['max'] || $field_settings['max'] == '>' && $value >= $field_settings['min']) {
    $class = 'green';
  }
  else {
    $class = 'red';
  }
  $separator = $field_settings['min'] == '<' || $field_settings['max'] == '>' ? '' : ' - ';
  $range = $field_settings['max'] == '>' ? ' (' . $field_settings['max'] . $separator . $field_settings['min'] . ' ' . $suffix . ')' : ' (' . $field_settings['min'] . $separator . $field_settings['max'] . ' ' . $suffix . ')';
  return $prefix . '<span class=' . $class . '>' . $output . '</span>' . $range;
}