XOOPS  2.6.0
Radio.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Form;
13 
26 class Radio extends Element
27 {
33  private $options = array();
34 
40  protected $value = null;
41 
47  private $inline;
48 
57  public function __construct($caption, $name, $value = null, $inline = true)
58  {
59  $this->setAttribute('type', 'radio');
60  $this->setAttribute('name', $name);
61  $this->setCaption($caption);
62  if (isset($value)) {
63  $this->setValue($value);
64  }
65  $this->inline = $inline;
66  }
67 
75  public function getValue($encode = false)
76  {
77  return ($encode && $this->value !== null) ? htmlspecialchars($this->value, ENT_QUOTES) : $this->value;
78  }
79 
88  public function addOption($value, $buttonCaption = '')
89  {
90  if ($buttonCaption != '') {
91  $this->options[$value] = $buttonCaption;
92  } else {
93  $this->options[$value] = $value;
94  }
95  }
96 
104  public function addOptionArray($options)
105  {
106  if (is_array($options)) {
107  foreach ($options as $k => $v) {
108  $this->addOption($k, $v);
109  }
110  }
111  }
112 
123  public function getOptions($encode = 0)
124  {
125  if (!$encode) {
126  return (array) $this->options;
127  }
128  $value = array();
129  foreach ($this->options as $option => $buttonCaption) {
130  $value[$encode ? htmlspecialchars($option, ENT_QUOTES) : $option] = ($encode > 1)
131  ? htmlspecialchars($buttonCaption, ENT_QUOTES) : $buttonCaption;
132  }
133  return $value;
134  }
135 
141  public function getInline()
142  {
143  if ($this->inline == true) {
144  return ' inline';
145  } else {
146  return '';
147  }
148  }
149 
155  public function render()
156  {
157  $ele_options = $this->getOptions();
158  $ele_value = $this->getValue();
159  $ele_name = $this->getName();
160  $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
161  $ret = "";
162  static $id_ele = 0;
163  foreach ($ele_options as $value => $buttonCaption) {
164  $this->unsetAttribute('checked');
165  if (isset($ele_value) && $value == $ele_value) {
166  $this->setAttribute('checked');
167  }
168  $this->setAttribute('value', $value);
169  ++$id_ele;
170  $this->setAttribute('id', $ele_name . $id_ele);
171  $ret .= '<label class="radio' . $this->getInline() . '">' . NWLINE;
172  $ret .= '<input ' . $this->renderAttributeString() . $extra . ">" . NWLINE;
173  $ret .= $buttonCaption . NWLINE;
174  $ret .= "</label>" . NWLINE;
175  }
176  return $ret;
177  }
178 }
setValue($value)
Definition: Element.php:199
setCaption($caption)
Definition: Element.php:396
addOption($value, $buttonCaption= '')
Definition: Radio.php:88
setAttribute($name, $value=null)
Definition: Attributes.php:42
$option
Definition: Xoops.php:68
__construct($caption, $name, $value=null, $inline=true)
Definition: Radio.php:57
getValue($encode=false)
Definition: Radio.php:75
getExtra($encode=false)
Definition: Element.php:539
addOptionArray($options)
Definition: Radio.php:104
getOptions($encode=0)
Definition: Radio.php:123