XOOPS  2.6.0
Select.php
Go to the documentation of this file.
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 
26 class Select extends Element
27 {
33  private $options = array();
34 
40  //private $multiple = false;
41 
47  //private $size;
48 
54  protected $value = array();
55 
61  private $optgroup = array();
62 
72  public function __construct($caption, $name, $value = null, $size = 1, $multiple = false)
73  {
74  $this->setCaption($caption);
75  $this->setAttribute('name', $name);
76  $this->setAttribute('size', intval($size));
77  if ($multiple) {
78  $this->setAttribute('multiple');
79  }
80  if (isset($value)) {
81  $this->setValue($value);
82  }
83  }
84 
90  public function isMultiple()
91  {
92  return $this->hasAttribute('multiple');
93  }
94 
100  public function getSize()
101  {
102  return (int) $this->getAttribute('size');
103  }
104 
113  public function addOption($value, $name = '')
114  {
115  if ($name != '') {
116  $this->options[$value] = $name;
117  } else {
118  $this->options[$value] = $value;
119  }
120  }
121 
129  public function addOptionArray($options)
130  {
131  if (is_array($options)) {
132  foreach ($options as $k => $v) {
133  $this->addOption($k, $v);
134  }
135  }
136  }
137 
146  public function addOptgroup($name, $optgroup)
147  {
148  $this->optgroup[$name] = $optgroup;
149  }
150 
164  public function getOptions($encode = false)
165  {
166  if (!$encode) {
167  return $this->options;
168  }
169  $value = array();
170  foreach ($this->options as $val => $name) {
171  $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1)
172  ? htmlspecialchars($name, ENT_QUOTES) : $name;
173  }
174  return $value;
175  }
176 
182  public function getOptgroup()
183  {
184  return $this->optgroup;
185  }
186 
192  public function render()
193  {
194  $ele_value = $this->getValue();
195  if (!is_array($ele_value)) {
196  $ele_value = (array) $ele_value;
197  }
198  $ele_options = $this->getOptions();
199  $ele_optgroup = $this->getOptgroup();
200 
201  $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
203  $ret = '<select ' . $attributes . $extra .' >' . NWLINE;
204 
205  if (empty($ele_optgroup)) {
206  foreach ($ele_options as $value => $name) {
207  $ret .= '<option value="' . htmlspecialchars($value, ENT_QUOTES) . '"';
208  if (count($ele_value) > 0 && in_array($value, $ele_value)) {
209  $ret .= ' selected="selected"';
210  }
211  $ret .= '>' . $name . '</option>' . NWLINE;
212  }
213  } else {
214  foreach ($ele_optgroup as $name_optgroup => $value_optgroup) {
215  $ret .= '<optgroup label="' . $name_optgroup . '">' . NWLINE;
216  foreach ($value_optgroup as $value => $name) {
217  $ret .= '<option value="' . htmlspecialchars($value, ENT_QUOTES) . '"';
218  if (count($ele_value) > 0 && in_array($value, $ele_value)) {
219  $ret .= ' selected="selected"';
220  }
221  $ret .= '>' . $name . '</option>' . NWLINE;
222  }
223  $ret .= '</optgroup>' . NWLINE;
224  }
225  }
226  $ret .= '</select>' . NWLINE;
227  return $ret;
228  }
229 
235  public function renderValidationJS()
236  {
237  // render custom validation code if any
238  if (!empty($this->customValidationCode)) {
239  return implode("\n", $this->customValidationCode);
240  // generate validation code if required
241  } elseif ($this->isRequired()) {
242  $eltname = $this->getName();
243  $eltcaption = $this->getCaption();
244  $eltmsg = empty($eltcaption)
245  ? sprintf(\XoopsLocale::F_ENTER, $eltname)
246  : sprintf(\XoopsLocale::F_ENTER, $eltcaption);
247  $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
248  return "\nvar hasSelected = false; var selectBox = myform.{$eltname};"
249  . "for (i = 0; i < selectBox.options.length; i++ ) { "
250  . "if (selectBox.options[i].selected == true && selectBox.options[i].value != '') "
251  . "{ hasSelected = true; break; } }" . "if (!hasSelected) "
252  . "{ window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
253  }
254  return '';
255  }
256 }
setValue($value)
Definition: Element.php:199
getValue($encode=false)
Definition: Element.php:180
setCaption($caption)
Definition: Element.php:396
const F_ENTER
Definition: en_US.php:408
setAttribute($name, $value=null)
Definition: Attributes.php:42
addOptionArray($options)
Definition: Select.php:129
__construct($caption, $name, $value=null, $size=1, $multiple=false)
Definition: Select.php:72
getExtra($encode=false)
Definition: Element.php:539
addOption($value, $name= '')
Definition: Select.php:113
getOptions($encode=false)
Definition: Select.php:164
addOptgroup($name, $optgroup)
Definition: Select.php:146