| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: |
|
| 20: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
| 21: |
|
| 22: | xoops_load('XoopsFormElement');
|
| 23: |
|
| 24: | |
| 25: | |
| 26: |
|
| 27: | class XoopsFormCheckBox extends XoopsFormElement
|
| 28: | {
|
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: |
|
| 35: | public $_options = array();
|
| 36: |
|
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: |
|
| 43: | public $_value = array();
|
| 44: |
|
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: |
|
| 51: | public $_delimeter;
|
| 52: |
|
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: |
|
| 62: | public $columns;
|
| 63: |
|
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 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: | |
| 85: | |
| 86: | |
| 87: | |
| 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: | |
| 104: | |
| 105: | |
| 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: | |
| 122: | |
| 123: | |
| 124: | |
| 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: | |
| 137: | |
| 138: | |
| 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: | |
| 151: | |
| 152: | |
| 153: | |
| 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: | |
| 170: | |
| 171: | |
| 172: | |
| 173: |
|
| 174: | public function getDelimeter($encode = false)
|
| 175: | {
|
| 176: | return $encode ? htmlspecialchars(str_replace(' ', ' ', $this->_delimeter), ENT_QUOTES) : $this->_delimeter;
|
| 177: | }
|
| 178: |
|
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: |
|
| 184: | public function render()
|
| 185: | {
|
| 186: | return XoopsFormRenderer::getInstance()->get()->renderFormCheckBox($this);
|
| 187: | }
|
| 188: |
|
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: |
|
| 194: | public function renderValidationJS()
|
| 195: | {
|
| 196: |
|
| 197: | if (!empty($this->customValidationCode)) {
|
| 198: | return implode(NWLINE, $this->customValidationCode);
|
| 199: |
|
| 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: | |