Commit 399fb3df authored by metzlerd's avatar metzlerd
Browse files

Updated from head.

parent 802406b0
<?php
// $Id$
/**
* @file
* Class that defines default methods for access control in an FrxDataEngine
*
*/
class FrxDataEngine{
public $conf;
......@@ -18,7 +24,8 @@ class FrxDataEngine{
$f = $this->conf['access callback'];
if ($f && is_callable($f)) {
return $f($arg);
} else {
}
else {
return user_access('access content');
}
}
......
<?php
// $Id$
/**
* @file
* Basic report engine. Controls the rendering of the report.
*/
require_once('FrxSyntaxEngine.inc');
class FrxReport {
private $rpt_xml;
private $cur_data;
private $output;
private $teng;
public $fields;
public $categories;
public $access;
public function __construct($xhtml, $data=array()) {
$this->teng = new FrxSyntaxEngine(FRX_TOKEN_EXP,'{}',$this);
if (!is_object($xhtml)) {
$this->rpt_xml = new SimpleXMLElement($xhtml);
} else {
$this->rpt_xml = $xhtml;
}
$this->cur_data = $data;
// Load header data
$rpt_xml = $this->rpt_xml;
if ($rpt_xml->head) {
$this->title = (string)$rpt_xml->head->title;
foreach($rpt_xml->head->children('urn:FrxReports') as $name=>$node) {
private $rpt_xml;
private $cur_data;
private $output;
private $teng;
public $fields;
public $categories;
public $access;
public function __construct($xhtml, $data=array()) {
$this->teng = new FrxSyntaxEngine(FRX_TOKEN_EXP, '{}', $this);
if (!is_object($xhtml)) {
$this->rpt_xml = new SimpleXMLElement($xhtml);
}
else {
$this->rpt_xml = $xhtml;
}
$this->cur_data = $data;
// Load header data
$rpt_xml = $this->rpt_xml;
if ($rpt_xml->head) {
$this->title = (string)$rpt_xml->head->title;
foreach ($rpt_xml->head->children('urn:FrxReports') as $name => $node) {
switch ($name) {
case 'fields':
$this->fields = $node;
......@@ -39,148 +44,146 @@ class FrxReport {
}
}
}
}
/**
* Get the data block
* @param $block
* @return unknown_type
*/
private function get_data($block) {
//@TODO: Merge xml data parameters into the report paramters
$this->cur_data = forena_invoke_data_engine($block, $this->cur_data);
}
private function process_frx_attributes(SimpleXMLElement $node) {
$attrs = $node->attributes('urn:FrxReports');
if ($attrs) foreach ($attrs as $key => $value) {
switch ($key) {
case "block":
$this->get_data((string)$value);
break;
}
}
}
}
/**
* Get the data block
* @param $block
* @return unknown_type
*/
private function get_data($block) {
//@TODO: Merge xml data parameters into the report paramters
$this->cur_data = forena_invoke_data_engine($block, $this->cur_data);
}
private function process_frx_attributes(SimpleXMLElement $node) {
$attrs = $node->attributes('urn:FrxReports');
if ($attrs) foreach ($attrs as $key => $value) {
switch ($key) {
case "block":
$this->get_data((string)$value);
break;
}
}
}
/**
* Recursive report renderer
* Walks the nodes renering the report.
*/
public function render_section(SimpleXMLElement $node) {
public function render_section(SimpleXMLElement $node) {
$elements = count($node->xpath('*'));
$frx = $node->attributes('urn:FrxReports');
// Test to see if we have any nodes that are contains data url
if ($node->xpath('*//@frx:*') || $frx) {
if ($node->xpath('*//@frx:*') || $frx) {
$attrs = $node->attributes();
$tag = $node->getName();
$this->process_frx_attributes($node);
if ($attrs) foreach ($attrs as $key => $value) {
$attr_text = ' '. $key .'="'. (string)$value .'"';
}
$attrs = $node->attributes();
$tag = $node->getName();
$this->process_frx_attributes($node);
if ($attrs) foreach ($attrs as $key => $value) {
$attr_text = ' ' .$key. '="'. (string)$value .'"';
}
$frx = $node->attributes('urn:FrxReports');
$frx = $node->attributes('urn:FrxReports');
if ((string)$frx['foreach'] ){
// Save xml
$path = (string)$frx['foreach'];
$data = $this->cur_data;
if($data) $nodes = $data->xpath($path);
if ($nodes) foreach ($nodes as $x) {
$this->cur_data = $x;
$o .= $this->teng->replace('<'. $tag. $attr_text . '>',$this->cur_data);
foreach($node->children() as $child) {
$o .= $this->render_section($child);
}
$o .= '</'. $tag .'>';
}
$this->cur_data = $data;
} else {
$o .= $this->teng->replace('<'. $tag. $attr_text . '>',$this->cur_data);
foreach ($node->children() as $child) {
$o.= $this->render_section($child);
}
$o .= '</'. $tag .'>';
}
} else {
if ((string)$frx['foreach'] ) {
// Save xml
$path = (string)$frx['foreach'];
$data = $this->cur_data;
if ($data) $nodes = $data->xpath($path);
if ($nodes) foreach ($nodes as $x) {
$this->cur_data = $x;
$o .= $this->teng->replace('<'. $tag . $attr_text .'>', $this->cur_data);
foreach ($node->children() as $child) {
$o .= $this->render_section($child);
}
$o .= '</'. $tag .'>';
}
$this->cur_data = $data;
}
else {
$o .= $this->teng->replace('<'. $tag . $attr_text .'>', $this->cur_data);
foreach ($node->children() as $child) {
$o .= $this->render_section($child);
}
$o .= '</'. $tag .'>';
}
}
else {
$tag = $node->getName();
// We can render so lets do it.
$text = $node->asXML(); ;
$o.=$this->teng->replace($text,$this->cur_data);
}
return $o;
}
/**
* Render the report
* @return unknown_type
*/
public function render($data= array()) {
$rpt_xml = $this->rpt_xml;
// We can render so lets do it.
$text = $node->asXML();
$o .= $this->teng->replace($text, $this->cur_data);
}
return $o;
}
/**
* Render the report
* @return unknown_type
*/
public function render($format) {
$rpt_xml = $this->rpt_xml;
drupal_set_title($this->title);
if ($rpt_xml->body) $rpt_xml = $rpt_xml->body;
$body_xml = $rpt_xml;
foreach ($body_xml->children() as $node) {
$o .= $this->render_section($node);
}
return $o ;
}
/*
* Formatter used by the syntax engine to alter data that gets extracted.
* This invokes the field translation engine
*/
public function format($value, $key, $data) {
// Determine if there is a field overide entry
if ($this->fields) {
$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'])) {
//@TODO: Replace the default extraction with something that will get sub elements of the string
$default = (string)$formatter;
$link = (string) $formatter['link'];
$format = (string) $formatter['format'];
$format_str = (string) $formatter['format-string'];
}
}
}
if ($format) {
forena_define_plugins('formatter',$format);
if (class_exists($format)) {
$f = new $format();
if (method_exists(f,'format')) {
$f->format($value, $format_str);
}
}
}
// Default if specified
if (!$value && $default) {
$value = $default;
}
if ($link) {
$link = $this->teng->replace($link, $data, TRUE);
list($url,$query) = explode('?',$link);
$value = l(htmlspecialchars_decode($value),$url, array('query' => $query));
}
return $value;
}
if ($rpt_xml->body) $rpt_xml = $rpt_xml->body;
$body_xml = $rpt_xml;
foreach ($body_xml->children() as $node) {
$o .= $this->render_section($node);
}
return $o ;
}
/*
* Formatter used by the syntax engine to alter data that gets extracted.
* This invokes the field translation engine
*/
public function format($value, $key, $data) {
// Determine if there is a field overide entry
if ($this->fields) {
$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'])) {
//@TODO: Replace the default extraction with something that will get sub elements of the string
$default = (string)$formatter;
$link = (string) $formatter['link'];
$format = (string) $formatter['format'];
$format_str = (string) $formatter['format-string'];
}
}
}
if ($format) {
forena_define_plugins('formatter', $format);
if (class_exists($format)) {
$f = new $format();
if (method_exists(f, 'format')) {
$f->format($value, $format_str);
}
}
}
// Default if specified
if (!$value && $default) {
$value = $default;
}
if ($link) {
$link = $this->teng->replace($link, $data, TRUE);
list($url, $query) = explode('?', $link);
$value = l(htmlspecialchars_decode($value), $url, array('query' => $query));
}
return $value;
}
}
\ No newline at end of file
<?php
define('FRX_TOKEN_EXP','/\{[^}]+}/');
define('FRX_SQL_TOKEN','/:([a-z]|[0-9]|{A-Z]|[-_])+/');
// $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.
*
*/
define('FRX_TOKEN_EXP', '/\{[^}]+}/');
define('FRX_SQL_TOKEN', '/:([a-z]|[0-9]|{A-Z]|[-_])+/');
class FrxSyntaxEngine {
private $tpattern;
private $trim_chars;
......@@ -18,39 +26,38 @@ class FrxSyntaxEngine {
}
}
/**
* 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($data, $key, $raw=FALSE)
{
$retvar = '';
if (is_array($data))
{
$retvar = $data[$key];
} elseif (is_object($data)) {
$rows = $data->xpath($key);
if ($rows) $x = $rows[0];
if ($x) $retvar = $x->asXML();
if ($retvar) {
// Find the end of the first tag.
//print $root_tag;
$p = strpos($retvar,'>');
$retvar = substr_replace($retvar,' ',0,$p+1);
$p = strrpos($retvar,'<',-1);
$retvar = substr_replace($retvar,'',$p,strlen($retvar) - $p);
}
}
// Call the formatter object if neccessary
$f = $this->formatter;
if (!$raw && is_object($f) && method_exists($f,'format')) $retvar = $f->format($retvar,$key,$data);
$retvar = trim($retvar);
return $retvar;
}
/**
* 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($data, $key, $raw=FALSE) {
$retvar = '';
if (is_array($data)) {
$retvar = $data[$key];
}
elseif (is_object($data)) {
$rows = $data->xpath($key);
if ($rows) $x = $rows[0];
if ($x) $retvar = $x->asXML();
if ($retvar) {
// Find the end of the first tag.
//print $root_tag;
$p = strpos($retvar, '>');
$retvar = substr_replace($retvar, ' ', 0, $p+1);
$p = strrpos($retvar, '<', -1);
$retvar = substr_replace($retvar, '', $p, strlen($retvar) - $p);
}
}
// Call the formatter object if neccessary
$f = $this->formatter;
if (!$raw && is_object($f) && method_exists($f, 'format')) $retvar = $f->format($retvar, $key, $data);
$retvar = trim($retvar);
return $retvar;
}
/**
......@@ -60,23 +67,18 @@ class FrxSyntaxEngine {
* @return unknown_type
*/
public function replace($text, $data, $raw=FALSE) {
$match=array();
$o_text = $text;
if (preg_match_all($this->tpattern,$o_text,$match))
{
//list($params) = $match[1];
$i=0;
foreach($match[0] as $match_num=>$token)
{
$path = trim($token,$this->trim_chars);
$value = $this->get_value($data, $path, $raw);
$text = str_replace($token, $value, $text);
}
$match=array();
$o_text = $text;
if (preg_match_all($this->tpattern, $o_text, $match)) {
//list($params) = $match[1];
$i=0;
foreach ($match[0] as $match_num => $token) {
$path = trim($token, $this->trim_chars);
$value = $this->get_value($data, $path, $raw);
$text = str_replace($token, $value, $text);
}
return $text;
}
return $text;
}
}
<?php
// $Id$
/**
* @file
* Report administration forms and functions.
*/
require_once('forena.common.inc');
/**
* Save the report file to disk
......@@ -9,7 +14,7 @@ require_once('forena.common.inc');
function forena_save_report($name, $data) {
$report_path = forena_report_path();
//@TODO: Clean up filename to make sure
$filepath = $report_path.'/'.$name. 'frx';
$filepath = $report_path .'/'. $name .'frx';
if (is_object($data)) {
$data = $data->asXML();
}
......@@ -28,7 +33,7 @@ function forena_save_report($name, $data) {
*/
function forena_settings() {
$report_path = forena_report_path();
$path = variable_get('forena_path','reports');
$path = variable_get('forena_path', 'reports');
$form['forena_path'] = array(
'#type' => 'textfield',
......@@ -69,12 +74,12 @@ function forena_settings() {
function forena_settings_submit($form, &$form_state) {
$values = $form_state['values'];
$path = $values['forena_report_repos'];
$src_dir = drupal_get_path('module','forena') . '/repos/reports';
$src_dir = drupal_get_path('module', 'forena') . '/repos/reports';
if (!file_exists($path)) {
try {
if (file_exists($path)) {
drupal_set_message (t('Created directory %s', array($path))) ;
drupal_set_message(t('Created directory %s', array($path))) ;
}
mkdir($path);
......@@ -82,23 +87,24 @@ function forena_settings_submit($form, &$form_state) {
forena_error('Unable to create report directory', $e->getMessage());
}
} elseif ($path != $src_dir) {
}
elseif ($path != $src_dir) {
// Copy the reports from the
$d = dir($src_dir);
$dest_dir = $d->path;
$i=0;
while (false !== ($rpt_file = $d->read())) {
echo $entry."\n";
echo $entry ."\n";
$src_file = $d->path .'/'. $rpt_file;
$dest_file = $path .'/'. $rpt_file;
if (is_file($src_file)) {
file_put_contents($dest_file,file_get_contents($src_file));
file_put_contents($dest_file, file_get_contents($src_file));
$i++;
}
}
$d->close();
drupal_set_message($i .' delivered reports copied from '. $src_dir. ' to '. $path);
drupal_set_message($i .' delivered reports copied from '. $src_dir .' to '. $path);
}
menu_cache_clear();
}
<?php
// $Id$
/**
* @file
* Common functions used throughout the project but loaded in this
* file to keep the module file lean.
*/
// Include Report renderer.
require_once('FrxReport.inc');
require_once('FrxDataEngine.inc');
......@@ -7,9 +13,9 @@ require_once('FrxDataEngine.inc');
function __forena_load_repository(&$repo) {
// First determine if the class file exisits
$path = $repo['path'];
if (file_exists($path.'/settings.php')) {
if (file_exists($path .'/settings.php')) {
// This should at minimum set
include($path. '/settings.php');
include($path .'/settings.php');
}
......@@ -27,11 +33,8 @@ function __forena_load_repository(&$repo) {
function __forena_load_engine($conf, $repo_path) {
$name = $conf['data_engine'];
$path = drupal_get_path( 'module', 'forena'). '/plugins/' . $name;
// Make sure that we don't override predefined classes
if (!class_exists($name) && file_exists($path. '.inc')) {
include_once($path.'.inc');
}
forena_define_plugins('data');
// Instantiate the path
if (class_exists($name)) {
$o = new $name($conf, $repo_path);
return $o;
......@@ -43,26 +46,26 @@ function __forena_load_engine($conf, $repo_path) {
* If no repository is specified the descriptors for all repositories are returned.
*/
function forena_repository($name='') {
global $forena_repositories;
global $_forena_repositories;
static $repos ='';
// Empty repository so we need to initialize
if (!$repos) {
// Build the default sample one
$path = drupal_get_path('module','forena');
$path = drupal_get_path('module', 'forena');
$repos = array();
// Load the repository list from the global settings.php file.
if ($forena_repositories) {
$repos = $forena_repositories;
if ($_forena_repositories) {
$repos = $_forena_repositories;
}
// Overide difinitions of the sample and drupal repositories.
$repos['sample'] = array('path' => $path.'/repos/sample',
$repos['sample'] = array('path' => $path .'/repos/sample',
'title' => 'Forena Sample Reports');
$repos['drupal'] = array('path' => $path.'/repos/drupal',
$repos['drupal'] = array('path' => $path .'/repos/drupal',
'title' => 'Drupal Reports');