Adding a printable view

From ATK Wiki

Jump to: navigation, search

ATK Howto: Adding a printable view

Complexity: Medium
Author: ihcfan <>

List of other Howto's



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->addJoin("item", "", " = stuff.item_id", false);
      $query->addJoin("notes", "", " = item.notes_id", false);
      $query->addField(" 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();
         $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();
      $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;

Finally, add the action handler.

    function action_newattribute(&$handler)
        $ui = &$this->getUi();
        $newattribute = &$this->getAttribute("newattribute");
        $sel = $this->m_postvars["atkselector"];
        // create 'fake' record to pass to
        $theid = explode("=",$sel);
        $record = array("id"=>$theid[1]);
        if (is_object($ui) && is_object($newattribute))
            $page = &$this->getPage();
            $content_box = $ui->render("mytemplate.tpl",array("title"=>"NEW ATTRIBUTE",
    }// end function

Create Templates

The template mentioned above (mytemplate.tpl) is created to strip the typical 'achievo' look from the print window. In it's most basic form it looks like:

<div style="padding: 4px; bgcolor=#ffffff;"> {$content} </div> <br>

The style sheet is used to alter the format of the table created in the custom attribute's display() method.

TABLE.newattribute TD.superbold {

 border-width: 2px;
 border-color: #000000;
 border-style: solid;
 padding: 1px;
 margin: 0px;


Helpful Hints

When altering the stylesheet, clear your browser's cache before reloading.

When altering the template, delete the compiled crud from the atktmp directory.

Personal tools