<?php
$have_filters = true;

//echo '<pre>$_POST = ', var_export($_POST, true), '</pre>';
$search = array(
    'event_category' => "",
    'event_location' => "",
    'event_period' => "",
    'event_keyword' => "",
    'display' => "yes",
    'i_from' => 0,
    'i_limit' => 10
);

foreach($search as $k=>$v) {
    ${$k} = mysql_real_escape_string($v);
}


$q = "SELECT * FROM events WHERE active='1' AND crawled='1'";

if($event_srch['event_category'] != '') {
    $ev_cats = explode("-", $event_srch['event_category']);
    if(count($ev_cats) == 2 && intval($ev_cats['1']) > 0 ) {
        $q .= " AND category_group_id='{$ev_cats['0']}' AND category_id='{$ev_cats['1']}' ";
    }
    else {
        $q .= " AND category_group_id='{$ev_cats['0']}' ";
    }
}

// Location
if($event_srch['event_location'] != "") {
    $q .= " AND dcode = '{$event_srch['event_location']}'";
}

// Keyword
if($event_srch['event_keyword'] != "") {
    $keyword = mysql_real_escape_string(trim(stripslashes($event_srch['event_keyword'])));
    $q .= " AND (title like '%$keyword%' OR description like '%$keyword%') ";
}

// Period
$event_period = '';
$_date = new DateTime(date('Y-m-d'));
if($event_srch['event_period'] != "") {
    $event_period = $event_srch['event_period'];
    switch (strval($event_period)) {
        case "today":
            $today = $_date->format('Y-m-d');
            $q .= " AND date_from <='$today' AND date_to >='$today'";
            break;
        case "tomorrow":
            $tomorrow = $_date->add(new DateInterval('P1D'))->format('Y-m-d');
            $q .= " AND date_from <='$tomorrow' AND date_to >='$tomorrow'";
            break;
        case "next7days":
            $interval_start = $_date->add(new DateInterval('P1D'))->format('Y-m-d');
            $interval_end = $_date->add(new DateInterval('P6D'))->format('Y-m-d');

            $q .= " AND (";
            $q .= " '$interval_start' <= date_to AND '$interval_end' >= date_from ";
            $q .= ") ";

            break;
        case "next30days":
            $interval_start = $_date->add(new DateInterval('P1D'))->format('Y-m-d');
            $interval_end = $_date->add(new DateInterval('P30D'))->format('Y-m-d');

            $q .= " AND (";
            $q .= " '$interval_start' <= date_to AND '$interval_end' >= date_from ";
            $q .= ") ";
            break;
        case "next60days":
            $interval_start = $_date->add(new DateInterval('P1D'))->format('Y-m-d');
            $interval_end = $_date->add(new DateInterval('P60D'))->format('Y-m-d');

            $q .= " AND (";
            $q .= " '$interval_start' <= date_to AND '$interval_end' >= date_from ";
            $q .= ") ";
            break;
    }
}
else {
    $today = $_date->format('Y-m-d');
    $q .= " AND date_to >='$today'";
}

$q .= " ORDER BY date_from ASC ";

//echo $q;
$total_records = 0;
$r = @mysql_query($q);
if($r && mysql_num_rows($r)>0){
    $total_records = mysql_num_rows($r);
}

$q .= "	LIMIT $i_from, $i_limit;";