Subversion Repositories Applications.papyrus

Rev

Rev 1688 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1463 alexandre_ 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.1 2007-06-25 09:55:28 alexandre_tb Exp $
15
 
16
/**
17
 * I18Nv2::DecoratedList::HtmlSelect
18
 *
19
 * @package     I18Nv2
20
 * @category    Internationalization
21
 */
22
 
23
require_once 'I18Nv2/DecoratedList.php';
24
 
25
/**
26
 * I18Nv2_DecoratedList_HtmlSelect
27
 *
28
 * Example:
29
 * <code>
30
 *   require_once 'I18Nv2/Country.php';
31
 *   require_once 'I18Nv2/DecoratedList/HtmlSelect.php';
32
 *
33
 *   $country = &new I18Nv2_Country('de', 'iso-8859-1');
34
 *   $select  = &new I18Nv2_DecoratedList_HtmlSelect($country);
35
 *   $select->attributes['select']['name'] = 'country';
36
 *   $select->selected['DE'] = true;
37
 *   echo $select->getAllCodes();
38
 * </code>
39
 *
40
 * @author      Michael Wallner <mike@php.net>
41
 * @version     $Revision: 1.1 $
42
 * @package     I18Nv2
43
 * @access      public
44
 */
45
class I18Nv2_DecoratedList_HtmlSelect extends I18Nv2_DecoratedList
46
{
47
    /**
48
     * HTML attributes of the select and the option tags
49
     *
50
     * <code>
51
     * $HtmlSelect->attributes['select']['onchange'] = 'this.form.submit()';
52
     * </code>
53
     *
54
     * @access  public
55
     * @var     array
56
     */
57
    var $attributes = array(
58
        'select' => array(
59
            'size' => 1,
60
        ),
61
        'option' => array(
62
        )
63
    );
64
 
65
    /**
66
     * Selected option(s)
67
     *
68
     * <code>
69
     * $HtmlSelect->selected[$code] = true;
70
     * </code>
71
     *
72
     * @access  public
73
     * @var     array
74
     */
75
    var $selected = array();
76
 
77
    /**
78
     * decorate
79
     *
80
     * @access  protected
81
     * @return  string
82
     * @param   mixed   $value
83
     */
84
    function decorate($value)
85
    {
86
        static $codes;
87
 
88
        if (is_scalar($value)) {
89
            if (!isset($codes)) {
90
                $codes = $this->list->getAllCodes();
91
            }
92
            $key = array_search($value, $codes);
93
            return
94
                '<option ' . $this->_optAttr($key) . '>' .
95
                    $value .
96
                '</option>';
97
        } elseif(is_array($value)) {
98
            return
99
                '<select ' . $this->_getAttr() . '>' .
100
                    implode('', array_map(array(&$this, 'decorate'), $value)) .
101
                '</select>';
102
        }
103
        return $value;
104
    }
105
 
106
    /**
107
     * Get HTML attributes for the option tag
108
     *
109
     * @access  private
110
     * @return  string
111
     * @param   string  $key
112
     */
113
    function _optAttr($key)
114
    {
115
        $attributes = 'value="' . $key . '" ' . $this->_getAttr('option');
116
        if (isset($this->selected[$key]) && $this->selected[$key]) {
117
            $attributes .= 'selected="selected"';
118
        }
119
        return $attributes;
120
    }
121
 
122
    /**
123
     * Get HTML attributes
124
     *
125
     * @access  private
126
     * @return  string
127
     * @param   string  $of
128
     */
129
    function _getAttr($of = 'select')
130
    {
131
        $attributes = '';
132
        foreach ($this->attributes[$of] as $attr => $value) {
133
            $attributes .= $attr . '="' . $value .'" ';
134
        }
135
        return $attributes;
136
    }
137
}
138
?>