Source for file TableDisplay.php

Documentation is available at TableDisplay.php

  1. <?php
  2. /* -------------------------------------------------------------------------- */
  3. /* Module      : TableDisplay                                                 */
  4. /*                                                                            */
  5. /* Version History :                                                          */
  6. /*                                                                            */
  7. /* Date        Vsn     Author          Description                            */
  8. /* -------------------------------------------------------------------------- */
  9. /* 21/11/08    1.0     Sonja Lindo     Initial Version                        */
  10. /* 24/02/09    2.0     Sonja Lindo     Converted to to the Zend Framework     */
  11. /* -------------------------------------------------------------------------- */
  12. /**
  13.  * Used to display data in a table. The row colours may alternate.
  14.  * 
  15.  * @package Default
  16.  */
  17. class Zend_View_Helper_TableDisplay extends Zend_View_Helper_Abstract
  18. {
  19.     // Ordinary member variables
  20.     private $obj_Sort_Data;
  21.     private $formatted_table null;
  22.     private $result_sorted;
  23.     private $header_array Array();
  24.     private $row_array    Array();
  25.     private $cell_array   Array();
  26.     private $table_class 'standard';  // may be overwritten
  27.     private $row_class;                 // colour set in displayRows
  28.     private $cell_class 'admin_standard';  // may be overwritten
  29.     private $bg_colour_1='booking_col_1';
  30.     private $bg_colour_2='booking_col_2';
  31.  
  32.     //////////////////////////////////////////////////////////    tabledisplay
  33.     /**
  34.      * Column headers and individual table rows are formatted for display
  35.      * 
  36.      * @return mixed String containing all HTML for a table of data
  37.      */
  38.     public function tabledisplay($in_table_array$in_edit_allowed$in_delete_allowed)
  39.     {
  40.         $this->formatted_table null;
  41.         $this->header_array Array();
  42.         $this->row_array Array();
  43.         $this->cell_array Array();
  44.  
  45.         $errorNoRecords 'There is nothing to display on the ' $this->view->pageTitle ' screen';
  46.         if (is_array($in_table_array))
  47.         {
  48.             return $errorNoRecords;
  49.         else {
  50.             if (count($in_table_array== 0)
  51.             {
  52.                 return $errorNoRecords;
  53.             }
  54.         }
  55.  
  56. // SORT THE RETRIEVED DATA
  57. /*
  58.         $obj_Table_Display->sortData($_SESSION['Session']->getClientId(), $result);
  59.  
  60. */
  61.         // COLUMN HEADERS
  62.         foreach ($in_table_array['colHeaders'as $colHeader)
  63.         {
  64.             $this->addToHeader$colHeader);
  65.         }
  66.         if ($in_edit_allowed)
  67.         {
  68.             $this->addToHeader (  '&nbsp;' );
  69.         }
  70.         if ($in_delete_allowed)
  71.         {
  72.             $this->addToHeader (  '&nbsp;' );
  73.         }
  74.  
  75.  
  76.         // ROWS
  77.         foreach ($in_table_array['data'as $currentRow)
  78.         {
  79.             $this->cell_array array();
  80.             foreach ($currentRow as $name => $value)
  81.             {
  82.                 if !is_array($value) )
  83.                 {
  84.                     // Php auto converts types. '===' forces typed comparison
  85.                     // $name may be an integer or a string so beware
  86.                     if ($name === 'edit_ids')
  87.                     {
  88.                         continue;
  89.                     }
  90.                     $this->addToRow $value );
  91.  
  92.                 else {
  93.  
  94.                     // Field is a href/link - build the url
  95.                     if $value['action'== 'edit'   ||
  96.                          $value['action'== 'insert' ||
  97.                          $value['action'== 'delete' )
  98.                     {
  99.                         $urlDetails array
  100.                                       'controller'=>$this->view->controllerName,
  101.                                         'action'=>$value['action'],
  102.                                         'table'=> $value['table'],
  103.                                         'ids'=>$value['ids']
  104.                                       );
  105.                     else {
  106.                         $urlDetails array
  107.                                       'controller'=>$this->view->controllerName,
  108.                                         'action'=>$value['action'],
  109.                                         'params'=> $value['params']
  110.                                       );
  111.                     }
  112.  
  113.                     if (isset($value['heading']))
  114.                     {
  115.                         $urlDetails['heading'$value['heading'];
  116.                     }
  117.                     $this->addToRow ('<a href="' 
  118.                         $this->view->url ($urlDetails.
  119.                             '">' $value['display''</a>');
  120.  
  121.                 }
  122.             }
  123.  
  124.             if ($in_edit_allowed)
  125.             {
  126.                 $this->addToRow ('<a href="' 
  127.                                  $this->view->url (
  128.                                         array 'controller'=> $this->view->controllerName,
  129.                                                 'action'    => 'edit',
  130.                                                 'table'     => $this->view->table,
  131.                                                 'ids'       => $currentRow['edit_ids'])) .
  132.                                  '">Edit</a>');
  133.             }
  134.             if ($in_delete_allowed)
  135.             {
  136.                 $this->addToRow ('<a href="' 
  137.                                  $this->view->url (
  138.                                         array 'controller' => $this->view->controllerName,
  139.                                                 'action'     => 'delete',
  140.                                                 'table'      => $this->view->table,
  141.                                                 'ids'        => $currentRow['edit_ids'])) .
  142.                                  '">Delete</a>');
  143.             }
  144.             $this->endCurrRow();
  145.         }
  146.  
  147.         // FORMAT THE WHOLE TABLE
  148.         $this->constructTable('medium_width',''true);
  149.  
  150.         return $this->formatted_table;
  151.     }
  152.  
  153.     //////////////////////////////////////////////////////////    addToOutput
  154.     /**
  155.      * Input string is added to the formatted table HTML
  156.      * 
  157.      * @return void 
  158.      */
  159.     public function addToOutput($in_string)
  160.     {
  161.         $this->formatted_table .= $in_string;
  162.     }
  163.  
  164.     ////////////////////////////////////////////////////////////////// sortData
  165.     /**
  166.      * Sorts data according to the chosen 'order by' value.
  167.      * 
  168.      * @return void 
  169.      */
  170.     public function sortData($in_client_id$in_result)
  171.     {
  172.         $this->obj_Sort_Data newObject('Sort_Data');
  173.         $this->obj_Sort_Data->setup ($in_client_id$in_result);
  174.         $this->result_sorted $this->obj_Sort_Data->select('$_POST[order_by]'$_POST[order_dir]);
  175.     }
  176.  
  177.     /////////////////////////////////////////////////////////////////// constructTable
  178.     /**
  179.      * Sets up the final table ready to be displayed.
  180.      * 
  181.      * @return void 
  182.      */
  183.     public function constructTable($in_table_class$in_cell_class$in_alternate_flag)
  184.     {
  185.         // Overwrite default table/cell classes?
  186.         if ($in_table_class != '')
  187.         {
  188.             $this->table_class $in_table_class;
  189.         }
  190.         if ($this->cell_class != '')
  191.         {
  192.             $this->cell_class $in_cell_class;
  193.         }
  194.  
  195.  
  196.         // DISPLAY THE TABLE HERE
  197.         $this->addToOutput"\n\n<table class=\"" $this->table_class "\">\n");
  198.  
  199.         $this->displayHeader();
  200.         $this->displayRows ($in_alternate_flag);
  201.  
  202.         $this->addToOutput"\n</table>\n" );
  203.     }
  204.  
  205.  
  206.  
  207.     /**
  208.      * Formats the column headers for the table
  209.      * 
  210.      * @return void 
  211.      */
  212.     //////////////////////////////////////////////////////////// displayHeader
  213.     public function displayHeader()
  214.     {
  215.         $this->addToOutput"\n\t<tr class=\"header\">" );
  216.  
  217.         foreach ($this->header_array as $header_cell)
  218.         {
  219.             $this->addToOutput"\n\t\t<td class=\"admin_standard\">" $header_cell "</td>" );
  220.         }
  221.         $this->addToOutput"\n\t</tr>\n" );
  222.     }
  223.  
  224.     //////////////////////////////////////////////////////////// displayRows
  225.     /**
  226.      * Zebra stripes optional
  227.      * 
  228.      * @return void 
  229.      */
  230.     public function displayRows($in_alternate_flag)
  231.     {
  232.         $this->row_class $this->bg_colour_1;
  233.  
  234.         foreach ($this->row_array as $curr_cell_array)
  235.         {
  236.             if ($in_alternate_flag == true)
  237.             {
  238.                 // 2 row colors are required, each row alternating the display.
  239.                 if ($this->row_class == $this->bg_colour_2)
  240.                 {
  241.                     $this->row_class $this->bg_colour_1;
  242.                 else {
  243.                     $this->row_class $this->bg_colour_2;
  244.                 }
  245.             }
  246.  
  247.             // ROW START
  248.             $this->addToOutput"\n\t<tr class=\"" .$this->row_class "\">\n" );
  249.  
  250.             // Display all cells for this row
  251.             foreach ($curr_cell_array as $cell)
  252.             {
  253.                 // Does this cell need a styling class?
  254.                 $cell_as_array=explode(CELL_CLASS_DELIM$cell);
  255.                 $class_as_array=explode('='$cell_as_array[0]);
  256.                 if ($class_as_array[0== CELL_CLASS_TEXT)
  257.                 {
  258.                     $this->cell_class $class_as_array[1];
  259.                     $cell str_replace(CELL_CLASS_TEXT.CELL_CLASS_DELIM''$cell_as_array[1]);
  260.                 else {
  261.                     $this->cell_class '';
  262.                 }
  263.  
  264.                 // CELL
  265.                 $this->addToOutput"\n\t\t<td class=\"" $this->cell_class "\">" $cell "</td>" );
  266.             }
  267.  
  268.             // ROW END
  269.             $this->addToOutput"\n\t</tr>" );
  270.         }
  271.     }
  272.  
  273.     /////////////////////////////////////////////////////////   addToHeader
  274.     /**
  275.      * Input column is added to the header
  276.      * 
  277.      * @return void 
  278.      */
  279.     public function addToHeader($in_col_header)
  280.     {
  281.         $this->header_array[$in_col_header;
  282.  
  283.     }
  284.  
  285.     /////////////////////////////////////////////////////////  addToOrderHeader
  286.     /**
  287.      * Input column is added to the header and is a valid 'order by' column
  288.      * 
  289.      * @return void 
  290.      */
  291.     public function addToOrderHeader($in_order_by$in_order_text)
  292.     {
  293.         if ($in_order_by == '')
  294.         {
  295.             $in_order_by 'booking_id';
  296.         }
  297.         if ($_POST['order_dir'== 'asc')
  298.         {
  299.             $order_dir 'desc';
  300.         else {
  301.             $order_dir 'asc';
  302.         }
  303.  
  304.         //$this->addToHeader('<A class=in-line-link href=javascript:document.forms['form'].order_by.value='$in_order_by';document.forms['form'].order_dir.value='$order_dir';trigger_submit('$_POST[submit_trigger]')>$in_order_text</A>');
  305.     }
  306.  
  307.  
  308.     //////////////////////////////////////////////////////////    addToRow
  309.     /**
  310.      * Input cell is added to the current row
  311.      * 
  312.      * @return void 
  313.      */
  314.     public function addToRow($in_cell_value)
  315.     {
  316.         $this->cell_array[$in_cell_value;
  317.     }
  318.  
  319.  
  320.     //////////////////////////////////////////////////////////    endCurrRow
  321.     /**
  322.      * @return void 
  323.      */
  324.     public function endCurrRow()
  325.     {
  326.         $this->row_array[$this->cell_array;
  327.         $this->cell_array Array();
  328.     }
  329. }

Documentation generated on Fri, 27 Mar 2009 13:48:45 +0000 by phpDocumentor 1.4.1