|
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
|
37 |
|
public function __construct($caption, $name = null, $value = null, $size = 1, $multiple = false) |
|
44
|
|
|
{ |
|
45
|
37 |
|
if (is_array($caption)) { |
|
46
|
11 |
|
parent::__construct($caption); |
|
47
|
11 |
|
$this->setIfNotSet('size', 1); |
|
48
|
|
|
} else { |
|
49
|
33 |
|
$this->setWithDefaults('caption', $caption, ''); |
|
50
|
33 |
|
$this->setWithDefaults('name', $name, 'name_error'); |
|
51
|
33 |
|
$this->set('value', $value); |
|
52
|
33 |
|
$this->setWithDefaults('size', $size, 1); |
|
53
|
33 |
|
if ($multiple) { |
|
54
|
1 |
|
$this->set('multiple'); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
37 |
|
} |
|
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
|
2 |
|
public function addOptionGroup($name, $optgroup) |
|
88
|
|
|
{ |
|
89
|
2 |
|
$this->setArrayItem('option', $name, $optgroup); |
|
90
|
2 |
|
} |
|
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
|
20 |
|
public function renderOption($optionValue, $optionDisplay, $selected) |
|
102
|
|
|
{ |
|
103
|
20 |
|
$rendered = '<option value="' . htmlspecialchars($optionValue, ENT_QUOTES) . '"'; |
|
104
|
20 |
|
if (in_array($optionValue, $selected)) { |
|
105
|
8 |
|
$rendered .= ' selected="selected"'; |
|
106
|
|
|
} |
|
107
|
20 |
|
$rendered .= '>' . $optionDisplay . '</option>' . "\n"; |
|
108
|
|
|
|
|
109
|
20 |
|
return $rendered; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* defaultRender |
|
114
|
|
|
* |
|
115
|
|
|
* @return string rendered form element |
|
116
|
|
|
*/ |
|
117
|
20 |
|
public function defaultRender() |
|
118
|
|
|
{ |
|
119
|
20 |
|
$selected = (array) $this->getValue(); |
|
120
|
|
|
|
|
121
|
20 |
|
$ele_options = $this->getOptions(); |
|
122
|
|
|
|
|
123
|
20 |
|
$extra = ($this->getExtra() != '' ? " " . $this->getExtra() : ''); |
|
124
|
20 |
|
$attributes = $this->renderAttributeString(); |
|
125
|
20 |
|
$rendered = '<select ' . $attributes . $extra .' >' . "\n"; |
|
126
|
20 |
|
|
|
127
|
|
|
if (empty($ele_optgroup)) { |
|
|
|
|
|
|
128
|
20 |
|
foreach ($ele_options as $value => $display) { |
|
129
|
20 |
|
if (is_array($display)) { |
|
130
|
20 |
|
$rendered .= '<optgroup label="' . $value . '">' . "\n"; |
|
131
|
1 |
|
foreach ($display as $optvalue => $optdisplay) { |
|
132
|
1 |
|
$rendered .= $this->renderOption($optvalue, $optdisplay, $selected); |
|
133
|
1 |
|
} |
|
134
|
|
|
} else { |
|
135
|
|
|
$rendered .= $this->renderOption($value, $display, $selected); |
|
136
|
20 |
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
$rendered .= '</select>' . "\n"; |
|
140
|
20 |
|
|
|
141
|
|
|
return $rendered; |
|
142
|
20 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Render custom javascript validation code |
|
146
|
|
|
* |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
public function renderValidationJS() |
|
150
|
2 |
|
{ |
|
151
|
|
|
// render custom validation code if any |
|
152
|
|
|
if (!empty($this->customValidationCode)) { |
|
153
|
2 |
|
return implode("\n", $this->customValidationCode); |
|
154
|
|
|
// generate validation code if required |
|
155
|
|
|
} elseif ($this->isRequired()) { |
|
156
|
2 |
|
$eltname = $this->getName(); |
|
157
|
1 |
|
$eltcaption = $this->getCaption(); |
|
158
|
1 |
|
$eltmsg = empty($eltcaption) |
|
159
|
1 |
|
? sprintf(\XoopsLocale::F_ENTER, $eltname) |
|
160
|
|
|
: sprintf(\XoopsLocale::F_ENTER, $eltcaption); |
|
161
|
1 |
|
$eltmsg = str_replace('"', '\"', stripslashes($eltmsg)); |
|
162
|
1 |
|
return "\nvar hasSelected = false; var selectBox = myform.{$eltname};" |
|
163
|
1 |
|
. "for (i = 0; i < selectBox.options.length; i++ ) { " |
|
164
|
1 |
|
. "if (selectBox.options[i].selected == true && selectBox.options[i].value != '') " |
|
165
|
1 |
|
. "{ hasSelected = true; break; } }" . "if (!hasSelected) " |
|
166
|
1 |
|
. "{ window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }"; |
|
167
|
1 |
|
} |
|
168
|
|
|
return ''; |
|
169
|
2 |
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|