1: <?php
2: /**
3: * XOOPS form checkbox compo
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2017 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: * @author Skalpa Keo <skalpa@xoops.org>
18: * @author Taiwen Jiang <phppp@users.sourceforge.net>
19: */
20: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21:
22: xoops_load('XoopsFormElement');
23:
24: /**
25: * Class XoopsFormCheckBox
26: */
27: class XoopsFormCheckBox extends XoopsFormElement
28: {
29: /**
30: * Availlable options
31: *
32: * @var array
33: * @access private
34: */
35: public $_options = array();
36:
37: /**
38: * pre-selected values in array
39: *
40: * @var array
41: * @access private
42: */
43: public $_value = array();
44:
45: /**
46: * HTML to seperate the elements
47: *
48: * @var string
49: * @access private
50: */
51: public $_delimeter;
52:
53: /**
54: * Columns per line for rendering
55: * Leave unset (null) to put all options in one line
56: * Set to 1 to put each option on its own line
57: * Any other positive integer 'n' to put 'n' options on each line
58: *
59: * @var int
60: * @access public
61: */
62: public $columns;
63:
64: /**
65: * Constructor
66: *
67: * @param string $caption
68: * @param string $name
69: * @param mixed $value Either one value as a string or an array of them.
70: * @param string $delimeter
71: */
72: public function __construct($caption, $name, $value = null, $delimeter = ' ')
73: {
74: $this->setCaption($caption);
75: $this->setName($name);
76: if (isset($value)) {
77: $this->setValue($value);
78: }
79: $this->_delimeter = $delimeter;
80: $this->setFormType('checkbox');
81: }
82:
83: /**
84: * Get the "value"
85: *
86: * @param bool $encode To sanitizer the text?
87: * @return array
88: */
89: public function getValue($encode = false)
90: {
91: if (!$encode) {
92: return $this->_value;
93: }
94: $value = array();
95: foreach ($this->_value as $val) {
96: $value[] = $val ? htmlspecialchars($val, ENT_QUOTES) : $val;
97: }
98:
99: return $value;
100: }
101:
102: /**
103: * Set the "value"
104: *
105: * @param array $value
106: *
107: */
108: public function setValue($value)
109: {
110: $this->_value = array();
111: if (is_array($value)) {
112: foreach ($value as $v) {
113: $this->_value[] = $v;
114: }
115: } else {
116: $this->_value[] = $value;
117: }
118: }
119:
120: /**
121: * Add an option
122: *
123: * @param string $value
124: * @param string $name
125: */
126: public function addOption($value, $name = '')
127: {
128: if ($name != '') {
129: $this->_options[$value] = $name;
130: } else {
131: $this->_options[$value] = $value;
132: }
133: }
134:
135: /**
136: * Add multiple Options at once
137: *
138: * @param array $options Associative array of value->name pairs
139: */
140: public function addOptionArray($options)
141: {
142: if (is_array($options)) {
143: foreach ($options as $k => $v) {
144: $this->addOption($k, $v);
145: }
146: }
147: }
148:
149: /**
150: * Get an array with all the options
151: *
152: * @param bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name
153: * @return array Associative array of value->name pairs
154: */
155: public function getOptions($encode = false)
156: {
157: if (!$encode) {
158: return $this->_options;
159: }
160: $value = array();
161: foreach ($this->_options as $val => $name) {
162: $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name;
163: }
164:
165: return $value;
166: }
167:
168: /**
169: * Get the delimiter of this group
170: *
171: * @param bool $encode To sanitizer the text?
172: * @return string The delimiter
173: */
174: public function getDelimeter($encode = false)
175: {
176: return $encode ? htmlspecialchars(str_replace(' ', ' ', $this->_delimeter)) : $this->_delimeter;
177: }
178:
179: /**
180: * prepare HTML for output
181: *
182: * @return string
183: */
184: public function render()
185: {
186: return XoopsFormRenderer::getInstance()->get()->renderFormCheckBox($this);
187: }
188:
189: /**
190: * Render custom javascript validation code
191: *
192: * @seealso XoopsForm::renderValidationJS
193: */
194: public function renderValidationJS()
195: {
196: // render custom validation code if any
197: if (!empty($this->customValidationCode)) {
198: return implode(NWLINE, $this->customValidationCode);
199: // generate validation code if required
200: } elseif ($this->isRequired()) {
201: $eltname = $this->getName();
202: $eltcaption = $this->getCaption();
203: $eltmsg = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
204: $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
205:
206: 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;}";
207: }
208:
209: return '';
210: }
211: }
212: