/**
     * Generate code that will be inserted into the ModelConnector to connect a database object with this control.
     * This is called during the codegen process. This is very similar to the QListControl code, but there are
     * some differences. In particular, this control does not support ManyToMany references.
     *
     * @param QCodeGenBase $objCodeGen
     * @param QSqlTable $objTable
     * @param QSqlColumn|QReverseReference|QManyToManyReference $objColumn
     * @throws Exception
     * @return string
     */
    public function ConnectorCreate(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn)
    {
        $strLabelName = addslashes(QCodeGen::ModelConnectorControlName($objColumn));
        $strControlType = 'QLabel';
        $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
        $strControlVarName = $this->VarName($strPropName);
        $strDateTimeExtra = '';
        $strDateTimeParamExtra = '';
        if ($objColumn->VariableType == 'QDateTime') {
            $strDateTimeExtra = ', $strDateTimeFormat = null';
            $strDateTimeParamExtra = "\n\t\t * @param string \$strDateTimeFormat";
        }
        $strRet = <<<TMPL
\t\t/**
\t\t * Create and setup {$strControlType} {$strControlVarName}
\t\t *
\t\t * @param string \$strControlId optional ControlId to use{$strDateTimeParamExtra}
\t\t * @return {$strControlType}
\t\t */
\t\tpublic function {$strControlVarName}_Create(\$strControlId = null{$strDateTimeExtra}) {

TMPL;
        $strControlIdOverride = $objCodeGen->GenerateControlId($objTable, $objColumn);
        if ($strControlIdOverride) {
            $strRet .= <<<TMPL
\t\t\tif (!\$strControlId) {
\t\t\t\t\$strControlId = '{$strControlIdOverride}';
\t\t\t}

TMPL;
        }
        $strRet .= <<<TMPL
\t\t\t\$this->{$strControlVarName} = new {$strControlType}(\$this->objParentObject, \$strControlId);
\t\t\t\$this->{$strControlVarName}->Name = QApplication::Translate('{$strLabelName}');

TMPL;
        if ($objColumn->VariableType == 'QDateTime') {
            $strRet .= <<<TMPL
\t\t\t\$this->str{$strPropName}DateTimeFormat = \$strDateTimeFormat;

TMPL;
        }
        if ($strMethod = QCodeGen::$PreferredRenderMethod) {
            $strRet .= <<<TMPL
\t\t\t\$this->{$strControlVarName}->PreferredRenderMethod = '{$strMethod}';

TMPL;
        }
        $strRet .= $this->ConnectorCreateOptions($objCodeGen, $objTable, $objColumn, $strControlVarName);
        $strRet .= $this->ConnectorRefresh($objCodeGen, $objTable, $objColumn, true);
        $strRet .= <<<TMPL
\t\t\treturn \$this->{$strControlVarName};
\t\t}


TMPL;
        return $strRet;
    }
    /**
     * Generate code that will be inserted into the ModelConnector to connect a database object with this control.
     * This is called during the codegen process. This is very similar to the QListControl code, but there are
     * some differences. In particular, this control does not support ManyToMany references.
     *
     * @param QCodeGenBase $objCodeGen
     * @param QSqlTable $objTable
     * @param QSqlColumn|QReverseReference|QManyToManyReference $objColumn
     * @return string
     */
    public function ConnectorCreate(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn)
    {
        $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
        $strControlVarName = $objCodeGen->ModelConnectorVariableName($objColumn);
        $strLabelName = addslashes(QCodeGen::ModelConnectorControlName($objColumn));
        // Read the control type in case we are generating code for a subclass
        $strControlType = $objCodeGen->GetControlCodeGenerator($objColumn)->GetControlClass();
        $strRet = <<<TMPL
\t\t/**
\t\t * Create and setup a {$strControlType} {$strControlVarName}
\t\t * @param string \$strControlId optional ControlId to use
\t\t * @return {$strControlType}
\t\t */
\t\tpublic function {$strControlVarName}_Create(\$strControlId = null) {

TMPL;
        $strControlIdOverride = $objCodeGen->GenerateControlId($objTable, $objColumn);
        if ($strControlIdOverride) {
            $strRet .= <<<TMPL
\t\t\tif (!\$strControlId) {
\t\t\t\t\$strControlId = '{$strControlIdOverride}';
\t\t\t}

TMPL;
        }
        $strRet .= <<<TMPL
\t\t\t\$this->{$strControlVarName} = new {$strControlType}(\$this->objParentObject, \$strControlId);
\t\t\t\$this->{$strControlVarName}->Name = QApplication::Translate('{$strLabelName}');
\t\t\t\$this->{$strControlVarName}->DateTime = \$this->{$strObjectName}->{$objColumn->PropertyName};

TMPL;
        switch ($objColumn->DbType) {
            case QDatabaseFieldType::DateTime:
                $strRet .= "\t\t\t\$this->{$strControlVarName}->DateTimePickerType = QDateTimePickerType::DateTime;\n";
                break;
            case QDatabaseFieldType::Time:
                $strRet .= "\t\t\t\$this->{$strControlVarName}->DateTimePickerType = QDateTimePickerType::Time;\n";
                break;
            default:
                $strRet .= "\t\t\t\$this->{$strControlVarName}->DateTimePickerType = QDateTimePickerType::Date;\n";
        }
        if ($strMethod = QCodeGen::$PreferredRenderMethod) {
            $strRet .= <<<TMPL
\t\t\t\$this->{$strControlVarName}->PreferredRenderMethod = '{$strMethod}';

TMPL;
        }
        $strRet .= $this->ConnectorCreateOptions($objCodeGen, $objTable, $objColumn, $strControlVarName);
        $strRet .= <<<TMPL
\t\t\treturn \$this->{$strControlVarName};
\t\t}


TMPL;
        return $strRet;
    }
    /**
     * Generate code that will be inserted into the ModelConnector to connect a database object with this control.
     * This is called during the codegen process. This is very similar to the QListControl code, but there are
     * some differences. In particular, this control does not support ManyToMany references.
     *
     * @param QCodeGenBase $objCodeGen
     * @param QSqlTable $objTable
     * @param QSqlColumn|QReverseReference|QManyToManyReference $objColumn
     * @return string
     */
    public function ConnectorCreate(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn)
    {
        $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
        $strClassName = $objTable->ClassName;
        $strControlVarName = $objCodeGen->ModelConnectorVariableName($objColumn);
        $strLabelName = addslashes(QCodeGen::ModelConnectorControlName($objColumn));
        // Read the control type in case we are generating code for a subclass of QTextBox
        $strControlType = $objCodeGen->GetControlCodeGenerator($objColumn)->GetControlClass();
        $strRet = <<<TMPL
\t\t/**
\t\t * Create and setup a {$strControlType} {$strControlVarName}
\t\t * @param string \$strControlId optional ControlId to use
\t\t * @return {$strControlType}
\t\t */
\t\tpublic function {$strControlVarName}_Create(\$strControlId = null) {

TMPL;
        $strControlIdOverride = $objCodeGen->GenerateControlId($objTable, $objColumn);
        if ($strControlIdOverride) {
            $strRet .= <<<TMPL
\t\t\tif (!\$strControlId) {
\t\t\t\t\$strControlId = '{$strControlIdOverride}';
\t\t\t}

TMPL;
        }
        $strRet .= <<<TMPL
\t\t\t\$this->{$strControlVarName} = new {$strControlType}(\$this->objParentObject, \$strControlId);
\t\t\t\$this->{$strControlVarName}->Name = QApplication::Translate('{$strLabelName}');

TMPL;
        if ($strMethod = QCodeGen::$PreferredRenderMethod) {
            $strRet .= <<<TMPL
\t\t\t\$this->{$strControlVarName}->PreferredRenderMethod = '{$strMethod}';

TMPL;
        }
        $strRet .= $this->ConnectorCreateOptions($objCodeGen, $objTable, $objColumn, $strControlVarName);
        $strRet .= $this->ConnectorRefresh($objCodeGen, $objTable, $objColumn, true);
        $strRet .= <<<TMPL
\t\t\treturn \$this->{$strControlVarName};
\t\t}


TMPL;
        return $strRet;
    }
    /**
     * Generate code that will be inserted into the ModelConnector to connect a database object with this control.
     * This is called during the codegen process. This is very similar to the QListControl code, but there are
     * some differences. In particular, this control does not support ManyToMany references.
     *
     * @param QCodeGenBase $objCodeGen
     * @param QSqlTable $objTable
     * @param QSqlColumn|QReverseReference|QManyToManyReference $objColumn
     * @return string
     */
    public function ConnectorCreate(QCodeGenBase $objCodeGen, QSqlTable $objTable, $objColumn)
    {
        $strObjectName = $objCodeGen->ModelVariableName($objTable->Name);
        $strControlVarName = $objCodeGen->ModelConnectorVariableName($objColumn);
        $strLabelName = addslashes(QCodeGen::ModelConnectorControlName($objColumn));
        $strPropName = QCodeGen::ModelConnectorPropertyName($objColumn);
        // Read the control type in case we are generating code for a similar class
        $strControlType = $objCodeGen->GetControlCodeGenerator($objColumn)->GetControlClass();
        // Create a control designed just for selecting from a type table
        if ($objColumn instanceof QSqlColumn && $objColumn->Reference->IsType || $objColumn instanceof QManyToManyReference && $objColumn->IsTypeAssociation) {
            $strRet = <<<TMPL
\t\t/**
\t\t * Create and setup {$strControlType} {$strControlVarName}
\t\t * @param string \$strControlId optional ControlId to use
\t\t * @return {$strControlType}
\t\t */

\t\tpublic function {$strControlVarName}_Create(\$strControlId = null) {

TMPL;
        } else {
            // Create a control that presents a list taken from the database
            $strRet = <<<TMPL
\t\t/**
\t\t * Create and setup {$strControlType} {$strControlVarName}
\t\t * @param string \$strControlId optional ControlId to use
\t\t * @param QQCondition \$objConditions override the default condition of QQ::All() to the query, itself
\t\t * @param QQClause[] \$objClauses additional QQClause object or array of QQClause objects for the query
\t\t * @return QListBox
\t\t */

\t\tpublic function {$strControlVarName}_Create(\$strControlId = null, QQCondition \$objCondition = null, \$objClauses = null) {
\t\t\t\$this->obj{$strPropName}Condition = \$objCondition;
\t\t\t\$this->obj{$strPropName}Clauses = \$objClauses;

TMPL;
        }
        // Allow the codegen process to either create custom ids based on the field/table names, or to be
        // Specified by the developer.
        $strControlIdOverride = $objCodeGen->GenerateControlId($objTable, $objColumn);
        if ($strControlIdOverride) {
            $strRet .= <<<TMPL
\t\t\tif (!\$strControlId) {
\t\t\t\t\$strControlId = '{$strControlIdOverride}';
\t\t\t}

TMPL;
        }
        $strRet .= <<<TMPL
\t\t\t\$this->{$strControlVarName} = new {$strControlType}(\$this->objParentObject, \$strControlId);
\t\t\t\$this->{$strControlVarName}->Name = QApplication::Translate('{$strLabelName}');

TMPL;
        if ($objColumn instanceof QSqlColumn && $objColumn->NotNull) {
            $strRet .= <<<TMPL
\t\t\t\$this->{$strControlVarName}->Required = true;

TMPL;
        }
        if ($strMethod = QCodeGen::$PreferredRenderMethod) {
            $strRet .= <<<TMPL
\t\t\t\$this->{$strControlVarName}->PreferredRenderMethod = '{$strMethod}';

TMPL;
        }
        $strRet .= $this->ConnectorCreateOptions($objCodeGen, $objTable, $objColumn, $strControlVarName);
        $strRet .= $this->ConnectorRefresh($objCodeGen, $objTable, $objColumn, true);
        $strRet .= <<<TMPL
\t\t\treturn \$this->{$strControlVarName};
\t\t}

TMPL;
        if ($objColumn instanceof QSqlColumn && $objColumn->Reference->IsType || $objColumn instanceof QManyToManyReference && $objColumn->IsTypeAssociation) {
            if ($objColumn instanceof QSqlColumn) {
                $strVarType = $objColumn->Reference->VariableType;
            } else {
                $strVarType = $objColumn->VariableType;
            }
            $strRefVarName = null;
            $strRet .= <<<TMPL

\t\t/**
\t\t *\tCreate item list for use by {$strControlVarName}
\t\t */
\t\tpublic function {$strControlVarName}_GetItems() {
\t\t\treturn {$strVarType}::\$NameArray;
\t\t}


TMPL;
        } elseif ($objColumn instanceof QManyToManyReference) {
            $strRefVarName = $objColumn->VariableName;
            $strVarType = $objColumn->VariableType;
            $strRefTable = $objColumn->AssociatedTable;
            $strRefPropName = $objColumn->OppositeObjectDescription;
            $strRefPK = $objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName;
            //$strPK = $objTable->PrimaryKeyColumnArray[0]->PropertyName;
            $strRet .= <<<TMPL
\t\t/**
\t\t *\tCreate item list for use by {$strControlVarName}
\t\t */
\t\tpublic function {$strControlVarName}_GetItems() {
\t\t\t\$a = array();
\t\t\t\$objCondition = \$this->obj{$strPropName}Condition;
\t\t\tif (is_null(\$objCondition)) \$objCondition = QQ::All();
\t\t\t\$objClauses = \$this->obj{$strPropName}Clauses;

\t\t\t\$objClauses[] =
\t\t\t\tQQ::Expand(QQN::{$strVarType}()->{$strRefPropName}->{$objTable->ClassName}, QQ::Equal(QQN::{$strVarType}()->{$strRefPropName}->{$objColumn->PropertyName}, \$this->{$strObjectName}->{$strRefPK}));

\t\t\t\$obj{$strVarType}Cursor = {$strVarType}::QueryCursor(\$objCondition, \$objClauses);

\t\t\t// Iterate through the Cursor
\t\t\twhile (\${$strRefVarName} = {$strVarType}::InstantiateCursor(\$obj{$strVarType}Cursor)) {
\t\t\t\t\$objListItem = new QListItem(\${$strRefVarName}->__toString(), \${$strRefVarName}->{$strRefPK}, \${$strRefVarName}->_{$strRefPropName} !== null);
\t\t\t\t\$a[] = \$objListItem;
\t\t\t}
\t\t\treturn \$a;
\t\t}

TMPL;
        } else {
            if ($objColumn instanceof QSqlColumn) {
                $strRefVarType = $objColumn->Reference->VariableType;
                $strRefVarName = $objColumn->Reference->VariableName;
                //$strRefPropName = $objColumn->Reference->PropertyName;
                $strRefTable = $objColumn->Reference->Table;
            } elseif ($objColumn instanceof QReverseReference) {
                $strRefVarType = $objColumn->VariableType;
                $strRefVarName = $objColumn->VariableName;
                //$strRefPropName = $objColumn->PropertyName;
                $strRefTable = $objColumn->Table;
            }
            $strRet .= <<<TMPL

\t\t/**
\t\t *\tCreate item list for use by {$strControlVarName}
\t\t */
\t\t public function {$strControlVarName}_GetItems() {
\t\t\t\$a = array();
\t\t\t\$objCondition = \$this->obj{$strPropName}Condition;
\t\t\tif (is_null(\$objCondition)) \$objCondition = QQ::All();
\t\t\t\${$strRefVarName}Cursor = {$strRefVarType}::QueryCursor(\$objCondition, \$this->obj{$strPropName}Clauses);

\t\t\t// Iterate through the Cursor
\t\t\twhile (\${$strRefVarName} = {$strRefVarType}::InstantiateCursor(\${$strRefVarName}Cursor)) {
\t\t\t\t\$objListItem = new QListItem(\${$strRefVarName}->__toString(), \${$strRefVarName}->{$objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName});
\t\t\t\tif ((\$this->{$strObjectName}->{$strPropName}) && (\$this->{$strObjectName}->{$strPropName}->{$objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName} == \${$strRefVarName}->{$objCodeGen->GetTable($strRefTable)->PrimaryKeyColumnArray[0]->PropertyName}))
\t\t\t\t\t\$objListItem->Selected = true;
\t\t\t\t\$a[] = \$objListItem;
\t\t\t}
\t\t\treturn \$a;
\t\t }


TMPL;
        }
        return $strRet;
    }
 public function GenerateControlId($objTable, $objColumn)
 {
     $strControlId = null;
     if (isset($objColumn->Options['ControlId'])) {
         $strControlId = $objColumn->Options['ControlId'];
     } elseif ($this->blnGenerateControlId) {
         $strObjectName = $this->ModelVariableName($objTable->Name);
         $strClassName = $objTable->ClassName;
         $strControlVarName = $this->ModelConnectorVariableName($objColumn);
         $strLabelName = QCodeGen::ModelConnectorControlName($objColumn);
         $strControlId = $strControlVarName . $strClassName;
     }
     return $strControlId;
 }