Source for file checkboxtablelist.php

Documentation is available at checkboxtablelist.php

  1. <?php
  2. /**
  3. * a table of checkboxes for different options
  4. *
  5. @author    Stuart Prescott
  6. @copyright  Copyright Stuart Prescott
  7. @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  8. @version    $Id$
  9. @package    Bumblebee
  10. @subpackage FormsLibrary
  11. */
  12.  
  13. /** Load ancillary functions */
  14. require_once 'inc/typeinfo.php';
  15.  
  16. /** parent object */
  17. require_once 'choicelist.php';
  18. /** uses checkbox objects */
  19. require_once 'checkbox.php';
  20. /** uses textfield objects */
  21. require_once 'textfield.php';
  22.  
  23. /**
  24. * a table of checkboxes for different options
  25. *
  26. @package    Bumblebee
  27. @subpackage FormsLibrary
  28. */
  29. class CheckBoxTableList extends ChoiceList {
  30.   /** @var integer  number of columns in the table  */
  31.   var $numcols    = '';
  32.   /** @var integer  number of extra columns to be added to the table  */
  33.   var $numExtraInfoCols = '';
  34.   /** @var string   html/css class for each row in the table  */
  35.   var $trclass    = 'itemrow';
  36.   /** @var string   html/css class for left-side table cell */
  37.   var $tdlclass   = 'itemL';
  38.   /** @var string   html/css class for right-side table cell */
  39.   var $tdrclass   = 'checkBoxEntry';
  40.   /** @var string   html/css class for entire table */
  41.   var $tableclass = 'selectlist';
  42.   /** @var array    list of table headings to be put at the top of the table */
  43.   var $tableHeading;
  44.   /** @var array    list of checkboxes (each checkbox added creates a new column) */
  45.   var $checkboxes;
  46.   /** @var Field    a hidden field that takes a different user-supplied value for each row in the table */
  47.   var $followHidden;
  48.   /** @var string   the key in $data used to populated the $this->followHidden field */
  49.   /** @var TextField  a hidden field that takes a programatically-generated value for each row in the table */
  50.   var $hidden;
  51.   /** @var array    list of strings to be included in the footer of the table */
  52.   var $footer;
  53.   /** @var boolean  generate select/deselect links at the bottom of each column */
  54.   var $includeSelectAll = false;
  55.   /** @var array of arary of checkboxes   for individual customisation of boxes before display */
  56.   var $boxMatrix = NULL;
  57.   /** @var array    list of values thar are checked */
  58.   var $valueList = array();
  59.   var $currentNumCols;
  60.   var $currentRow;
  61.  
  62.   /**
  63.   *  Create a new AnchorTableList
  64.   *
  65.   * @param string $name   the name of the field (db name, and html field name
  66.   * @param string $description  used in the html title of the list
  67.   * @param integer $numExtraInfoCols (optional) number of *extra* columns in the table
  68.   */
  69.   function CheckBoxTableList($name$description=''$numExtraInfoCols=-1{
  70.     $this->ChoiceList($name$description);
  71.     $this->numExtraInfoCols = $numExtraInfoCols;
  72.     $this->checkboxes = array();
  73.     $this->footer = array();
  74.     $this->hidden = new TextField('row');
  75.     $this->hidden->hidden 1;
  76.   }
  77.  
  78.   /**
  79.   *  Accessor method to set the table column headings
  80.   *
  81.   * @param array   new headings to use for the table
  82.   */
  83.   function setTableHeadings($headings{
  84.     $this->tableHeadings $headings;
  85.   }
  86.  
  87.   /**
  88.   * Add a new column of checkboxes to the table
  89.   *
  90.   * @param CheckBox $cb checkbox Field to add
  91.   */
  92.   function addCheckBox($cb{
  93.     $this->checkboxes[$cb;
  94.     $this->numcols = count($this->checkboxes);
  95.   }
  96.  
  97.   /**
  98.   * Add an extra hidden field to each row to record further details about what the selections mean
  99.   *
  100.   * @param Field $h  hidden field to replicate throughout table
  101.   * @param string  $follow    index in the $data provided to CheckBoxTableList::format to use for the field's value
  102.   */
  103.   function addFollowHidden($h$follow='id'{
  104.     $h->hidden 1;
  105.     $this->followHidden = $h;
  106.     $this->followHiddenField = $follow;
  107.   }
  108.  
  109.   /**
  110.   * Toggle the creation of a footer with javascript select all/deselect all buttons
  111.   *
  112.   * @param boolean  $bool   include the footer
  113.   */
  114.   function addSelectAllFooter($bool{
  115.     $this->includeSelectAll = $bool;
  116.   }
  117.  
  118.   /**
  119.   * Include an additional footer in the table
  120.   *
  121.   * @param array $f  array of fields to be included at the bottom of the table
  122.   */
  123.   function addFooter($f{
  124.     $this->footer = $f;
  125.   }
  126.  
  127.   function format($data{
  128.     $j $this->currentRow;
  129.     $numcols $this->currentNumCols;
  130.  
  131.     $aclass  (isset($this->aclass" class='$this->aclass''');
  132.     $trclass  (isset($this->trclass" class='$this->trclass''');
  133.     $tdlclass  (isset($this->tdlclass" class='$this->tdlclass''');
  134.     $tdrclass  (isset($this->tdrclass" class='$this->tdrclass''');
  135.  
  136.     $namebase $this->namebase.$this->name.'-'.$j.'-';
  137.     $fh $this->followHidden;
  138.     $fh->value $data[$this->followHiddenField];
  139.     $fh->namebase $namebase;
  140.     $h $this->hidden;
  141.     $h->value $j;
  142.     $h->namebase $namebase;
  143.  
  144.     $t  "<tr $trclass>"
  145.          ."<td $tdlclass>";
  146.     $t .= "<span $aclass>"
  147.          .$this->formatter[0]->format($data)
  148.          .'</span>';
  149.     $t .= $fh->hidden($h->hidden();
  150.     $t .= "</td>\n";
  151.     for ($i=1$i<=$this->numExtraInfoCols$i++{
  152.       $t .= "<td $tdlclass>"
  153.            .$this->formatter[$i]->format($data);
  154.       $t .= "</td>";
  155.     }
  156.  
  157.     for ($i=0$i<$this->numcols$i++{
  158.       if ($this->boxMatrix !== NULL{
  159.         $cb $this->boxMatrix[$j][$i];
  160.       else {
  161.         $cb clone($this->checkboxes[$i]);
  162.         $cb->namebase $namebase;
  163.         if (in_array($fh->value$this->valueList)) {
  164.           $cb->value 1;
  165.         }
  166.       }
  167.       $t .= "<td $tdrclass>".$cb->selectable().'</td>';
  168.     }
  169.     for ($i=0$i<=$numcols$i++{
  170.       $t .= '<th></th>';
  171.     }
  172.     $t .= "</tr>\n";
  173.     return $t;
  174.   }
  175.  
  176.   function MakeBoxMatrix($force=false{
  177.     if (is_array($this->list->choicelist)) return;
  178.     if (($force&& is_array($this->boxMatrix)) return;
  179.     $this->boxMatrix = array();
  180.     for ($j=0$j<count($this->list->choicelist)$j++{
  181.       $this->boxMatrix[$j]=array();
  182.       for ($i=0$i<$this->numcols$i++{
  183.         $this->boxMatrix[$j][$iclone($this->checkboxes[$i]);
  184.         $this->boxMatrix[$j][$i]->namebase $this->namebase.$this->name.'-'.$j.'-';
  185.       }
  186.     }
  187.   }
  188.  
  189.   function display({
  190.     $tableclass (isset($this->tableclass" class='$this->tableclass'"");
  191.     $t  "<table title='$this->description$tableclass>\n";
  192.     $t .= $this->displayInTable($this->numcols);
  193.     $t .= "</table>\n";
  194.     return $t;
  195.   }
  196.  
  197.  
  198.   function displayInTable($numCols=3{
  199.     $totalCols $this->numExtraInfoCols + $this->numcols;
  200.     $t='';
  201.     if ($this->numExtraInfoCols = -1{
  202.       $this->numExtraInfoCols = count($this->formatter)-1;
  203.     }
  204.     if (isset($this->tableHeadings&& is_array($this->tableHeadings)) {
  205.       $t .= '<tr>';
  206.       foreach ($this->tableHeadings as $heading{
  207.         $t .= "<th>$heading</th>";
  208.       }
  209.       $t .= "</tr>\n";
  210.     }
  211.     $t  .= '<tr>';
  212.     for ($i=0$i<=$this->numExtraInfoCols$i++{
  213.       $t .= '<th></th>';
  214.     }
  215.     for ($i=0$i<$this->numcols$i++{
  216.       $t .= '<th>'.$this->checkboxes[$i]->longname.'</th>';
  217.     }
  218.     for ($i=$totalCols$i<=$numCols$i++{
  219.       $t .= '<th></th>';
  220.     }
  221.     $t .= '</tr>'."\n";
  222.     if (is_array($this->list->choicelist)) {
  223.       for ($j=0$j<count($this->list->choicelist)$j++{
  224.         $this->currentRow = $j;
  225.         $this->currentNumCols = $numCols $totalCols;
  226.         $t .= $this->format($this->list->choicelist[$j]);
  227.       }
  228.     }
  229.     // SelectAll/DeselectAll footer
  230.     if ($this->includeSelectAll{
  231.       $t .= '<tr>';
  232.       for ($i=0$i<=$this->numExtraInfoCols$i++{
  233.         $t .= '<td></td>';
  234.       }
  235.       for ($i=0$i<$this->numcols$i++{
  236.           $t .= '<td>'.$this->_getSelectAllFooter($i).'</td>';
  237.       }
  238.       for ($i=$totalCols$i<=$numCols$i++{
  239.         $t .= '<td></td>';
  240.       }
  241.       $t .= '</tr>'."\n";
  242.     }
  243.     if (is_array($this->footer&& count($this->footer)) {
  244.       $t .= '<tr>';
  245.       for ($i=0$i<=$this->numExtraInfoCols$i++{
  246.         $t .= '<td></td>';
  247.       }
  248.       for ($i=0$i<$this->numcols$i++{
  249.           $t .= '<td>'.sprintf($this->footer[$i]$i,
  250.                                               $i).'</td>';
  251.       }
  252.       for ($i=$totalCols$i<=$numCols$i++{
  253.         $t .= '<td></td>';
  254.       }
  255.       $t .= '</tr>'."\n";
  256.     }
  257.     return $t;
  258.   }
  259.  
  260.   /**
  261.   * create a pair of select/deselect all quick buttons or links
  262.   *
  263.   * @uses jsfunctions.php
  264.   * @param integer $col  the column number to run the quick select sequence on.
  265.   * @return string html for links
  266.   * @todo //TODO: include interface for groups of instruments.
  267.   */
  268.   function _getSelectAllFooter($col{
  269.     return '(<a href="#" onclick="return deselectsome(\''.$this->namebase.$this->name.'-\', '.$col.' ,'.$this->numcols.');">'.T_('deselect all').'</a>)<br />'
  270.         .'(<a href="#" onclick="return selectsome(\''.$this->namebase.$this->name.'-\', '.$col.' ,'.$this->numcols.');">'.T_('select all').'</a>)';
  271.   }
  272.  
  273.   /**
  274.   * PHP5 clone method
  275.   *
  276.   * PHP5 clone statement will perform only a shallow copy of the object. Any subobjects must also be cloned
  277.   */
  278.   function __clone({
  279.     //echo "cloning checkboxtablelist";
  280.     parent::__clone();
  281.     // Force a copy of various members
  282.     if (is_object($this->followHidden)) $this->followHidden = clone($this->followHidden);
  283.     if (is_object($this->hidden))       $this->hidden       = clone($this->hidden);
  284.     if (is_object($this->tableHeading)) $this->tableHeading = clone($this->tableHeading);
  285.     if (is_object($this->checkboxes))   $this->checkboxes   = clone($this->checkboxes);
  286.  
  287.     $bm $this->boxMatrix;
  288.     for ($i=0$i<count($bm)$i++{
  289.       $this->boxMatrix[$iarray();
  290.       for ($j=0$j<count($bm[$i])$j++{
  291.         $this->boxMatrix[$i][$j$bm[$i][$j];
  292.       }
  293.     }
  294.   }
  295.  
  296. // class CheckBoxTableList
  297.  
  298.  
  299. ?>

Documentation generated on Tue, 06 Mar 2007 10:01:03 +0000 by phpDocumentor 1.3.0