You are here

public function FrxData::getValue in Forena Reports 7.4

Same name and namespace in other branches
  1. 7.3 FrxData.inc \FrxData::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 FrxContext::getValue

File

./FrxData.inc, line 146

Class

FrxData

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 = FrxData::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;
}