1: <?php
2: /**
3: * This file contains the QNumericTextBox class
4: *
5: * @package Controls
6: */
7:
8:
9: /**
10: * A subclass of TextBox with its validate method overridden -- Validate will also ensure
11: * that the Text is a valid integer/float and (if applicable) is in the range of Minimum <= x <= Maximum.
12: * This class is abstract. QIntegerTextBox and QFloatTextBox are derived from it.
13: *
14: * @package Controls
15: * @property mixed $Maximum (optional) is the maximum value the integer/float can be
16: * @property mixed $Minimum (optional) is the minimum value the integer/float can be
17: * @property mixed $Step (optional) is the step interval for allowed values ( beginning from $Minimum if set)
18: * @property string $LabelForGreater Text to show when the input is greater than the allowed value
19: * @property string $LabelForLess Text to show when the input is lesser than the minimum allowed value
20: * @property string $LabelForNotStepAligned
21: * set this property to show an error message if the entered value is not step-aligned
22: * if not set the value is changed to the next step-aligned value (no error)
23: */
24: abstract class QNumericTextBox extends QTextBox {
25: ///////////////////////////
26: // Private Member Variables
27: ///////////////////////////
28:
29: //DATA TYPE
30: /** @var string Data type of the input (float|integer) */
31: protected $strDataType = null;
32: // MISC
33: /** @var mixed Maximum allowed Value */
34: protected $mixMaximum = null;
35: /** @var mixed Minimum allowed value */
36: protected $mixMinimum = null;
37: /** @var mixed Float or Integer value, the multiple of which the input must be */
38: protected $mixStep = null;
39:
40: /** @var string Text to show when the input value is less than minimum allowed value */
41: protected $strLabelForLess;
42: /** @var string Text to show when the input value is greater than the maximum allowed value */
43: protected $strLabelForGreater;
44: /** @var string Text to show when the input value is not step aligned */
45: protected $strLabelForNotStepAligned = null;
46:
47: //////////
48: // Methods
49: //////////
50: /**
51: * Constructor for the control
52: *
53: * @param QControl|QForm $objParentObject
54: * @param null|string $strControlId
55: */
56: public function __construct($objParentObject, $strControlId = null) {
57: parent::__construct($objParentObject, $strControlId);
58:
59: $this->strLabelForInvalid = QApplication::Translate('Invalid Number');
60: $this->strLabelForLess = QApplication::Translate('Value must be less than %s');
61: $this->strLabelForGreater = QApplication::Translate('Value must be greater than %s');
62: }
63:
64: /**
65: * @return bool whether or not the input passed all the values
66: */
67: public function Validate() {
68: if (parent::Validate()) {
69: if ($this->strText != "") {
70: try {
71: $this->strText = QType::Cast($this->strText, $this->strDataType);
72: } catch (QInvalidCastException $objExc) {
73: $this->ValidationError = $this->strLabelForInvalid;
74: $this->MarkAsModified();
75: return false;
76: }
77:
78: if (!is_numeric($this->strText)) {
79: $this->ValidationError = $this->strLabelForInvalid;
80: $this->MarkAsModified();
81: return false;
82: }
83:
84: if (!is_null($this->mixStep)) {
85: $newVal = QType::Cast(round(($this->strText - $this->mixMinimum) / $this->mixStep) * $this->mixStep + $this->mixMinimum, $this->strDataType);
86:
87: if ($newVal != $this->strText) {
88: if ($this->strLabelForNotStepAligned) {
89: $this->ValidationError = sprintf($this->strLabelForNotStepAligned, $this->mixStep);
90: $this->MarkAsModified();
91: return false;
92: }
93: $this->strText = $newVal;
94: $this->MarkAsModified();
95: }
96: }
97:
98: if ((!is_null($this->mixMinimum)) && ($this->strText < $this->mixMinimum)) {
99: $this->ValidationError = sprintf($this->strLabelForGreater, $this->mixMinimum);
100: $this->MarkAsModified();
101: return false;
102: }
103:
104: if ((!is_null($this->mixMaximum)) && ($this->strText > $this->mixMaximum)) {
105: $this->ValidationError = sprintf($this->strLabelForLess, $this->mixMaximum);
106: $this->MarkAsModified();
107: return false;
108: }
109: }
110: } else
111: return false;
112:
113: return true;
114: }
115:
116: /////////////////////////
117: // Public Properties: GET
118: /////////////////////////
119: /**
120: * PHP __get magic method implementation
121: * @param string $strName Name of the property
122: *
123: * @return array|bool|int|mixed|null|QControl|QForm|string
124: * @throws Exception|QCallerException
125: */
126: public function __get($strName) {
127: switch ($strName) {
128: // MISC
129: case "Maximum":
130: return $this->mixMaximum;
131: case "Minimum":
132: return $this->mixMinimum;
133: case 'Step':
134: return $this->mixStep;
135: case 'LabelForGreater':
136: return $this->strLabelForGreater;
137: case 'LabelForLess':
138: return $this->strLabelForLess;
139: case 'LabelForNotStepAligned':
140: return $this->strLabelForNotStepAligned;
141:
142: default:
143: try {
144: return parent::__get($strName);
145: } catch (QCallerException $objExc) {
146: $objExc->IncrementOffset();
147: throw $objExc;
148: }
149: }
150: }
151:
152: /////////////////////////
153: // Public Properties: SET
154: /////////////////////////
155: /**
156: * PHP __set magic method implementation
157: *
158: * @param string $strName Property Name
159: * @param string $mixValue Property value
160: *
161: * @return mixed
162: * @throws Exception|QCallerException|QInvalidCastException
163: */
164: public function __set($strName, $mixValue) {
165: $this->blnModified = true;
166:
167: switch ($strName) {
168: // MISC
169: case "Maximum":
170: try {
171: $this->mixMaximum = QType::Cast($mixValue, $this->strDataType);
172: break;
173: } catch (QInvalidCastException $objExc) {
174: $objExc->IncrementOffset();
175: throw $objExc;
176: }
177: case "Minimum":
178: try {
179: $this->mixMinimum = QType::Cast($mixValue, $this->strDataType);
180: break;
181: } catch (QInvalidCastException $objExc) {
182: $objExc->IncrementOffset();
183: throw $objExc;
184: }
185: case "Step":
186: try {
187: $this->mixStep = QType::Cast($mixValue, $this->strDataType);
188: break;
189: } catch (QInvalidCastException $objExc) {
190: $objExc->IncrementOffset();
191: throw $objExc;
192: }
193:
194: case 'LabelForGreater':
195: try {
196: $this->strLabelForGreater = QType::Cast($mixValue, QType::String);
197: break;
198: } catch (QInvalidCastException $objExc) {
199: $objExc->IncrementOffset();
200: throw $objExc;
201: }
202:
203: case 'LabelForLess':
204: try {
205: $this->strLabelForLess = QType::Cast($mixValue, QType::String);
206: break;
207: } catch (QInvalidCastException $objExc) {
208: $objExc->IncrementOffset();
209: throw $objExc;
210: }
211:
212: case 'LabelForNotStepAligned':
213: try {
214: $this->strLabelForNotStepAligned = QType::Cast($mixValue, QType::String);
215: break;
216: } catch (QInvalidCastException $objExc) {
217: $objExc->IncrementOffset();
218: throw $objExc;
219: }
220:
221: default:
222: try {
223: parent::__set($strName, $mixValue);
224: } catch (QCallerException $objExc) {
225: $objExc->IncrementOffset();
226: throw $objExc;
227: }
228: break;
229: }
230: }
231:
232: /**
233: * Returns an description of the options available to modify by the designer for the code generator.
234: *
235: * @return array
236: */
237: public static function GetModelConnectorParams() {
238: return array_merge(parent::GetModelConnectorParams(), array(
239: new QModelConnectorParam (get_called_class(), 'Maximum', 'Meximum value allowed', QType::String),// float or integer
240: new QModelConnectorParam (get_called_class(), 'Minimum', 'Meximum value allowed', QType::String),
241: new QModelConnectorParam (get_called_class(), 'Step', 'If value must be aligned on a step, the step amount', QType::String),
242: new QModelConnectorParam (get_called_class(), 'LabelForLess', 'If value is too small, override the default error message', QType::String),
243: new QModelConnectorParam (get_called_class(), 'LabelForGreater', 'If value is too big, override the default error message', QType::String),
244: new QModelConnectorParam (get_called_class(), 'LabelForNotStepAligned', 'If value is not step aligned, override the default error message', QType::String)
245: ));
246: }
247:
248: }