Commit 78be96ec authored by metzlerd's avatar metzlerd
Browse files

Working version with permissions and categories.

parent 5e4ff972
......@@ -8,9 +8,16 @@
class FrxDataEngine{
public $conf;
public $block_path;
public $comment_prefix;
public $comment_suffix;
public $block_ext;
public function __construct($conf, $repos_path) {
$this->conf = $conf;
$this->conf = $conf;
$this->comment_prefix = '--';
$this->block_ext = 'sql';
$this->block_path = $repos_path;
}
/**
......@@ -30,4 +37,10 @@ class FrxDataEngine{
}
}
public function load_block($block_name) {
$filename = $this->block_path .'/'. $block_name .'.'. $this->block_ext;
$block = forena_load_block_file($filename, $this->comment_prefix, $this->comment_suffix);
return $block;
}
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ class FrxReport {
private $output;
private $teng;
public $fields;
public $categories;
public $category;
public $access;
public function __construct($xhtml, $data=array()) {
......@@ -37,11 +37,8 @@ class FrxReport {
$this->fields = $node;
break;
case 'category':
$this->categories[] = (string)$node;
$this->category = (string)$node;
break;
case 'access':
$this->access[] = (string)$node;
break;
}
}
}
......
......@@ -13,35 +13,61 @@ require_once('forena.common.inc');
*/
function forena_save_report($data, $report, $save_file = FALSE) {
static $save_count=0;
if ($report && !is_object($report)) $report = new SimpleXMLElement($report);
$report_path = forena_report_path();
//@TODO: Clean up filename to make sure
$name = $data['name'];
$filepath = $report_path .'/'. $data['name'] .'.frx';
$data['enabled'] = $data['enabled'] ? 1 : 0;
// If we need to save this to the file system
if ($save_file) {
// Serialize the report for saving
if (is_object($data)) {
$data = $data->asXML();
if (is_object($report)) {
$r_xml = $report->asXML();
}
try {
file_put_contents($filepath, $data);
file_put_contents($filepath, $r_xml);
} catch (Exception $e) {
fornea_error('Error Saving Report', $e->getMessage());
}
}
// Get the security caches from the reports
if ($report) $cache = forena_load_cache($report); else $cache='';
if ($cache) $rpt_cache = serialize($cache);
// Set default interpretations of data
$data['enabled'] = $data['enabled'] ? 1 : 0;
if (!$data['category']) $data['category'] = 'All';
$data['hidden'] = ($data['hidden'] && $data['hidden']!='N' && $data['hidden']!='0') ? 1:0;
// Save to the Database
if (file_exists($filepath)) {
$result = db_query("SELECT report_name FROM {forena_reports} WHERE report_name='%s'", $name);
if ($rpt = db_fetch_object($result)) {
db_query("UPDATE {forena_reports} SET title='%s' WHERE report_name='%s'", array($data['title'], $name));
db_query("UPDATE {forena_reports} SET title='%s', category='%s', repository='%s', access='%s'".
", hidden='%s', cache='%s' WHERE report_name='%s'",
array($data['title'],
$data['category'],
$data['repository'],
$data['access'],
$data['hidden'],
$rpt_cache,
$name ));
}
else {
db_query("INSERT INTO {forena_reports} (report_name,title) VALUES ('%s','%s')", array($name, $data['title']));
db_query("INSERT INTO {forena_reports} (report_name, title, category, repository, access, hidden, cache) ".
"VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s')",
array($name,
$data['title'],
$data['category'],
$data['repository'],
$data['access'],
$data['hidden'],
$rpt_cache,
));
}
$save_count++;
......@@ -68,10 +94,14 @@ function forena_db_sync($subdir='') {
list($report_name, $ext) = explode('.', $rpt_file, 2);
if ($ext == 'frx') {
$report_name = trim($prefix .'/'. $report_name, '/');
$r_xml = file_get_contents($src_file);
$r_xml = new SimpleXMLElement(file_get_contents($src_file));
// Load the report
$r = new FrxReport($r_xml);
$data['title'] = $r->title;
$data['category'] = $r->category;
$data['repository'] = $r->repository;
$data['access'] = $r->access;
$data['name'] = $report_name;
$save_count = forena_save_report($data, $r_xml);
}
......
......@@ -84,7 +84,50 @@ function forena_repository($name='') {
}
function forena_load_cache($r_xhtml) {
$blocks = array();
if (is_object($r_xhtml)) {
$block_xml = $r_xhtml->xpath('//*[@frx:block]');
// Extract all the blocks and organize by provider
foreach ($block_xml as $key => $block_node) {
$attrs = $block_node->attributes('urn:FrxReports');
foreach ($attrs as $key => $value) {
list($provider, $block) = explode('/', $value, 2);
$repos[$provider][] = $block;
}
}
foreach ($repos as $provider => $blocks) {
$repos = forena_repository($provider);
$engine = $repos['data'];
$conf = $engine->conf;
$access = array();
foreach ($blocks as $block_name) {
if ($engine && $block_name) {
if (method_exists($engine, 'load_block')) {
$conf = $engine->conf;
$block = $engine->load_block($block_name);
if (array_search($block['access'], $access)===FALSE) $access[]=$block['access'];
}
}
else {
//drupal_set_message('no engine found', 'error');
}
}
$cache['access'][$conf['access callback']]=$access;
}
}
return $cache;
}
/**
* Extract the data by running a block
*
* @param unknown_type $data_block
* @param unknown_type $parameters
* @param unknown_type $subquery
* @return unknown
*/
function forena_invoke_data_engine($data_block, $parameters=array(), $subquery='') {
list($provider, $block) = explode('/', $data_block, 2);
// Get the data
......@@ -107,7 +150,7 @@ function forena_invoke_data_engine($data_block, $parameters=array(), $subquery='
* @param unknown_type $filepath
* @param unknown_type $comment
*/
function forena_load_block_file($filepath, $comment='--') {
function forena_load_block_file($filepath, $comment='--', $trim) {
$block_data = file_get_contents($filepath);
$lines = explode("\n", $block_data);
$cnt = count($lines);
......@@ -117,6 +160,7 @@ function forena_load_block_file($filepath, $comment='--') {
while ($i<$cnt) {
$l = trim($lines[$i], "\r");
list($d, $c) = explode($comment, $l, 2);
if ($trim) $c = trim($d, $trim);
if ($c) {
list($a, $o) = explode('=', $c, 2);
if (trim($a)=='ACCESS') {
......
......@@ -19,6 +19,7 @@ function forena_schema() {
'repository' => array('type' => 'varchar', 'length' => 255, 'NOT NULL' => FALSE),
'access' => array('type' => 'varchar', 'length' => 255, 'NOT NULL' => FALSE),
'hidden' => array('type' => 'int', 'NOT NULL' => FALSE, ),
'cache' => array('type' => 'text'),
),
'primary key' => array('report_name'),
'indexes' => array(
......@@ -29,7 +30,7 @@ function forena_schema() {
}
/**
* Basic install and uninstall
* Basic install and uninstall hooks.
*
*/
function forena_install() {
......
......@@ -6,13 +6,6 @@
*/
function forena_menu() {
$items = array();
$items['ft'] = array(
'page callback' => 'forena_test',
'page arguments' => array(),
'title' => t('Forena Testing'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['admin/settings/forena'] = array(
'page callback' => 'drupal_get_form',
......@@ -30,8 +23,17 @@ function forena_menu() {
'page arguments' => array(),
'title' => t('Reports'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['forena'] = array(
'page callback' => 'forena_user_reports',
'page arguments' => array(),
'title' => t('My Reports'),
'access arguments' => array('list reports'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
......@@ -43,10 +45,11 @@ function forena_menu() {
*/
function forena_perm() {
$perms = array(
'administer forena reports',
'list reports',
'create any report',
'design any report',
'administer forena reports'
);
);
//@TODO: Add the ability to create subrepositories with different permissions.
return $perms;
}
......@@ -92,15 +95,53 @@ function forena_report() {
drupal_not_found();
}
}
else {
// @TODO: List reports
$result = db_query('SELECT * FROM {forena_reports} ORDER BY title asc');
else {
drupal_not_found();
}
return $output;
}
function forena_user_reports() {
$result = db_query('SELECT * FROM {forena_reports} ORDER BY category,title asc');
$categories = array();
while ($row = db_fetch_object($result)) {
$cache = $row->cache;
if ($cache) {
$cache = unserialize($cache);
$access = TRUE;
// Check each callback function to see if we have an error.
foreach ($cache['access'] as $callback => $args) {
if ($callback && $args) foreach ($args as $arg) {
if (function_exists($callback)) {
$a = $callback($arg);
}
if (!$a) $access = FALSE;
}
else {
$access = FALSE;
}
}
}
if ($access) {
$categories[$row->category][] = array(
'title' => $row->title,
'report_name' => $row->report_name,
);
}
}
$report_repos = variable_get('forena_path', 'reports');
foreach ($categories as $category => $reports) {
$output .= '<h3>'. $category .'</h3>';
$output .= '<ul>';
while ($row = db_fetch_object($result)) {
$output .= '<li>'. l($row->title, variable_get('forena_path', 'reports') .'/'. $row->report_name) .'</li>';
foreach ($reports as $r) {
$output .= '<li>'. l($r['title'], $report_repos .'/'. $r['report_name']) .'</li>';
}
$output .= '</ul>';
}
$output .= '</ul>';
return $output;
}
......
......@@ -12,7 +12,6 @@ class frxDrupal extends FrxDataEngine {
*/
private $te;
private $db;
private $block_path;
/**
* Object constructor
......@@ -24,7 +23,6 @@ class frxDrupal extends FrxDataEngine {
parent::__construct($conf, $repos_path);
// Set up the stuff required to translate.
$this->block_path = $repos_path;
$this->te = new FrxSyntaxEngine(FRX_SQL_TOKEN, ':', $this);
}
/**
......@@ -37,8 +35,7 @@ class frxDrupal extends FrxDataEngine {
public function data($block_name, $params=array(), $subQuery='') {
// Load the block from the file
$filename = $this->block_path .'/'. $block_name . '.sql';
$block = forena_load_block_file($filename);
$block = $this->load_block($block_name);
$xml ='';
if ($block['source'] && $this->access($block['access'])) {
$sql = $block['source'];
......@@ -74,4 +71,11 @@ class frxDrupal extends FrxDataEngine {
return $value;
}
/**
* @section
* Formatters
* Below here are formatting types
*/
}
\ No newline at end of file
......@@ -12,6 +12,8 @@ class FrxFileEngine extends FrxDataEngine {
private $path;
public function __construct($conf, $repos_path) {
parent::__construct($conf, $repos_path);
$this->comment_prefix = '<!--';
$this->comment_suffix = '-->';
list($protocol, $path) = explode('://', $conf['uri'], 2);
if (!$path) $path = $protocol;
$this->path = $path;
......
......@@ -11,7 +11,6 @@ class FrxOracle extends FrxDataEngine {
private $te;
private $db;
private $block_path;
/**
* Object constructor
......@@ -36,7 +35,6 @@ class FrxOracle extends FrxDataEngine {
}
// Set up the stuff required to translate.
$this->block_path = $repos_path;
$this->te = new FrxSyntaxEngine(FRX_SQL_TOKEN, ':', $this);
}
/**
......@@ -49,8 +47,7 @@ class FrxOracle extends FrxDataEngine {
public function data($block_name, $params=array(), $subQuery='') {
// Load the block from the file
$db = $this->db;
$filename = $this->block_path .'/'. $block_name .'.sql';
$block = forena_load_block_file($filename);
$block = $this->load_block($block_name);
$xml ='';
if ($block['source'] && $this->access($block['access']) && $db) {
$sql = $block['source'];
......
......@@ -10,7 +10,7 @@ class FrxPDO extends FrxDataEngine {
private $te;
private $db;
private $block_path;
/**
* Object constructor
......@@ -34,7 +34,6 @@ class FrxPDO extends FrxDataEngine {
}
// Set up the stuff required to translate.
$this->block_path = $repos_path;
$this->te = new FrxSyntaxEngine(FRX_SQL_TOKEN, ':', $this);
}
/**
......@@ -46,9 +45,8 @@ class FrxPDO extends FrxDataEngine {
*/
public function data($block_name, $params=array(), $subQuery='') {
// Load the block from the file
$db = $this->db;
$filename = $this->block_path .'/'. $block_name . '.sql';
$block = forena_load_block_file($filename);
$db = $this->db;
$block = $this->load_block($block_name);
$xml ='';
if ($block['source'] && $this->access($block['access']) && $db) {
$sql = $block['source'];
......
<html xmlns:frx="urn:FrxReports">
<head>
<title>User logs</title>
<frx:category>Drupal Administration</frx:category>
<frx:fields>
<frx:field id="details" link="admin/reports/event/{wid}">Details</frx:field>
<frx:field id="total" link="reports/DrupalUserLogs?name={name}"></frx:field>
......
<?xml version="1.0" encoding="UTF-8"?>
<!--ACCESS=access content -->
<root>
<row>
<last_name>Metzler</last_name>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment