<?php
include("../../includes/connect.php");
print_r($_POST);
echo "<hr />";

$campaign_id = $_POST['campaign_id'];

$cname = $_POST['campaignName'];
$clink = $_POST['campaignLink'];

$ctitle = $_POST['campaignTitle'];
$cdescr = $_POST['campaignDescription'];
$clayout = $_POST['campaignLayout']; // ADDON giugno 2021

$aname = $_POST['agencyName'];
$cowner = $_POST['campaignOwner'];
$clanguage = $_POST['campaignLanguage'];
$questionset = $_POST['question_set'];
$saveanswers = $_POST['saveanswers'];
$fbCommentSet = $_POST['comment_set'];
$redirectSet = $_POST['redirect_set'];
$sharingText = $_POST['sharingText'];

$questionset_after = $_POST['question_set_after'];



$campaign_start_date = $_POST['campaignStartDate'];
$campaign_start_date_timestamp = strtotime($campaign_start_date);
$campaign_end_date = $_POST['campaignEndDate'];
$campaign_end_date_timestamp = strtotime($campaign_end_date);
$data = date('Y-m-d H:i:s');
$folder_name = str_replace(" ","-",strtolower($clink));

$mainColor = $_POST['main-color'];
$borderColor = $_POST['border-color'];

// fields 
$fieldNames = array("sesso","nome","cognome","email","nascita","nazione","cap,","citta","cellulare","privacy1","privacy2","privacy3");

$fields = "{";
for($f = 0; $f < count($fieldNames); $f++)
{
$nomeCampo = $fieldNames[$f];

$valoreCampo = isset($_POST["f_" . $nomeCampo]) ? 1 : 0;

$fields .= "\"$nomeCampo\" : \"$valoreCampo\"";
if($f < count($fieldNames)-1)
{
	$fields .= ",";
}
}

$fields .= "}";

echo $fields;

// domande e risposte



$bg = isset($_POST['file1']) ? $_POST['file1'] : "";
$frontDesktop = isset($_POST['file2']) ? $_POST['file2'] : "";
$frontMobile = isset($_POST['file3']) ? $_POST['file3'] : "";
$regolamento = isset($_POST['file4']) ? $_POST['file4'] : "";
$privacy = isset($_POST['file5']) ? $_POST['file5'] : "";
$cookie = isset($_POST['file6']) ? $_POST['file6'] : "";


$connection->query("UPDATE campaigns SET campaign_name = '$cname', campaign_owner = '$cowner' WHERE id = '$campaign_id'");
print_r($connection->errorInfo());

$connection->query("UPDATE campaigns_meta SET meta_value = '$cname' WHERE campaign_id = '$campaign_id' AND meta_key = 'campaign_name'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$aname' WHERE campaign_id = '$campaign_id' AND meta_key = 'agency_name'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$ctitle' WHERE campaign_id = '$campaign_id' AND meta_key = 'campaign_title'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$cdescr' WHERE campaign_id = '$campaign_id' AND meta_key = 'campaign_description'");

$connection->query("UPDATE campaigns_meta SET meta_value = '$clayout' WHERE campaign_id = '$campaign_id' AND meta_key = 'campaign_layout'"); // ADDON Giugno 2021

$connection->query("UPDATE campaigns_meta SET meta_value = '$folder_name' WHERE campaign_id = '$campaign_id' AND meta_key = 'campaign_folder'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$clanguage' WHERE campaign_id = '$campaign_id' AND meta_key = 'language'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$saveanswers' WHERE campaign_id = '$campaign_id' AND meta_key = 'saveanswer'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$campaign_start_date' WHERE campaign_id = '$campaign_id' AND meta_key = 'start'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$campaign_start_date_timestamp' WHERE campaign_id = '$campaign_id' AND meta_key='start_ts'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$campaign_end_date' WHERE campaign_id = '$campaign_id' AND meta_key = 'end'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$campaign_end_date_timestamp' WHERE campaign_id = '$campaign_id' AND meta_key='end_ts'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$mainColor' WHERE campaign_id = '$campaign_id' AND meta_key='mainColor'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$borderColor' WHERE campaign_id = '$campaign_id' AND meta_key='borderColor'");

