Jump to content

Basic HTML reports: Difference between revisions

From NusaATK
 
(10 intermediate revisions by 2 users not shown)
Line 20: Line 20:


= action_report Action =
= action_report Action =
This appears to be where the action happens.
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 ==
== action_report code ==
Line 32: Line 32:
// Add the given content to the page
// Add the given content to the page
$page = &$this->getPage();
$page = &$this->getPage();
$page->addContent($this->renderActionPage("admin", $content));
$page->addContent($this->renderActionPage("report", $content));
}
}
</syntaxhighlight>
</syntaxhighlight>
Here we use a function to manage everything called getMyReport();
Here we use a function to manage everything called getMyReport();


= Main report code =
= Main Report code =
All of the action and the rest of the stuff stems from here:
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 ==
== getMyReport function ==
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
function getproductiontallyreport()
  function getMyReport()
{
  {
// Get a reference to the sessionmanager
    // Get a reference to the sessionmanager
global $g_sessionManager;
    global $sessionManager = &atkGetSessionManager();


$this->addStyle("style.css");
    $this->addStyle("style.css");


        // Try to get a reference to the User Interface object and raise an error if it fails
    // Try to get a reference to the User Interface object and raise an error if it fails
        $ui = &$this->getUi();
    $ui = &$this->getUi();


        // Add a newline (vertical spacing)
    // Add a newline (vertical spacing)
        $output.= '<br/>';
    $output.= '<br/>';
$go = $g_sessionManager->pageVar("go");
    $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/>';
    }
    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
//use this to show the filter screen first, and the report second. $go get's set as a hidden filed in the getFilterBar function.
      // Render a box around the generated output
if ($go != 1)
{
        // Start composing the output by adding the filterbar as topmost item
      $output = $this->getFilterBar();
$output.= '<br/>';
} 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));
       $boxedoutput = $ui->renderBox(array("title"=>$this->actionTitle('ProductionTallySheet'), "content"=>$output));


       // Return the generated boxed output
       // Return the generated boxed output
       return $boxedoutput;
       return $boxedoutput;
}
    }
  }
</syntaxhighlight>
</syntaxhighlight>


Line 77: Line 82:
In this function you can build the filter form.
In this function you can build the filter form.
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
function getfilterbar()
  function getfilterbar()
{
  {
  //get data to filter based on constraints for the report.
    // get data to filter based on constraints for the report.
  $record = $this->getFilterRecord();
    $record = $this->getFilterRecord();
   
   
  //get the filter attributes
    //get the filter attributes
  $attrs = &$this->getFilterAttrs();
    $attrs = &$this->getFilterAttrs();
   
   
  $output = '<form action="dispatch.php" method="get">';
    $output = '<form action="dispatch.php" method="get">';
      $output.= session_form();
    $output.= atkSessionManager::formState();
      $output.= '<input type="hidden" name="go" value="1">'; //trigger to prevent loading the first time
    $output.= '<input type="hidden" name="go" value="1">'; //trigger to prevent loading the first time
  $output.= atktext("Date","production").' '.$attrs["date"]->edit($record);
    $output.= atktext("Date","production").' '.$attrs["date"]->edit($record);  
  $output.= '<br>';
    $output.= '<br>';
  $output.= atktext("name").": ".$attrs["name"]->edit($record)." ";
    $output.= atktext("name").": ".$attrs["name"]->edit($record)." ";
  $output.= '<br>';
    $output.= '<br>';
  $output.= atktext("shift").": ".$attrs["shift"]->edit($record)." ";
    $output.= atktext("shift").": ".$attrs["shift"]->edit($record)." ";


      $output.= ' <input type="submit" value="'.atktext("refresh").'">';
    $output.= ' <input type="submit" value="'.atktext("refresh").'">';
      $output.= '</form>';
    $output.= '</form>';
  return $output;
    return $output;
}
  }
</syntaxhighlight>
</syntaxhighlight>


