Commit 10cfd983 authored by metzlerd's avatar metzlerd
Browse files

Working template replacement.

parent e2292f2a
......@@ -145,11 +145,25 @@ class FrxBlockEditor {
}
}
return $preview;
}
public function data($parms = array()) {
if ($parms) {
// Merge in current_context
$data = array_merge(Frx::Data()->currentContextArray(), $data);
$id = str_replace('/', '-', $this->block_name) . '-parm';
Frx::Data()->push($data, $id);
}
$xml = Frx::RepoMan()->data($this->block_name, NULL);
if ($parms) Frx::Data()->pop();
return $xml;
}
/**
* @TODO: Write code to revert blocks saved to database.
* Writes blocks from the old db structure to the new one.
*/
public function revertDBBLocks() {
$block = array();
......
......@@ -32,6 +32,8 @@ class FrxEditor {
public function __construct($report_name) {
$this->dom = new DOMDocument('1.0', 'UTF-8');
$dom = $this->dom;
$dom->formatOutput = TRUE;
$dom->preserveWhiteSpace = TRUE;
$this->load($report_name);
$this->frxReport = new FrxReport();
$this->frxReport->setReport($this->dom);
......@@ -631,20 +633,73 @@ class FrxEditor {
$dom=dom_import_simplexml($node);
$dom->parentNode->removeChild($dom);
}
return $this;
}
/**
* Apply a template based on the block id.
* @param unknown $id
* @param unknown $class
* @param unknown $config
*/
public function applyTemplate($id, $template_class, $config=array()) {
$path = "//*[@id='$id']";
$nodes = $this->simplexml->xpath($path);
if ($nodes) ( $node = dom_import_simplexml($nodes[0]));
if (!$node) {
drupal_set_message(t('Could not find %s in report', array('%s' => $id)), 'error');
return;
}
$block_name = $node->getAttributeNS($this->xmlns, 'block');
$class = $node->getAttribute("class");
$config['block'] = $block_name;
$data= Frx::BlockEditor($block_name)->data();
$c = Frx::Template($template_class);
if ($c) {
$c->initReportNode($node, $this->frxReport);
if (strpos($class, $template_class)===FALSE) {
$c->resetTemplate();
}
$c->generate($data, $config);
}
else {
drupal_set_message(t('Could not find template %s', array('%s' => $templateClass)), 'error');
}
}
public function addTemplate( $class, $conf, $data, $id='') {
/**
* Add a data blcok
* @param unknown $block
* @param unknown $class
* @param unknown $id
* @return FrxEditor
*/
public function addBlock($block_name, $template_class='FrxTable', $config=array(), $id='') {
if ($id) {
$node = $this->dom->getElementById($id);
}
else {
$nodes = $this->dom->getElementsByTagName('body');
$node = $nodes->item(0);
$pnode = $nodes->item(0);
$node = $this->dom->createElement('div');
$pnode->appendChild($node);
}
$config['block'] = $block_name;
$data= Frx::BlockEditor($block_name)->data();
$c = Frx::Template($template_class);
if ($c) {
$c->initReportNode($node, $this->frxReport);
$c->generate($data, $config);
}
else {
drupal_set_message(t('Could not find template %s', array('%s' => $template_class)), 'error');
}
$c = Frx::Template($class);
$c->initReportNode($node, $this->frxReport);
$c->generate($data, $config);
return $this;
}
}
\ No newline at end of file
......@@ -358,12 +358,9 @@ class FrxReport {
* @return unknown
*/
public function asXML() {
if ($this->rpt_xml) {
return $this->rpt_xml->asXML();
}
else {
return '';
}
$dom = $this->dom;
$dom->formatOutput = TRUE;
return $dom->saveXML();
}
/**
* Make sure all xml elements have ids
......
......@@ -1074,11 +1074,31 @@ function forena_forena_controls() {
'class' => 'FrxDrupalControls',
);
$controls[] = array(
'file' => 'renderers/FrxCrosstab.inc',
'class' => 'FrxCrosstab',
);
$controls[] = array(
'file' => 'renderers/FrxEmailMerge.inc',
'class' => 'FrxEmailMerge',
);
$controls[] = array(
'file' => 'renderers/FrxFieldTable.inc',
'class' => 'FrxFieldTable',
);
$controls[] = array(
'file' => 'renderers/FrxRenderer.inc',
'class' => 'FrxRenderer',
);
$controls[] = array(
'file' => 'renderers/FrxTable.inc',
'class' => 'FrxTable',
);
$controls[] = array(
'file' => 'renderers/FrxSource.inc',
'class' => 'FrxSource',
......@@ -1109,11 +1129,6 @@ function forena_forena_controls() {
'class' => 'FrxTitle',
);
$controls[] = array(
'file' => 'renderers/FrxCrosstab.inc',
'class' => 'FrxCrosstab',
);
$library_dir = FrxReportGenerator::instance()->configuration('library_path');
$library = rtrim($library_dir,'/') . '/SVGGraph/SVGGraph.php';
if (file_exists($library)) {
......
......@@ -3,22 +3,24 @@
* @file FrxFieldTable
* Template that displays a table of column/value vertically.
*/
class FrxFieldTable extends FrxTemplate {
class FrxFieldTable extends FrxRenderer {
public function generate($xml, $config) {
$config['foreach'] = '*';
$config['class'] = 'FrxFieldTable';
$div = $this->blockDiv($config);
$columns = $this->columns($xml);
$table = $this->addNode($div, 4, 'table', NULL, NULL, NULL);
$table = $this->addNode($div, 4, 'table');
$r=0;
if ($columns) foreach ($columns as $col => $label) {
$r++;
$tbody = $this->addNode($table, 6, 'tbody');
$tr = $this->addNode($tbody, 8, 'tr');
$th = $this->addNode($tr, 10, 'th', $label);
$th_attrs = array();
if ($r==1) {
$th->setAttribute('style', 'width: 25em;');
$th_attrs = array('style' => 'width: 25em;');
}
$th = $this->addNode($tr, 10, 'th', $label);
$td = $this->addNode($tr, 10, 'td', '{' . $col . '}');
}
}
......
......@@ -18,8 +18,10 @@ class FrxRenderer {
public $frxReport; // The report object being used.
public $columns;
public $numeric_columns;
public $xmlns = 'urn:FrxReports';
public function initReportNode(DOMNode $domNode, FrxReport $frxReport) {
public function initReportNode(DOMElement $domNode, FrxReport $frxReport) {
$this->dom = $frxReport->dom;
$this->reportDocDomNode = $domNode;
$this->dataProvider = Frx::Data();
......@@ -316,6 +318,61 @@ class FrxRenderer {
}
}
public function resetTemplate() {
$node = $this->reportDocDomNode;
$this->removeChildren($node);
$tag = $node->tagName;
$new_node = $this->dom->createElement($tag);
$this->frxAttributes = array();
$parent = $node->parentNode;
$parent->replaceChild($new_node, $node);
$this->reportDocDomNode = $new_node;
$this->initReportNode($new_node, $this->frxReport);
return $node;
}
/**
* Set FRX attributes.
* @param DOMNode $node
* @param unknown $attributes
* @param unknown $frxattributes
*/
public function setAttributes(DOMElement $node, $attributes, $frx_attributes) {
if ($attributes) foreach ($attributes as $key => $value) {
$node->setAttribute($key, $value);
}
// Iterate the value
if ($frx_attributes) foreach ($frx_attributes as $key => $value) {
// If the value is an array create multiple attributes
// that are of the form key_1, key_2 .... etc.
if (is_array($value)) {
$i=0;
$done=FALSE;
while(!$done) {
$v = '';
if ($value) $v = array_shift($value);
$i++;
$k = $key . '_' . trim((string)$i);
$node->setAttribute($v,NULL);
if (!$v) {
$done = TRUE;
}
}
}
// A normal value.
else {
if ($value) $node->setAttributeNS($this->xmlns, $key, $value);
}
}
}
/**
* Standard php array containing merged attributes
* Enter description here ...
......@@ -408,48 +465,21 @@ class FrxRenderer {
return $form_ctl;
}
function addNode($cur_node, $indent=0, $tag='div', $value='', $attributes=array(), $frx_attributes=array()) {
function addNode($cur_node, $indent, $tag='div', $value='', $attributes=array(), $frx_attributes=array()) {
$dom = $this->dom;
if (!$cur_node) {
return;
}
if ($indent!==FALSE) {
if ($indent) {
$tnode = $dom->createTextNode("\n" . str_repeat(' ', $indent));
$cur_node->appendChild($tnode);
}
$pnode = $dom->createElement($tag, $value);
if ($attributes) foreach ($attributes as $key => $value) {
if ($value) {
$attr = $dom->createAttribute($key);
$attr->value = $value;
$pnode->appendChild($attr);
}
}
if ($frx_attributes) foreach ($frx_attributes as $key => $value) {
if ($value) {
// If the value is an array create multiple attributes
// that are of the form key_1, key_2 .... etc.
if (is_array($value)) {
$i=0;
foreach ($value as $v) {
$i++;
$k = $key . '_' . trim((string)$i);
$attr = $dom->createAttributeNS($this->xmlns, $k);
$attr->value = htmlentities($v);
$pnode->appendChild($attr);
}
}
// A normal value.
else {
$attr = $dom->createAttributeNS($this->xmlns, $key);
$attr->value = htmlentities($value);
$pnode->appendChild($attr);
}
}
}
$cur_node->appendChild($pnode);
return $pnode;
$node = $this->dom->createElement($tag, $value);
$cur_node->appendChild($node);
$this->setAttributes($node, $attributes, $frx_attributes);
$cur_node->appendChild($this->dom->createTextNode(""));
return $node;
}
public function columns($xml, $path='/*/*') {
......@@ -509,7 +539,7 @@ class FrxRenderer {
* @param unknown_type $text
*/
public function blockDiv(&$config, $text='') {
$body = $this->reportDocDomNode;
$node = $this->reportDocDomNode;
$heading = $this->extract('heading', $config);
$descr = $this->extract('description', $config);
$include = $this->extract('include', $config);
......@@ -522,15 +552,14 @@ class FrxRenderer {
$class = $this->extract('class', $config);
$frx_attributes = array(
'block' => $block,
'foreach' => $foreach,
);
if ($foreach) $frx_attributes['foreach'] = $foreach;
$attributes = array(
'id' => $id,
'class' => $class,
);
$node = $this->addNode($body, 2, 'div', $text, $attributes, $frx_attributes);
$this->setAttributes($node, $attributes, $frx_attributes);
if ($heading) {
$this->addNode($node, 4, 'h2', $heading);
}
......@@ -539,7 +568,7 @@ class FrxRenderer {
}
if ($include) {
$src = 'reports/' . str_replace('/', '.', $include);
$this->addNode($node, 4, 'div', NULL, NULL, array('renderer' => 'FrxInclude', 'src' => $src));
$this->addNode($node, 4, 'div', NULL, NULL, array('renderer' => get_class($this), 'src' => $src));
}
return $node;
......@@ -591,19 +620,35 @@ class FrxRenderer {
return $id;
}
public function verifyElement(DOMNode $node, $tag, $indent) {
$e = '';
foreach ($domNode->childNodes as $child) {
if (is_object($child) && $child->getName()==$tag) {
$e = $child;
break;
}
/**
* Sets the first child element to a node and returns it.
* IF the node
* @param DOMNode $node
* @param unknown $tag
* @param unknown $indent
* @return Ambigous <string, void, DOMElement>
*/
public function setFirstNode(DOMElement $parent_node, $indent=0, $tag='div', $value='', $attributes=array(), $frx_attributes=array()) {
$dom = $this->dom;
if (!$parent_node) {
return;
}
$nodes = $parent_node->getElementsByTagName($tag);
if ($nodes->length) {
$node = $nodes->item(0);
$this->setAttributes($node, $attributes, $frx_attributes);
}
else {
$node = $this->addNode($parent_node, $indent, $tag, $value, $attributes, $frx_attributes);
}
return $node;
}
if (!$e) {
$e = $this->addNode($node, $indent, $tag );
public function removeChildren(DOMNode $node) {
while (isset($node->firstChild)) {
$this->removeChildren($node->firstChild);
$node->removeChild($node->firstChild);
}
return $e;
}
......
......@@ -20,11 +20,12 @@ class FrxTable extends FrxRenderer {
$columns = $this->columns($xml, '/*');
$attrs = array();
}
$table = $this->addNode($div, 4, 'table');
$thead = $this->addNode($table, 6, 'thead');
$throw = $this->addNode($thead, 8, 'tr');
$tbody = $this->addNode($table, 6, 'tbody');
$tdrow = $this->addNode($tbody, 8, 'tr', NULL, array('id' => $id),$attrs);
$table = $this->setFirstNode($div, 4, 'table');
$thead = $this->setFirstNode($table, 6, 'thead');
$throw = $this->setFirstNode($thead, 8, 'tr');
$tbody = $this->setFirstNode($table, 6, 'tbody');
$tdrow = $this->setFirstNode($tbody, 8, 'tr', NULL, array('id' => $id),$attrs);
$this->removeChildren($tdrow);
if ($columns) foreach ($columns as $col => $label) {
$this->addNode($throw, 10, 'th', $label);
$this->addNode($tdrow, 10, 'td', '{' . $col . '}');
......
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