1: <?php
2: /**
3: * This file contains the QRadioButton class.
4: *
5: * @package Controls
6: */
7:
8: /**
9: * This class will render an HTML Radio button.
10: *
11: * Based on a QCheckbox, which is very similar to a checkbox.
12: *
13: * @package Controls
14: *
15: * @property string $Text is used to display text that is displayed next to the radio. The text is rendered as an html "Label For" the radio
16: * @property string $TextAlign specifies if "Text" should be displayed to the left or to the right of the radio.
17: * @property string $GroupName assigns the radio button into a radio button group (optional) so that no more than one radio in that group may be selected at a time.
18: * @property boolean $HtmlEntities
19: * @property boolean $Checked specifices whether or not the radio is selected
20: */
21: class QRadioButton extends QCheckBox {
22: /**
23: * Group to which this radio button belongs
24: * Groups determine the 'radio' behavior wherein you can select only one option out of all buttons in that group
25: * @var null|string Name of the group
26: */
27: protected $strGroupName = null;
28:
29: /**
30: * Parse the data posted
31: */
32: public function ParsePostData() {
33: $val = $this->objForm->CheckableControlValue($this->strControlId);
34: $val = QType::Cast($val, QType::Boolean);
35: $this->blnChecked = !empty($val);
36: }
37:
38: /**
39: * Returns the HTML code for the control which can be sent to the client.
40: *
41: * Note, previous version wrapped this in a div and made the control a block level control unnecessarily. To
42: * achieve a block control, set blnUseWrapper and blnIsBlockElement.
43: *
44: * @return string THe HTML for the control
45: */
46: protected function GetControlHtml() {
47: if ($this->strGroupName)
48: $strGroupName = $this->strGroupName;
49: else
50: $strGroupName = $this->strControlId;
51:
52: $attrOverride = array('type'=>'radio', 'name'=>$strGroupName, 'value'=>$this->strControlId);
53: return $this->RenderButton($attrOverride);
54: }
55:
56: /**
57: * Returns the current state of the control to be able to restore it later.
58: * @return mixed
59: */
60: public function GetState(){
61: return array('Checked'=>$this->Checked);
62: }
63:
64: /**
65: * Restore the state of the control.
66: * @param mixed $state Previously saved state as returned by GetState above.
67: */
68: public function PutState($state) {
69: if (isset($state['Checked'])) {
70: $this->SelectedValues = $state['Checked'];
71: }
72: }
73:
74:
75: /////////////////////////
76: // Public Properties: GET
77: /////////////////////////
78: /**
79: * PHP __get magic method implementation for the QRadioButton class
80: * @param string $strName Name of the property
81: *
82: * @return array|bool|int|mixed|null|QControl|QForm|string
83: * @throws Exception|QCallerException
84: */
85: public function __get($strName) {
86: switch ($strName) {
87: // APPEARANCE
88: case "GroupName": return $this->strGroupName;
89:
90: default:
91: try {
92: return parent::__get($strName);
93: } catch (QCallerException $objExc) {
94: $objExc->IncrementOffset();
95: throw $objExc;
96: }
97: }
98: }
99:
100: /////////////////////////
101: // Public Properties: SET
102: /////////////////////////
103: /**
104: * PHP __set magic method implementation
105: *
106: * @param string $strName Name of the property
107: * @param string $mixValue Value of the property
108: *
109: * @return mixed
110: * @throws Exception|QCallerException|QInvalidCastException
111: */
112: public function __set($strName, $mixValue) {
113: switch ($strName) {
114: case "GroupName":
115: try {
116: $strGroupName = QType::Cast($mixValue, QType::String);
117: if ($this->strGroupName != $strGroupName) {
118: $this->strGroupName = $strGroupName;
119: $this->blnModified = true;
120: }
121: break;
122: } catch (QInvalidCastException $objExc) {
123: $objExc->IncrementOffset();
124: throw $objExc;
125: }
126:
127: case "Checked":
128: try {
129: $val = QType::Cast($mixValue, QType::Boolean);
130: if ($val != $this->blnChecked) {
131: $this->blnChecked = $val;
132: if ($this->GroupName && $val == true) {
133: QApplication::ExecuteJsFunction('qcubed.setRadioInGroup', $this->strControlId);
134: } else {
135: $this->AddAttributeScript('prop', 'checked', $val); // just set the one radio
136: }
137: }
138: break;
139: } catch (QInvalidCastException $objExc) {
140: $objExc->IncrementOffset();
141: throw $objExc;
142: }
143:
144: default:
145: try {
146: parent::__set($strName, $mixValue);
147: } catch (QCallerException $objExc) {
148: $objExc->IncrementOffset();
149: throw $objExc;
150: }
151: break;
152: }
153: }
154: }