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

Source for file SelectFilter.php

Documentation is available at SelectFilter.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Nicolas Hoizey <nicolas@hoizey.com>                         |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20.  
  21. require_once 'HTML/QuickForm.php';
  22. require_once 'HTML/QuickForm/text.php';
  23.  
  24. /**
  25.  * Class to dynamically create a filter for an HTML SELECT
  26.  *
  27.  * @author       Nicolas Hoizey <nicolas@hoizey.com>
  28.  * @version      1.0
  29.  * @since        PHP4.04pl1
  30.  * @access       public
  31.  */
  32. class HTML_QuickForm_SelectFilter extends HTML_QuickForm_text {
  33.     
  34.     // {{{ properties
  35.  
  36.     /**
  37.      * Contains the select targets
  38.      *
  39.      * @var       array 
  40.      * @since     1.0
  41.      * @access    private
  42.      */
  43.     var $_targets = array();
  44.  
  45.     // }}}
  46.     // {{{ constructor
  47.         
  48.     /**
  49.      * Class constructor
  50.      * 
  51.      * @param     string    $elementName    (optional)Input field name attribute
  52.      * @param     string    $elementLabel   (optional)Input field label
  53.      * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
  54.      *                                       or an associative array
  55.      * @param     string    $selectName     (optional)Target select field name
  56.      * @param     boolean   $mustBeginWith  (optional)Indicates wether the select
  57.      *                                       must begin with the filtered value or
  58.      *                                       can just contain it
  59.      * @since     1.0
  60.      * @access    public
  61.      * @return    void 
  62.      */
  63.     function HTML_QuickForm_SelectFilter($elementName = null$elementLabel = null$attributes = null$selectName = null$mustBeginWith = false)
  64.     {
  65.         $this->HTML_QuickForm_text($elementName$elementLabel$attributes);
  66.         $this->_type 'selectfilter';
  67.         if (!is_null($selectName)) {
  68.             $this->addSelect($selectName$mustBeginWith);
  69.         }
  70.     //end constructor
  71.     
  72.     // }}}
  73.     // {{{ addSelect()
  74.  
  75.     /**
  76.      * Adds a select as a target of the filter
  77.      * 
  78.      * @param     string    $selectName     Target select field name
  79.      * @param     boolean   $mustBeginWith  (optional)Indicates wether the select
  80.      *                                       must begin with the filtered value or
  81.      *                                       can just contain it
  82.      * @since     1.0
  83.      * @access    public
  84.      * @return    void 
  85.      */
  86.     function addSelect($selectName$mustBeginWith = false)
  87.     {
  88.         $this->_targets[$selectName$mustBeginWith;
  89.     //end func addSelect
  90.  
  91.     // }}}
  92.     // {{{ apiVersion()
  93.  
  94.     /**
  95.      * Returns the current API version
  96.      * 
  97.      * @since     1.0
  98.      * @access    public
  99.      * @return    double 
  100.      */
  101.     function apiVersion()
  102.     {
  103.         return 1.0;
  104.     //end func apiVersion
  105.  
  106.     // }}}
  107.     // {{{ toHtml()
  108.  
  109.     /**
  110.      * Returns the filter in HTML
  111.      * 
  112.      * @since     1.0
  113.      * @access    public
  114.      * @return    string 
  115.      */
  116.     function toHtml()
  117.     {
  118.         $onKeyUp '';
  119.         foreach($this->_targets as $target => $mustBeginWith{
  120.                $onKeyUp .= 'javascript:QF_SelectFilter(this.value, this.form[\''.$target.'\'], '.($mustBeginWith 'true' 'false').');';
  121.         }
  122.         $this->updateAttributes(array('onkeyup' => $onKeyUp));
  123.  
  124.         if ($this->_flagFrozen{
  125.             return $this->getFrozenHtml();
  126.         else {
  127.             $tabs $this->_getTabs();
  128.             $js '';
  129.             if (count($this->_targets> 0 && !defined('HTML_QUICKFORM_SELECTFILTER_EXISTS')) {
  130.                 $js '
  131. <script type="text/javascript">
  132. //<![CDATA[
  133. // begin javascript for filtered select
  134. var values = new Array();
  135.  
  136. function QF_SelectFilter(str, list, muststart) {
  137.     if (!values[list.name]) {
  138.         // first use of this list, we store initial elements
  139.         nb = list.options.length;
  140.         values[list.name] = new Array(nb);
  141.         for (i = 0; i < nb; i++) {
  142.             values[list.name][i] = list.options[i];
  143.         }
  144.     }
  145.  
  146.     // empty list
  147.     for(i = list.options.length - 1; i >= 0; i--) {
  148.         list.options[i] = null;
  149.     }
  150.  
  151.     // put needed elements
  152.     index = 0;
  153.     for (i = 0; i < nb; i++) {
  154.         if (str == ""
  155.                 || (muststart && values[list.name][i].text.toUpperCase().indexOf(str.toUpperCase()) == 0)
  156.                 || (!muststart && values[list.name][i].text.toUpperCase().indexOf(str.toUpperCase()) != -1)) {
  157.             list.options[index] = new Option(values[list.name][i].text, values[list.name][i].value);
  158.             index++;
  159.         }
  160.     }
  161.     if (index == 1) {
  162.         list.options[0].selected = true;
  163.     }
  164. }
  165. // end javascript for filtered select
  166. //]]>
  167. </script>
  168. ';
  169.                 define('HTML_QUICKFORM_SELECTFILTER_EXISTS'true);
  170.             }
  171.             return $js . parent::toHtml();
  172.         }
  173.     //end func toHtml
  174.  
  175.     // }}}
  176. //end class HTML_QuickForm_SelectFilter
  177.  
  178. if (class_exists('HTML_QuickForm')) {
  179.     HTML_QuickForm::registerElementType('SelectFilter''HTML/QuickForm/SelectFilter.php''HTML_QuickForm_SelectFilter');
  180. }
  181. ?>

Documentation generated on Mon, 11 Mar 2019 13:59:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.