Commit e61ece45 authored by David Metzler's avatar David Metzler
Browse files

Implemented sortable columns for FrxTable template.

parent ac8cfa99
......@@ -748,7 +748,7 @@ class FrxEditor {
* @param unknown $id
* @return FrxEditor
*/
public function addBlock($block_name, $template_class='FrxTable', $config=array(), $id='') {
public function addBlock($block_name, $template_class, &$config, $id='') {
$block_name = str_replace('.', '/', $block_name);
if ($id) {
$node = $this->dom->getElementById($id);
......
......@@ -54,7 +54,8 @@ function forena_data_block_preview($block_name, $parms = array(), $edit=FALSE) {
$o = Frx::Editor('__forena_block_preview__');
$o->frxReport->block_edit_mode = $edit;
$o->setEditorParms($parms);
$o->addBlock($block_name);
$config = array();
$o->addBlock($block_name, 'FrxTable', $config);
$o->update();
$content = $o->preview($parms);
$o->cancel();
......
......@@ -1462,6 +1462,10 @@ function forena_theme() {
'file' => 'forena.report.inc',
'render element' => 'form',
),
'forena_element_draggable' => array(
'file' => 'forena.report.inc',
'render element' => 'element',
),
);
}
......
......@@ -623,17 +623,19 @@ function forena_report_data_block_form($formid, &$form_state, $report_name, $act
"#tree" => TRUE,
);
$form['template']['config'] = array_merge($form['template']['config'], $r->templateConfigForm($template_class, $config));
$form['preview'] = array('#type' => 'button', '#value' => t('Preview'));
// Generate a preview
// Generate Data block with preview
$parms = $form_state['storage']['parms'];
$r = Frx::Editor('__report_block_preview__');
$r->setEditorParms($parms);
$r->addBlock($block_name, $template_class, $config);
$form['template']['config'] = array_merge($form['template']['config'], $r->templateConfigForm($template_class, $config));
$r->update();
$preview = $r->preview($parms);
$form['template']['preview_button'] = array('#type' => 'button', '#value' => t('Preview'));
if (!$preview['#has_data']) {
$path = "reports/$report_name/edit/preview-data/$action/$block_name";
if ($id) $path .= "/$id";
......@@ -1369,6 +1371,38 @@ function theme_forena_report_parameters_form($variables) {
return $output;
}
function __forena_element_theme(&$element) {
}
function theme_forena_element_draggable($variables) {
$output = '';
$element = $variables['element'];
$id = $element['#draggable_id'];
$headers = array();
$rows = array();
foreach (element_children($element) as $e_key) {
$element[$e_key]['weight']['#attributes']['class'] = array($id . '-weight');
$row = array();
foreach (element_children($element[$e_key]) as $ctl) {
if (isset($element[$e_key][$ctl]['#title'])) {
$headers[$ctl] = $element[$e_key][$ctl]['#title'];
$element[$e_key][$ctl]['#title_display'] = 'invisible';
}
$e = drupal_render($element[$e_key][$ctl]);
$row[] = $e;
}
$rows[] = array('data' => $row, 'class' => array('draggable'));
}
$output = theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => $id)));
drupal_add_tabledrag($id, 'order', 'group', $id . '-weight');
//$output .= drupal_render_children($element);
return $output;
}
function forena_report_layout_form($form, &$form_state, $report_name) {
$desc = Frx::Menu()->parseURL($report_name);
......
......@@ -23,7 +23,7 @@ class FrxEmailMerge extends FrxRenderer {
return $config;
}
public function configForm($config, $xml='') {
public function configForm($config) {
$form_ctl['from'] = array(
'#type' => 'textfield',
......@@ -56,7 +56,7 @@ class FrxEmailMerge extends FrxRenderer {
$this->validateTextFormats($config, array('body'));
}
public function generate($xml, $config) {
public function generate($xml, &$config) {
$from = $this->extract('from', $config);
$to = $this->extract('to', $config);
$subject = $this->extract('subject', $config);
......
......@@ -5,7 +5,7 @@
*/
class FrxFieldTable extends FrxRenderer {
public $templateName = 'Table of Fields';
public function generate($xml, $config) {
public function generate($xml, &$config) {
$config['foreach'] = '*';
$config['class'] = 'FrxFieldTable';
$div = $this->blockDiv($config);
......
......@@ -77,7 +77,7 @@ class FrxInclude extends FrxRenderer {
* Implement template generator.
* @see FrxRenderer::generate()
*/
public function generate($xml, $config) {
public function generate($xml, &$config) {
$src = $this->extract('src', $key);
$div = $this->blockDiv($config);
$frx = array('src' => $src, 'renderer' => 'FrxInclude');
......
......@@ -6,7 +6,7 @@ class FrxMergeDocument extends FrxRenderer {
* Returns the section
* Enter description here ...
*/
public function configForm($config, $xml='') {
public function configForm($config) {
$form_ctl = array();
$form_ctl['content'] = array(
'#type' => 'text_format',
......@@ -29,7 +29,7 @@ class FrxMergeDocument extends FrxRenderer {
return $config;
}
public function generate($xml, $config) {
public function generate($xml, &$config) {
$config['class'] = get_class($this);
$div = $this->blockDiv($config);
// $config['foreach'] = '*';
......
......@@ -490,7 +490,7 @@ class FrxRenderer {
* Returns the section
* Enter description here ...
*/
public function configForm($config, $xml='') {
public function configForm($config) {
$form_ctl = array();
$form_ctl['header'] = array(
'#type' => 'text_format',
......@@ -524,7 +524,7 @@ class FrxRenderer {
$errors = array();
foreach ($elements as $element) if (isset($config[$element])) {
if ($config[$element]) {
@$frag->appendXML($config[$element]);
@$frag->appendXML($config[$element]['value']);
if (!isset($frag->firstChild)) {
$errors[$element] = t('Invalid XHTML in %s', array('%s' => $element));
}
......@@ -732,12 +732,9 @@ class FrxRenderer {
* @param SimpleXMLElement $xml
* @param array $config
*/
public function generate($xml , $config) {
public function generate($xml , &$config) {
if (!@$config['foreach']) $config['foreach']='*';
$columns = $this->columns($xml);
$text = '';
if ($columns) foreach ($columns as $col => $label) {
......@@ -888,15 +885,15 @@ class FrxRenderer {
elseif ($tags && $cur_section == 'content') {
$cur_section = 'footer';
}
$content[$cur_section]['value'] .= $this->dom->saveXML($child);
@$content[$cur_section]['value'] .= $this->dom->saveXML($child);
break;
case XML_TEXT_NODE:
case XML_ENTITY_REF_NODE:
case XML_ENTITY_NODE:
$content[$cur_section]['value'] .= $child->textContent;
@$content[$cur_section]['value'] .= $child->textContent;
break;
case XML_COMMENT_NODE:
$content[$cur_section]['value'] .= '<!--' . $child->data . '-->';
@$content[$cur_section]['value'] .= '<!--' . $child->data . '-->';
break;
}
}
......@@ -944,4 +941,18 @@ class FrxRenderer {
}
return $attributes;
}
// Helper sort functoin for sorting config by weight.
public static function weight_sort_comp($a, $b) {
if ($a['weight'] == $b['weight']) return 0;
return $a['weight'] < $b['weight'] ? -1 : 1;
}
/**
* Sort a column list by weight.
* @param unknown $columns
*/
public function weight_sort(&$entries) {
uasort($entries, 'FrxRenderer::weight_sort_comp');
}
}
\ No newline at end of file
......@@ -203,7 +203,7 @@ class FrxSVGGraph extends FrxRenderer {
return $output;
}
public function configForm($config, $xml='') {
public function configForm($config) {
$form_ctl = array();
$graph_types = array(
......@@ -304,7 +304,7 @@ class FrxSVGGraph extends FrxRenderer {
return $form_ctl;
}
public function generate($xml, $config) {
public function generate($xml, &$config) {
$config['class'] = get_class($this);
$media = 'FrxSVGGraph';
$div = $this->blockDiv($config);
......
......@@ -6,13 +6,30 @@
class FrxTable extends FrxRenderer {
public $templateName = 'Simple Table';
/**
* Extract table configuration from the HTML
* @see FrxRenderer::scrapeConfig()
*/
public function scrapeConfig() {
$config=array();
$this->extractTemplateHTML($this->reportDocDomNode, $config, array('table'));
$tds = $this->extractXPathInnerHTML('//td', $this->reportDocDomNode, FALSE);
$ths = $this->extractXPathInnerHTML('//th', $this->reportDocDomNode, FALSE);
$columns = array_combine($ths, $tds);
foreach($columns as $label=>$token) {
$key = trim($token, '{}');
$config['columns'][$key] = array(
'contents' => $token,
'label' => $label,
'include' => 1,
'weight' => '0',
);
}
return $config;
}
public function generate($xml, $config) {
public function generate($xml, &$config) {
$config['class'] = get_class($this);
$block = @$config['block'];
$id = @$config['id'];
......@@ -27,13 +44,30 @@ class FrxTable extends FrxRenderer {
$this->removeChildrenExcept($div, array('table'));
if (isset($config['header']['value'])) $this->addFragment($div, $config['header']['value']);
$columns = $this->columns($xml);
$attrs = array('foreach' => '*');
if (!$columns) {
$columns = $this->columns($xml, '/*');
// Decide to inlcude columns
$found_columns = $this->columns($xml);
if (!$found_columns) {
$found_columns = $this->columns($xml, '/*');
$attrs = array();
}
$include_column = 0;
if (!@$config['columns']) {
$include_column = 1;
}
foreach ($found_columns as $column) {
$token = '{' . $column . '}';
if (!isset($config['columns'][$column])) {
$config['columns'][$column] = array(
'contents' => $token,
'include' => $include_column,
'label' => str_replace('_', ' ', $column),
'weight' => 1,
);
}
}
$attrs = array('foreach' => '*');
$table = $this->setFirstNode($div, 4, 'table');
$thead = $this->setFirstNode($table, 6, 'thead');
$throw = $this->setFirstNode($thead, 8, 'tr');
......@@ -41,11 +75,56 @@ class FrxTable extends FrxRenderer {
$tdrow = $this->setFirstNode($tbody, 8, 'tr', NULL, array('id' => $id),$attrs);
$this->removeChildren($tdrow);
$this->removeChildren($throw);
if ($columns) foreach ($columns as $col => $label) {
$this->addNode($throw, 10, 'th', $label);
$this->addNode($tdrow, 10, 'td', '{' . $col . '}');
if ($config['columns']) foreach ($config['columns'] as $key => $col) if ($col['include']) {
$this->addNode($throw, 10, 'th', $col['label']);
$this->addNode($tdrow, 10, 'td', $col['contents']);
}
if (isset($config['footer']['value'])) $this->addFragment($div, $config['footer']['value']);
}
public function configForm($config) {
// Load header informationi from parent config.
$form = parent::configForm($config);
$this->weight_sort($config['columns']);
$form['columns'] = array('#theme' => 'forena_element_draggable', '#draggable_id' => 'FrxTable-columns');
foreach ($config['columns'] as $key => $col) {
$ctl = array();
$ctl['label'] = array(
'#type' => 'textfield',
'#size' => 30,
'#title' => t('Label'),
'#default_value' => $col['label'],
);
$ctl['contents'] = array(
'#type' => 'textfield',
'#size' => '30',
'#title' => t('Data'),
'#default_value' => $col['contents'],
);
$ctl['include'] = array(
'#type' => 'checkbox',
'#title' => t('Include'),
'#default_value' => $col['include'],
'#ajax' => $this->configAjax()
);
$ctl['weight'] = array(
"#type" => 'weight',
'#title' => t('Weight'),
'#default_value' => $col['weight'],
);
$form['columns'][$key] = $ctl;
}
return $form;
}
public function configValidate(&$config) {
parent::configValidate($config);
$this->weight_sort($config['columns']);
}
}
\ No newline at end of file
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