| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: |
|
| 18: |
|
| 19: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
| 20: |
|
| 21: | |
| 22: | |
| 23: |
|
| 24: | class XoopsFormElement
|
| 25: | {
|
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: |
|
| 36: | public $customValidationCode = array();
|
| 37: |
|
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: |
|
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: |
|
| 48: | public $_name;
|
| 49: |
|
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: |
|
| 55: | public $_caption;
|
| 56: |
|
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: |
|
| 62: | public $_accesskey = '';
|
| 63: |
|
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: |
|
| 69: | public $_class = array();
|
| 70: |
|
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: |
|
| 76: | public $_hidden = false;
|
| 77: |
|
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: |
|
| 83: | public $_extra = array();
|
| 84: |
|
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: |
|
| 90: | public $_required = false;
|
| 91: |
|
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: |
|
| 97: | public $_description = '';
|
| 98: |
|
| 99: | |
| 100: | |
| 101: |
|
| 102: |
|
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: |
|
| 113: | public $_nocolspan = false;
|
| 114: |
|
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: |
|
| 120: | public $_formtype = '';
|
| 121: |
|
| 122: | |
| 123: | |
| 124: |
|
| 125: | public function __construct()
|
| 126: | {
|
| 127: | exit('This class cannot be instantiated!');
|
| 128: | }
|
| 129: |
|
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: |
|
| 135: | public function isContainer()
|
| 136: | {
|
| 137: | return false;
|
| 138: | }
|
| 139: |
|
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: |
|
| 145: | public function setName($name)
|
| 146: | {
|
| 147: | $this->_name = trim($name);
|
| 148: | }
|
| 149: |
|
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: |
|
| 157: | public function getName($encode = true)
|
| 158: | {
|
| 159: | if (false !== (bool)$encode) {
|
| 160: | return str_replace('&', '&', htmlspecialchars((string)$this->_name, ENT_QUOTES));
|
| 161: | }
|
| 162: |
|
| 163: | return $this->_name;
|
| 164: | }
|
| 165: |
|
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: |
|
| 171: | public function setAccessKey($key)
|
| 172: | {
|
| 173: | $this->_accesskey = trim($key);
|
| 174: | }
|
| 175: |
|
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: |
|
| 181: | public function getAccessKey()
|
| 182: | {
|
| 183: | return $this->_accesskey;
|
| 184: | }
|
| 185: |
|
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: |
|
| 192: | public function getAccessString($str)
|
| 193: | {
|
| 194: | $access = $this->getAccessKey();
|
| 195: | if (!empty($access) && (false !== ($pos = strpos($str, $access)))) {
|
| 196: | return htmlspecialchars(substr($str, 0, $pos), ENT_QUOTES) . '<span style="text-decoration: underline;">' . htmlspecialchars(substr($str, $pos, 1), ENT_QUOTES) . '</span>' . htmlspecialchars(substr($str, $pos + 1), ENT_QUOTES);
|
| 197: | }
|
| 198: |
|
| 199: | return htmlspecialchars($str, ENT_QUOTES);
|
| 200: | }
|
| 201: |
|
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: |
|
| 207: | public function setClass($class)
|
| 208: | {
|
| 209: | $class = trim($class);
|
| 210: | if (!empty($class)) {
|
| 211: | $this->_class[] = $class;
|
| 212: | }
|
| 213: | }
|
| 214: |
|
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: |
|
| 220: | public function getClass()
|
| 221: | {
|
| 222: | if (empty($this->_class)) {
|
| 223: | return false;
|
| 224: | }
|
| 225: | $classes = array();
|
| 226: | foreach ($this->_class as $class) {
|
| 227: | $classes[] = htmlspecialchars($class, ENT_QUOTES);
|
| 228: | }
|
| 229: |
|
| 230: | return implode(' ', $classes);
|
| 231: | }
|
| 232: |
|
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: |
|
| 238: | public function setCaption($caption)
|
| 239: | {
|
| 240: | $this->_caption = trim($caption);
|
| 241: | }
|
| 242: |
|
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: |
|
| 249: | public function getCaption($encode = false)
|
| 250: | {
|
| 251: | return $encode ? htmlspecialchars($this->_caption, ENT_QUOTES) : $this->_caption;
|
| 252: | }
|
| 253: |
|
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: |
|
| 260: | public function getTitle($encode = true)
|
| 261: | {
|
| 262: | if(!isset($this->_caption)) {
|
| 263: | $this->_caption = '';
|
| 264: | }
|
| 265: | if (strlen($this->_description) > 0) {
|
| 266: | return $encode ? htmlspecialchars(strip_tags($this->_caption . ' - ' . $this->_description), ENT_QUOTES) : strip_tags($this->_caption . ' - ' . $this->_description);
|
| 267: | } else {
|
| 268: | return $encode ? htmlspecialchars(strip_tags($this->_caption), ENT_QUOTES) : strip_tags($this->_caption);
|
| 269: | }
|
| 270: | }
|
| 271: |
|
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: |
|
| 277: | public function setDescription($description)
|
| 278: | {
|
| 279: | $this->_description = (isset($description) && !empty($description)) ? trim($description) : $description;
|
| 280: | }
|
| 281: |
|
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: |
|
| 288: | public function getDescription($encode = false)
|
| 289: | {
|
| 290: | return $encode ? htmlspecialchars($this->_description, ENT_QUOTES) : $this->_description;
|
| 291: | }
|
| 292: |
|
| 293: | |
| 294: | |
| 295: |
|
| 296: | public function setHidden()
|
| 297: | {
|
| 298: | $this->_hidden = true;
|
| 299: | }
|
| 300: |
|
| 301: | |
| 302: | |
| 303: | |
| 304: | |
| 305: |
|
| 306: | public function isHidden()
|
| 307: | {
|
| 308: | return $this->_hidden;
|
| 309: | }
|
| 310: |
|
| 311: | |
| 312: | |
| 313: | |
| 314: | |
| 315: |
|
| 316: | public function isRequired()
|
| 317: | {
|
| 318: | return $this->_required;
|
| 319: | }
|
| 320: |
|
| 321: | |
| 322: | |
| 323: | |
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: | |
| 330: |
|
| 331: | public function setExtra($extra, $replace = false)
|
| 332: | {
|
| 333: | if ($replace) {
|
| 334: | $this->_extra = array(trim($extra));
|
| 335: | } else {
|
| 336: | $this->_extra[] = trim($extra);
|
| 337: | }
|
| 338: |
|
| 339: | return $this->_extra;
|
| 340: | }
|
| 341: |
|
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: |
|
| 348: | public function getExtra($encode = false)
|
| 349: | {
|
| 350: | if (!$encode) {
|
| 351: | return ' ' . implode(' ', $this->_extra);
|
| 352: | }
|
| 353: | $value = array();
|
| 354: | foreach ($this->_extra as $val) {
|
| 355: | $value[] = str_replace('>', '>', str_replace('<', '<', $val));
|
| 356: | }
|
| 357: |
|
| 358: | return empty($value) ? '' : ' ' . implode(' ', $value);
|
| 359: | }
|
| 360: |
|
| 361: | |
| 362: | |
| 363: | |
| 364: | |
| 365: | |
| 366: | |
| 367: | |
| 368: |
|
| 369: | public function setNocolspan($nocolspan = true)
|
| 370: | {
|
| 371: | $this->_nocolspan = $nocolspan;
|
| 372: | }
|
| 373: |
|
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: | |
| 379: | |
| 380: | |
| 381: |
|
| 382: | public function getNocolspan()
|
| 383: | {
|
| 384: | return $this->_nocolspan;
|
| 385: | }
|
| 386: |
|
| 387: | |
| 388: | |
| 389: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: |
|
| 395: | public function getFormType()
|
| 396: | {
|
| 397: | return $this->_formtype;
|
| 398: | }
|
| 399: |
|
| 400: | |
| 401: | |
| 402: | |
| 403: | |
| 404: | |
| 405: | |
| 406: | |
| 407: |
|
| 408: | public function setFormType($value = '')
|
| 409: | {
|
| 410: | $this->_formtype = $value;
|
| 411: | }
|
| 412: |
|
| 413: | |
| 414: | |
| 415: | |
| 416: | |
| 417: |
|
| 418: | public function renderValidationJS()
|
| 419: | {
|
| 420: |
|
| 421: | if (!empty($this->customValidationCode)) {
|
| 422: | return implode(NWLINE, $this->customValidationCode);
|
| 423: |
|
| 424: | } elseif ($this->isRequired() && $eltname = $this->getName()) {
|
| 425: |
|
| 426: | $eltcaption = $this->getCaption();
|
| 427: | $eltmsg = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
|
| 428: | $eltmsg = str_replace(array(':', '?', '%'), '', $eltmsg);
|
| 429: | $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
|
| 430: | $eltmsg = strip_tags($eltmsg);
|
| 431: | echo $this->getFormType();
|
| 432: | switch ($this->getFormType()) {
|
| 433: | case 'checkbox':
|
| 434: | return NWLINE . "if (!myform.{$eltname}.checked) { window.alert(\"{$eltmsg}\"); myform.{$eltname}.focus(); return false; }\n";
|
| 435: | default:
|
| 436: | return NWLINE . "if (myform.{$eltname}.value == \"\") { window.alert(\"{$eltmsg}\"); myform.{$eltname}.focus(); return false; }\n";
|
| 437: | }
|
| 438: | }
|
| 439: |
|
| 440: | return false;
|
| 441: | }
|
| 442: |
|
| 443: | |
| 444: | |
| 445: | |
| 446: | |
| 447: | |
| 448: | |
| 449: |
|
| 450: | public function render()
|
| 451: | {
|
| 452: | }
|
| 453: | }
|
| 454: | |