1
|
|
|
<?php namespace XoopsModules\Smartobject\Form\Elements; |
2
|
|
|
|
3
|
|
|
use XoopsModules\Smartobject; |
4
|
|
|
|
5
|
|
|
// defined('XOOPS_ROOT_PATH') || die('Restricted access'); |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class SmartFormCheckElement |
9
|
|
|
*/ |
10
|
|
|
class SmartFormCheckElement extends \XoopsFormCheckBox |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* /** |
15
|
|
|
* prepare HTML for output |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public function render() |
20
|
|
|
{ |
21
|
|
|
$ret = ''; |
22
|
|
|
if (count($this->getOptions()) > 1 && '[]' !== substr($this->getName(), -2, 2)) { |
|
|
|
|
23
|
|
|
$newname = $this->getName() . '[]'; |
|
|
|
|
24
|
|
|
$this->setName($newname); |
25
|
|
|
} |
26
|
|
|
foreach ($this->getOptions() as $value => $name) { |
27
|
|
|
$ret .= "<input type='checkbox' name='" . $this->getName() . "' value='" . $value . "'"; |
|
|
|
|
28
|
|
|
if (count($this->getValue()) > 0 && in_array($value, $this->getValue())) { |
29
|
|
|
$ret .= ' checked'; |
30
|
|
|
} |
31
|
|
|
$ret .= $this->getExtra() . '>' . $name . '<br>'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $ret; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function renderValidationJS() |
41
|
|
|
{ |
42
|
|
|
$js .= 'var hasSelections = false;'; |
|
|
|
|
43
|
|
|
//sometimes, there is an implicit '[]', sometimes not |
44
|
|
|
$eltname = $this->getName(); |
|
|
|
|
45
|
|
View Code Duplication |
if (false === strpos($eltname, '[')) { |
|
|
|
|
46
|
|
|
$js .= "for (var i = 0; i < myform['{$eltname}[]'].length; i++) { |
47
|
|
|
if (myform['{$eltname}[]'][i].checked) { |
48
|
|
|
hasSelections = true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
if (hasSelections === false) { |
53
|
|
|
window.alert(\"{$eltmsg}\"); myform['{$eltname}[]'][0].focus(); return false; }\n"; |
|
|
|
|
54
|
|
|
} else { |
55
|
|
|
$js .= "for (var i = 0; i < myform['" . $eltname . "'].length; i++) { |
56
|
|
|
if (myform['{$eltname}'][i].checked) { |
57
|
|
|
hasSelections = true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
if (hasSelections === false) { |
62
|
|
|
window.alert(\"{$eltmsg}\"); myform['{$eltname}'][0].focus(); return false; }\n"; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $js; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|