Commit de308cdb authored by metzlerd's avatar metzlerd
Browse files

UI and help for updating cache settings.

parent f8a01916
......@@ -378,6 +378,23 @@ class FrxEditor {
}
/*
* Retrieve menu data for the report
*/
public function getCache() {
$dom = $this->dom;
$this->verifyHeaderElements(array('cache'));
$opts = $dom->getElementsByTagNameNS($this->xmlns, 'cache')->item(0);
$ret = array();
// Simplexml is easier to work with
$options = simplexml_import_dom($opts);
foreach ($options->attributes() as $key => $value) {
$ret[(string)$key] = (string)$value;
}
return $ret;
}
/*
* Set menu data for the report
* @param $menu_data array of key values for menu options.
......@@ -398,6 +415,27 @@ class FrxEditor {
}
}
/*
* Set Cache data for the report
* @param $cache_data array of key values for menu options.
*/
public function setCache($data) {
$dom = $this->dom;
$this->verifyHeaderElements(array('cache'));
$options = $dom->getElementsByTagNameNS($this->xmlns, 'cache')->item(0);
foreach ($data as $key => $value) {
if ($value) {
$options->setAttribute($key, $value);
}
else {
if ($options->hasAttribute($key)) {
$options->removeAttribute($key);
}
}
}
}
/*
* Set CSS Style Data
* @param $menu_data array of key values for menu options.
......
......@@ -13,6 +13,7 @@ class FrxReport {
public $rpt_xml;
public $fields;
public $category;
public $cache;
public $descriptor;
public $form;
public $access;
......@@ -73,7 +74,6 @@ class FrxReport {
case 'category':
$this->category = (string)$node;
break;
case 'descriptor':
$this->descriptor = (string)$node;
break;
......@@ -82,6 +82,10 @@ class FrxReport {
$this->options[$key] = (string)$value;
}
break;
case 'cache':
foreach ($node->attributes() as $key => $value) {
$this->cache[$key] = (string)$value;
}
case 'title':
$this->frx_title = (string)$node;
break;
......@@ -304,7 +308,7 @@ class FrxReport {
* Render the report
* @return unknown_type
*/
public function render($format, $render_form=TRUE) {
public function render($format, $render_form=TRUE, $cache_data=array()) {
if (!$format) $format = 'web';
// Only push the parameter conte
Frx::Data()->push($this->parms, 'parm');
......@@ -314,9 +318,17 @@ class FrxReport {
$body = $dom->getElementsByTagName('body')->item(0);
$o='';
if (!$this->missing_parms || $this->rpt_xml->xpath('//*[@frx:renderer="FrxParameterForm"]')) foreach ($body->childNodes as $node) {
$o .= $this->render_section($node);
if (!$this->missing_parms || $this->rpt_xml->xpath('//*[@frx:renderer="FrxParameterForm"]')) {
if ($cache_data && $cache_data['content']) {
$o .= $cache_data['content'];
}
else {
foreach ($body->childNodes as $node) {
$o .= $this->render_section($node);
}
}
}
$this->html = $o;
// Default in dynamic title from head.
if ($this->frx_title) {
......@@ -327,9 +339,11 @@ class FrxReport {
if ($render_form && !$this->rpt_xml->xpath('//*[@frx:renderer="FrxParameterForm"]') && $format=='web') {
$variables = array('collapse' => $this->blocks_loaded);
$this->parameters_form = $form = $this->parametersForm($variables);
//$o = drupal_render($form) . $o;
}
Frx::Data()->pop();
if ($this->cache) {
$this->cache['content'] = $this->html;
}
return $this->html;
}
......@@ -688,6 +702,7 @@ class FrxReport {
return drupal_get_form('forena_parameter_form', $parms, $variables);
}
}
......@@ -237,8 +237,37 @@ class FrxReportGenerator {
$r->processParameters();
Frx::Skin()->load($r->skin);
Frx::Skin()->loadSkinFiles($name);
$cached_data = FALSE;
$cache = array();
// Check for cache data
if ($r->cache) {
$cid = 'forena:report:' . $name . ':' . drupal_http_build_query($parms);
$cache = cache_get($cid, 'cache');
if (!$cache || (isset($r->cache['duration']) && $cache->expire < time())) {
$r->render($format, $print);
$time = null;
if (isset($r->cache['duration'])) {
try {
$time = @new DateTime($r->cache['duration']);
}
catch (Exception $e) {
drupal_set_message('Invalid Cache Duration', 'error', TRUE);
}
if ($time) $time = $time->getTimeStamp();
}
if (!$time) {
$time = CACHE_PERMANENT;
}
cache_set($cid, $r->cache, 'cache', $time);
}
$r->render($format, $print, $cache->data);
}
else {
$r->render($format, $print);
}
$r->render($format, $print);
$o = Frx::Document($format);
if ($o) {
$output = $o->render($r, $format, array());
......
......@@ -385,6 +385,7 @@ function forena_layout_form($form, $form_state, $report_name) {
$attributes = $r->get_attributes_by_id();
$category = $r->getCategory();
$menu = $r->getMenu();
$cache = $r->getCache();
$body = $r->simplexml->body->asXML();
$css = @(string)$r->simplexml->head->style;
......@@ -499,6 +500,21 @@ function forena_layout_form($form, $form_state, $report_name) {
'#cols' => 80,
);
$form['cache'] = array(
'#type' => 'fieldset',
'#title' => t('Cache'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$form['cache']['duration'] = array(
'#type' => 'textfield',
'#title' => t('Duration'),
'#description' => t('Specify a php strtotime relative date duration, (e.g. +1 hour, +2 days)'),
'#default_value' => @$cache['duration'],
);
$form['buttons']['save'] = array(
'#type' => 'submit',
'#value' => 'Save',
......@@ -544,6 +560,14 @@ function forena_layout_form_validate($form, &$form_state) {
form_set_error('menu][path', t('Invalid Path'));
}
}
if ($values['cache']['duration']) {
try {
$time = @new DateTime($values['cache']['duration']);
}
catch (Exception $e) {
}
if (!$time) form_set_error('cache][duration' , t('Invalid Cache Duration'));
}
}
/**
......@@ -570,6 +594,11 @@ function forena_layout_form_submit($form, &$form_state) {
$rebuild_menu = TRUE;
}
$cache = $r->cache;
if ($values['cache']!=$cache) {
$r->setCache($values['cache']);
}
// Body
$r->setBody($values['body']['value']);
// CSS
......
......@@ -459,7 +459,7 @@ function forena_parameter_form($formid, &$form_state, $parameters, $attributes=
$menu_parms = Frx::Data()->getContext('menu-parms');
if ($menu_parms) $form_state['storage']['menu-parms'] = $menu_parms;
// Set Descriptors
$desc = FrxData::instance()->getContext('forenaReport');
$desc = FrxData::instance()->getContext('report');
if ($desc) $form_state['storage']['desc'] = $desc;
$desc = $form_state['storage']['desc'];
$report_name = @$desc['name'];
......@@ -772,6 +772,9 @@ function forena_user_reports() {
return $output;
}
/**
* Tries to load data tables libary and other libraries that help.
*/
function forena_include_data_tables() {
static $init = FALSE;
if (!$init) {
......@@ -800,15 +803,15 @@ function forena_include_data_tables() {
function forena_report($name_in, $parms = array(), $print = TRUE) {
require_once('forena.common.inc');
$desc = Frx::Menu()->parseURL($name_in);
$time = @new DateTime($value);
$desc['time'] = date_format($time, 'Y-m-d H:i:s');
if (!$desc['exists']) {
drupal_set_message(t('Report %s not found', array('%s' => $name_in)), 'error', FALSE);
return '';
}
FrxData::instance()->setContext('cookie', $_COOKIE);
FrxData::instance()->setContext('forenaReport', $desc);
FrxData::instance()->setContext('report', $desc);
$report_name = $desc['name'];
// Load dataTable plugin if possible.
//forena_include_data_tables();
......@@ -821,9 +824,11 @@ function forena_report($name_in, $parms = array(), $print = TRUE) {
$forena_js['form'] = preg_replace('/[^\w\-]+/u', '_', Frx::Skin()->name);
$forena_js['report'] = preg_replace('/[^\w\-]+/u', '_', $name_in);
if ($print) drupal_add_js(array('forena' => $forena_js), 'setting');
if (!$print && is_array($output)) $output = drupal_render($output);
return $output;
}
}
/**
......
......@@ -310,5 +310,17 @@ can define parameters in the head section of the .frx file as follows:
the select description.</td>
</tr>
</table>
<h2 id="cache">Cache</h2>
<p>The frx:cache element specifies the caching properties of each report. The following poperties
may be defined</p>
<table>
<tbody>
<tr>
<th>duration</th><td>The duration that the cache is supposed to be kept.
Use <a href="http://php.net/manual/en/datetime.formats.relative.php">
relative formats</a> according to the php documentation.</td>
</tr>
</tbody>
</table>
</body>
</html>
Markdown is supported
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