XOOPS  2.6.0
Checkbox.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 
27 class Checkbox extends Element
28 {
34  private $options = array();
35 
41  protected $value = array();
42 
48  private $inline;
49 
58  public function __construct($caption, $name, $value = null, $inline = true)
59  {
60  $this->setAttribute('type', 'checkbox');
61  $this->setAttribute('name', $name);
62  $this->setCaption($caption);
63  if (isset($value)) {
64  $this->setValue($value);
65  }
66  $this->inline = $inline;
67  }
68 
77  public function addOption($value, $name = '')
78  {
79  if ($name != '') {
80  $this->options[$value] = $name;
81  } else {
82  $this->options[$value] = $value;
83  }
84  }
85 
93  public function addOptionArray($options)
94  {
95  if (is_array($options)) {
96  foreach ($options as $k => $v) {
97  $this->addOption($k, $v);
98  }
99  }
100  }
101 
112  public function getOptions($encode = 0)
113  {
114  if (!$encode) {
115  return $this->options;
116  }
117  $value = array();
118  foreach ($this->options as $val => $name) {
119  $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1)
120  ? htmlspecialchars($name, ENT_QUOTES) : $name;
121  }
122  return $value;
123  }
124 
130  public function getInline()
131  {
132  if ($this->inline == true) {
133  return ' inline';
134  } else {
135  return '';
136  }
137  }
138 
144  public function render()
145  {
146  $required = $this->hasAttribute('required');
147  $ele_options = $this->getOptions();
148  $ele_value = $this->getValue();
149  if (!is_array($ele_value)) {
150  $ele_value = (array) $ele_value;
151  }
152  $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
153 
154  $ele_name = $this->getName();
155  $ele_id = $ele_name;
156  if (count($ele_options) > 1 && substr($ele_name, -2, 2) != '[]') {
157  $ele_name = $ele_name . '[]';
158  $this->setName($ele_name);
159  // If required is set, all checkboxes will be required by the browser,
160  // which is not usually useful. We stash the value of required above
161  // and unset now. We restore it before return so JS validation will still
162  // be triggered. This is only a problem if there is more than one checkbox.
163  $this->unsetAttribute('required');
164  }
165 
166  $ret = "";
167  $id_ele = 0;
168  foreach ($ele_options as $value => $name) {
169  $this->unsetAttribute('checked');
170  if (!empty($ele_value) && in_array($value, $ele_value)) {
171  $this->setAttribute('checked');
172  }
173  $this->setAttribute('value', $value);
174  ++$id_ele;
175  $this->setAttribute('id', $ele_id . $id_ele);
176  $ret .= '<label class="checkbox' . $this->getInline() . '">' . NWLINE;
177  $ret .= '<input ' . $this->renderAttributeString() . $extra . '>' . NWLINE;
178  $ret .= $name . NWLINE;
179  $ret .= "</label>" . NWLINE;
180  }
181  if ($required) {
182  $this->setAttribute('required');
183  }
184  return $ret;
185  }
186 
192  public function renderValidationJS()
193  {
194  // render custom validation code if any
195  if (!empty($this->customValidationCode)) {
196  return implode(NWLINE, $this->customValidationCode);
197  // generate validation code if required
198  } elseif ($this->isRequired()) {
199  $eltname = $this->getName();
200  $eltcaption = $this->getCaption();
201  $eltmsg = empty($eltcaption)
202  ? sprintf(\XoopsLocale::F_ENTER, $eltname)
203  : sprintf(\XoopsLocale::F_ENTER, $eltcaption);
204  $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
205  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;}";
206  }
207  return '';
208  }
209 }
__construct($caption, $name, $value=null, $inline=true)
Definition: Checkbox.php:58
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
getExtra($encode=false)
Definition: Element.php:539
addOption($value, $name= '')
Definition: Checkbox.php:77
getOptions($encode=0)
Definition: Checkbox.php:112
addOptionArray($options)
Definition: Checkbox.php:93