Commit c925cbb4 authored by metzlerd's avatar metzlerd
Browse files

Basic d7 port.

parent b373042b
<?php
require_once('forena.common.inc');
// $Id$
require_once('forena.common.inc');
/**
* @file
* Class that defines default methods for access control in an FrxDataProvider
......@@ -7,112 +8,112 @@ require_once('forena.common.inc');
*/
class FrxDataProvider{
public $conf;
public $block_path;
public $comment_prefix;
public $comment_suffix;
public $block_ext;
protected $te;
public function __construct($conf, $repos_path) {
public $conf;
public $block_path;
public $comment_prefix;
public $comment_suffix;
public $block_ext;
protected $te;
public function __construct($conf, $repos_path) {
$this->conf = $conf;
$this->comment_prefix = '--';
$this->comment_prefix = '--';
$this->block_ext = 'sql';
$this->block_path = $repos_path;
$this->block_path = $repos_path;
}
/**
* Implements the basic default security check of calling
* an access method.
* Implements the basic default security check of calling
* an access method.
*
* @param unknown_type $arg
* @return unknown
*/
public function access($arg) {
$f = $this->conf['access callback'];
$f = $this->conf['access callback'];
if ($f && is_callable($f)) {
return $f($arg);
}
return $f($arg);
}
elseif (isset($this->conf['access block'])) {
$block = $this->conf['access block'];
if (isset($this->conf['access path'])) $path = $this->conf['access path'];
return forena_block_access($block, $path, $arg);
$block = $this->conf['access block'];
if (isset($this->conf['access path'])) $path = $this->conf['access path'];
return forena_block_access($block, $path, $arg);
}
else {
return user_access('access content');
else {
return user_access('access content');
}
}
/**
* Default block load
* Loads the data block based on the block name from the file system. The classes that
* are derived from this will set the block_ext property, which in most cases is .sql but
* Loads the data block based on the block name from the file system. The classes that
* are derived from this will set the block_ext property, which in most cases is .sql but
* might be something different. The load of the block file should return data, access and
* probably parameters to.
* probably parameters to.
* @param unknown_type $block_name
* @return unknown
*/
public function load_block($block_name, $clause='') {
$filename = $this->block_path .'/'. $block_name .'.'. $this->block_ext;
public function load_block($block_name, $clause='') {
$filename = $this->block_path . '/' . $block_name . '.' . $this->block_ext;
$block = forena_load_block_file($filename, $this->comment_prefix, $this->comment_suffix);
// If we have a regular expression token parser, then get the tokens out of the block.
// If we have a regular expression token parser, then get the tokens out of the block.
if ($this->te) {
if ($this->te) {
$tokens = $this->te->tokens($block['source']);
//drupal_set_message("tokens: ". print_r($tokens, 1));
//check tokens in the where clause
if ($clause) {
$clause_tokens = $this->te->tokens($clause);
//drupal_set_message("clause tokens: ". print_r($clause_tokens, 1));
$temp = array();
$temp = array_merge($tokens, $clause_tokens);
//check for duplicates in block tokens
if ($clause_tokens) foreach ($clause_tokens as $ct) {
if (!$temp[$ct]) {
array_push($tokens,$ct);
}
}
}
$block['tokens'] = $tokens;
//check tokens in the where clause
if ($clause) {
$clause_tokens = $this->te->tokens($clause);
//drupal_set_message("clause tokens: ". print_r($clause_tokens, 1));
$temp = array();
$temp = array_merge($tokens, $clause_tokens);
//check for duplicates in block tokens
if ($clause_tokens) foreach ($clause_tokens as $ct) {
if (!$temp[$ct]) {
array_push($tokens, $ct);
}
}
}
$block['tokens'] = $tokens;
}
return $block;
return $block;
}
/**
* Find all the blocks matching a provided search string
*
* @param string $search part block names to search for
* @return unknown
*/
public function list_blocks($search, $subdir='', $this_list=array()) {
public function list_blocks($search, $subdir='', $this_list=array()) {
$block_list = $this_list;
// First find files that match the search string
$path = $this->block_path .'/';
if ($subdir) $path = $subdir .'/';
$block_path .= $path . '*'. $search . '*.' . $this->block_ext;
$path = $this->block_path . '/';
if ($subdir) $path = $subdir . '/';
$block_path .= $path . '*' . $search . '*.' . $this->block_ext;
// Find sql files
$d = glob($block_path);
$d = glob($block_path);
foreach ($d as $file_name) {
list($block_name, $ext) = explode('.', $file_name);
$block_list[] = str_replace($this->block_path .'/', '', $block_name);
}
list($block_name, $ext) = explode('.', $file_name);
$block_list[] = str_replace($this->block_path . '/', '', $block_name);
}
// Find directories
$d = glob($path .'*');
$d = glob($path . '*');
foreach ($d as $dir_name) {
if (is_dir($dir_name)) {
$block_list += $this->list_blocks($search, $dir_name, $block_list);
if (is_dir($dir_name)) {
$block_list += $this->list_blocks($search, $dir_name, $block_list);
}
}
return $block_list;
}
return $block_list;
}
......
<?php
// $Id$
/**
* @file
* Basic report provider. Controls the rendering of the report.
......@@ -37,7 +38,7 @@ class FrxReport {
$this->rpt_xml = $xhtml;
}
$this->cur_data = $data;
$this->teng->push_data($data,'parm');
$this->teng->push_data($data, 'parm');
// Load header data
$rpt_xml = $this->rpt_xml;
$this->body = $rpt_xml->body;
......@@ -85,11 +86,11 @@ class FrxReport {
* @param $block
* @return unknown_type
*/
private function get_data($block, $clause='',$id='') {
private function get_data($block, $clause='', $id='') {
//@TODO: Merge xml data parameters into the report paramters
$this->cur_data = forena_invoke_data_provider($block, $this->cur_data, $clause);
$this->teng->push_data($this->cur_data,$id);
$this->teng->push_data($this->cur_data, $id);
if ($this->cur_data) $this->blocks_loaded = TRUE;
}
......@@ -104,6 +105,7 @@ class FrxReport {
$continue = TRUE;
$is_data_block = FALSE;
$frx = $node->attributes(FRX_NS);
$o = '';
// Test to see if we have any nodes that are contains data url
if ($node->xpath('*//@frx:*') || $frx) {
$attrs = $node->attributes();
......@@ -119,7 +121,7 @@ class FrxReport {
$attr_text='';
$tmp_attrs = array();
if ($attrs) foreach ($attrs as $key => $value) {
$attr_text = ' '. $key .'="'. (string)$value .'"';
$attr_text = ' ' . $key . '="' . (string)$value . '"';
$tmp_attr[$key] = (string)$value;
}
......@@ -137,21 +139,21 @@ class FrxReport {
if ($nodes) foreach ($nodes as $x) {
$this->cur_data = $x;
$this->teng->push_data($x,$id);
$this->teng->push_data($x, $id);
$i++;
$odd = $i & 1;
$row_class = $odd ? 'odd' : 'even';
$tmp_attrs['class'] = trim($attrs['class'] .' '. $row_class);
$tmp_attrs['class'] = trim($attrs['class'] . ' ' . $row_class);
$r_attr_text = '';
foreach ($tmp_attrs as $key => $value) {
$r_attr_text .= ' '. $key .'="'. (string)$value .'"';
$r_attr_text .= ' ' . $key . '="' . (string)$value . '"';
}
$o .= $this->teng->replace('<'. $tag . $r_attr_text .'>', $cur_data);
$o .= $this->teng->replace('<' . $tag . $r_attr_text . '>', $cur_data);
foreach ($node->children() as $child) {
$o .= $this->render_section($child);
}
$o .= '</'. $tag .'>';
$o .= '</' . $tag . '>';
}
$this->cur_data = $data;
......@@ -159,11 +161,11 @@ class FrxReport {
}
elseif ($continue) {
$o .= $this->teng->replace('<'. $tag . $attr_text .'>');
$o .= $this->teng->replace('<' . $tag . $attr_text . '>');
foreach ($node->children() as $child) {
$o .= $this->render_section($child);
}
$o .= '</'. $tag .'>';
$o .= '</' . $tag . '>';
}
}
else {
......@@ -185,6 +187,7 @@ class FrxReport {
*/
public function render($format) {
$rpt_xml = $this->rpt_xml;
$o='';
if ($rpt_xml->body) $rpt_xml = $rpt_xml->body;
$body_xml = $rpt_xml;
......@@ -200,9 +203,14 @@ class FrxReport {
*/
public function format($value, $key, $data) {
// Determine if there is a field overide entry
$default='';
$link ='';
$format='';
$format_str='';
$target='';
if ($this->fields) {
$path = 'frx:field[@id="'. $key .'"]';
$path = 'frx:field[@id="' . $key . '"]';
$formatters = $this->fields->xpath($path);
if ($formatters) foreach ($formatters as $formatter) {
if (((string)$formatter['block'] == $this->block) || (!(string)$formatter['block'])) {
......@@ -230,7 +238,7 @@ class FrxReport {
if ($link) {
$link = $this->teng->replace($link, $data, TRUE);
list($url, $query) = explode('?', $link);
$value = l(htmlspecialchars_decode($value), $url, array('query' => $query, 'attributes'=> array('target' => $target)));
$value = l(htmlspecialchars_decode($value), $url, array('query' => $query, 'attributes' => array('target' => $target)));
}
return $value;
}
......@@ -241,7 +249,7 @@ class FrxReport {
* @return unknown_type
*/
public function deleteNode($id) {
$path = '//*[@id="'. $id .'"]';
$path = '//*[@id="' . $id . '"]';
$nodes = $this->rpt_xml->xpath($path);
$node = $nodes[0];
$dom=dom_import_simplexml($node);
......@@ -276,7 +284,7 @@ class FrxReport {
if ($attr_nodes) {
// Make sure every element has an id
$i++;
$id = 'forena-'. $i;
$id = 'forena-' . $i;
if (!(string)$node['id']) {
$node->addAttribute('id', $id);
......@@ -333,7 +341,7 @@ class FrxReport {
public function save_attributes_by_id($attributes) {
$rpt_xml = $this->rpt_xml;
if ($attributes) foreach ($attributes as $id => $att_list) {
$id_search_path = '//*[@id="'. $id . '"]';
$id_search_path = '//*[@id="' . $id . '"]';
$fnd = $rpt_xml->xpath($id_search_path);
if ($fnd) {
$node = $fnd[0];
......@@ -343,11 +351,11 @@ class FrxReport {
foreach ($att_list as $key => $value) {
if (!$frx_attributes[$key]) {
$node['frx:'. $key] = $value;
$node['frx:' . $key] = $value;
}
else {
unset($frx_attributes[$key]);
$node['frx:'. $key] = $value;
$node['frx:' . $key] = $value;
}
}
}
......@@ -360,18 +368,16 @@ class FrxReport {
* @param string $value Value to be saved.
* @return unknown_type
*/
public function set_value($xpath, $value) {
public function set_value($xpath, $value) {
$xml = $this->rpt_xml;
drupal_set_message($xpath);
$i = strrpos($xpath,'/');
$path = substr($xpath,0,$i);
$key = substr($xpath,$i+1);
drupal_set_message($path. '-' .$key);
$i = strrpos($xpath, '/');
$path = substr($xpath, 0, $i);
$key = substr($xpath, $i+1);
$nodes = $xml->xpath($path);
if ($nodes) {
// if the last part of the xpath is a key then assume the key
if (strpos($key,'@')===0) {
$key = trim($key,'@');
if (strpos($key, '@')===0) {
$key = trim($key, '@');
if (is_null($value)) {
unset($nodes[0][$key]);
}
......
<?php
// $Id$
/**
* @file
* FrXSytnaxEngine defines how regular expression procesing/token substitution takes place.
* It includes support for passing in a formatter oobject that will escape strings properly
* before substituting them.
* FrXSytnaxEngine defines how regular expression procesing/token substitution takes place.
* It includes support for passing in a formatter oobject that will escape strings properly
* before substituting them.
*
*/
define('FRX_TOKEN_EXP', '/\{[^\n^\r^}]+}/');
define('FRX_SQL_TOKEN', '/:([a-z]|[0-9]|{A-Z]|[-_])+/');
class FrxSyntaxEngine {
private $tpattern;
private $trim_chars;
define('FRX_TOKEN_EXP', '/\{[^\n^\r^}]+}/');
define('FRX_SQL_TOKEN', '/:([a-z]|[0-9]|{A-Z]|[-_])+/');
class FrxSyntaxEngine {
private $tpattern;
private $trim_chars;
private $formatter; // Object used to format the data
private $data_stack;
private $data_sources;
private $data_stack;
private $data_sources;
/**
* Class for doing syntax replacements;
* Class for doing syntax replacements;
* @param $regexp
* @return unknown_type
*/
public function __construct($regexp, $trim, $formatter=NULL) {
$this->tpattern = $regexp;
$this->trim_chars = $trim;
if (is_object($formatter)) {
$this->formatter=$formatter;
$this->tpattern = $regexp;
$this->trim_chars = $trim;
if (is_object($formatter)) {
$this->formatter=$formatter;
}
$this->data_stack = array();
$this->data_sources = array();
$this->data_stack = array();
$this->data_sources = array();
}
/**
* Push a data context onto the data stacks
* to make sure that we can address these using an
* appropriate syntax. I think we don't need data_stack
* Push a data context onto the data stacks
* to make sure that we can address these using an
* appropriate syntax. I think we don't need data_stack
* but i'm holding it there in case we develop a "relative" path syntax.
* @param $data
* @param $id
* @return unknown_type
*/
public function push_data($data, $id='') {
$this->data_stack[]=$data;
public function push_data($data, $id='') {
$this->data_stack[] = $data;
if ($id) {
$this->data_sources[$id] = $data;
}
if ($id) {
$this->data_sources[$id] = $data;
}
}
/**
* Remove data from the data stack. This will make data unavaiable
* when we leave the context of the current nested reports.
* Remove data from the data stack. This will make data unavaiable
* when we leave the context of the current nested reports.
* @param $id
* @return unknown_type
*/
public function pop_data($id='') {
array_pop($this->data_stack);
if ($id) {
unset($this->data_stack[$id]);
}
public function pop_data($id='') {
array_pop($this->data_stack);
if ($id) {
unset($this->data_stack[$id]);
}
}
/**
* Provides an api to the {=xpath} syntax that can be used
* to evaluat expressions such as sum and count in a report. We
* need to use the DOM object here, because this method isn't exposed
* with simplexml.
*
* Provides an api to the {=xpath} syntax that can be used
* to evaluat expressions such as sum and count in a report. We
* need to use the DOM object here, because this method isn't exposed
* with simplexml.
*
* @param $xml
* @param $path
* @return unknown_type
*/
protected function simplexml_evaluate($xml, $path) {
protected function simplexml_evaluate($xml, $path) {
$dom_node = dom_import_simplexml($xml);
$dom_doc = new DOMDocument('');
$dom_doc = new DOMDocument('');
$dom_node = $dom_doc->importNode($dom_node, TRUE);
$dom_doc->appendChild($dom_node);
// Do we also need to call AppendChild?
$xpath = new DOMXpath($dom_doc);
$dom_doc->appendChild($dom_node);
// Do we also need to call AppendChild?
$xpath = new DOMXpath($dom_doc);
$ret = $xpath->evaluate($path, $dom_node);
return $ret;
return $ret;
}
/**
* Get the value from the data.
* This is used by token_replace method to extract the data based on the path provided.
* Get the value from the data.
* This is used by token_replace method to extract the data based on the path provided.
* @param $data
* @param $key
* @return unknown_type
*/
protected function get_value( $key, $raw=FALSE) {
$retvar = '';
$retvar = '';
// Determne which $data var we're going to get
if ($this->data_stack) {
$i = count($this->data_stack) - 1;
$data = $this->data_stack[$i];
if ($this->data_stack) {
$i = count($this->data_stack) - 1;
$data = $this->data_stack[$i];
}
// Determine if we have a . syntax for the id.
if ($key) {
list($id,$path) = explode('.',$key,2);
if ($data && $this->data_sources[$id]) {
$data = $this->data_sources[$id];
$key = $path;
// Determine if we have a . syntax for the id.
if ($key) {
@list($id, $path) = explode('.', $key, 2);
if ($data && isset($this->data_sources[$id])) {
$data = $this->data_sources[$id];
$key = $path;
}
}
if (is_array($data)) {
$retvar = $data[$key];
}
}
elseif (is_object($data)) {
if (strpos($key,'=')===0) {
$retvar = $this->simplexml_evaluate($data,ltrim($key,'='));
if (strpos($key, '=')===0) {
$retvar = $this->simplexml_evaluate($data, ltrim($key, '='));
}
else {
else {
$rows = $data->xpath($key);
if ($rows) $x = $rows[0];
if ($x) $retvar = $x->asXML();
if ($x) $retvar = $x->asXML();
// Check to see if there are child nodes
// If so use asXML otherwise string cast.
// If so use asXML otherwise string cast.
if ($retvar && strpos($retvar, '<')!==FALSE) {
// Find the end of the first tag.
$p = strpos($retvar, '>');
$p = strpos($retvar, '>');
$retvar = substr_replace($retvar, '', 0, $p+1);
$p = strrpos($retvar, '<', -1);
$p = strrpos($retvar, '<', -1);
$retvar = substr_replace($retvar, '', $p, strlen($retvar) - $p);
}
else {
$retvar = (string)$x;
else {
$retvar = (string)$x;
}
}
}
// Call the formatter object if neccessary
$f = $this->formatter;
$f = $this->formatter;
if (!$raw && is_object($f) && method_exists($f, 'format')) {
$retvar = $f->format($retvar, $key, $data);
}
$retvar = trim($retvar);
return $retvar;
}
$retvar = trim($retvar);
return $retvar;
}
/**
*
*
* @param $text text that needs replacing
* @param $data
* @return unknown_type
......@@ -156,41 +157,41 @@ class FrxSyntaxEngine {
public function replace($text, $data='', $raw=FALSE) {
$match=array();
$o_text = $text;
// Put the data on the stack.
if ($data) $this->push_data($data);
// Put the data on the stack.
if ($data) $this->push_data($data);
if (preg_match_all($this->tpattern, $o_text, $match)) {
//list($params) = $match[1];
$i=0;
$i=0;
foreach ($match[0] as $match_num => $token) {