1: <?php
2: 3: 4:
5:
6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class QCsvTextBox extends QTextBox {
17:
18: protected $strDelimiter = ',';
19:
20: protected $strEnclosure = '"';
21:
22: protected $strEscape = '\\';
23:
24: protected $intMinItemCount = null;
25:
26: protected $intMaxItemCount = null;
27:
28: 29: 30: 31: 32: 33:
34: public function __construct($objParentObject, $strControlId = null) {
35: parent::__construct($objParentObject, $strControlId);
36:
37: $this->strLabelForTooShort = QApplication::Translate('Enter at least %s items.');
38: $this->strLabelForTooLong = QApplication::Translate('Enter no more than %s items.');
39: }
40:
41: 42: 43: 44:
45: public function Validate() {
46: $blnRet = parent::Validate();
47: if ($blnRet) {
48: $a = str_getcsv($this->strText);
49:
50: if ($this->intMinItemCount !== null &&
51: count ($a) < $this->intMinItemCount) {
52: $this->ValidationError = sprintf($this->strLabelForTooShort, $this->intMinItemCount);
53: return false;
54: }
55:
56: if ($this->intMaxItemCount !== null &&
57: count ($a) > $this->intMaxItemCount) {
58: $this->ValidationError = sprintf($this->strLabelForTooLong, $this->intMaxItemCount);
59: return false;
60: }
61:
62: }
63:
64:
65: return true;
66: }
67:
68: 69: 70: 71: 72: 73: 74:
75: public function __get($strName) {
76: switch ($strName) {
77:
78: case "Delimiter": return $this->strDelimiter;
79: case "Enclosure": return $this->strEnclosure;
80: case "Escape": return $this->strEscape;
81: case "MinItemCount": return $this->intMinItemCount;
82: case "MaxItemCount": return $this->intMaxItemCount;
83: case 'Value':
84: if (empty($this->strText)) return array();
85: return str_getcsv($this->strText, $this->strDelimiter, $this->strEnclosure, $this->strEscape);
86:
87: default:
88: try {
89: return parent::__get($strName);
90: } catch (QCallerException $objExc) {
91: $objExc->IncrementOffset();
92: throw $objExc;
93: }
94: }
95: }
96:
97:
98:
99:
100: 101: 102: 103: 104: 105: 106: 107: 108:
109: public function __set($strName, $mixValue) {
110: $this->blnModified = true;
111:
112: switch ($strName) {
113:
114: case "Delimiter":
115: try {
116: $this->strDelimiter = QType::Cast($mixValue, QType::String);
117: break;
118: } catch (QInvalidCastException $objExc) {
119: $objExc->IncrementOffset();
120: throw $objExc;
121: }
122: case "Enclosure":
123: try {
124: $this->strEnclosure = QType::Cast($mixValue, QType::String);
125: break;
126: } catch (QInvalidCastException $objExc) {
127: $objExc->IncrementOffset();
128: throw $objExc;
129: }
130: case "Escape":
131: try {
132: $this->strEscape = QType::Cast($mixValue, QType::String);
133: break;
134: } catch (QInvalidCastException $objExc) {
135: $objExc->IncrementOffset();
136: throw $objExc;
137: }
138: case "MinItemCount":
139: try {
140: $this->intMinItemCount = QType::Cast($mixValue, QType::Integer);
141: break;
142: } catch (QInvalidCastException $objExc) {
143: $objExc->IncrementOffset();
144: throw $objExc;
145: }
146: case "MaxItemCount":
147: try {
148: $this->intMaxItemCount = QType::Cast($mixValue, QType::Integer);
149: break;
150: } catch (QInvalidCastException $objExc) {
151: $objExc->IncrementOffset();
152: throw $objExc;
153: }
154: case "Value":
155: try {
156: $a = QType::Cast($mixValue, QType::ArrayType);
157: $temp_memory = fopen('php://memory', 'w');
158: fputcsv($temp_memory, $a, $this->strDelimiter, $this->strEnclosure);
159: rewind($temp_memory);
160: $this->strText = fgets($temp_memory);
161: fclose($temp_memory);
162: break;
163: } catch (QInvalidCastException $objExc) {
164: $objExc->IncrementOffset();
165: throw $objExc;
166: }
167:
168: default:
169: try {
170: parent::__set($strName, $mixValue);
171: } catch (QCallerException $objExc) {
172: $objExc->IncrementOffset();
173: throw $objExc;
174: }
175: break;
176: }
177: }
178:
179: 180: 181: 182: 183:
184: public static function GetModelConnectorParams() {
185: return array_merge(parent::GetModelConnectorParams(), array(
186: new QModelConnectorParam (get_called_class(), 'Delimiter', 'Default: , (comma)', QType::String),
187: new QModelConnectorParam (get_called_class(), 'Enclosure', 'Default: " (double-quote)', QType::String),
188: new QModelConnectorParam (get_called_class(), 'Escape', 'Default: \\ (backslash)', QType::String),
189: new QModelConnectorParam (get_called_class(), 'MinItemCount', 'Minimum number of items required.', QType::Integer),
190: new QModelConnectorParam (get_called_class(), 'MaxItemCount', 'Maximum number of items allowed.', QType::Integer)
191: ));
192: }
193:
194: }