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

Source for file select.php

Documentation is available at select.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: Adam Daniel <adaniel1@eesus.jnj.com>                        |
  17. // |          Bertrand Mansion <bmansion@mamasam.com>                     |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: select.php,v 1.29 2005/07/22 17:30:51 avb Exp $
  21.  
  22. require_once('HTML/QuickForm/element.php');
  23.  
  24. /**
  25.  * Class to dynamically create an HTML SELECT
  26.  *
  27.  * @author       Adam Daniel <adaniel1@eesus.jnj.com>
  28.  * @author       Bertrand Mansion <bmansion@mamasam.com>
  29.  * @version      1.0
  30.  * @since        PHP4.04pl1
  31.  * @access       public
  32.  */
  33.     
  34.     // {{{ properties
  35.  
  36.     /**
  37.      * Contains the select options
  38.      *
  39.      * @var       array 
  40.      * @since     1.0
  41.      * @access    private
  42.      */
  43.     var $_options = array();
  44.     
  45.     /**
  46.      * Default values of the SELECT
  47.      * 
  48.      * @var       string 
  49.      * @since     1.0
  50.      * @access    private
  51.      */
  52.     var $_values = null;
  53.  
  54.     // }}}
  55.     // {{{ constructor
  56.         
  57.     /**
  58.      * Class constructor
  59.      * 
  60.      * @param     string    Select name attribute
  61.      * @param     mixed     Label(s) for the select
  62.      * @param     mixed     Data to be used to populate options
  63.      * @param     mixed     Either a typical HTML attribute string or an associative array
  64.      * @since     1.0
  65.      * @access    public
  66.      * @return    void 
  67.      */
  68.     function HTML_QuickForm_select($elementName=null$elementLabel=null$options=null$attributes=null)
  69.     {
  70.         HTML_QuickForm_element::HTML_QuickForm_element($elementName$elementLabel$attributes);
  71.         $this->_persistantFreeze = true;
  72.         $this->_type 'select';
  73.         if (isset($options)) {
  74.             $this->load($options);
  75.         }
  76.     //end constructor
  77.     
  78.     // }}}
  79.     // {{{ apiVersion()
  80.  
  81.     /**
  82.      * Returns the current API version
  83.      * 
  84.      * @since     1.0
  85.      * @access    public
  86.      * @return    double 
  87.      */
  88.     function apiVersion()
  89.     {
  90.         return 2.3;
  91.     //end func apiVersion
  92.  
  93.     // }}}
  94.     // {{{ setSelected()
  95.  
  96.     /**
  97.      * Sets the default values of the select box
  98.      * 
  99.      * @param     mixed    $values  Array or comma delimited string of selected values
  100.      * @since     1.0
  101.      * @access    public
  102.      * @return    void 
  103.      */
  104.     function setSelected($values)
  105.     {
  106.         if (is_string($values&& $this->getMultiple()) {
  107.             $values split("[ ]?,[ ]?"$values);
  108.         }
  109.         if (is_array($values)) {
  110.             $this->_values array_values($values);
  111.         else {
  112.             $this->_values = array($values);
  113.         }
  114.     //end func setSelected
  115.     
  116.     // }}}
  117.     // {{{ getSelected()
  118.  
  119.     /**
  120.      * Returns an array of the selected values
  121.      * 
  122.      * @since     1.0
  123.      * @access    public
  124.      * @return    array of selected values
  125.      */
  126.     function getSelected()
  127.     {
  128.         return $this->_values;
  129.     // end func getSelected
  130.  
  131.     // }}}
  132.     // {{{ setName()
  133.  
  134.     /**
  135.      * Sets the input field name
  136.      * 
  137.      * @param     string    $name   Input field name attribute
  138.      * @since     1.0
  139.      * @access    public
  140.      * @return    void 
  141.      */
  142.     function setName($name)
  143.     {
  144.         $this->updateAttributes(array('name' => $name));
  145.     //end func setName
  146.     
  147.     // }}}
  148.     // {{{ getName()
  149.  
  150.     /**
  151.      * Returns the element name
  152.      * 
  153.      * @since     1.0
  154.      * @access    public
  155.      * @return    string 
  156.      */
  157.     function getName()
  158.     {
  159.         return $this->getAttribute('name');
  160.     //end func getName
  161.  
  162.     // }}}
  163.     // {{{ getPrivateName()
  164.  
  165.     /**
  166.      * Returns the element name (possibly with brackets appended)
  167.      * 
  168.      * @since     1.0
  169.      * @access    public
  170.      * @return    string 
  171.      */
  172.     function getPrivateName()
  173.     {
  174.         if ($this->getAttribute('multiple')) {
  175.             return $this->getName('[]';
  176.         else {
  177.             return $this->getName();
  178.         }
  179.     //end func getPrivateName
  180.  
  181.     // }}}
  182.     // {{{ setValue()
  183.  
  184.     /**
  185.      * Sets the value of the form element
  186.      *
  187.      * @param     mixed    $values  Array or comma delimited string of selected values
  188.      * @since     1.0
  189.      * @access    public
  190.      * @return    void 
  191.      */
  192.     function setValue($value)
  193.     {
  194.         $this->setSelected($value);
  195.     // end func setValue
  196.  
  197.     // }}}
  198.     // {{{ getValue()
  199.  
  200.     /**
  201.      * Returns an array of the selected values
  202.      * 
  203.      * @since     1.0
  204.      * @access    public
  205.      * @return    array of selected values
  206.      */
  207.     function getValue()
  208.     {
  209.         return $this->_values;
  210.     // end func getValue
  211.  
  212.     // }}}
  213.     // {{{ setSize()
  214.  
  215.     /**
  216.      * Sets the select field size, only applies to 'multiple' selects
  217.      * 
  218.      * @param     int    $size  Size of select  field
  219.      * @since     1.0
  220.      * @access    public
  221.      * @return    void 
  222.      */
  223.     function setSize($size)
  224.     {
  225.         $this->updateAttributes(array('size' => $size));
  226.     //end func setSize
  227.     
  228.     // }}}
  229.     // {{{ getSize()
  230.  
  231.     /**
  232.      * Returns the select field size
  233.      * 
  234.      * @since     1.0
  235.      * @access    public
  236.      * @return    int 
  237.      */
  238.     function getSize()
  239.     {
  240.         return $this->getAttribute('size');
  241.     //end func getSize
  242.  
  243.     // }}}
  244.     // {{{ setMultiple()
  245.  
  246.     /**
  247.      * Sets the select mutiple attribute
  248.      * 
  249.      * @param     bool    $multiple  Whether the select supports multi-selections
  250.      * @since     1.2
  251.      * @access    public
  252.      * @return    void 
  253.      */
  254.     function setMultiple($multiple)
  255.     {
  256.         if ($multiple{
  257.             $this->updateAttributes(array('multiple' => 'multiple'));
  258.         else {
  259.             $this->removeAttribute('multiple');
  260.         }
  261.     //end func setMultiple
  262.     
  263.     // }}}
  264.     // {{{ getMultiple()
  265.  
  266.     /**
  267.      * Returns the select mutiple attribute
  268.      * 
  269.      * @since     1.2
  270.      * @access    public
  271.      * @return    bool    true if multiple select, false otherwise
  272.      */
  273.     function getMultiple()
  274.     {
  275.         return (bool)$this->getAttribute('multiple');
  276.     //end func getMultiple
  277.  
  278.     // }}}
  279.     // {{{ addOption()
  280.  
  281.     /**
  282.      * Adds a new OPTION to the SELECT
  283.      *
  284.      * @param     string    $text       Display text for the OPTION
  285.      * @param     string    $value      Value for the OPTION
  286.      * @param     mixed     $attributes Either a typical HTML attribute string
  287.      *                                   or an associative array
  288.      * @since     1.0
  289.      * @access    public
  290.      * @return    void 
  291.      */
  292.     function addOption($text$value$attributes=null)
  293.     {
  294.         if (null === $attributes{
  295.             $attributes = array('value' => $value);
  296.         else {
  297.             $attributes $this->_parseAttributes($attributes);
  298.             if (isset($attributes['selected'])) {
  299.                 // the 'selected' attribute will be set in toHtml()
  300.                 $this->_removeAttr('selected'$attributes);
  301.                 if (is_null($this->_values)) {
  302.                     $this->_values = array($value);
  303.                 elseif (!in_array($value$this->_values)) {
  304.                     $this->_values[$value;
  305.                 }
  306.             }
  307.             $this->_updateAttrArray($attributesarray('value' => $value));
  308.         }
  309.         $this->_options[= array('text' => $text'attr' => $attributes);
  310.     // end func addOption
  311.     
  312.     // }}}
  313.     // {{{ loadArray()
  314.  
  315.     /**
  316.      * Loads the options from an associative array
  317.      * 
  318.      * @param     array    $arr     Associative array of options
  319.      * @param     mixed    $values  (optional) Array or comma delimited string of selected values
  320.      * @since     1.0
  321.      * @access    public
  322.      * @return    PEAR_Error on error or true
  323.      * @throws    PEAR_Error
  324.      */
  325.     function loadArray($arr$values=null)
  326.     {
  327.         if (!is_array($arr)) {
  328.             return PEAR::raiseError('Argument 1 of HTML_Select::loadArray is not a valid array');
  329.         }
  330.         if (isset($values)) {
  331.             $this->setSelected($values);
  332.         }
  333.         foreach ($arr as $key => $val{
  334.             // Warning: new API since release 2.3
  335.             $this->addOption($val$key);
  336.         }
  337.         return true;
  338.     // end func loadArray
  339.  
  340.     // }}}
  341.     // {{{ loadDbResult()
  342.  
  343.     /**
  344.      * Loads the options from DB_result object
  345.      * 
  346.      * If no column names are specified the first two columns of the result are
  347.      * used as the text and value columns respectively
  348.      * @param     object    $result     DB_result object
  349.      * @param     string    $textCol    (optional) Name of column to display as the OPTION text
  350.      * @param     string    $valueCol   (optional) Name of column to use as the OPTION value
  351.      * @param     mixed     $values     (optional) Array or comma delimited string of selected values
  352.      * @since     1.0
  353.      * @access    public
  354.      * @return    PEAR_Error on error or true
  355.      * @throws    PEAR_Error
  356.      */
  357.     function loadDbResult(&$result$textCol=null$valueCol=null$values=null)
  358.     {
  359.         if (!is_object($result|| !is_a($result'db_result')) {
  360.             return PEAR::raiseError('Argument 1 of HTML_Select::loadDbResult is not a valid DB_result');
  361.         }
  362.         if (isset($values)) {
  363.             $this->setValue($values);
  364.         }
  365.         $fetchMode ($textCol && $valueCol? DB_FETCHMODE_ASSOC : DB_FETCHMODE_DEFAULT;
  366.         while (is_array($row $result->fetchRow($fetchMode)) ) {
  367.             if ($fetchMode == DB_FETCHMODE_ASSOC{
  368.                 $this->addOption($row[$textCol]$row[$valueCol]);
  369.             else {
  370.                 $this->addOption($row[0]$row[1]);
  371.             }
  372.         }
  373.         return true;
  374.     // end func loadDbResult
  375.     
  376.     // }}}
  377.     // {{{ loadQuery()
  378.  
  379.     /**
  380.      * Queries a database and loads the options from the results
  381.      *
  382.      * @param     mixed     $conn       Either an existing DB connection or a valid dsn
  383.      * @param     string    $sql        SQL query string
  384.      * @param     string    $textCol    (optional) Name of column to display as the OPTION text
  385.      * @param     string    $valueCol   (optional) Name of column to use as the OPTION value
  386.      * @param     mixed     $values     (optional) Array or comma delimited string of selected values
  387.      * @since     1.1
  388.      * @access    public
  389.      * @return    void 
  390.      * @throws    PEAR_Error
  391.      */
  392.     function loadQuery(&$conn$sql$textCol=null$valueCol=null$values=null)
  393.     {
  394.         if (is_string($conn)) {
  395.             require_once('DB.php');
  396.             $dbConn &DB::connect($conntrue);
  397.             if (DB::isError($dbConn)) {
  398.                 return $dbConn;
  399.             }
  400.         elseif (is_subclass_of($conn"db_common")) {
  401.             $dbConn &$conn;
  402.         else {
  403.             return PEAR::raiseError('Argument 1 of HTML_Select::loadQuery is not a valid type');
  404.         }
  405.         $result $dbConn->query($sql);
  406.         if (DB::isError($result)) {
  407.             return $result;
  408.         }
  409.         $this->loadDbResult($result$textCol$valueCol$values);
  410.         $result->free();
  411.         if (is_string($conn)) {
  412.             $dbConn->disconnect();
  413.         }
  414.         return true;
  415.     // end func loadQuery
  416.  
  417.     // }}}
  418.     // {{{ load()
  419.  
  420.     /**
  421.      * Loads options from different types of data sources
  422.      *
  423.      * This method is a simulated overloaded method.  The arguments, other than the
  424.      * first are optional and only mean something depending on the type of the first argument.
  425.      * If the first argument is an array then all arguments are passed in order to loadArray.
  426.      * If the first argument is a db_result then all arguments are passed in order to loadDbResult.
  427.      * If the first argument is a string or a DB connection then all arguments are
  428.      * passed in order to loadQuery.
  429.      * @param     mixed     $options     Options source currently supports assoc array or DB_result
  430.      * @param     mixed     $param1     (optional) See function detail
  431.      * @param     mixed     $param2     (optional) See function detail
  432.      * @param     mixed     $param3     (optional) See function detail
  433.      * @param     mixed     $param4     (optional) See function detail
  434.      * @since     1.1
  435.      * @access    public
  436.      * @return    PEAR_Error on error or true
  437.      * @throws    PEAR_Error
  438.      */
  439.     function load(&$options$param1=null$param2=null$param3=null$param4=null)
  440.     {
  441.         switch (true{
  442.             case is_array($options):
  443.                 return $this->loadArray($options$param1);
  444.                 break;
  445.             case (is_a($options'db_result')):
  446.                 return $this->loadDbResult($options$param1$param2$param3);
  447.                 break;
  448.             case (is_string($options&& !empty($options|| is_subclass_of($options"db_common")):
  449.                 return $this->loadQuery($options$param1$param2$param3$param4);
  450.                 break;
  451.         }
  452.     // end func load
  453.     
  454.     // }}}
  455.     // {{{ toHtml()
  456.  
  457.     /**
  458.      * Returns the SELECT in HTML
  459.      *
  460.      * @since     1.0
  461.      * @access    public
  462.      * @return    string 
  463.      */
  464.     function toHtml()
  465.     {
  466.         if ($this->_flagFrozen{
  467.             return $this->getFrozenHtml();
  468.         else {
  469.             $tabs    $this->_getTabs();
  470.             $strHtml '';
  471.  
  472.             if ($this->getComment(!= ''{
  473.                 $strHtml .= $tabs '<!-- ' $this->getComment(" //-->\n";
  474.             }
  475.  
  476.             if (!$this->getMultiple()) {
  477.                 $attrString $this->_getAttrString($this->_attributes);
  478.             else {
  479.                 $myName $this->getName();
  480.                 $this->setName($myName '[]');
  481.                 $attrString $this->_getAttrString($this->_attributes);
  482.                 $this->setName($myName);
  483.             }
  484.             $strHtml .= $tabs '<select' $attrString ">\n";
  485.  
  486.             foreach ($this->_options as $option{
  487.                 if (is_array($this->_values&& in_array((string)$option['attr']['value']$this->_values)) {
  488.                     $this->_updateAttrArray($option['attr']array('selected' => 'selected'));
  489.                 }
  490.                 $strHtml .= $tabs "\t<option" $this->_getAttrString($option['attr']'>' .
  491.                             $option['text'"</option>\n";
  492.             }
  493.  
  494.             return $strHtml $tabs '</select>';
  495.         }
  496.     //end func toHtml
  497.     
  498.     // }}}
  499.     // {{{ getFrozenHtml()
  500.  
  501.     /**
  502.      * Returns the value of field without HTML tags
  503.      * 
  504.      * @since     1.0
  505.      * @access    public
  506.      * @return    string 
  507.      */
  508.     function getFrozenHtml()
  509.     {
  510.         $value = array();
  511.         if (is_array($this->_values)) {
  512.             foreach ($this->_values as $key => $val{
  513.                 for ($i = 0$optCount count($this->_options)$i $optCount$i++{
  514.                     if ((string)$val == (string)$this->_options[$i]['attr']['value']{
  515.                         $value[$key$this->_options[$i]['text'];
  516.                         break;
  517.                     }
  518.                 }
  519.             }
  520.         }
  521.         $html = empty($value)'&nbsp;'join('<br />'$value);
  522.         if ($this->_persistantFreeze{
  523.             $name $this->getPrivateName();
  524.             // Only use id attribute if doing single hidden input
  525.             if (1 == count($value)) {
  526.                 $id     $this->getAttribute('id');
  527.                 $idAttr = isset($id)? array('id' => $id): array();
  528.             else {
  529.                 $idAttr = array();
  530.             }
  531.             foreach ($value as $key => $item{
  532.                 $html .= '<input' $this->_getAttrString(array(
  533.                              'type'  => 'hidden',
  534.                              'name'  => $name,
  535.                              'value' => $this->_values[$key]
  536.                          $idAttr' />';
  537.             }
  538.         }
  539.         return $html;
  540.     //end func getFrozenHtml
  541.  
  542.     // }}}
  543.     // {{{ exportValue()
  544.  
  545.    /**
  546.     * We check the options and return only the values that _could_ have been
  547.     * selected. We also return a scalar value if select is not "multiple"
  548.     */
  549.     function exportValue(&$submitValues$assoc = false)
  550.     {
  551.         $value $this->_findValue($submitValues);
  552.         if (is_null($value)) {
  553.             $value $this->getValue();
  554.         elseif(!is_array($value)) {
  555.             $value = array($value);
  556.         }
  557.         if (is_array($value&& !empty($this->_options)) {
  558.             $cleanValue = null;
  559.             foreach ($value as $v{
  560.                 for ($i = 0$optCount count($this->_options)$i $optCount$i++{
  561.                     if ($v == $this->_options[$i]['attr']['value']{
  562.                         $cleanValue[$v;
  563.                         break;
  564.                     }
  565.                 }
  566.             }
  567.         else {
  568.             $cleanValue $value;
  569.         }
  570.         if (is_array($cleanValue&& !$this->getMultiple()) {
  571.             return $this->_prepareValue($cleanValue[0]$assoc);
  572.         else {
  573.             return $this->_prepareValue($cleanValue$assoc);
  574.         }
  575.     }
  576.     
  577.     // }}}
  578.     // {{{ onQuickFormEvent()
  579.  
  580.     function onQuickFormEvent($event$arg&$caller)
  581.     {
  582.         if ('updateValue' == $event{
  583.             $value $this->_findValue($caller->_constantValues);
  584.             if (null === $value{
  585.                 $value $this->_findValue($caller->_submitValues);
  586.                 // Fix for bug #4465
  587.                 // XXX: should we push this to element::onQuickFormEvent()?
  588.                 if (null === $value && !$caller->isSubmitted()) {
  589.                     $value $this->_findValue($caller->_defaultValues);
  590.                 }
  591.             }
  592.             if (null !== $value{
  593.                 $this->setValue($value);
  594.             }
  595.             return true;
  596.         else {
  597.             return parent::onQuickFormEvent($event$arg$caller);
  598.         }
  599.     }
  600.  
  601.     // }}}
  602. //end class HTML_QuickForm_select
  603. ?>

Documentation generated on Mon, 11 Mar 2019 14:16:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.