You are here

public function DataContext::getValue in Forena Reports 8

Same name and namespace in other branches
  1. 7.5 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

$key: The key/tken to search for.

string $context: The id of the context to get the value from.

Return value

mixed Value or array returned for replacement

File

src/Context/DataContext.php, line 127
Implements \Drupal\forena\Context\DataContext

Class

DataContext
The DataContext 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 invoke them.

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 (!preg_match('/[\\=\\@\\[\\/\\]\\(\\)]/', $key)) {
    if (is_array($data)) {
      $retvar = @$data[$key];
    }
    elseif (is_object($data)) {
      $retvar = $data->{$key};
    }
  }
  elseif (is_object($data) || is_array($data)) {
    if (is_array($data)) {
      if (!$this->cur_context_xml) {
        $this->cur_context_xml = DataContext::arrayToXml($data);
      }
      $data = $this->cur_context_xml;
    }
    elseif (!method_exists($data, 'xpath')) {
      if (method_exists($data, 'asXML')) {
        $xml = $data
          ->asXML();
        if (!is_object($xml) && $xml) {
          $xml = new SimpleXMLElement($xml);
        }
        $this->cur_context_xml = $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;
}