<?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";
	}
	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".$labels[$column->name]." NOTIFY ".$column->name."Changed)\n";
}
?>

public:
    explicit <?php echo $modelClass; ?>();

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

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"?>

