View source
<?php
require_once 'FrxContext.inc';
class FrxData extends FrxContext {
public $id;
private $cur_context;
private $cur_context_xml;
private $data_sources = array();
private $data_stack = array();
private $id_stack = array();
public function __construct() {
$site = array();
global $language;
global $user;
global $theme_path;
global $base_root;
$site['base_path'] = base_path();
$site['dir'] = rtrim(base_path(), '/');
$site['theme_path'] = base_path() . $theme_path;
$site['theme_dir'] =& $theme_path;
$site['base_url'] =& $base_root;
$site['user_name'] = $user->uid ? $user->name : '';
$site['uid'] = $user ? $user->uid : 0;
$site['language'] =& $language;
$site['args'] = arg();
$site['page'] = base_path() . $_GET['q'];
$dest = drupal_get_destination();
$site['destination'] = $dest['destination'];
$this
->setContext('site', $site);
}
public static function instance() {
return Frx::Data();
}
public function currentContext() {
return $this->cur_context;
}
public function currentContextArray() {
$data = $this->cur_context;
if (is_array($data)) {
return $data;
}
$ret = array();
if (is_object($data)) {
$ret = get_object_vars($data);
if (method_exists($data, 'attributes')) {
foreach ($data
->attributes() as $key => $value) {
$ret[$key] = (string) $value;
}
}
}
else {
$ret = (array) $data;
}
return $ret;
}
protected function simplexml_evaluate($xml, $path) {
if (!method_exists($xml, 'xpath')) {
return '';
}
$dom_node = dom_import_simplexml($xml);
$dom_doc = new DOMDocument('');
$dom_node = $dom_doc
->importNode($dom_node, TRUE);
$dom_doc
->appendChild($dom_node);
$xpath = new DOMXpath($dom_doc);
$ret = $xpath
->evaluate($path, $dom_node);
return $ret;
}
public static function arrayToXml($a, &$xml = NULL) {
if (!$xml) {
$xml = new SimpleXMLElement('<root/>');
}
$tag = '';
foreach ($a as $k => $v) {
if (preg_match('/^[0-9\\-\\.]/', $k)) {
if (!$tag) {
$tag = "element";
}
}
else {
$tag = $k;
}
if (is_array($v) || is_object($v)) {
$node = $xml
->addChild($tag, '');
if ($tag != $k) {
$node['key'] = $k;
}
$node['type'] = is_object($v) ? 'object' : 'array';
FrxData::arrayToXml($v, $node);
}
else {
$node = $xml
->addChild($tag, htmlspecialchars($v));
$node['key'] = $k;
}
$tag = preg_replace('/[^a-zA-Z0-9]/', '_', (string) $k);
}
return $xml;
}
public function getValue($key, $context = '') {
$retvar = '';
$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();
if ($retvar && strpos($retvar, '<') !== FALSE) {
$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;
}
public function setValue($key, $value, $context = '') {
if (is_array($this->cur_context)) {
$this->cur_context[$key] = $value;
if ($this->cur_context_xml) {
$this->cur_context_xml->{$key} = $value;
}
}
elseif (is_object($this->cur_context)) {
if (strpos($key, '@') === 0) {
$this->cur_context[trim($key, '@')] = $value;
}
else {
$this->cur_context->{$key} = $value;
}
}
}
public function push($data, $id = '') {
$this->data_stack[] = $this->cur_context;
$this->id_stack[] = $this->id;
$this->id = $id;
$this->cur_context = $data;
$this->cur_context_xml = '';
if ($id) {
$this->data_sources[$id] = $data;
}
}
public function setContext($id, &$data) {
if (is_object($data)) {
$this->data_sources[$id] = $data;
}
else {
$this->data_sources[$id] =& $data;
}
}
public function pop() {
$this->id = array_pop($this->id_stack);
$this->cur_context = array_pop($this->data_stack);
$this->cur_context_xml = '';
}
public function contextExists($id) {
$exists = FALSE;
if (array_key_exists($id, $this->data_sources)) {
$exists = TRUE;
}
else {
$contexts = Frx::getContextPlugins();
if (isset($contexts[$id])) {
$plugin = $contexts[$id];
if (isset($plugin['file'])) {
include_once $plugin['file'];
}
$class = $contexts[$id]['class'];
if (class_exists($class)) {
$object = new $class();
$this
->setContext($id, $object);
$exists = TRUE;
}
}
}
return $exists;
}
public function getContext($id) {
return @$this->data_sources[$id];
}
public function dumpContext() {
drupal_set_message(filter_xss('cur_context ' . $this->id . '<br/>Stack<br/><pre>' . print_r($this->cur_context, 1) . '</pre>'));
}
public function group($data, $group = '') {
$rows = array();
if (is_array($group)) {
$group = implode(' ', $group);
}
$group = (string) $group;
if (is_array($data) || is_object($data)) {
foreach ($data as $row) {
Frx::Data()
->push($row, '_group');
$gval = $this->teng
->replace($group, TRUE);
Frx::Data()
->pop();
$rows[$gval][] = $row;
}
}
return $rows;
}
}