<?php

$accessControl = array(
    "ProfileRequired" => true
);
require_once '../../AccessControl.php';

/* Deleting data should not take longer than a day */
set_time_limit(86400);

if (!isset($_POST['fldvalue'])) {
    $fldvalue = $_POST['fldvalue'];
}
if (!isset($_POST['$fldname'])) {
    $fldname = $_POST['fldname'];
}


if (!isset($_POST['fromdate'])) {
    $fd = explode("/", $_POST['fromdate']);
}
$from = mktime(0, 0, 0, $fd[1], $fd[0], $fd[2]);

if (!isset($_POST['todate'])) {
    $fd = explode("/", $_POST['todate']);
}
$to = mktime(0, 0, 0, $fd[1], $fd[0], $fd[2]);

$message = "";

if ($profile->profileloaded) {
    $fldname = $_REQUEST["fldname"];
    if (!isset($_POST['fldvalue'])) {
        $fldvalue = $_POST['fldvalue'];
    }

    if ($fldname == "page") {
        $q = $db->Execute("select id from $profile->tablename_urls where url='$fldvalue'");
        $page = $q->FetchRow();
        $db->Execute("delete from $profile->tablename where timestamp >=$from and timestamp <=$to and url='$page[id]'");
        $db->Execute(
            "delete from $profile->tablename_conversions where timestamp >=$from and timestamp <=$to and url='$page[id]'"
        );
        //$message.="delete from $profile->tablename where timestamp >=$from and timestamp <=$to and url='$page[id]'";
    } elseif ($fldname == "ipnumber") {
        $q = $db->Execute("select id from $profile->tablename_visitorids where ipnumber='$fldvalue'");
        while ($page = $q->FetchRow()) {
            $db->Execute(
                "delete from $profile->tablename where timestamp >=$from and timestamp <=$to and visitorid='$page[id]'"
            );
            $db->Execute(
                "delete from $profile->tablename_conversions where timestamp >=$from and timestamp <=$to and visitorid='$page[id]'"
            );
        }
    }

    DeleteDataFiles($profile, $from, $to);

    $message .= _IF_YOU_WANT_TO_KEEP . " <b>$fldvalue</b> " . _OUT_OF_FUTURE_STATS . " ";

    if ($fldname == "page") {
        $message .= _SKIP_FILES . " ";
    } else {
        $message .= _SKIP_IP_NUMBERS . " ";
    }
	$message .= _FIELD_ON_THE_DATA_FILTERS_TAB_IN_YOUR." $profile->profilename "._PROFILE_SETTINGS.".</i>";   

	die('{ "Status" : "Success", "Message" : "'. $message .'" }');
}
die('{ "Status" : "Error", "Message" : "'. _NOTHING_DONE .'.." }');
