You are here

public function DataContext::getValue in Forena Reports 7.5

Same name and namespace in other branches
  1. 8 src/Context/DataContext.php \Drupal\forena\Context\DataContext::getValue()

Get the value from the data. This is used by token_replace method to extract the data based on the path provided.

Parameters

$data:

$key:

Return value

unknown_type

Overrides Context::getValue

File

src/Context/DataContext.php, line 150
DataContext.php The FrxData class holds all of the data contexts during the report rendering process. The general idea is that during the report render, data objects are pushed on the stack with the id's of the block or foreach objects that…

Class

DataContext

Namespace

Drupal\forena\Context

Code

public function getValue($key, $context = '') {
  $retvar = '';

  // Default to theo current context
  $data = $this
    ->currentContext();
  if ($context && $this
    ->contextExists($context)) {
    $data = $this
      ->getContext($context);
  }
  if (is_array($data) && !preg_match('/[\\=\\[\\/\\]\\(\\)]/', $key)) {
    $retvar = @$data[$key];
  }
  elseif (is_object($data) || is_array($data)) {
    if (is_array($data) || !method_exists($data, 'xpath')) {
      if (!$this->cur_context_xml) {
        $this->cur_context_xml = \Drupal\forena\Context\DataContext::arrayToXml($data);
      }
      $data = $this->cur_context_xml;
    }
    if (strpos($key, '=') === 0) {
      $retvar = $this
        ->simplexml_evaluate($data, ltrim($key, '='));
    }
    else {
      $x = '';
      if (isset($data->{$key})) {
        $x = $data->{$key};
      }
      elseif (method_exists($data, 'xpath')) {
        $rows = @$data
          ->xpath($key);
        if ($rows === FALSE) {
          drupal_set_message(t('Invalid field: "%s"', array(
            '%s' => $key,
          )), 'error', FALSE);
        }
        if ($rows) {
          $x = $rows[0];
        }
      }
      if ($x && is_object($x) && method_exists($x, 'asXML')) {
        $retvar = $x
          ->asXML();

        // Check to see if there are child nodes
        // If so use asXML otherwise string cast.
        if ($retvar && strpos($retvar, '<') !== FALSE) {

          // Find the end of the first tag.
          $p = strpos($retvar, '>');
          $retvar = substr_replace($retvar, '', 0, $p + 1);
          $p = strrpos($retvar, '<', -1);
          $retvar = substr_replace($retvar, '', $p, strlen($retvar) - $p);
        }
        else {
          $retvar = (string) $x;
        }
      }
      else {
        $retvar =& $x;
      }
    }
  }
  if (!is_array($retvar)) {
    if (is_object($retvar) && is_a($retvar, 'DOMNodeList')) {
      $retvar = $retvar
        ->item(0);
      if ($retvar) {
        $retvar = trim($retvar->textContent);
      }
    }
    else {
      $retvar = trim((string) $retvar);
    }
  }
  return $retvar;
}