Basic HTML Reports
|
ATK Howto: Basic HTML Reports
|
Preface
There are many formats the reports can be sent to, Infact I use the FPDF library to turn mine into PDFs and a recent thread about report writing mentioned using the ATKOpenDocumentWriter class in ATK to create ODF files.
I'm going to focus firstly on the basics to get a report to appear on the screen, and then possibly expand this to include how I used the FPDF library to save PDF files since first you want to know how to do it in ATK.
Note: There is now a fully working set of example code submitted as a patch to the bugtracker. http://bugzilla.achievo.org/show_bug.cgi?id=1071 It is setup to be lesson11 of the demo.
Start with any empty node
First you start off with a normal node, but leave it empty:
Empty myReport Node
class myReport extends atkNode
{
function myReport ()
{
$this->atkNode("myReport");
}
}
action_report Action
This appears to be where the action happens. It start's the page and calls the [getMyReport] function which both presents the form to filter/sort your data on, and display the resulting report.
action_report code
Add the following to your report class below the constructor:
function action_report()
{
// Here is the meat of the report and where everything happens
$content = $this->getMyReport();
// Add the given content to the page
$page = &$this->getPage();
$page->addContent($this->renderActionPage("report", $content));
}
Here we use a function to manage everything called getMyReport();
Main Report code
As stated above this is where the the report is generated and presented, also the sorting/filtering choices are presented/configured here as well.
getMyReport function
function getMyReport()
{
// Get a reference to the sessionmanager
global $sessionManager = &atkGetSessionManager();
$this->addStyle("style.css");
// Try to get a reference to the User Interface object and raise an error if it fails
$ui = &$this->getUi();
// Add a newline (vertical spacing)
$output.= '<br/>';
$go = $sessionManager->pageVar("go");
//use this to show the filter screen first, and the report second.
// $go gets set as a hidden field in the getFilterBar function.
if ($go != 1)
{
// Start composing the output by adding the filterbar as topmost item
$output = $this->getFilterBar();
$output.= '<br/>';
// Render a box around the generated output
$boxedoutput = $ui->renderBox(array("title"=>$this->actionTitle('ProductionTallySheet'), "content"=>$output));
// Return the generated boxed output
return $boxedoutput;
}
else
{
// Reporting code goes here, use techniques learned in the db
// access section to get your data out and then just format
// standard html to output with
// Render a box around the generated output
$boxedoutput = $ui->renderBox(array("title"=>$this->actionTitle('ProductionTallySheet'), "content"=>$output));
// Return the generated boxed output
return $boxedoutput;
}
}
getFilterBar function
In this function you can build the filter form.
function getfilterbar()
{
// get data to filter based on constraints for the report.
$record = $this->getFilterRecord();
//get the filter attributes
$attrs = &$this->getFilterAttrs();
$output = '<form action="dispatch.php" method="get">';
$output.= atkSessionManager::formState();
$output.= '<input type="hidden" name="go" value="1">'; //trigger to prevent loading the first time
$output.= atktext("Date","production").' '.$attrs["date"]->edit($record);
$output.= '<br>';
$output.= atktext("name").": ".$attrs["name"]->edit($record)." ";
$output.= '<br>';
$output.= atktext("shift").": ".$attrs["shift"]->edit($record)." ";
$output.= ' <input type="submit" value="'.atktext("refresh").'">';
$output.= '</form>';
return $output;
}
getFilterRecord
Grab the data to be filtered/sorted on
function getFilterRecord()
{
static $s_record = NULL;
$sessionManager = atkGetSessionManager();
if ($s_record==NULL)
{
$s_record = array();
$s_record["name"] = $sessionManager->pageVar("name");
$s_record["shift"] = $sessionManager->pageVar("shift");
$s_record["date"] = $sessionManager->pageVar("date");
$attrs = &$this->getFilterAttrs();
foreach ($attrs as $attribname => $p_attrib)
{
$s_record[$attribname] = &$p_attrib->fetchValue($s_record);
}
$go = $sessionManager->pageVar("go");
if ($go != 1)
{
// initial loading.
//set name to current logged in person
$user = getUser();
$s_record["name"] = array("id"=>$user["id"]);
// We'll put the to to todays date
$s_record["date"] = date("Ymd");
}
}
return $s_record;
}
getFilterAttrs
Get the attribute info for the fields you're going to use to sort/filter on so they render normally on the screen.
function &getFilterAttrs()
{
userelation("atkmanytoonerelation");
useattrib("atkradioattribute");
$attrs["name"] = &new atkManyToOneRelation("name", "access.users");
//$attrs["name"]->setNoneLabel("Do not filter");
$attrs["shift"] = &new atkRadioAttribute("shift", array("1 ST","2 ND","3 RD"), array("1 ST","2 ND","3 RD"));
$attrs["date"] = &new atkDateAttribute("date");
return $attrs;
}
Adding Menu items in Modules.inc
Note that while adding this report node in the getMenuItems and getNodes sections of the modules.inc file you will want to define them like:
class mymodule extends atkModule
{
function getMenuItems()
{
//Menu item Title
$this->menuitem("mymodule", "", "main");
$this->menuitem("-"); //adds a space on the main menu but does not affect the module's menu
$this->menuitem("My Report", dispatch_url("mymodule.myReport", "report"), "mymodule", array("mymodule.myReport", "report"));
$this->menuitem("-", "", "Production", array("mymodule.myReport", "admin"));
}
function getNodes()
{
registerNode("mymodule.myReport", array("report","any_user"));
}
}
Conclusion
There are many things that can be expanded upon here but this is the basic skeleton method borrowed from Achievo examples.
You may also want to have a look at atkBaseReport. It's a predefined class containing some of the constructs mentioned above.