Hi,
I would like to create a summary row in my table (total). I think I have to create a custom renderer. How can i achieve this without copy my new file inside the renderers directory of forena ? Is there a hook ok some classes to override ?
thanks for your help.

Comments

romaingar’s picture

Ok i've read carrefully the doc. So my renderer works !
i've use the hook_forena_controls to refer my class file.
like this :

function mymodule_forena_controls() {

  $controls[] = array('file' => 'renderers/FrxTotal.inc',

                      'class' => 'FrxTotal',

                     );

  return $controls;

}


and i've writing this renderer
(i didn't fid the good way to replace the reportDocNode with an array of token->value... so i use the old way...)

/**
 * @file
 * Implements a total renderer
 * @author romain garçon
 *
 */
class FrxTotal extends FrxRenderer {
  public function render() {
    $xml = FrxData::instance()->currentContext();
    $html = $xml->asXML();
        $total=array_flip($this->teng->tokens($this->reportDocNode->asXML()));
        $total= array_pad($total, count($total), 0);
        foreach ($total as $key => &$row) {
          $data=$xml->xpath("//".$key."|".$key."/*[last()]");
          foreach ($data as $k => $cell) {
           $row+=(float)$cell[0];
          }
        }
        $tokens=array_map(function($token){return '{'.$token.'}';},array_keys($total));
        $output=str_replace($tokens, $total, $this->reportDocNode->asXML());
return $output;
}

(clear cache)
and in my layout :

<div class="FrxTable" frx:block="mymodule/report1" id="report1_block">
<table>
	<thead>
		<tr>
			<th>term</th>
			<th><strong>No. exp2</strong></th>
			<th><strong>No. of exp1<br />
			(on-time)</strong></th>
			<th><strong>The title</strong></th>
		</tr>
	</thead>
	<tbody>
		<tr frx:foreach="*" id="report1">
			<td>{taxonomy_term_data_name}</td>
			<td>{exp2} {total+1}</td>
			<td>{exp1}</td>
			<td class="forena-3">{percent}</td>
		</tr>
	</tbody>
	<tfoot frx:renderer="FrxTotal" id="forena-7">
		<tr>
			<td>Total</td>
			<td>{exp2}</td>
			<td>{exp1}</td>
			<td>{percent}</td>
		</tr>
	</tfoot>
</table>

et voilà.. maybe it could be usefull for someone...
thanks !

metzlerd’s picture

This is a very nice example of a simple custom renderer implementation.

FYI: For those interested in rows with totals, there are a couple of ways to achieve this without the need for custom renderers.

MySQL and other databases provide a ROLLUP clause that can be used to generate summary rows of a table.

Alternatively you can use xpath expressions in forena to achieve totals:

<div class="FrxTable" frx:block="mymodule/report1" id="report1_block">
	<tfoot frx:renderer="FrxTotal" >
		<tr>
			<td>Total</td>
			<td>{=sum(*/exp2)}</td>
			<td>{=sum(*/exp1)}</td>
		</tr>
	</tfoot>
</table>

Disclaimer: This syntax was inadvertantly broken in 7.x-3.9, but 7.x-3.11 has the issue resolved so that the xpath expressions may be used in the summary
rows as well.

Pierre.Vriens’s picture

Assigned: Unassigned » Pierre.Vriens
Issue summary: View changes
Status: Active » Needs work

Reassigning this issue to get this info included in the forena help reports (ie the report designer guide and/or the forena developer guide).

Pierre.Vriens’s picture

Assigned: Pierre.Vriens » Unassigned

I might not have sufficient time to get this issue completed in the 7.x-4.x release, apart of course from the enhanced tutorials regarding available hooks, etc which did make it already in the current 7.x-4.x release. If that's the case I'll keep in on my list of whatever next release ... unless of course anybody else worked on this issue in the meantime ...