You are here

public function FrxReport::format in Forena Reports 7.4

Same name and namespace in other branches
  1. 6.2 FrxReport.inc \FrxReport::format()
  2. 6 FrxReport.inc \FrxReport::format()
  3. 7 FrxReport.inc \FrxReport::format()
  4. 7.2 FrxReport.inc \FrxReport::format()
  5. 7.3 FrxReport.inc \FrxReport::format()

File

./FrxReport.inc, line 410
Basic report provider. Controls the rendering of the report.

Class

FrxReport

Code

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

  // Determine if there is a field overide entry
  $default = '';
  $link = '';
  $format = '';
  $format_str = '';
  $target = '';
  $class = '';
  $rel = '';
  $calc = '';
  if ($this->fields) {
    $path = 'frx:field[@id="' . $key . '"]';
    $formatters = $this->fields
      ->xpath($path);
    if ($formatters) {
      foreach ($formatters as $formatter) {
        if (isset($formatter['block']) && (string) $formatter['block'] == $this->block || !(string) $formatter['block']) {

          //@TODO: Replace the default extraction with something that will get sub elements of the string
          $default = (string) $formatter;
          $link = (string) $formatter['link'];
          $add_query = (string) $formatter['add-query'];
          $class = (string) $formatter['class'];
          $rel = (string) $formatter['rel'];
          $format = (string) $formatter['format'];
          $calc = (string) $formatter['calc'];
          $context = (string) $formatter['context'];
          $format_str = (string) $formatter['format-string'];
          $target = (string) $formatter['target'];
        }
      }
    }
  }

  // Evaluate any calculations first.
  if ($calc) {
    if ($context) {
      $context .= ".";
    }
    $calc = $this->teng
      ->replace($calc);
    if ($calc) {
      $value = $this->teng
        ->replace('{' . $context . '=' . $calc . '}', TRUE);
    }
  }
  if ($format && !$raw) {
    $value = FrxReportGenerator::instance()
      ->format_data($value, $format, $format_str, $this->teng, $default);
    $value = trim($value);
  }
  if (is_array($value) && !$raw) {
    $value = implode(' ', $value);
  }

  // Default if specified
  if (!$value && $default) {
    $value = $default;
  }
  if ($link && !$raw) {
    $attributes = array();
    $target = $this->teng
      ->replace($target, TRUE);

    // use the target attribute to open links in new tabs or as popups.
    if (@strpos(strtolower($target), 'popup') === 0) {
      $options = "status=1";
      $attributes = array(
        'onclick' => 'window.open(this.href, \'' . $target . '\', "' . $options . '"); return false;',
      );
    }
    else {
      if ($target) {
        $attributes['target'] = $target;
      }
    }

    // Adding support for ctools modals.
    if ($link && strpos($class, 'ctools-use-modal') !== FALSE && is_callable('ctools_include')) {
      ctools_include('modal');
      ctools_modal_add_js();
    }
    if ($rel) {
      $attributes['rel'] = $this->teng
        ->replace($rel, TRUE);
    }
    if ($class) {
      $attributes['class'] = explode(' ', trim($this->teng
        ->replace($class, TRUE)));
    }
    @(list($url, $query) = explode('?', $link));
    $url = $this->teng
      ->replace($url, TRUE);
    @(list($query, $queryFrag) = explode('#', $query));
    @(list($url, $fragment) = explode('#', $url));
    $fragment = $fragment . $queryFrag;
    $data = array();
    parse_str($query, $data);
    if ($data) {
      foreach ($data as $k => $v) {
        $data[$k] = $this->teng
          ->replace($v, TRUE);
      }
    }
    if ($add_query) {
      $parms = $_GET;
      unset($parms['q']);
      $data = array_merge($parms, $data);
    }
    if (trim($url)) {
      $value = $this
        ->link(htmlspecialchars_decode($value), $url, array(
        'fragment' => $fragment,
        'query' => $data,
        'attributes' => $attributes,
        'absolute' => TRUE,
      ));
    }
  }
  if ($this->preview_mode && !$raw) {
    $value .= Frx::Editor()
      ->fieldLink($key, $value);
  }
  return $value;
}