MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
mradiobuttongroup.class
Ir para a documentação deste ficheiro.
1<?php
2
26{
30 public $default;
31
35 public $options;
36
40 private $container;
41
57 public function __construct($name='', $label='', $options='', $default='', $hint='', $disposition='vertical', $border='none', $showRequiredLabel=false)
58 {
59 $bkpHint = $hint;
60 $controls = array();
61
62 if ( !is_array($options) )
63 {
64 $options = array( $options );
65 }
66 $this->options = $options;
67
68 $n = count($options);
69
70 // manage options when they are a value => label array
71 reset($options);
72 $keys = array_keys($options);
73 if ( is_string(current($keys)) )
74 {
75 foreach ( $options as $i => $option )
76 {
77 // Coloca o hint apenas ao lado do ultimo radiobutton para que não fiquem vários hints sobrescritos
78 if ($disposition == 'horizontal')
79 $i != ($n -1) ? $hint = null : $hint = $bkpHint;
80
81 if ( $options[$i] instanceof MRadioButton )
82 {
83 $options[$i]->SetName($name);
84 $options[$i]->SetId($name . '_' . $i);
85 $options[$i]->checked = ( $options[$i]->checked || ( $options[$i]->value == $default ) );
86 $controls[] = clone $options[$i];
87 }
88 else
89 {
90 $oName = $name . '_' . $i;
91 $oValue = $i;
92 $oLabel = $options[$i];
93 $oChecked = ( $oValue == $default ) || ( $oValue == $_REQUEST[$oName] );
94
95 $control = new MRadioButton($oName, $oValue, $oLabel, $oChecked, $oLabel, $hint);
96 $control->SetName($name);
97 if ( $options[$i] instanceof MOption )
98 {
99 $control->attrs = $options[$i]->attrs;
100 }
101
102 $controls[] = $control;
103 }
104 }
105 }
106 else
107 {
108 for ( $i = 0; $i < $n; $i++ )
109 {
110 // Coloca o hint apenas ao lado do ultimo radiobutton para que não fiquem vários hints sobrescritos
111 if ($disposition == 'horizontal')
112 $i != ($n -1) ? $hint = null : $hint = $bkpHint;
113
114 // we will accept an array of RadioButton ...
115 if ( $options[$i] instanceof MRadioButton )
116 {
117 $options[$i]->SetName($name);
118 $options[$i]->SetId($name . '_' . $i);
119 $options[$i]->checked = ( $options[$i]->checked || ( $options[$i]->value == $default ) );
120 $controls[] = clone $options[$i];
121 }
122 else
123 {
124 $oName = $name;
125
126 // we will accept an array of Options ...
127 if ( $options[$i] instanceof MOption )
128 {
129 $oName = $name . '_' . $options[$i]->name;
130 $oLabel = $options[$i]->label;
131 $oValue = $options[$i]->value;
132 $oChecked = ( $oValue == $default ) || $options[$i]->checked || ( $oValue == $_REQUEST[$oName] );
133 }
134 // or an array of label/value pairs ...
135 elseif ( is_array($options[$i]) )
136 {
137 $oName = $name . '_' . $i;
138 $oLabel = $options[$i][0];
139 $oValue = $options[$i][1];
140 $oChecked = ( $oValue == $default ) || ( $oValue == $_REQUEST[$oName] );
141 }
142 // or a simple array of values
143 else
144 {
145 $oName = $name . '_' . $i;
146 $oLabel = $oValue = $options[$i];
147 $oChecked = ( $oValue == $default ) || ( $oValue == $_REQUEST[$oName] );
148 }
149
150 $control = new MRadioButton($oName, $oValue, $oLabel, $oChecked, $oLabel, $hint);
151 $control->SetName($name);
152 if ( $options[$i] instanceof MOption )
153 {
154 $control->attrs = $options[$i]->attrs;
155 }
156
157 $controls[] = $control;
158 }
159 }
160 }
161
162 $fields[] = $this->container = new MContainer("{$name}_container", $controls, $disposition);
163 $this->container->setClass('m-radiobutton-group');
164
165 parent::__construct($name, $fields, 'horizontal', self::FORM_MODE_SHOW_SIDE);
166 $this->value = $default;
167 $this->showRequiredLabel = $showRequiredLabel;
168 $this->label = $label;
169 $this->setShowLabeL(false);
170 }
171
175 public function setValue($v)
176 {
177 if ( $v )
178 {
179 $this->_setValue($v);
180 }
181 }
182
183 private function _setValue($v)
184 {
185 $n = 0;
186
187 foreach ( $this->container->getControls() as $option )
188 {
189 if ( $option->value )
190 {
191 if ( $v == $option->value )
192 {
193 $option->checked = true;
194 $this->value = $v;
195 }
196 else
197 {
198 $option->checked = false;
199 }
200 }
201
202 $n++;
203 }
204 }
205
209 public function getValue()
210 {
211 $value = NULL;
212
213 foreach ( $this->getControls() as $control )
214 {
215 if ( $control->checked )
216 {
217 $this->value = $value = $control->GetValue();
218 }
219 }
220
221 return $value;
222 }
223}
224
234{
235 public function __construct($name='', $value='', $label='', $options=array(), $hint='', $disposition='vertical', $border='none', $showRequiredLabel=false)
236 {
237 $newOptions = array();
238
239 foreach ( $options as $key => $val )
240 {
241 $newOptions[] = array($val, $key);
242 }
243
244 parent::__construct($name, $label, $newOptions, $value, $hint, $disposition, $border, $showRequiredLabel);
245 }
246}
247
248?>
__construct($name='', $value='', $label='', $options=array(), $hint='', $disposition='vertical', $border='none', $showRequiredLabel=false)
__construct($name='', $label='', $options='', $default='', $hint='', $disposition='vertical', $border='none', $showRequiredLabel=false)