$connection->query("UPDATE campaigns_meta SET meta_value = '$fields' WHERE campaign_id = '$campaign_id' AND meta_key='fieldsVisibility'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$fbCommentSet' WHERE campaign_id = '$campaign_id' AND meta_key='commentsSet'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$redirectSet' WHERE campaign_id = '$campaign_id' AND meta_key='redirectSet'");
$connection->query("UPDATE campaigns_meta SET meta_value = '$sharingText' WHERE campaign_id = '$campaign_id' AND meta_key='sharingText'");

if($bg != "")
{
$connection->query("UPDATE campaigns_meta SET meta_value = '$bg' WHERE campaign_id = '$campaign_id' AND meta_key = 'bg'");	
rename("../../tmp/$bg", "../../../$folder_name/assets/$bg");
}

if($front_desktop != "")
{
	$connection->query("UPDATE campaigns_meta SET meta_value = '$frontDesktop WHERE campaign_id = '$campaign_id' AND meta_key = 'front_desktop'");
    rename("../../tmp/$frontDesktop", "../../../$folder_name/assets/$frontDesktop");
}

if($front_mobile != "")
{
	$connection->query("UPDATE campaigns_meta SET meta_value = '$frontMobile WHERE campaign_id = '$campaign_id' AND meta_key = 'front_mobile'");
rename("../../tmp/$frontMobile", "../../../$folder_name/assets/$frontMobile");
}

if($regolamento != "")
{
    rename("../../tmp/$regolamento", "../../../$folder_name/docs/regolamento.pdf");
}

if($privacy != "")
{
rename("../../tmp/$privacy", "../../../$folder_name/docs/privacy.pdf");
}

if($cookie != "")
{
    rename("../../tmp/$cookie", "../../../$folder_name/docs/cookie.pdf");
}




// set il set di domande non è pre-esistente lo creo

/*
if($questionset == "")
{
	
	echo "Creo il nuovo set di domande";
	$questionSetName = $_POST['questionSetName'];
	echo "INSERT INTO questions_set (set_name,set_language) VALUES ('$questionSetName','$clanguage')";
	$connection->query("INSERT INTO questions_sets (set_name,set_language) VALUES ('$questionSetName','$clanguage')");
	$questionset = $connection->lastInsertId();
	$domande = $_POST['domande'];


for($d = 0; $d < count($domande); $d++)
{
	$questionText = $domande[$d]['domanda'];
	$connection->query("INSERT INTO questions (text,sets_in) VALUES ('$questionText','$questionset')");
	$idQuestion = $connection->lastInsertId();
	for($r = 0; $r < count($domande[$d]['risposte']); $r++)
	{
		$answerText = $domande[$d]['risposte'][$r]['risposta'];
		$connection->query("INSERT INTO answers (question_id,text) VALUES ('$idQuestion','$answerText')");
	}
}


}
*/


$connection->query("UPDATE campaigns_meta SET meta_value = '$questionset' WHERE campaign_id = '$campaign_id' AND meta_key='questionset'");
if($_POST['saveAnswerAfter'] == "on")
{
    $connection->query("UPDATE campaigns_meta SET meta_value = '$questionset_after' WHERE campaign_id = '$campaign_id' AND meta_key='questionset_after'");
}
else
{
    $connection->query("UPDATE campaigns_meta SET meta_value = '' WHERE campaign_id = '$campaign_id' AND meta_key='questionset_after'");
}

/*
mkdir("../" . $folder_name);

function custom_copy($src, $dst) {  
  
    // open the source directory 
    $dir = opendir($src);  
  
    // Make the destination directory if not exist 
    @mkdir($dst);  
  
    // Loop through the files in source directory 
    while( $file = readdir($dir) ) {  
  
        if (( $file != '.' ) && ( $file != '..' )) {  
            if ( is_dir($src . '/' . $file) )  
            {  
  
                // Recursively calling custom copy function 
                // for sub directory  
                custom_copy($src . '/' . $file, $dst . '/' . $file);  
  
            }  
            else {  
                copy($src . '/' . $file, $dst . '/' . $file);  
            }  
        }  
    }  
  
    closedir($dir); 
}  


custom_copy("../baseAssets","../" . $folder_name );
*/




?>