1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xoops\Form; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Select - a select element |
16
|
|
|
* |
17
|
|
|
* @category Xoops\Form\Select |
18
|
|
|
* @package Xoops\Form |
19
|
|
|
* @author Kazumi Ono <[email protected]> |
20
|
|
|
* @author Taiwen Jiang <[email protected]> |
21
|
|
|
* @copyright 2001-2015 XOOPS Project (http://xoops.org) |
22
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
23
|
|
|
* @link http://xoops.org |
24
|
|
|
*/ |
25
|
|
|
class Select extends OptionElement |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Pre-selected values |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $value = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor |
36
|
|
|
* |
37
|
|
|
* @param string|array $caption Caption or array of all attributes |
38
|
|
|
* @param string $name name" attribute |
39
|
|
|
* @param mixed $value Pre-selected value (or array of them). |
40
|
|
|
* @param integer $size Number or rows. "1" makes a drop-down-list |
41
|
|
|
* @param boolean $multiple Allow multiple selections? |
42
|
|
|
*/ |
43
|
12 |
|
public function __construct($caption, $name = null, $value = null, $size = 1, $multiple = false) |
44
|
|
|
{ |
45
|
12 |
|
if (is_array($caption)) { |
46
|
2 |
|
parent::__construct($caption); |
47
|
2 |
|
$this->setIfNotSet('size', 1); |
48
|
|
|
} else { |
49
|
11 |
|
$this->setWithDefaults('caption', $caption, ''); |
50
|
11 |
|
$this->setWithDefaults('name', $name, 'name_error'); |
51
|
11 |
|
$this->set('value', $value); |
52
|
11 |
|
$this->setWithDefaults('size', $size, 1); |
53
|
11 |
|
if ($multiple) { |
54
|
|
|
$this->set('multiple'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
12 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Are multiple selections allowed? |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
1 |
|
public function isMultiple() |
65
|
|
|
{ |
66
|
1 |
|
return $this->has('multiple'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the size |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
1 |
|
public function getSize() |
75
|
|
|
{ |
76
|
1 |
|
return (int) $this->get('size'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Add multiple optgroup |
81
|
|
|
* |
82
|
|
|
* @param string $name name attribute |
83
|
|
|
* @param array $optgroup Associative array of value->name pairs |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
1 |
|
public function addOptionGroup($name, $optgroup) |
88
|
|
|
{ |
89
|
1 |
|
$this->setArrayItem('option', $name, $optgroup); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* render a single option |
94
|
|
|
* |
95
|
|
|
* @param string $optionValue option element value |
96
|
|
|
* @param string $optionDisplay displayed text |
97
|
|
|
* @param string[] $selected selected option values |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
2 |
|
protected function renderOption($optionValue, $optionDisplay, $selected) |
102
|
|
|
{ |
103
|
2 |
|
$rendered = '<option value="' . htmlspecialchars($optionValue, ENT_QUOTES) . '"'; |
104
|
2 |
|
if (in_array($optionValue, $selected)) { |
105
|
1 |
|
$rendered .= ' selected="selected"'; |
106
|
|
|
} |
107
|
2 |
|
$rendered .= '>' . $optionDisplay . '</option>' . "\n"; |
108
|
|
|
|
109
|
2 |
|
return $rendered; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Prepare HTML for output |
114
|
|
|
* |
115
|
|
|
* @return string HTML |
116
|
|
|
*/ |
117
|
16 |
|
public function render() |
118
|
|
|
{ |
119
|
16 |
|
$selected = (array) $this->getValue(); |
120
|
|
|
|
121
|
16 |
|
$ele_options = $this->getOptions(); |
122
|
|
|
|
123
|
16 |
|
$extra = ($this->getExtra() != '' ? " " . $this->getExtra() : ''); |
124
|
16 |
|
$this->themeDecorateElement(); |
125
|
16 |
|
$attributes = $this->renderAttributeString(); |
126
|
16 |
|
$rendered = '<select ' . $attributes . $extra .' >' . "\n"; |
127
|
|
|
|
128
|
16 |
|
if (empty($ele_optgroup)) { |
|
|
|
|
129
|
16 |
|
foreach ($ele_options as $value => $display) { |
130
|
16 |
|
if (is_array($display)) { |
131
|
1 |
|
$rendered .= '<optgroup label="' . $value . '">' . "\n"; |
132
|
1 |
|
foreach ($display as $optvalue => $optdisplay) { |
133
|
1 |
|
$rendered .= $this->renderOption($optvalue, $optdisplay, $selected); |
134
|
|
|
} |
135
|
|
|
} else { |
136
|
16 |
|
$rendered .= $this->renderOption($value, $display, $selected); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
16 |
|
$rendered .= '</select>' . "\n"; |
141
|
|
|
|
142
|
16 |
|
return $rendered; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Render custom javascript validation code |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
2 |
View Code Duplication |
public function renderValidationJS() |
151
|
|
|
{ |
152
|
|
|
// render custom validation code if any |
153
|
2 |
|
if (!empty($this->customValidationCode)) { |
154
|
|
|
return implode("\n", $this->customValidationCode); |
155
|
|
|
// generate validation code if required |
156
|
2 |
|
} elseif ($this->isRequired()) { |
157
|
1 |
|
$eltname = $this->getName(); |
158
|
1 |
|
$eltcaption = $this->getCaption(); |
159
|
1 |
|
$eltmsg = empty($eltcaption) |
160
|
|
|
? sprintf(\XoopsLocale::F_ENTER, $eltname) |
161
|
1 |
|
: sprintf(\XoopsLocale::F_ENTER, $eltcaption); |
162
|
1 |
|
$eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
163
|
1 |
|
return "\nvar hasSelected = false; var selectBox = myform.{$eltname};" |
164
|
1 |
|
. "for (i = 0; i < selectBox.options.length; i++ ) { " |
165
|
1 |
|
. "if (selectBox.options[i].selected == true && selectBox.options[i].value != '') " |
166
|
1 |
|
. "{ hasSelected = true; break; } }" . "if (!hasSelected) " |
167
|
1 |
|
. "{ window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
168
|
|
|
} |
169
|
2 |
|
return ''; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.