1: <?php
2:
3: class QListControlBase_CodeGenerator extends QControl_CodeGenerator {
4: public function __construct($strControlClassName = 'QListControl') {
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: public function ConnectorCreate(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn) {
27: $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
28: $strControlVarName = $objCodeGen->ModelConnectorVariableName($objColumn);
29: $strLabelName = addslashes(QCodeGen::ModelConnectorControlName($objColumn));
30: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
31:
32:
33: $strControlType = $objCodeGen->GetControlCodeGenerator($objColumn)->GetControlClass();
34:
35:
36: if (($objColumn instanceof QSqlColumn && $objColumn->Reference->IsType) ||
37: ($objColumn instanceof QManyToManyReference && $objColumn->IsTypeAssociation)
38: ) {
39: $strRet = <<<TMPL
40: /**
41: * Create and setup {$strControlType} {$strControlVarName}
42: * @param string \$strControlId optional ControlId to use
43: * @return {$strControlType}
44: */
45:
46: public function {$strControlVarName}_Create(\$strControlId = null) {
47:
48: TMPL;
49:
50: } else {
51:
52: $strRet = <<<TMPL
53: /**
54: * Create and setup {$strControlType} {$strControlVarName}
55: * @param string \$strControlId optional ControlId to use
56: * @param QQCondition \$objConditions override the default condition of QQ::All() to the query, itself
57: * @param QQClause[] \$objClauses additional QQClause object or array of QQClause objects for the query
58: * @return QListBox
59: */
60:
61: public function {$strControlVarName}_Create(\$strControlId = null, QQCondition \$objCondition = null, \$objClauses = null) {
62: \$this->obj{$strPropName}Condition = \$objCondition;
63: \$this->obj{$strPropName}Clauses = \$objClauses;
64:
65: TMPL;
66:
67: }
68:
69:
70: $strControlIdOverride = $objCodeGen->GenerateControlId($objTable, $objColumn);
71:
72: if ($strControlIdOverride) {
73: $strRet .= <<<TMPL
74: if (!\$strControlId) {
75: \$strControlId = '$strControlIdOverride';
76: }
77:
78: TMPL;
79: }
80:
81: $strRet .= <<<TMPL
82: \$this->{$strControlVarName} = new {$strControlType}(\$this->objParentObject, \$strControlId);
83: \$this->{$strControlVarName}->Name = QApplication::Translate('{$strLabelName}');
84:
85: TMPL;
86:
87: if ($objColumn instanceof QSqlColumn && $objColumn->NotNull) {
88: $strRet .= <<<TMPL
89: \$this->{$strControlVarName}->Required = true;
90:
91: TMPL;
92: }
93:
94: if ($strMethod = QCodeGen::$PreferredRenderMethod) {
95: $strRet .= <<<TMPL
96: \$this->{$strControlVarName}->PreferredRenderMethod = '$strMethod';
97:
98: TMPL;
99: }
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: $objColumn instanceof QManyToManyReference && $objColumn->IsTypeAssociation
112: ) {
113: if ($objColumn instanceof QSqlColumn) {
114: $strVarType = $objColumn->Reference->VariableType;
115: } else {
116: $strVarType = $objColumn->VariableType;
117: }
118: $strRefVarName = null;
119: $strRet .= <<<TMPL
120:
121: /**
122: * Create item list for use by {$strControlVarName}
123: */
124: public function {$strControlVarName}_GetItems() {
125: return {$strVarType}::\$NameArray;
126: }
127:
128:
129: TMPL;
130: } elseif ($objColumn instanceof QManyToManyReference) {
131: $strRefVarName = $objColumn->VariableName;
132: $strVarType = $objColumn->VariableType;
133: $strRefTable = $objColumn->AssociatedTable;
134: $strRefPropName = $objColumn->OppositeObjectDescription;
135: $strRefPK = $objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName;
136:
137:
138: $strRet .= <<<TMPL
139: /**
140: * Create item list for use by {$strControlVarName}
141: */
142: public function {$strControlVarName}_GetItems() {
143: \$a = array();
144: \$objCondition = \$this->obj{$strPropName}Condition;
145: if (is_null(\$objCondition)) \$objCondition = QQ::All();
146: \$objClauses = \$this->obj{$strPropName}Clauses;
147:
148: \$objClauses[] =
149: QQ::Expand(QQN::{$strVarType}()->{$strRefPropName}->{$objTable->ClassName}, QQ::Equal(QQN::{$strVarType}()->{$strRefPropName}->{$objColumn->PropertyName}, \$this->{$strObjectName}->{$strRefPK}));
150:
151: \$obj{$strVarType}Cursor = {$strVarType}::QueryCursor(\$objCondition, \$objClauses);
152:
153: // Iterate through the Cursor
154: while (\${$strRefVarName} = {$strVarType}::InstantiateCursor(\$obj{$strVarType}Cursor)) {
155: \$objListItem = new QListItem(\${$strRefVarName}->__toString(), \${$strRefVarName}->{$strRefPK}, \${$strRefVarName}->_{$strRefPropName} !== null);
156: \$a[] = \$objListItem;
157: }
158: return \$a;
159: }
160:
161: TMPL;
162: } else {
163: if ($objColumn instanceof QSqlColumn) {
164: $strRefVarType = $objColumn->Reference->VariableType;
165: $strRefVarName = $objColumn->Reference->VariableName;
166:
167: $strRefTable = $objColumn->Reference->Table;
168: } elseif ($objColumn instanceof QReverseReference) {
169: $strRefVarType = $objColumn->VariableType;
170: $strRefVarName = $objColumn->VariableName;
171:
172: $strRefTable = $objColumn->Table;
173: }
174: $strRet .= <<<TMPL
175:
176: /**
177: * Create item list for use by {$strControlVarName}
178: */
179: public function {$strControlVarName}_GetItems() {
180: \$a = array();
181: \$objCondition = \$this->obj{$strPropName}Condition;
182: if (is_null(\$objCondition)) \$objCondition = QQ::All();
183: \${$strRefVarName}Cursor = {$strRefVarType}::QueryCursor(\$objCondition, \$this->obj{$strPropName}Clauses);
184:
185: // Iterate through the Cursor
186: while (\${$strRefVarName} = {$strRefVarType}::InstantiateCursor(\${$strRefVarName}Cursor)) {
187: \$objListItem = new QListItem(\${$strRefVarName}->__toString(), \${$strRefVarName}->{$objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName});
188: if ((\$this->{$strObjectName}->{$strPropName}) && (\$this->{$strObjectName}->{$strPropName}->{$objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName} == \${$strRefVarName}->{$objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName}))
189: \$objListItem->Selected = true;
190: \$a[] = \$objListItem;
191: }
192: return \$a;
193: }
194:
195:
196: TMPL;
197: }
198:
199: return $strRet;
200: }
201:
202: 203: 204: 205: 206:
207: public function ConnectorVariableDeclaration(QCodeGenBase $objCodeGen, $objColumn) {
208: $strClassName = $objCodeGen->GetControlCodeGenerator($objColumn)->GetControlClass();
209: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
210: $strControlVarName = $this->VarName($strPropName);
211:
212: $strRet = <<<TMPL
213: /**
214: * @var {$strClassName} {$strControlVarName}
215: * @access protected
216: */
217: protected \${$strControlVarName};
218:
219: /**
220: * @var string str{$strPropName}NullLabel
221: * @access protected
222: */
223: protected \$str{$strPropName}NullLabel;
224:
225:
226: TMPL;
227:
228: if (($objColumn instanceof QSqlColumn && !$objColumn->Reference->IsType) ||
229: ($objColumn instanceof QManyToManyReference && !$objColumn->IsTypeAssociation) ||
230: ($objColumn instanceof QReverseReference)
231: ) {
232: $strRet .= <<<TMPL
233: /**
234: * @var obj{$strPropName}Condition
235: * @access protected
236: */
237: protected \$obj{$strPropName}Condition;
238:
239: /**
240: * @var obj{$strPropName}Clauses
241: * @access protected
242: */
243: protected \$obj{$strPropName}Clauses;
244:
245: TMPL;
246: }
247: return $strRet;
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258:
259: public function ConnectorRefresh(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn, $blnInit = false) {
260: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
261: $strControlVarName = $this->VarName($strPropName);
262: $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
263:
264: $strRet = '';
265:
266: if ($blnInit) {
267: $strRet .= <<<TMPL
268: if (!\$this->str{$strPropName}NullLabel) {
269: if (!\$this->{$strControlVarName}->Required) {
270: \$this->str{$strPropName}NullLabel = '- None -';
271: }
272: elseif (!\$this->blnEditMode) {
273: \$this->str{$strPropName}NullLabel = '- Select One -';
274: }
275: }
276:
277: TMPL;
278: }
279: else {
280: $strRet .= "\$this->{$strControlVarName}->RemoveAllItems();\n";
281: }
282: $strRet .= <<<TMPL
283: \$this->{$strControlVarName}->AddItem(QApplication::Translate(\$this->str{$strPropName}NullLabel), null);
284:
285: TMPL;
286:
287: $options = $objColumn->Options;
288: if (!$options || !isset ($options['NoAutoLoad'])) {
289: $strRet .= "\$this->{$strControlVarName}->AddItems(\$this->{$strControlVarName}_GetItems());\n";
290: }
291:
292: if ($objColumn instanceof QSqlColumn) {
293: $strRet .= "\$this->{$strControlVarName}->SelectedValue = \$this->{$strObjectName}->{$objColumn->PropertyName};\n";
294: } elseif ($objColumn instanceof QReverseReference && $objColumn->Unique) {
295: $strRet .= "if (\$this->{$strObjectName}->{$objColumn->ObjectPropertyName})\n";
296: $strRet .= _indent("\$this->{$strControlVarName}->SelectedValue = \$this->{$strObjectName}->{$objColumn->ObjectPropertyName}->{$objCodeGen->GetTable($objColumn->Table)->PrimaryKeyColumnArray[0]->PropertyName};\n");
297: } elseif ($objColumn instanceof QManyToManyReference) {
298: if ($objColumn->IsTypeAssociation) {
299: $strRet .= "\$this->{$strControlVarName}->SelectedValues = array_keys(\$this->{$strObjectName}->Get{$objColumn->ObjectDescription}Array());\n";
300: } else {
301:
302: }
303: }
304: if (!$blnInit) {
305: $strRet = _indent($strRet);
306: $strRet = <<<TMPL
307: if (\$this->{$strControlVarName}) {
308: $strRet
309: }
310:
311: TMPL;
312: }
313: $strRet = _indent($strRet, 3);
314: return $strRet;
315: }
316:
317: 318: 319: 320: 321: 322:
323: public function ConnectorUpdate(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn) {
324: $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
325: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
326: $strControlVarName = $this->VarName($strPropName);
327: $strRet = '';
328: if ($objColumn instanceof QSqlColumn) {
329: $strRet = <<<TMPL
330: if (\$this->{$strControlVarName}) \$this->{$strObjectName}->{$objColumn->PropertyName} = \$this->{$strControlVarName}->SelectedValue;
331:
332: TMPL;
333: } elseif ($objColumn instanceof QReverseReference) {
334: $strRet = <<<TMPL
335: if (\$this->{$strControlVarName}) \$this->{$strObjectName}->{$objColumn->ObjectPropertyName} = {$objColumn->VariableType}::Load(\$this->{$strControlVarName}->SelectedValue);
336:
337: TMPL;
338: }
339: return $strRet;
340: }
341:
342: 343: 344: 345: 346: 347: 348: 349: 350:
351: public function ConnectorUpdateMethod(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn) {
352: $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
353: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
354: $strControlVarName = $this->VarName($strPropName);
355: $strRet = <<<TMPL
356: protected function {$strControlVarName}_Update() {
357: if (\$this->{$strControlVarName}) {
358:
359: TMPL;
360:
361: if ($objColumn instanceof QManyToManyReference) {
362: if ($objColumn->IsTypeAssociation) {
363: $strRet .= <<<TMPL
364: \$this->{$strObjectName}->UnassociateAll{$objColumn->ObjectDescriptionPlural}();
365: \$this->{$strObjectName}->Associate{$objColumn->ObjectDescription}(\$this->{$strControlVarName}->SelectedValues);
366:
367: TMPL;
368: } else {
369: $strRet .= <<<TMPL
370: \$this->{$strObjectName}->UnassociateAll{$objColumn->ObjectDescriptionPlural}();
371: foreach(\$this->{$strControlVarName}->SelectedValues as \$id) {
372: \$this->{$strObjectName}->Associate{$objColumn->ObjectDescription}ByKey(\$id);
373: }
374:
375: TMPL;
376: }
377: }
378:
379: $strRet .= <<<TMPL
380: }
381: }
382:
383: TMPL;
384:
385: return $strRet;
386: }
387:
388: public function ConnectorSet(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn) {
389: $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
390: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
391: $strControlVarName = $this->VarName($strPropName);
392: $strRet = <<<TMPL
393: case '{$strPropName}NullLabel':
394: return \$this->str{$strPropName}NullLabel = \$mixValue;
395:
396: TMPL;
397: return $strRet;
398: }
399:
400:
401: public function ConnectorGet(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn) {
402: $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
403: $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
404: $strControlVarName = $this->VarName($strPropName);
405: $strRet = <<<TMPL
406: case '{$strPropName}NullLabel':
407: return \$this->str{$strPropName}NullLabel;
408:
409: TMPL;
410: return $strRet;
411: }
412:
413: }
414:
415: