Cnz_Html
[ class tree: Cnz_Html ] [ index: Cnz_Html ] [ all elements ]

Source for file Table.php

Documentation is available at Table.php

  1. <?php
  2. /**
  3.  * CNZ Framework
  4.  *
  5.  * A Framework for Creating and Using Complex Web Elements
  6.  *
  7.  * The purpose of this framework is to provide a library of high-level objects
  8.  * to facilitate common HTML coding tasks, such as menus, tables, and forms.
  9.  * The intent is to reduce repetitive HTML coding as much as possible, replacing
  10.  * it with a combination of configuration files and style sheets with
  11.  * standardized naming conventions.
  12.  *
  13.  * This framework is built on and requires the
  14.  * {@link http://framework.zend.com/ Zend Framework}.
  15.  *
  16.  * @category   Cnz
  17.  * @package    Cnz_Html
  18.  * @subpackage Table
  19.  *
  20.  * @author    Lyle Frost <lfrost@cnz.com>
  21.  * @copyright Copyright (c) 2006-2007 Citadel Network <{@link http://www.citadelnetwork.com/}>
  22.  * @filesource
  23.  * @license   http://www.citadelnetwork.com/license/cnzframework New BSD License
  24.  * @version   $Id: Table.php 27 2007-07-19 18:47:54Z lfrost $
  25.  */
  26.  
  27. /**
  28.  * Required classes.
  29.  *
  30.  * @ignore
  31.  */
  32. Zend_Loader::loadClass('Cnz_Html_Element');
  33. Zend_Loader::loadClass('Cnz_Html_Table_Button');
  34. Zend_Loader::loadClass('Cnz_Html_Table_Column');
  35. Zend_Loader::loadClass('Zend_Filter');
  36. Zend_Loader::loadClass('Zend_Filter_Alnum');
  37. Zend_Loader::loadClass('Zend_Filter_Int');
  38.  
  39. /**
  40.  * HTML Table class
  41.  *
  42.  * If greenbar is true, the style suffix 'hilight' is used for even-numbered
  43.  * rows.
  44.  *
  45.  * Example INI:
  46.  *
  47.  * <pre><samp>
  48.  * [HtmlTable]
  49.  * greenbar                = true
  50.  * greenbarBandSize        = 2
  51.  * 
  52.  * 
  53.  * [HtmlTable-team : HtmlTable]
  54.  * caption = "The CF Demo Team"
  55.  * order   = number:desc
  56.  * 
  57.  * column.1.name           = name
  58.  * column.1.heading        = Name
  59.  * 
  60.  * column.2.name           = number
  61.  * column.2.heading        = Number
  62.  * column.2.align          = right
  63.  * column.2.type           = int
  64.  * 
  65.  * column.3.name           = position
  66.  * column.3.heading        = Position
  67.  * 
  68.  * 
  69.  * [HtmlTable-directory : HtmlTable]
  70.  * from    = employee
  71.  * order   = name_last,name_first
  72.  * limit   = 8
  73.  * 
  74.  * column.name.heading     = Name
  75.  * column.name.name        = name
  76.  * column.name.expression  = "name_first||' '||name_last"
  77.  * 
  78.  * column.email.heading    = Email
  79.  * column.email.name       = email
  80.  * column.email.link       = mailto
  81.  * </samp></pre>
  82.  *
  83.  * Example PHP:
  84.  *
  85.  * <code>
  86.  * Cnz_Html::loadAndDisplay('Cnz_Html_Table_Csv', array('name' => 'team', 'filename' => 'table.ini', 'config' => true), 'float:right;');
  87.  * $teamTable->display();
  88.  * </code>
  89.  *
  90.  * @category   Cnz
  91.  * @package    Cnz_Html
  92.  * @subpackage Table
  93.  */
  94. class Cnz_Html_Table extends Cnz_Html_Element implements Countable
  95. {
  96.     const HAS_CONFIG        = true;
  97.     const PREFIX            = 'HtmlTable';
  98.     const BUTTON_PREFIX        = 'button';
  99.     const COLUMN_PREFIX        = 'column';
  100.     const ITEM_PREFIX        = 'item';
  101.     const DEFAULT_LIMIT        = 24;
  102.     const FIRST_BUTTON        = 'first.png';
  103.     const LAST_BUTTON        = 'last.png';
  104.     const NEXT_BUTTON        = 'next.png';
  105.     const PREV_BUTTON        = 'prev.png';
  106.     const ORDER_ASC            = 'asc';
  107.     const ORDER_DESC        = 'desc';
  108.  
  109.     /**#@+ @var array */
  110.     protected $columnArray        = array();
  111.     protected $buttonArray        = array();
  112.     protected $rowArray        = array();
  113.     protected $orderArray        = array();
  114.     /**#@-*/
  115.  
  116.     /**#@+ @var int */
  117.     protected $limit        = NULL;
  118.     protected $offset        = 0;
  119.     protected $rowCount        = 0;
  120.     protected $pageCount        = 0;
  121.     protected $pageNumber        = 0;
  122.     /**#@-*/
  123.  
  124.     /**#@+ @var string */
  125.     protected $whereAdd        = NULL;
  126.     /**#@-*/
  127.  
  128.     /*====================================================================*/
  129.  
  130.     /**#@+ @var boolean */
  131.     private   $headerRowFlag    false;
  132.     private   $greenbarFlag        false;
  133.     /** Records are in columns */
  134.     private   $transposeFlag    false;
  135.     /**#@-*/
  136.  
  137.     /**#@+ @var int */
  138.     private   $greenbarBandSize    1;
  139.     /**#@-*/
  140.  
  141.     /**#@+ @var string */
  142.     private   $caption        NULL;
  143.     /**#@-*/
  144.  
  145.     /* Getters/Setters ===================================================*/
  146.  
  147.     /**
  148.      * @param string New order
  149.      * @return void 
  150.      */
  151.     public function setOrderBy($order)
  152.     {
  153.         $this->orderArray = Cnz_Html::listToArray($order);
  154.         return;
  155.     }
  156.  
  157.     /* Generators ========================================================*/
  158.  
  159.     /**
  160.      * @return array Sequential array of column names
  161.      */
  162.     protected function genColumnNameArray()
  163.     {
  164.         $cna array();
  165.         foreach ($this->columnArray as $column)
  166.         {
  167.             $cna[$column->getName();
  168.         }
  169.         return $cna;
  170.     }
  171.  
  172.     /* Methods ===========================================================*/
  173.  
  174.     /**
  175.      * Option fields:
  176.      *   none
  177.      *
  178.      * Configuration fields:
  179.      *   caption
  180.      *   greenbar
  181.      *   greenbarBandSize
  182.      *   limit
  183.      *   order
  184.      *
  185.      * @param  array       $options Options
  186.      */
  187.     public function __construct(array $options array())
  188.     {
  189.         parent::__construct($options);
  190.  
  191.         // Process configuration.
  192.         if (isset($this->config->caption)) $this->caption $this->config->caption;
  193.         if (isset($this->config->greenbar)) $this->greenbarFlag Cnz_Html::configFlag($this->config->greenbar);
  194.         if (isset($this->config->greenbarBandSize)) $this->greenbarBandSize = (int)$this->config->greenbarBandSize;
  195.         if (isset($this->config->limit)) $this->limit = (int)$this->config->limit;
  196.         if (isset($this->config->order)) $this->orderArray = Cnz_Html::listToArray($this->config->order);
  197.  
  198.         // Create buttons and columns.
  199.         $i 0;
  200.         if (isset($this->config->column))
  201.         {
  202.             for ($this->config->column->rewind()$this->config->column->valid()$this->config->column->next())
  203.             {
  204.                 $options['name'$this->config->column->key();
  205.                 $this->columnArray[$i++new Cnz_Html_Table_Column($options$this->config->column->current());
  206.             }
  207.         }
  208.         $i 0;
  209.         if (isset($this->config->button))
  210.         {
  211.             for ($this->config->button->rewind()$this->config->button->valid()$this->config->button->next())
  212.             {
  213.                 $options['name'$this->config->button->key();
  214.                 $this->buttonArray[$i++new Cnz_Html_Table_Button($options$this->config->button->current());
  215.             }
  216.         }
  217.  
  218.         // Process _GET.
  219.         if (isset($_GET['offset']))
  220.         {
  221.             $filter new Zend_Filter_Int();
  222.             $this->offset = $filter->filter($_GET['offset']);
  223.         }
  224.         if (isset($_GET['order']))
  225.         {
  226.             $filter new Zend_Filter_Alnum();
  227.             $order $filter->filter($_GET['order']);
  228.             if (!empty($order)) $this->orderArray = Cnz_Html::listToArray($order);
  229.         }
  230.         if (isset($_GET['where']))
  231.         {
  232.             $filter new Zend_Filter_Alnum();
  233.             $this->whereAdd = $filter->filter($_GET['where']);
  234.         }
  235.  
  236.         return;
  237.     }
  238.  
  239.     /**
  240.      * @param  string $style Value for outermost style attribute
  241.      * @return void 
  242.      */
  243.     public function display($style NULL)
  244.     {
  245.         $this->readData();
  246. //if (!empty($this->whereAdd)) echo '<p>', $this->whereAdd, '</p>';
  247.  
  248.         $indent Cnz_Html::indentGenerate();
  249.  
  250.         echo $indent'<div class = "'$this->genStyles()'"';
  251.         if (!empty($style)) echo ' style = "'$style'"';
  252.         echo '>'"\n";
  253.         Cnz_Html::indentInc();
  254.         $indent Cnz_Html::indentGenerate();
  255.  
  256.         echo $indent'<table class = "'$this->genStyles()'" style = "width:100%;">'"\n";
  257.         Cnz_Html::indentInc();
  258.         $indent Cnz_Html::indentGenerate();
  259.         if (isset($this->caption))
  260.         {
  261.             echo $indent'<caption class = "'$this->genStyles()'">';
  262.             echo $this->caption;
  263.             echo '</caption>'"\n";
  264.         }
  265.  
  266.         if (count($this->columnArray0)
  267.         {
  268.             $this->displayHeader();
  269.         }
  270.         $rowCount count($this->rowArray);
  271.         for ($rowNumber 0$rowNumber $rowCount$rowNumber++)
  272.         {
  273.             $this->displayRow($rowNumber);
  274.         }
  275.  
  276.         Cnz_Html::indentDec();
  277.         $indent Cnz_Html::indentGenerate();
  278.         echo $indent'</table>'"\n";
  279.  
  280.         // Paging controls.
  281.         if (isset($this->limit))
  282.         {
  283.             echo $indent'<div style = "text-align: center;">';
  284.             $next_offset $this->offset + $this->limit;
  285.             $prev_offset $this->offset - $this->limit;
  286.             if ($prev_offset >= 0)
  287.             {
  288.                 // first button
  289.                 echo '<a href = ""><img alt = "[First]" src = "'Cnz_Html::getButtonDir()self::FIRST_BUTTON'" style = "border:none;"/></a>';
  290.  
  291.                 // prev button
  292.                 echo '<a href = "?offset=' $prev_offset;
  293.                 echo '&amp;order='Cnz_Html::arrayToList($this->orderArray);
  294.                 echo '"><img alt = "[Previous]" src = "'Cnz_Html::getButtonDir()self::PREV_BUTTON'" style = "border:none;"/></a> ';
  295.             }
  296.             echo 'Page '$this->pageNumber' of '$this->pageCount;
  297.             if ($this->pageNumber < $this->pageCount)
  298.             {
  299.                 echo ' <a href = "?offset=' $next_offset;
  300.                 echo '&amp;order='Cnz_Html::arrayToList($this->orderArray);
  301.                 echo '"><img alt = "[Next]" src = "'Cnz_Html::getButtonDir()self::NEXT_BUTTON'" style = "border:none;"/></a>';
  302.                 echo '<a href = "?offset='($this->pageCount - 1$this->limit;
  303.                 echo '&amp;order='Cnz_Html::arrayToList($this->orderArray);
  304.                 echo '"><img alt = "[Last]" src = "'Cnz_Html::getButtonDir()self::LAST_BUTTON'" style = "border:none;"/></a>';
  305.             }
  306.             echo '</div>'"\n";
  307.         }
  308.  
  309.         Cnz_Html::indentDec();
  310.         $indent Cnz_Html::indentGenerate();
  311.         echo $indent'</div>'"\n";
  312.  
  313.         return;
  314.     }
  315.  
  316.     protected function readData()
  317.     {
  318.     }
  319.  
  320.     /**
  321.      * @param  string $name Column name
  322.      * @return int    Column number, or false on error
  323.      */
  324.     protected function columnNameToNumber($name)
  325.     {
  326.         $n count($this->columnArray);
  327.         for ($i 0$i $n$i++)
  328.         {
  329.             if ($name == $this->columnArray[$i]->getName()) return $i;
  330.         }
  331.  
  332.         return false;
  333.     }
  334.  
  335.     /**
  336.      * Defined by the Countable interface
  337.      *
  338.      * @return int 
  339.      */
  340.     public function count()
  341.     {
  342.         return count($this->rowArray);
  343.     }
  344.  
  345.     /**
  346.      * @return void 
  347.      */
  348.     private function displayHeader()
  349.     {
  350.         $is Cnz_Html::getIndentString();
  351.         $indent Cnz_Html::indentGenerate();
  352.         echo $indent'<tr class = "'$this->genStyles()'">'"\n";
  353.         if (count($this->buttonArray0echo $indent$is'<th class = "'$this->genStyles()'">&nbsp;</th>'"\n";
  354.         $colNumber 0;
  355.         foreach ($this->columnArray as $column)
  356.         {
  357.             echo $indent$is'<th class = "'$this->genStyles()'">';
  358.             $order $column->getOrder();
  359.             if (!empty($order))
  360.             {
  361.                 echo '<a href = "?offset='$this->offset'&amp;order='$order'">';
  362.                 echo $column->getHeading();
  363.                 echo '</a>';
  364.             }
  365.             else
  366.             {
  367.                 echo $column->getHeading();
  368.             }
  369.             echo '</th>'"\n";
  370.         }
  371.         echo $indent'</tr>'"\n";
  372.         return;
  373.     }
  374.  
  375.     /**
  376.      * @return void 
  377.      */
  378.     private function displayRow($rowNumber)
  379.     {
  380.         $is Cnz_Html::getIndentString();
  381.         $indent Cnz_Html::indentGenerate();
  382.         $bandNum 0;
  383.         if ($this->greenbarFlag)
  384.         {
  385.             $bandNum = (int)($rowNumber $this->greenbarBandSize);
  386.         }
  387.         echo $indent'<tr class = "'$this->genStyles()'">'"\n";
  388.         if ($this->transposeFlag)
  389.         {
  390.             echo $indent$is'<th class = "'$this->genStyles()'">'"\n";
  391.             echo $this->columnArray[$rowNumber]->getHeading();
  392.             echo '</th>'"\n";
  393.         }
  394.  
  395.         // Greenbar
  396.         $styleSuffix '';
  397.         if ($this->greenbarFlag)
  398.         {
  399.             if ($bandNum == 1$styleSuffix 'hilight';
  400.         }
  401.  
  402.         $row $this->rowArray[$rowNumber];
  403.  
  404.         // Buttons
  405.         if (count($this->buttonArray0)
  406.         {
  407.             echo $indent$is'<td class = "'$this->genStyles(NULL$styleSuffix)'">'"\n";
  408.             foreach ($this->buttonArray as $button)
  409.             {
  410.                 $button->setRow($row);
  411.                 $button->display();
  412.             }
  413.             echo $indent$is'</td>'"\n";
  414.         }
  415.  
  416.         $colCount count($this->columnArray);
  417.         for ($colNum 0$colNum $colCount$colNum++)
  418.         {
  419.             $style '';
  420.             $column $this->columnArray[$colNum];
  421.             $align $column->getAlign();
  422.             $colName $this->columnArray[$colNum]->getName();
  423.             if (!empty($align)) $style ' style = "text-align:' $align ';"';
  424.             $cell $row[$colName];
  425.             echo $indent$is'<td class = "'$this->genStyles(NULL$styleSuffix)'"'$style;
  426.             if ($column->getNoWrap()) echo ' style = "white-space:nowrap;"';
  427.             echo '>';
  428.  
  429.             $link $column->getLink();
  430.             if (!empty($link&& !empty($cell))
  431.             {
  432.                 echo '<a href = "';
  433.                 switch ($link)
  434.                 {
  435.                 case 'web':
  436.                     echo 'http://';
  437.                     break;
  438.                 case 'weblocal':
  439.                     break;
  440.                 case 'mail':
  441.                 case 'mailto':
  442.                     echo 'mailto:';
  443.                     break;
  444.                 default:
  445.                     // error
  446.                     break;
  447.                 }
  448.                 echo $cell'">';
  449.                 echo $cell;
  450.                 echo '</a>';
  451.             }
  452.             else echo $cell;
  453.  
  454.             echo '</td>'"\n";
  455.         }
  456.         echo $indent'</tr>'"\n";
  457.         return;
  458.     }
  459. }

Documentation generated on Thu, 19 Jul 2007 15:02:16 -0400 by phpDocumentor 1.4.0RC2