XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
formselect.php
Go to the documentation of this file.
1 <?php
21 defined('XOOPS_ROOT_PATH') or die('Restricted access');
22 
24 
36 class XoopsFormSelect extends XoopsFormElement
37 {
44  var $_options = array();
45 
52  var $_multiple = false;
53 
60  var $_size;
61 
68  var $_value = array();
69 
79  function XoopsFormSelect($caption, $name, $value = null, $size = 1, $multiple = false)
80  {
81  $this->setCaption($caption);
82  $this->setName($name);
83  $this->_multiple = $multiple;
84  $this->_size = intval($size);
85  if (isset($value)) {
86  $this->setValue($value);
87  }
88  }
89 
95  function isMultiple()
96  {
97  return $this->_multiple;
98  }
99 
105  function getSize()
106  {
107  return $this->_size;
108  }
109 
116  function getValue($encode = false)
117  {
118  if (! $encode) {
119  return $this->_value;
120  }
121  $value = array();
122  foreach($this->_value as $val) {
123  $value[] = $val ? htmlspecialchars($val, ENT_QUOTES) : $val;
124  }
125  return $value;
126  }
127 
133  function setValue($value)
134  {
135  if (is_array($value)) {
136  foreach($value as $v) {
137  $this->_value[] = $v;
138  }
139  } elseif (isset($value)) {
140  $this->_value[] = $value;
141  }
142  }
143 
150  function addOption($value, $name = '')
151  {
152  if ($name != '') {
153  $this->_options[$value] = $name;
154  } else {
155  $this->_options[$value] = $value;
156  }
157  }
158 
164  function addOptionArray($options)
165  {
166  if (is_array($options)) {
167  foreach($options as $k => $v) {
168  $this->addOption($k, $v);
169  }
170  }
171  }
172 
181  function getOptions($encode = false)
182  {
183  if (! $encode) {
184  return $this->_options;
185  }
186  $value = array();
187  foreach($this->_options as $val => $name) {
188  $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name;
189  }
190  return $value;
191  }
192 
198  function render()
199  {
200  $ele_name = $this->getName();
201  $ele_title = $this->getTitle();
202  $ele_value = $this->getValue();
203  $ele_options = $this->getOptions();
204  $ret = '<select size="' . $this->getSize() . '"' . $this->getExtra();
205  if ($this->isMultiple() != false) {
206  $ret .= ' name="' . $ele_name . '[]" id="' . $ele_name . '" title="'. $ele_title. '" multiple="multiple">' ;
207  } else {
208  $ret .= ' name="' . $ele_name . '" id="' . $ele_name . '" title="'. $ele_title. '">' ;
209  }
210  foreach($ele_options as $value => $name) {
211  $ret .= '<option value="' . htmlspecialchars($value, ENT_QUOTES) . '"';
212  if (count($ele_value) > 0 && in_array($value, $ele_value)) {
213  $ret .= ' selected="selected"';
214  }
215  $ret .= '>' . $name . '</option>' ;
216  }
217  $ret .= '</select>';
218  return $ret;
219  }
220 
226  function renderValidationJS()
227  {
228  // render custom validation code if any
229  if (! empty($this->customValidationCode)) {
230  return implode("\n", $this->customValidationCode);
231  // generate validation code if required
232  } elseif ($this->isRequired()) {
233  $eltname = $this->getName();
234  $eltcaption = $this->getCaption();
235  $eltmsg = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
236  $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
237  return "\nvar hasSelected = false; var selectBox = myform.{$eltname};" . "for (i = 0; i < selectBox.options.length; i++ ) { if (selectBox.options[i].selected == true && selectBox.options[i].value != '') { hasSelected = true; break; } }" . "if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
238  }
239  return '';
240  }
241 }
242 
243 ?>