1: <?php
2:
3: class QAutocompleteBase_CodeGenerator extends QTextBox_CodeGenerator {
4: public function __construct($strControlClassName = 'QAutocomplete') {
5: parent::__construct($strControlClassName);
6: }
7:
8: 9: 10: 11:
12: public function VarName($strPropName) {
13: return 'lst' . $strPropName;
14: }
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: public function ConnectorCreate(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn) {
28: if ($objColumn instanceof QManyToManyReference) {
29: throw new Exception ("Autocomplete does not support many-to-many references.");
30: }
31:
32: $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
33: $strControlVarName = $objCodeGen->ModelConnectorVariableName($objColumn);
34: $strLabelName = addslashes(QCodeGen::ModelConnectorControlName($objColumn));
35: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
36:
37:
38: $strControlType = $objCodeGen->GetControlCodeGenerator($objColumn)->GetControlClass();
39:
40:
41: if ($objColumn instanceof QSqlColumn && $objColumn->Reference->IsType) {
42: $strRet = <<<TMPL
43: /**
44: * Create and setup {$strControlType} {$strControlVarName}
45: * @param string \$strControlId optional ControlId to use
46: * @return {$strControlType}
47: */
48:
49: public function {$strControlVarName}_Create(\$strControlId = null) {
50:
51: TMPL;
52: } else {
53:
54: $strRet = <<<TMPL
55: /**
56: * Create and setup {$strControlType} {$strControlVarName}
57: * @param null|string \$strControlId optional ControlId to use
58: * @param null|QQCondition \$objConditions override the default condition of QQ::All() to the query, itself
59: * @param null|QQClause[] \$objClauses additional QQClause object or array of QQClause objects for the query
60: * @return {$strControlType}
61: */
62:
63: public function {$strControlVarName}_Create(\$strControlId = null, QQCondition \$objCondition = null, \$objClauses = null) {
64: \$this->obj{$strPropName}Condition = \$objCondition;
65: \$this->obj{$strPropName}Clauses = \$objClauses;
66:
67: TMPL;
68: }
69:
70:
71:
72: $strControlIdOverride = $objCodeGen->GenerateControlId($objTable, $objColumn);
73:
74: if ($strControlIdOverride) {
75: $strRet .= <<<TMPL
76: if (!\$strControlId) {
77: \$strControlId = '$strControlIdOverride';
78: }
79:
80: TMPL;
81: }
82:
83: $strRet .= <<<TMPL
84: \$this->{$strControlVarName} = new {$strControlType}(\$this->objParentObject, \$strControlId);
85: \$this->{$strControlVarName}->Name = QApplication::Translate('{$strLabelName}');
86:
87: TMPL;
88: if ($objColumn instanceof QSqlColumn && $objColumn->NotNull) {
89: $strRet .= <<<TMPL
90: \$this->{$strControlVarName}->Required = true;
91:
92: TMPL;
93: }
94:
95: if ($strMethod = QCodeGen::$PreferredRenderMethod) {
96: $strRet .= <<<TMPL
97: \$this->{$strControlVarName}->PreferredRenderMethod = '$strMethod';
98:
99: TMPL;
100: }
101: $strRet .= $this->ConnectorCreateOptions($objCodeGen, $objTable, $objColumn, $strControlVarName);
102: $strRet .= $this->ConnectorRefresh($objCodeGen, $objTable, $objColumn, true);
103:
104: $strRet .= <<<TMPL
105: return \$this->{$strControlVarName};
106: }
107:
108: TMPL;
109:
110: if ($objColumn instanceof QSqlColumn && $objColumn->Reference->IsType) {
111: if ($objColumn instanceof QSqlColumn) {
112: $strVarType = $objColumn->Reference->VariableType;
113: } else {
114: $strVarType = $objColumn->VariableType;
115: }
116: $strRet .= <<<TMPL
117:
118: /**
119: * Create item list for use by {$strControlVarName}
120: */
121: public function {$strControlVarName}_GetItems() {
122: return {$strVarType}::\$NameArray;
123: }
124:
125:
126: TMPL;
127: } else {
128: if ($objColumn instanceof QSqlColumn) {
129: $strRefVarType = $objColumn->Reference->VariableType;
130: $strRefVarName = $objColumn->Reference->VariableName;
131: $strRefTable = $objColumn->Reference->Table;
132: } elseif ($objColumn instanceof QReverseReference) {
133: $strRefVarType = $objColumn->VariableType;
134: $strRefVarName = $objColumn->VariableName;
135: $strRefTable = $objColumn->Table;
136: } else {
137: throw new Exception ("Unprepared to handle this column type.");
138: }
139:
140: $strRet .= <<<TMPL
141:
142: /**
143: * Create item list for use by {$strControlVarName}
144: */
145: public function {$strControlVarName}_GetItems() {
146: \$a = array();
147: \$objCondition = \$this->obj{$strPropName}Condition;
148: if (is_null(\$objCondition)) \$objCondition = QQ::All();
149: \${$strRefVarName}Cursor = {$strRefVarType}::QueryCursor(\$objCondition, \$this->obj{$strPropName}Clauses);
150:
151: // Iterate through the Cursor
152: while (\${$strRefVarName} = {$strRefVarType}::InstantiateCursor(\${$strRefVarName}Cursor)) {
153: \$objListItem = new QListItem(\${$strRefVarName}->__toString(), \${$strRefVarName}->{$objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName});
154: if ((\$this->{$strObjectName}->{$strPropName}) && (\$this->{$strObjectName}->{$strPropName}->{$objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName} == \${$strRefVarName}->{$objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName}))
155: \$objListItem->Selected = true;
156: \$a[] = \$objListItem;
157: }
158: return \$a;
159: }
160:
161:
162: TMPL;
163: }
164: return $strRet;
165: }
166:
167: 168: 169: 170: 171:
172: public function ConnectorVariableDeclaration(QCodeGenBase $objCodeGen, $objColumn) {
173: $strClassName = $objCodeGen->GetControlCodeGenerator($objColumn)->GetControlClass();
174: $strPropName = $objCodeGen->ModelConnectorPropertyName($objColumn);
175: $strControlVarName = $this->VarName($strPropName);
176:
177: $strRet = <<<TMPL
178: /**
179: * @var {$strClassName} {$strControlVarName}
180: * @access protected
181: */
182: protected \${$strControlVarName};
183:
184: /**
185: * @var obj{$strPropName}Condition
186: * @access protected
187: */
188: protected \$obj{$strPropName}Condition;
189:
190: /**
191: * @var obj{$strPropName}Clauses
192: * @access protected
193: */
194: protected \$obj{$strPropName}Clauses;
195:
196: TMPL;
197:
198: return $strRet;
199: }
200:
201: 202: 203: 204: 205: 206: 207: 208: 209:
210: public function ConnectorRefresh(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn, $blnInit = false) {
211: $strPrimaryKey = $objCodeGen->GetTable($objColumn->Reference->Table)->PrimaryKeyColumnArray[0]->PropertyName;
212: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
213: $strControlVarName = $this->VarName($strPropName);
214: $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
215: $strRet = '';
216:
217: if (!$blnInit) {
218: $t = "\t";
219: $strRet = <<<TMPL
220: if (\$this->{$strControlVarName}) {
221:
222: TMPL;
223: } else {
224: $t = '';
225: }
226:
227: $options = $objColumn->Options;
228: if (!$options || !isset ($options['NoAutoLoad'])) {
229: $strRet .= <<<TMPL
230: $t \$this->{$strControlVarName}->Source = \$this->{$strControlVarName}_GetItems();
231:
232: TMPL;
233: }
234: $strRet .= <<<TMPL
235: $t if (\$this->{$strObjectName}->{$strPropName}) {
236: $t \$this->{$strControlVarName}->Text = \$this->{$strObjectName}->{$strPropName}->__toString();
237: $t \$this->{$strControlVarName}->SelectedValue = \$this->{$strObjectName}->{$strPropName}->{$strPrimaryKey};
238: $t }
239: $t else {
240: $t \$this->{$strControlVarName}->Text = '';
241: $t \$this->{$strControlVarName}->SelectedValue = null;
242: $t }
243:
244: TMPL;
245:
246: if (!$blnInit) {
247: $strRet .= <<<TMPL
248: }
249:
250: TMPL;
251: }
252: return $strRet;
253: }
254:
255: 256: 257: 258: 259: 260:
261: public function ConnectorUpdate(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn) {
262: $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
263: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
264: $strControlVarName = $this->VarName($strPropName);
265:
266: $strRet = '';
267: if ($objColumn instanceof QSqlColumn) {
268:
269: $strRet = <<<TMPL
270: if (\$this->{$strControlVarName}) \$this->{$strObjectName}->{$objColumn->PropertyName} = \$this->{$strControlVarName}->SelectedValue;
271:
272: TMPL;
273: } elseif ($objColumn instanceof QReverseReference) {
274: $strRet = <<<TMPL
275: if (\$this->{$strControlVarName}) \$this->{$strObjectName}->{$objColumn->ObjectPropertyName} = {$objColumn->VariableType}::Load(\$this->{$strControlVarName}->SelectedValue);
276:
277: TMPL;
278: }
279: return $strRet;
280: }
281: }