1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Form;
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class Select extends OptionElement
26: {
27: 28: 29: 30: 31:
32: protected $value = array();
33:
34: 35: 36: 37: 38: 39: 40: 41: 42:
43: public function __construct($caption, $name = null, $value = null, $size = 1, $multiple = false)
44: {
45: if (is_array($caption)) {
46: parent::__construct($caption);
47: $this->setIfNotSet('size', 1);
48: } else {
49: $this->setWithDefaults('caption', $caption, '');
50: $this->setWithDefaults('name', $name, 'name_error');
51: $this->set('value', $value);
52: $this->setWithDefaults('size', $size, 1);
53: if ($multiple) {
54: $this->set('multiple');
55: }
56: }
57: }
58:
59: 60: 61: 62: 63:
64: public function isMultiple()
65: {
66: return $this->has('multiple');
67: }
68:
69: 70: 71: 72: 73:
74: public function getSize()
75: {
76: return (int) $this->get('size');
77: }
78:
79: 80: 81: 82: 83: 84: 85: 86:
87: public function addOptionGroup($name, $optgroup)
88: {
89: $this->setArrayItem('option', $name, $optgroup);
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99: 100:
101: protected function renderOption($optionValue, $optionDisplay, $selected)
102: {
103: $rendered = '<option value="' . htmlspecialchars($optionValue, ENT_QUOTES) . '"';
104: if (in_array($optionValue, $selected)) {
105: $rendered .= ' selected="selected"';
106: }
107: $rendered .= '>' . $optionDisplay . '</option>' . "\n";
108:
109: return $rendered;
110: }
111:
112: 113: 114: 115: 116:
117: public function render()
118: {
119: $selected = (array) $this->getValue();
120:
121: $ele_options = $this->getOptions();
122:
123: $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
124: $this->themeDecorateElement();
125: $attributes = $this->renderAttributeString();
126: $rendered = '<select ' . $attributes . $extra .' >' . "\n";
127:
128: if (empty($ele_optgroup)) {
129: foreach ($ele_options as $value => $display) {
130: if (is_array($display)) {
131: $rendered .= '<optgroup label="' . $value . '">' . "\n";
132: foreach ($display as $optvalue => $optdisplay) {
133: $rendered .= $this->renderOption($optvalue, $optdisplay, $selected);
134: }
135: } else {
136: $rendered .= $this->renderOption($value, $display, $selected);
137: }
138: }
139: }
140: $rendered .= '</select>' . "\n";
141:
142: return $rendered;
143: }
144:
145: 146: 147: 148: 149:
150: public function renderValidationJS()
151: {
152:
153: if (!empty($this->customValidationCode)) {
154: return implode("\n", $this->customValidationCode);
155:
156: } elseif ($this->isRequired()) {
157: $eltname = $this->getName();
158: $eltcaption = $this->getCaption();
159: $eltmsg = empty($eltcaption)
160: ? sprintf(\XoopsLocale::F_ENTER, $eltname)
161: : sprintf(\XoopsLocale::F_ENTER, $eltcaption);
162: $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
163: return "\nvar hasSelected = false; var selectBox = myform.{$eltname};"
164: . "for (i = 0; i < selectBox.options.length; i++ ) { "
165: . "if (selectBox.options[i].selected == true && selectBox.options[i].value != '') "
166: . "{ hasSelected = true; break; } }" . "if (!hasSelected) "
167: . "{ window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
168: }
169: return '';
170: }
171: }
172: