2016-08-30 10:24:06 +02:00

93 lines
2.5 KiB
PHP
Executable File

<?php
class Metier_Sfr_Compile
{
/**
* Database adaptater
* @var Zend_Db_Adapter_Abstract
*/
protected $db;
/**
*
* @var string
*/
protected $version = null;
protected $compileTxt = "<?php\n";
public function __construct($db = null)
{
if ( null === $db) {
$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
} else {
$this->db = $db;
}
}
public function setVersion($version)
{
$this->version = $version;
}
public function getRulesFromDb($type)
{
$version = str_replace('.','',$this->version);
$sql = "SELECT * FROM jo.sfr_rules_".$version." AS r WHERE r.type='".$type."' ORDER BY r.ordre";
$result = $this->db->fetchAll($sql, array(), Zend_Db::FETCH_OBJ);
return $result;
}
public function getParamsFromDb($type, $codif)
{
$version = str_replace('.','',$this->version);
$sql = "SELECT * FROM jo.sfr_params_".$version." AS p WHERE p.type='".$type."' AND p.codif='".$codif."' ORDER BY p.ordre";
$result = $this->db->fetchAll($sql, array(), Zend_Db::FETCH_OBJ);
return $result;
}
public function construct($type)
{
$rules = $this->getRulesFromDb($type);
if ( count($rules) > 0 ) {
$this->compileTxt.= "return array(\n";
$this->addRules($rules);
$this->compileTxt.= ");\n";
}
$filename = realpath(__DIR__).'/Rules'.ucfirst(strtolower($type)).'-'.$this->version.'.php';
file_put_contents($filename, $this->compileTxt);
}
public function addRules($rules)
{
foreach ( $rules as $i => $rule ) {
$this->compileTxt.= "\t".$i." => array(\n";
$this->compileTxt.= "\t\t'name' => '".$rule->label."',\n";
$this->compileTxt.= "\t\t'value' => '".$rule->value."',\n";
$this->compileTxt.= "\t\t'comment' => \"".$rule->comment."\",\n";
$this->compileTxt.= "\t\t'po' => ".$rule->po.",\n";
$this->compileTxt.= "\t\t'params' => array(\n";
$this->addParams($rule->type, $rule->codif);
$this->compileTxt.= "\t\t),\n";
$this->compileTxt.= "\t),\n";
}
}
public function addParams($type, $codif)
{
$params = $this->getParamsFromDb($type, $codif);
if ( count($params) > 0 ) {
foreach ( $params as $i => $param ) {
if ( $param->define == '') {
$this->compileTxt.= "\t\t\t".$i." => array( 'var' => '".$param->var."', 'type' => '".$param->cond."', 'value' => '".$param->value."'),\n";
} else {
$this->compileTxt.= "\t\t\t".$i." => array( 'var' => '".$param->var."', 'type' => '".$param->cond."', 'value' => '".$param->value."', 'define' => array( 'var' => '".$param->define."', 'value' => '".$param->define_value."')),\n";
}
}
}
}
}