Line 103: Line 108:
Grab the data to be filtered/sorted on
Grab the data to be filtered/sorted on
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
function getFilterRecord()
  function getFilterRecord()
{
  {
static $s_record = NULL;
    static $s_record = NULL;
global $g_sessionManager;
 
    $sessionManager = atkGetSessionManager();
if ($s_record==NULL)
    if ($s_record==NULL)
{
    {
$s_record = array();
      $s_record = array();
        $s_record["name"] = $g_sessionManager->pageVar("name");
      $s_record["name"] = $sessionManager->pageVar("name");
$s_record["shift"] = $g_sessionManager->pageVar("shift");
      $s_record["shift"] = $sessionManager->pageVar("shift");
$s_record["date"] = $g_sessionManager->pageVar("date");
      $s_record["date"] = $g_sessionManager->pageVar("date");
$attrs = &$this->getFilterAttrs();
      $attrs = &$this->getFilterAttrs();
foreach (array_keys($attrs) as $attribname)
      foreach (array_keys($attrs) as $attribname)
{
      {
$p_attrib = &$attrs[$attribname];
$p_attrib = &$attrs[$attribname];
$s_record[$attribname] = &$p_attrib->fetchValue($s_record);
$s_record[$attribname] = &$p_attrib->fetchValue($s_record);
}
      }
$go = $g_sessionManager->pageVar("go");
      $go = $sessionManager->pageVar("go");
if ($go != 1)
      if ($go != 1)
{
      {
//initial loading.  
        // initial loading.  
//set name to current logged in person
//set name to current logged in person
$user = getUser();
$user = getUser();
$s_record["name"] = array("id"=>$user["id"]);
$s_record["name"] = array("id"=>$user["id"]);
//We'll put the to to todays date
$s_record["date"] = date("Ymd");
        // We'll put the to to todays date
}
$s_record["date"] = date("Ymd");
}
      }
return $s_record;
    }
}
    return $s_record;
  }
</syntaxhighlight>
</syntaxhighlight>


Line 140: Line 147:
Get the attribute info for the fields you're going to use to sort/filter on so they render normaly on the screen.
Get the attribute info for the fields you're going to use to sort/filter on so they render normaly on the screen.
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
function &getFilterAttrs()
  function &getFilterAttrs()
{
  {
userelation("atkmanytoonerelation");
    userelation("atkmanytoonerelation");
useattrib("atkradioattribute");
    useattrib("atkradioattribute");
$attrs["name"] = &new atkManyToOneRelation("name", "access.users");
    $attrs["name"] = &new atkManyToOneRelation("name", "access.users");
//$attrs["name"]->setNoneLabel("Do not filter");
    //$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["shift"] = &new atkRadioAttribute("shift", array("1 ST","2 ND","3 RD"), array("1 ST","2 ND","3 RD"));
$attrs["date"] = &new atkDateAttribute("date");
    $attrs["date"] = &new atkDateAttribute("date");
return $attrs;
 
}
    return $attrs;
  }
</syntaxhighlight>
</syntaxhighlight>


Line 155: Line 163:
There are many things that can be expanded upon here but this is the basic skeleton method borrowed from Achievo examples.
There are many things that can be expanded upon here but this is the basic skeleton method borrowed from Achievo examples.


== Code tied together ==
You may also want to have a look at [[atkBaseReport]]. It's a predefined class containing some of the constructs mentioned above.
Here's the code all together:
<syntaxhighlight lang="php">
class myReport extends atkNode
{
function myReport ()
{
$this->atkNode("myReport");
}
 
 
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("admin", $content));
}
 
function getproductiontallyreport()
{
// Get a reference to the sessionmanager
global $g_sessionManager;
 
$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 = $g_sessionManager->pageVar("go");
//use this to show the filter screen first, and the report second. $go get's set as a hidden filed in the getFilterBar function.
if ($go != 1)
{
        // Start composing the output by adding the filterbar as topmost item
      $output = $this->getFilterBar();
$output.= '<br/>';
} 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;
}
 
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.= session_form();
      $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;
}
 
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;
}
 
function getFilterRecord()
{
static $s_record = NULL;
global $g_sessionManager;
if ($s_record==NULL)
{
$s_record = array();
        $s_record["name"] = $g_sessionManager->pageVar("name");
$s_record["shift"] = $g_sessionManager->pageVar("shift");
$s_record["date"] = $g_sessionManager->pageVar("date");
$attrs = &$this->getFilterAttrs();
foreach (array_keys($attrs) as $attribname)
{
$p_attrib = &$attrs[$attribname];
$s_record[$attribname] = &$p_attrib->fetchValue($s_record);
}
$go = $g_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;
}
}
</syntaxhighlight>

Latest revision as of 14:04, 10 June 2008

ATK Howto: Basic HTML reports

Complexity: easy
Author: Rwolverine (PM me on the forums with questions)

List of other Howto's

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.

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/>';		
    } 
    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"] = $g_sessionManager->pageVar("date");
			
      $attrs = &$this->getFilterAttrs();
      foreach (array_keys($attrs) as $attribname)
      {
	$p_attrib = &$attrs[$attribname];
	$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 normaly 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;
  }

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.