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

Source for file HtmlSelect.php

Documentation is available at HtmlSelect.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: I18Nv2 :: DecoratedList :: HtmlSelect                        |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2004 Michael Wallner <mike@iworks.at>                  |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: HtmlSelect.php,v 1.5 2004/05/03 15:01:28 mike Exp $
  15.  
  16. require_once 'I18Nv2/DecoratedList.php';
  17.  
  18. /**
  19. * I18Nv2_DecoratedList_HtmlSelect
  20. * 
  21. * Example:
  22. * <code>
  23. *   require_once 'I18Nv2/Country.php';
  24. *   require_once 'I18Nv2/DecoratedList/HtmlSelect.php';
  25. * 
  26. *   $country = &new I18Nv2_Country('de', 'iso-8859-1');
  27. *   $select  = &new I18Nv2_DecoratedList_HtmlSelect($country);
  28. *   $select->attributes['select']['name'] = 'country';
  29. *   $select->selected['DE'] = true;
  30. *   echo $select->getAllCodes();
  31. * </code>
  32. *
  33. * @author       Michael Wallner <mike@php.net>
  34. * @version      $Revision: 1.5 $
  35. * @package      I18Nv2
  36. */
  37. {
  38.     /**
  39.     * HTML attributes of the select and the option tags
  40.     * 
  41.     * <code>
  42.     * $HtmlSelect->attributes['select']['onchange'] = 'this.form.submit()';
  43.     * </code>
  44.     * 
  45.     * @access   public
  46.     * @var      array 
  47.     */
  48.     var $attributes = array(
  49.         'select' => array(
  50.             'size' => 1,
  51.         ),
  52.         'option' => array(
  53.         )
  54.     );
  55.     
  56.     /**
  57.     * Selected option(s)
  58.     * 
  59.     * <code>
  60.     * $HtmlSelect->selected[$code] = true;
  61.     * </code>
  62.     * 
  63.     * @access   public
  64.     * @var      array 
  65.     */
  66.     var $selected = array();
  67.     
  68.     /** 
  69.     * decorate
  70.     * 
  71.     * @access   protected
  72.     * @return   string 
  73.     * @param    mixed   $value 
  74.     */
  75.     function decorate($value)
  76.     {
  77.         static $codes;
  78.         
  79.         if (is_string($value)) {
  80.             if (!isset($codes)) {
  81.                 $codes = $this->list->getAllCodes();
  82.             }
  83.             $key = array_search($value, $codes);
  84.             return
  85.                 '<option ' . $this->_optAttr($key) . '>' . 
  86.                     $value .
  87.                 '</option>';
  88.         } elseif(is_array($value)) {
  89.             return 
  90.                 '<select ' . $this->_getAttr() . '>' . 
  91.                     implode('', array_map(array(&$this, 'decorate'), $value)) . 
  92.                 '</select>';
  93.         }
  94.         return $value;
  95.     }
  96.     
  97.     /**
  98.     * Get HTML attributes for the option tag
  99.     * 
  100.     * @access   private
  101.     * @return   string 
  102.     * @param    string  $key 
  103.     */
  104.     function _optAttr($key)
  105.     {
  106.         $attributes = 'value="' . $key . '" ' . $this->_getAttr('option');
  107.         if (isset($this->selected[$key]) && $this->selected[$key]) {
  108.             $attributes .= 'selected="selected"';
  109.         }
  110.         return $attributes;
  111.     }
  112.     
  113.     /**
  114.     * Get HTML attributes
  115.     * 
  116.     * @access   private
  117.     * @return   string 
  118.     * @param    string  $of 
  119.     */
  120.     function _getAttr($of = 'select')
  121.     {
  122.         $attributes = '';
  123.         foreach ($this->attributes[$of] as $attr => $value) {
  124.             $attributes .= $attr . '="' . $value .'" ';
  125.         }
  126.         return $attributes;
  127.     }
  128. }
  129. ?>

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