<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

$gen = "
<?php

require_once(\$_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

\$dal = new DAL();

"; 

foreach ($_POST as $paramName => $paramValue) {
}

$controls =['sumControlInstances'];

$dal = new DAL();

foreach ($controls as $control) {

  $sql = "SELECT id, controlobject_id, instance, location FROM controlinstance WHERE instance = '$control' LIMIT 1";
  $ci = $dal->query($sql);

  $gen .= "
\$sql = \"INSERT INTO controlinstance (controlobject_id, instance, location) \" . 
        \"VALUES ($ci->controlobject_id, '$ci->instance', '$ci->location') \" .
\$ci_id = \$dal->query(\$sql);
  ";

//print_r ("id:$ci->id");

  $sql = "SELECT property, value FROM controlentry WHERE controlinstance_id = $ci->id ";
  $ce = $dal->query($sql);
$cnt = count($ce);

  foreach ($ce as $entry) {
//    $find = ["$", "'"];
//    $repl = ["\$", "\'"];
    
//    $newvalue = str_replace ($find, $repl, $entry->value);

//print_r ("cnt:$cnt");
    $gen .= "
    
\$sql = \"INSERT INTO controlentry (controlinstance_id, property, value) \" . 
        \"VALUES (\$ci_id, '$entry->property', '$entry->value') \" .
\$ce_id = \$dal->query(\$sql);
    ";
          
  }  
  
}

$gen .= "
?>
";

$genFile = fopen("gen.php", "w") or die("Unable to open file!");
fwrite($genFile, $gen);
fclose($genFile);

echo json_encode(1);

?>

