XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
formcheckbox.php
Go to the documentation of this file.
1 <?php
21 defined('XOOPS_ROOT_PATH') or die('Restricted access');
22 
24 
25 class XoopsFormCheckBox extends XoopsFormElement
26 {
33  var $_options = array();
34 
41  var $_value = array();
42 
50 
57  var $columns;
58 
66  function XoopsFormCheckBox($caption, $name, $value = null, $delimeter = '&nbsp;')
67  {
68  $this->setCaption($caption);
69  $this->setName($name);
70  if (isset($value)) {
71  $this->setValue($value);
72  }
73  $this->_delimeter = $delimeter;
74  $this->setFormType('checkbox');
75  }
76 
83  function getValue($encode = false)
84  {
85  if (! $encode) {
86  return $this->_value;
87  }
88  $value = array();
89  foreach($this->_value as $val) {
90  $value[] = $val ? htmlspecialchars($val, ENT_QUOTES) : $val;
91  }
92  return $value;
93  }
94 
100  function setValue($value)
101  {
102  $this->_value = array();
103  if (is_array($value)) {
104  foreach($value as $v) {
105  $this->_value[] = $v;
106  }
107  } else {
108  $this->_value[] = $value;
109  }
110  }
111 
118  function addOption($value, $name = '')
119  {
120  if ($name != '') {
121  $this->_options[$value] = $name;
122  } else {
123  $this->_options[$value] = $value;
124  }
125  }
126 
132  function addOptionArray($options)
133  {
134  if (is_array($options)) {
135  foreach($options as $k => $v) {
136  $this->addOption($k, $v);
137  }
138  }
139  }
140 
147  function getOptions($encode = false)
148  {
149  if (! $encode) {
150  return $this->_options;
151  }
152  $value = array();
153  foreach($this->_options as $val => $name) {
154  $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name;
155  }
156  return $value;
157  }
158 
165  function getDelimeter($encode = false)
166  {
167  return $encode ? htmlspecialchars(str_replace('&nbsp;', ' ', $this->_delimeter)) : $this->_delimeter;
168  }
169 
175  function render()
176  {
177  $ele_name = $this->getName();
178  $ele_title = $this->getTitle();
179  $ele_id = $ele_name;
180  $ele_value = $this->getValue();
181  $ele_options = $this->getOptions();
182  $ele_extra = $this->getExtra();
183  $ele_delimeter = empty($this->columns) ? $this->getDelimeter() : '';
184 
185  if (count($ele_options) > 1 && substr($ele_name, - 2, 2) != '[]') {
186  $ele_name = $ele_name . '[]';
187  $this->setName($ele_name);
188  }
189  $ret = '';
190  if (! empty($this->columns)) {
191  $ret .= '<table><tr>';
192  }
193  $i = 0;
194  $id_ele = 0;
195  foreach($ele_options as $value => $name) {
196  $id_ele ++;
197  if (! empty($this->columns)) {
198  if ($i % $this->columns == 0) {
199  $ret .= '<tr>';
200  }
201  $ret .= '<td>';
202  }
203  // $name may be a link, should we use $name in the title tag?
204  $ret .= "<input type='checkbox' name='{$ele_name}' id='{$ele_id}{$id_ele}' title='" . $ele_title . "' value='" . htmlspecialchars($value, ENT_QUOTES) . "'";
205  if (count($ele_value) > 0 && in_array($value, $ele_value)) {
206  $ret .= ' checked="checked"';
207  }
208  $ret .= $ele_extra . ' />'."<label name='xolb_{$ele_name}' for='{$ele_id}{$id_ele}'>" . $name . "</label>" . $ele_delimeter ;
209  if (! empty($this->columns)) {
210  $ret .= '</td>';
211  if (++ $i % $this->columns == 0) {
212  $ret .= '</tr>';
213  }
214  }
215  }
216  if (! empty($this->columns)) {
217  if ($span = $i % $this->columns) {
218  $ret .= '<td colspan="' . ($this->columns - $span) . '"></td></tr>';
219  }
220  $ret .= '</table>';
221  }
222  return $ret;
223  }
224 
230  function renderValidationJS()
231  {
232  // render custom validation code if any
233  if (! empty($this->customValidationCode)) {
234  return implode(NWLINE, $this->customValidationCode);
235  // generate validation code if required
236  } elseif ($this->isRequired()) {
237  $eltname = $this->getName();
238  $eltcaption = $this->getCaption();
239  $eltmsg = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
240  $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
241  return NWLINE . "var hasChecked = false; var checkBox = myform.elements['{$eltname}']; if (checkBox.length) {for (var i = 0; i < checkBox.length; i++) {if (checkBox[i].checked == true) {hasChecked = true; break;}}}else{if (checkBox.checked == true) {hasChecked = true;}}if (!hasChecked) {window.alert(\"{$eltmsg}\");if (checkBox.length) {checkBox[0].focus();}else{checkBox.focus();}return false;}";
242  }
243  return '';
244  }
245 }
246 
247 ?>