Creating a Printable Report

A place for users and developers of the Xataface to discuss and receive support.

Creating a Printable Report

Postby sworden » Thu Apr 05, 2012 8:52 am

I've been working my way through this Wiki article (http://xataface.com/wiki/Creating_Printable_Reports) and it's incredibly well written. One question. If I want this to be a universal printing option (not table-specific) for all tables, how do I need to alter the code?

Here is the full code if it helps:
Code: Select all
<?php
class actions_printable_report {
    function handle(&$params){
        $app =& Dataface_Application::getInstance();
        $query =& $app->getQuery();
        $query['-skip'] = 0;
        $query['-limit'] = 10000;
       
        if ( $query['-table'] != 'products' ){
            return PEAR::raiseError('This action can only be called on the Products table.');
        }
       
        $products = df_get_records_array('products', $query);
       
       
       
        echo '<html><head>'
            .'<title>Printable Report</title>'
            .'<style type="text/css">'
            .'    th { vertical-align: top}'
            .'</style>'
            .'</head>'
            .'<body>';
        foreach ($products as $p){
   

            echo '<table>'
                .'<tr><th>Product ID</th><td>'.$p->htmlValue('product_id').'</td></tr>'
                .'<tr><th>Product Name</th><td>'.$p->htmlValue('product_name').'</td></tr>'
                .'<tr><th>Description</th><td>'.$p->htmlValue('product_description').'</td></tr>'
                .'<tr><th>Photo</th><td>'.$p->htmlValue('product_image').'</td></tr>'
                .'</table>';
        }
       
        echo '</body></html>';
       
    }
}
sworden
 
Posts: 44
Joined: Thu Feb 16, 2012 9:05 am

Re: Creating a Printable Report

Postby shannah » Thu Apr 05, 2012 9:20 am

The tricky part here is that if you are making it work for all tables, then how are you going to decide which columns are displayed in the printable report? What will distinguish it from the normal list view?

The first steps for making this action available for all tables will be:

1. Remove the condition directive in the actions.ini file
2. Remove the if statement that throws an error if it isn't in the products table.
3. Change the df_get_records('products', $query) to df_get_records($query['-table'], $query);


The last part is looping over different fields for different tables. You can get the field definitions with:
Code: Select all
$table = Dataface_Table::loadTable($query['-table']);
$fields = $table->fields();
foreach($fields as $fieldname=>$fieldDef){
     //
}


-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Creating a Printable Report

Postby sworden » Thu Apr 05, 2012 10:12 am

OK. I thought I was making things simpler by having a universal print report, but I can see now that I was not. I think it will be best to create separate print report actions for each table that I actually want to print. So I would have a printable_report_dates.php, and a printable_report_exams.php, etc., creating one for each table that I want to print, correct?

I will also need an action for each in the actions.ini file ([printable_report_dates],[printable_reports_exams], etc.), correct?
sworden
 
Posts: 44
Joined: Thu Feb 16, 2012 9:05 am

Re: Creating a Printable Report

Postby sworden » Thu Apr 05, 2012 11:13 am

Steve,
Thanks so much for your help. I figured it out. Yes, it does work just fine if you create an action for each table in the actions.php file and a printable report for each table.

In case anyone wants to have their printable table in a format with labels at the top, here's how I did it:
Code: Select all
<?php
class actions_printable_report_dates {
    function handle(&$params){
        $app =& Dataface_Application::getInstance();
        $query =& $app->getQuery();
        $query['-skip'] = 0;
        $query['-limit'] = 10000;
       
        if ( $query['-table'] != 'dates' ){
            return PEAR::raiseError('This action can only be called on the Dates table.');
        }
       
        $dates = df_get_records_array('dates', $query);
       
       
       
        echo '<html><head>'
            .'<title>Printable Report</title>'
            .'<style type="text/css">'
            .'    th { vertical-align: top}'
            .'</style>'
            .'</head>'
            .'<body>'
         .'<table>'
         .'<tr><th>Applicant</th><th>Step</th><th>Date</th></tr>';
        foreach ($dates as $d){
   

            echo '<tr><td>'.$d->htmlValue('APPLICANTS_ID').'</td><td>'.$d->htmlValue('STEP').'</td><td>'.$d->htmlValue('STEP_DATE').'</td></tr>';
        }
       
      echo '</table>';
        echo '</body></html>';
       
    }
}

?>
sworden
 
Posts: 44
Joined: Thu Feb 16, 2012 9:05 am


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 11 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved