| 1: | <?php |
| 2: | |
| 3: | /** |
| 4: | * XOOPS form radio compo |
| 5: | * |
| 6: | * You may not change or alter any portion of this comment or credits |
| 7: | * of supporting developers from this source code or any supporting source code |
| 8: | * which is considered copyrighted (c) material of the original comment or credit authors. |
| 9: | * This program is distributed in the hope that it will be useful, |
| 10: | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11: | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12: | * |
| 13: | * @copyright (c) 2000-2017 XOOPS Project (www.xoops.org) |
| 14: | * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html) |
| 15: | * @package kernel |
| 16: | * @since 2.0 |
| 17: | * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/ |
| 18: | * @author Taiwen Jiang <phppp@users.sourceforge.net> |
| 19: | * @package kernel |
| 20: | * @subpackage form |
| 21: | * @todo template |
| 22: | */ |
| 23: | class XoopsFormRadio extends XoopsFormElement |
| 24: | { |
| 25: | /** |
| 26: | * Array of Options |
| 27: | * |
| 28: | * @var array |
| 29: | * @access private |
| 30: | */ |
| 31: | public $_options = array(); |
| 32: | |
| 33: | /** |
| 34: | * Pre-selected value |
| 35: | * |
| 36: | * @var string |
| 37: | * @access private |
| 38: | */ |
| 39: | public $_value; |
| 40: | |
| 41: | /** |
| 42: | * Columns per line for rendering |
| 43: | * Leave unset (null) to put all options in one line |
| 44: | * Set to 1 to put each option on its own line |
| 45: | * Any other positive integer 'n' to put 'n' options on each line |
| 46: | * |
| 47: | * @var int |
| 48: | * @access public |
| 49: | */ |
| 50: | public $columns; |
| 51: | |
| 52: | /** |
| 53: | * HTML to seperate the elements |
| 54: | * |
| 55: | * @var string |
| 56: | * @access private |
| 57: | */ |
| 58: | public $_delimeter; |
| 59: | |
| 60: | /** |
| 61: | * Constructor |
| 62: | * |
| 63: | * @param string $caption Caption |
| 64: | * @param string $name "name" attribute |
| 65: | * @param string|null $value Pre-selected value |
| 66: | * @param string $delimeter |
| 67: | */ |
| 68: | public function __construct($caption, $name, $value = null, $delimeter = ' ') |
| 69: | { |
| 70: | $this->setCaption($caption); |
| 71: | $this->setName($name); |
| 72: | if (isset($value)) { |
| 73: | $this->setValue($value); |
| 74: | } |
| 75: | $this->_delimeter = $delimeter; |
| 76: | } |
| 77: | |
| 78: | /** |
| 79: | * Get the "value" attribute |
| 80: | * |
| 81: | * @param bool $encode To sanitizer the text? |
| 82: | * @return string |
| 83: | */ |
| 84: | public function getValue($encode = false) |
| 85: | { |
| 86: | return ($encode && $this->_value !== null) ? htmlspecialchars($this->_value, ENT_QUOTES) : $this->_value; |
| 87: | } |
| 88: | |
| 89: | /** |
| 90: | * Set the pre-selected value |
| 91: | * |
| 92: | * @param string $value |
| 93: | */ |
| 94: | public function setValue($value) |
| 95: | { |
| 96: | $this->_value = $value; |
| 97: | } |
| 98: | |
| 99: | /** |
| 100: | * Add an option |
| 101: | * |
| 102: | * @param string $value "value" attribute - This gets submitted as form-data. |
| 103: | * @param string $name "name" attribute - This is displayed. If empty, we use the "value" instead. |
| 104: | */ |
| 105: | public function addOption($value, $name = '') |
| 106: | { |
| 107: | if ($name != '') { |
| 108: | $this->_options[$value] = $name; |
| 109: | } else { |
| 110: | $this->_options[$value] = $value; |
| 111: | } |
| 112: | } |
| 113: | |
| 114: | /** |
| 115: | * Adds multiple options |
| 116: | * |
| 117: | * @param array $options Associative array of value->name pairs. |
| 118: | */ |
| 119: | public function addOptionArray($options) |
| 120: | { |
| 121: | if (is_array($options)) { |
| 122: | foreach ($options as $k => $v) { |
| 123: | $this->addOption($k, $v); |
| 124: | } |
| 125: | } |
| 126: | } |
| 127: | |
| 128: | /** |
| 129: | * Get an array with all the options |
| 130: | * |
| 131: | * @param bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name |
| 132: | * |
| 133: | * @return array Associative array of value->name pairs |
| 134: | */ |
| 135: | public function getOptions($encode = false) |
| 136: | { |
| 137: | if (!$encode) { |
| 138: | return $this->_options; |
| 139: | } |
| 140: | $value = array(); |
| 141: | foreach ($this->_options as $val => $name) { |
| 142: | $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name; |
| 143: | } |
| 144: | |
| 145: | return $value; |
| 146: | } |
| 147: | |
| 148: | /** |
| 149: | * Get the delimiter of this group |
| 150: | * |
| 151: | * @param bool $encode To sanitizer the text? |
| 152: | * @return string The delimiter |
| 153: | */ |
| 154: | public function getDelimeter($encode = false) |
| 155: | { |
| 156: | return $encode ? htmlspecialchars(str_replace(' ', ' ', $this->_delimeter), ENT_QUOTES) : $this->_delimeter; |
| 157: | } |
| 158: | |
| 159: | /** |
| 160: | * Prepare HTML for output |
| 161: | * |
| 162: | * @return string HTML |
| 163: | */ |
| 164: | public function render() |
| 165: | { |
| 166: | return XoopsFormRenderer::getInstance()->get()->renderFormRadio($this); |
| 167: | } |
| 168: | } |
| 169: |