Completed
Push — master ( 8d9a9b...cd572c )
by Richard
11s
created

XoopsFormRadio::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * XOOPS form radio compo
5
 *
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 *
13
 * @copyright    (c) 2000-2017 XOOPS Project (www.xoops.org)
14
 * @license          GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package          kernel
16
 * @since            2.0
17
 * @author           Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
18
 * @author           Taiwen Jiang <[email protected]>
19
 * @package          kernel
20
 * @subpackage       form
21
 * @todo             template
22
 */
23
class XoopsFormRadio extends XoopsFormElement
24
{
25
    /**
26
     * Array of Options
27
     *
28
     * @var array
29
     * @access private
30
     */
31
    public $_options = array();
32
33
    /**
34
     * Pre-selected value
35
     *
36
     * @var string
37
     * @access private
38
     */
39
    public $_value;
40
41
    /**
42
     * HTML to seperate the elements
43
     *
44
     * @var string
45
     * @access private
46
     */
47
    public $_delimeter;
48
49
    /**
50
     * Column number for rendering
51
     *
52
     * @var int
53
     * @access public
54
     */
55
    public $columns;
56
57
    /**
58
     * Constructor
59
     *
60
     * @param string $caption Caption
61
     * @param string $name    "name" attribute
62
     * @param string $value   Pre-selected value
63
     * @param string $delimeter
64
     */
65
    public function __construct($caption, $name, $value = null, $delimeter = '&nbsp;')
66
    {
67
        $this->setCaption($caption);
68
        $this->setName($name);
69
        if (isset($value)) {
70
            $this->setValue($value);
71
        }
72
        $this->_delimeter = $delimeter;
73
    }
74
75
    /**
76
     * Get the "value" attribute
77
     *
78
     * @param  bool $encode To sanitizer the text?
79
     * @return string
80
     */
81
    public function getValue($encode = false)
82
    {
83
        return ($encode && $this->_value !== null) ? htmlspecialchars($this->_value, ENT_QUOTES) : $this->_value;
84
    }
85
86
    /**
87
     * Set the pre-selected value
88
     *
89
     * @param  $value string
90
     */
91
    public function setValue($value)
92
    {
93
        $this->_value = $value;
94
    }
95
96
    /**
97
     * Add an option
98
     *
99
     * @param string $value "value" attribute - This gets submitted as form-data.
100
     * @param string $name  "name" attribute - This is displayed. If empty, we use the "value" instead.
101
     */
102 View Code Duplication
    public function addOption($value, $name = '')
103
    {
104
        if ($name != '') {
105
            $this->_options[$value] = $name;
106
        } else {
107
            $this->_options[$value] = $value;
108
        }
109
    }
110
111
    /**
112
     * Adds multiple options
113
     *
114
     * @param array $options Associative array of value->name pairs.
115
     */
116
    public function addOptionArray($options)
117
    {
118
        if (is_array($options)) {
119
            foreach ($options as $k => $v) {
120
                $this->addOption($k, $v);
121
            }
122
        }
123
    }
124
125
    /**
126
     * Get an array with all the options
127
     *
128
     * @param bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name
129
     *
130
     * @return array Associative array of value->name pairs
131
     */
132 View Code Duplication
    public function getOptions($encode = false)
133
    {
134
        if (!$encode) {
135
            return $this->_options;
136
        }
137
        $value = array();
138
        foreach ($this->_options as $val => $name) {
139
            $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name;
140
        }
141
142
        return $value;
143
    }
144
145
    /**
146
     * Get the delimiter of this group
147
     *
148
     * @param  bool $encode To sanitizer the text?
149
     * @return string The delimiter
150
     */
151
    public function getDelimeter($encode = false)
152
    {
153
        return $encode ? htmlspecialchars(str_replace('&nbsp;', ' ', $this->_delimeter)) : $this->_delimeter;
154
    }
155
156
    /**
157
     * Prepare HTML for output
158
     *
159
     * @return string HTML
160
     */
161
    public function render()
162
    {
163
        return XoopsFormRenderer::getInstance()->get()->renderFormRadio($this);
164
    }
165
}
166