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

//print_r($_POST);

$id = $_POST['values']['who'];


if($_POST['values']['what'] == "campaign")
{

	function deleteDirectory($dir) {
    if (!file_exists($dir)) {
        return true;
    }

    if (!is_dir($dir)) {
        return unlink($dir);
    }

    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') {
            continue;
        }

        if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
            return false;
        }

    }

    return rmdir($dir);
}

$tabellaLead = "campaign" . $id . "_leads";
$tabellaRisposte = "campaign" . $id . "_user_answers";
	$connection->query("DELETE FROM campaigns WHERE id = '$id'");
	$connection->query("DELETE FROM campaigns_meta WHERE campaign_id = '$id'");
	$connection->query("DROP TABLE $tabellaLead");
	$connection->query("DROP TABLE $tabellaRisposte");

$cFolder = $_POST['values']['folder'];
if($cFolder != "")
{
if(file_exists("../../$cFolder") && is_dir("../../$cFolder"))
{
	deleteDirectory("../../$cFolder");
}
}
	echo '{"success":"true"}';
}
if($_POST['values']['what'] == "language")
{

}
if($_POST['values']['what'] == "customer")
{
	$connection->query("DELETE FROM customers WHERE id = '$id'");
	echo '{"success":"true"}';
}
if($_POST['values']['what'] == "questionSet")
{
	$connection->query("DELETE FROM questions_sets WHERE id = '$id'");
	$connection->query("DELETE FROM questions WHERE sets_in = '$id'");
	$connection->query("DELETE FROM answers WHERE sets_in = '$id'");
	echo '{"success":"true"}';
}
if($_POST['values']['what'] == "redirectSet")
{
	$connection->query("DELETE FROM redirect_sets WHERE id = '$id'");
	$connection->query("DELETE FROM redirects WHERE set_in = '$id'");
	echo '{"success":"true"}';
}
if($_POST['values']['what'] == "commentSet")
{
	$connection->query("DELETE FROM comments_sets WHERE id = '$id'");
	$connection->query("DELETE FROM comments WHERE sets_in = '$id'");
	echo '{"success":"true"}';
}
if($_POST['values']['what'] == "profilationSet")
{
	$connection->query("DELETE FROM profilation_sets WHERE id = '$id'");
	$connection->query("DELETE FROM profilation_questions WHERE sets_in = '$id'");
	$connection->query("DELETE FROM profilation_answers WHERE sets_in = '$id'");
	echo '{"success":"true"}';
}
?>