Jump to content

Basic HTML reports

From NusaATK
Revision as of 18:47, 8 February 2008 by Rwolverine (talk | contribs)

ATK Howto: Basic HTML reports

Complexity: easy
Author: Rwolverine (PM me on the forums for 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 them 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.

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("admin", $content));
	}

Here we use a function to manage everything called getMyReport();

Main report code

All of the action and the rest of the stuff stems from here:

getMyReport function

	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;
	}

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.= 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;
	}

getFilterRecord

Grab the data to be filtered/sorted on

	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;
	}

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.

Code tied together

Here's the code all together:

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;
	}
}