Saturday 17 November 2007

Reports with Symfony

I wondered what the easiest way was to create reports.
I looked into PM Report 4.1 by Zeke Walker, but did not think it would be what I needed.

So this is what I did:
edit the Peer class of your table, and add a function that will return the data that you will need in the report:
lib/model/MyTablePeer.php
static function getAllRowsWithBlabla()
{
$con = Propel::getConnection(self::DATABASE_NAME);
$sql = 'SELECT * FROM '.self::TABLE_NAME.' WHERE a=1 ORDER BY nachname, vorname';
$stmt = $con->prepareStatement($sql);
$rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM);
return BasePatePeer::populateObjects($rs);
}


Then in apps/myapp/modules/mytable/actions/actions.class.php add an action for the report:
public function executeReportVereinsmitglieder()
{
$this->items = MyTablePeer::getAllRowsWithBlabla();
}


Finally, create a success page:
apps/myapp/modules/mytable/templates/reportBlablaSuccess.php
<?php use_helper('Text', 'Date') ?>

<div id="datecreated">Date created:
<?php echo format_date(time(), 'd', 'de');?></div>

<div id="doprint"><a href="javascript:window.print()">
Print this report</a></div>

<h2>My blabla report</h2>

<?php $first = true; ?>

<?php foreach($items as $item): ?>

<?php
if ($item->getSomeCondition())
{
if ($first)
{
$first = false;
}
else
{
echo ', ';
}
echo $item->getMyValue();
}

?>

<?php endforeach ?>


To disable some items for printing, I changed the css styles
in file apps/myapp/templates/layout.php:
<style type="text/css">
@media print {
/* ... format definitions for printing ... */
#navigation { display:none; }
#title { display:none }
#doprint { display:none }
}
</style>

1 comment:

Unknown said...

Thank you for showing some light on total darkness. It is very common requirement if you are building some application