<?php
/**
 * This is the template for generating the model class of a specified table.
 * - $this: the ModelCode object
 * - $tableName: the table name for this class (prefix is already removed if necessary)
 * - $modelClass: the model class name
 * - $columns: list of table columns (name=>CDbColumnSchema)
 * - $labels: list of attribute labels (name=>label)
 * - $rules: list of validation rules
 * - $relations: list of relations (name=>relation declaration)
 */

function sqlTypeToCType($type) {

	if ($type=="string") {
		return "QString";
	} else if ($type == "integer") {
		return "int";
	} else if ($type == "boolean") {
                return "bool";
        }

	return $type;

}

function sqlTypeToQVariantConvert($type) {

        if ($type=="string") {
                return "toString()";
        } else if ($type == "integer") {
                return "toInt()";
        } else if ($type == "boolean") {
                return "toInt()";
        }
        return $type;

}


?>
<?php 
echo "#ifndef ".strtoupper($modelClass)."_H\n";
echo "#define ".strtoupper($modelClass)."_H\n";
?>

/**
 * This is the model class for table "<?php echo $tableName; ?>".
 *
 * The followings are the available columns in table '<?php echo $tableName; ?>':
<?php foreach($columns as $column): ?>
 * @property <?php echo $column->type.' '.$column->name."\n"; ?>
<?php endforeach; ?>
 *
**/

#include <QObject>
#include "cmodel.h"

class <?php echo $modelClass; ?> : public CModel
{
    Q_OBJECT
<?php foreach($columns as $column) { //Generates QProperty
	echo "    Q_PROPERTY(".sqlTypeToCType($column->type)." ".$column->name." READ ".$column->name." WRITE set".str_replace(" ","",$labels[$column->name])." NOTIFY ".$column->name."Changed)\n";
}
?>

public:
    explicit <?php echo $modelClass; ?>() { setTableName("<?php echo $tableName; ?>"); setModelName("<?php echo $modelClass; ?>"); }

<?php foreach($columns as $column) { //Generates Getters and Setters definitions
        echo "    void set".str_replace(" ","",$labels[$column->name])."(".sqlTypeToCType($column->type)." ".$column->name.") { m_".$column->name." = ".$column->name."; }\n";
	echo "    ".sqlTypeToCType($column->type)." ".$column->name."() { return m_".$column->name."; }\n";
} 
?>

    QStringList getFieldNames() {
        QStringList ret;
<?php
	foreach($columns as $column) {
	       echo '        ret << "'.$column->name.'";'."\n";
	}
?>
        return ret;
    }

    void setValueForKey(QString key, QVariant value) {
<?php
	foreach($columns as $column) {
               echo '        if (key == "'.$column->name.'") m_'.$column->name.'=value.'.sqlTypeToQVariantConvert($column->type).';'."\n";
        }
?>

    }

   QVariant valueForKey(QString key) {
<?php
        foreach($columns as $column) {
               echo '        if (key == "'.$column->name.'") return m_'.$column->name.";\n";
        }
?>
	return QVariant();
    }

    <?php echo $modelClass; ?>* findByPK(int pk) {
        QueryBuilder builder(QueryBuilder::TypeSelect);
        builder.SetTable(getTableName());

        QStringList fields = getFieldNames();
        foreach(const QString &field, fields) {
            builder.AddField(field, "");
        }

        builder.SetID(QString::number(pk));
        QSqlQuery query = CRessourceManager::instance()->getDB()->exec(builder.GetQueryString());

        QSqlRecord rec = query.record();
        if (query.next()) {
            <?php echo $modelClass; ?> *m = new <?php echo $modelClass; ?>();
            foreach(const QString &field, fields) {
                m->setValueForKey(field, query.value(rec.indexOf(field)));
            }
            return m;
        }

        return NULL;
    }

    <?php echo $modelClass; ?>* findByAttribute(QString name, QString value) {

        if (!this->getFieldNames().contains(name)) {
            return NULL;
        }

        QueryBuilder builder(QueryBuilder::TypeSelect);
        builder.SetTable(getTableName());

        QStringList fields = getFieldNames();
        foreach(const QString &field, fields) {
            builder.AddField(field, "");
        }

        builder.OverwriteWhere(name+" = \""+value+"\"");

        QSqlQuery query = CRessourceManager::instance()->getDB()->exec(builder.GetQueryString());

        QSqlRecord rec = query.record();
        if (query.next()) {
            <?php echo $modelClass; ?> *m = new <?php echo $modelClass; ?>();
            foreach(const QString &field, fields) {
                m->setValueForKey(field, query.value(rec.indexOf(field)));
            }
            return m;
        }

        return NULL;
    }

signals:
<?php foreach($columns as $column) { //Generates Signals
        echo "    void ".$column->name."Changed(".sqlTypeToCType($column->type).");\n";
} 
?>

public slots:

private:
<?php foreach($columns as $column) { //Generates Private members
	echo "    ".sqlTypeToCType($column->type)." m_".$column->name.";\n";
}
?>
};

#endif // <?php echo strtoupper($modelClass)."_H\n"?>

