<?php 
include("../../includes/connect.php");

$outputPath = "/xampp/htdocs/Piattaforma landing page/exports/";
$jsOutputPath = "/Piattaforma landing page/exports/";

$inizio = $_POST['start'];
$quanti = $_POST['length'];

$idCampaign = $_POST['campaignId'];
$tabellaLeads = "campaign" . $idCampaign . "_leads";
$tabellaAnwers = "campaign" . $idCampaign . "_user_answers";


$leadCount = $connection->query("SELECT COUNT(*) FROM $tabellaLeads");
$leadCount = $leadCount->fetchColumn();


// costruisco la query di ricerca

if($_POST['applyFilter'] != 0)
{

$join = "INNER JOIN $tabellaAnwers ON $tabellaLeads" . ".externalId = $tabellaAnwers" . ".externalId AND ";
$condizioni = "";

//echo $join; 
for($f = 0; $f < count($_POST['filterCount']); $f++)
{
if($_POST["filter$f"]['aId'] != 0)
{
	/* SELECT * FROM campaign17_leads 
INNER JOIN campaign17_user_answers ON campaign17_user_answers.externalId = campaign17_leads.externalid AND campaign17_user_answers.questionId = 2 AND campaign17_user_answers.answerId = 2

/* SELECT * FROM campaign17_leads,campaign17_user_answers WHERE campaign17_user_answers.externalId = campaign17_leads.externalid AND campaign17_user_answers.questionId = 2 AND campaign17_user_answers.answerId = 2
*/

$join .= $tabellaAnwers . ".questionId = " . $_POST["filter$f"]['qId'] . " AND $tabellaAnwers" . ".answerId = " . $_POST["filter$f"]['aId'];	

}
}
}
else
{
	$join = "";
	$condizioni = " WHERE 1";
}


if($_POST['searchByGender'])
{
	$condizioni .= " AND LOWER($tabellaLeads" . ".sesso) ='" . strtolower($_POST['searchByGender']) . "'";
}

if($_POST['searchCapFrom'])
{
	$condizioni .= " AND $tabellaLeads" . ".cap >= " . $_POST['searchCapFrom'];
}

if($_POST['searchCapTo'])
{
	$condizioni .= " AND $tabellaLeads" . ".cap <= " . $_POST['searchCapTo'];
}

if($_POST['searchByCity'])
{
	$condizioni .= " AND $tabellaLeads" . ".citta LIKE '%" . $_POST['searchByCity'] . "'";
}


$nomefile = "export-" . time() . ".csv";
//echo "SELECT * FROM $tabellaLeads $join $condizioni ORDER BY timestamp DESC LIMIT $inizio,$quanti";
$export = "INTO OUTFILE '/xampp/htdocs/Piattaforma landing page/exports/$nomefile'
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\\n';";


//echo "SELECT nome,cognome,sesso,nascita,email,cellulare,cap,citta,indirizzo,civico,ip,timestamp FROM $tabellaLeads $join $condizioni $export";

$leadList = $connection->query("SELECT nome,cognome,sesso,nascita,email,cellulare,cap,citta,indirizzo,civico,ip,timestamp FROM $tabellaLeads $join $condizioni $export");
// ORDER BY timestamp DESC LIMIT $inizio,$quanti 

//print_r($connection->errorInfo());


//$leads = $leadList->fetchAll();

$meta = $connection->query("SELECT meta_key,meta_value FROM campaigns_meta WHERE campaign_id = '$idCampaign'");
$campaignMeta = $meta->fetchAll(PDO::FETCH_KEY_PAIR);


// domande pre-form
$questionSet = $campaignMeta['questionset'];
// domande post-form
$profilationSet = $campaignMeta['questionset_after'];


if($profilationSet != "")
{
	$profilationQ = $connection->query("SELECT id,text FROM profilation_questions WHERE sets_in = '$profilationSet'");
	$profilationQuestions = $profilationQ->fetchAll(PDO::FETCH_ASSOC);
	//print_r($profilationQuestions);

	$profilationA = $connection->query("SELECT question_id,id,text FROM profilation_answers WHERE sets_in = '$profilationSet'");
	$profilationAnswers = $profilationA->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
//print_r($profilationAnswers);
}
else
{
	$profilationQuestions = array();
}

echo $jsOutputPath . "/" . $nomefile;
?>