Jump to content

Adding a printable view

From NusaATK
Revision as of 08:21, 15 October 2007 by Ihcfan (talk | contribs)

ATK Howto: Adding a printable view

Complexity: Medium
Author: ihcfan <ihcfan@braincookie.com>

List of other Howto's

Summary

In this howto you will create a popup window with altered format stripping away elements that interfere with cleanly rendering the information on a printer or other alternative output device.


Basic Process

I am sure there are many ways of achieving this, however this presents a method that I used, that worked for my application. There are four main development tasks to complete:

1. Develop a derived attribute capable of rendering the appropriate view.

2. Develop a node which uses the derived attribute.

3. Add a button to the node which pops up the the window.

4. Develop the appropriate template and style sheets if necessary.


Create Derived Attribute

The goal of the derived attribute is to handle rendering the record in both a popup window and on the edit/view pages. In this howto, only view mode is handled, since the primary goal is the display of a window suitable for printing.

Starting with the atkDummyAttribute, modify the display method. In this case, I've modified display to execute a query and display the contents.

  /**
   * Display a record
   * Here it will only returns the text.
   * @param $record  Array with fields
   * @return Parsed string
   */
   function display($record)
   {
     $db = &atkGetDb();
   
     $query = &$db->createQuery();
     $query->addTable("stuff");
     $query->addJoin("item", "", "item.id = stuff.item_id", false);
     $query->addJoin("notes", "", "notes.id = item.notes_id", false);
     $query->addField("item.item");
     $query->addField("item.name");
     $query->addField("notes.note");
     $query->addField("stuff.name as sname");
     $query->addCondition("stuff.person_id = ".$record["id"]);
     $stuff = $db->getrows($query->buildSelect());
     $tbl = &atknew("atk.utils.atktablerenderer");
     $data = array();
     // Develop a table however you'd like it to display
     // Use cascading style sheets to alter the appearance.
     $tbl->setRowClass(1, "superbold");
     // ....
     // Create data
     for ($i=0, $_i=count($stuff); $i<$_i; $i++)
     {
        $row = array();
        $row[]="";
        $row[]=$stuff[$i]["..."];
        $row[]=$stuff[$i]["..."];
        $data[] = $row;
     }
     // render table.
     $output = $tbl->render($data, 0, "navpers");
     return $output;
   }

Create New Node

The new node will use the derived attribute just created, and will place controls in the environment to actually pop up the window.


In the constructor add the code to instantiate the attribute.

     $this->add(new atkMyNewAttribute("newattribute", "id", AF_HIDE_LIST|AF_NO_LABEL), "tab name");


Next we'll develop a method that pops up the window using the dispatch handler. Note, some of the parameters to the NewWindow javascript code are altered to display the window. This code is pretty much boilerplate, and based on the atkPopup method in atk/ui.

   function dispatchPopup($target,$params,$winName,$width,$height,$scroll='no',$resize='no')
   {
     $url = session_url("dispatch.php?".$params, SESSION_NESTED);
     $popupurl ="javascript:NewWindow('".$url."','".$winName."',".$height.",".$width.",'".$scroll."','".$resize."','no','yes')";
     return $popupurl;
   }


To add a button above the display of the new attribute, we overload the display handler for that attribute within the node.

   function newattribute_display($record)
   {
     $page = &$this->getPage();
     $page->register_script(atkconfig("atkroot")."atk/javascript/newwindow.js");
     $newattribute = &$this->getAttribute("newattribute");
     $sParams = str_ireplace("atkaction=view", "atkaction=newattribute", $_SERVER['QUERY_STRING']);
     // this constructs the button HTML.
     $content = $this->dispatchPopup('dispatch.php',$sParams,'printWindow',500,800, 'yes', 'yes');
     $temp = '<input type="button" value="PRINT" onClick="' . $content . '" />';
     // return the output.
     $output = $temp.$newattribute->display($record);
     return $output;
   }



Add Button

Blah


Create Templates

Blah


Try it Out

Blah