Adding a printable view

From Achievo/ATK Wiki

Jump to: navigation, search

ATK Howto: Adding a printable view

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

List of other Howto's

Contents

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

Finally, add the action handler.

    function action_newattribute(&$handler)
    {
        $ui = &$this->getUi();
        $newattribute = &$this->getAttribute("newattribute");
        $this->addStyle("style.css");
        $this->addStyle("recordlist.css");               
        $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();
            $page->head('newattribute');
            $page->body();
 
            $content_box = $ui->render("mytemplate.tpl",array("title"=>"NEW ATTRIBUTE",
                                                 "content"=>$newattribute->display($record)),
                                       "node.module"
                                          );
 
            $page->addContent($content_box);
 
        }
 
    }// 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
Navigation