You are here

public function ReportReplacer::format in Forena Reports 8

File

src/Token/ReportReplacer.php, line 114

Class

ReportReplacer

Namespace

Drupal\forena\Token

Code

public function format($value, $key, $raw = FALSE) {

  // Determine if there is a field overide entry
  $default = '';
  $link = '';
  $format = '';
  $format_string = '';
  $calc = '';
  $context = '';
  $field = [];
  if (isset($this->fields[$key])) {
    $field = $this->fields[$key];
    extract($field);
    if (isset($field['format-string'])) {
      $format_string = $field['format-string'];
    }
  }

  // Evaluate any calculations first.
  if ($calc) {
    if ($context) {
      $context .= ".";
    }
    $calc = $this
      ->replace($calc, TRUE);
    if ($calc) {
      $value = $this
        ->replace('{' . $context . '=' . $calc . '}', TRUE);
    }
  }

  //@TODO: Figure out how to deal with formatters.
  if ($format && !$raw) {
    if (!empty($this->format_callbacks[$format])) {
      $value = call_user_func($this->format_callbacks[$format], $value, $format_string, $this, $default);
    }
    $value = trim($value);
  }
  if (is_array($value) && !$raw) {
    $value = implode(' ', $value);
  }

  // Default if specified
  if (!$value && $default) {
    $value = $default;
  }
  if ($link && !$raw) {
    $link_fields = array_merge(array_fill_keys([
      'link',
      'target',
      'rel',
      'class',
      'add_query',
    ], ''), $field);
    $this
      ->replaceNested($link_fields, TRUE);
    $value = AppService::instance()
      ->reportLink($value, $link_fields);
  }
  return $value;
}