<?php
@session_start();

require_once(dirname(__FILE__) . "/../../../adbcon.php");
require_once(dirname(__FILE__) . "/../../common/tool_functions.php");
require_once(dirname(__FILE__) . "/../events_config.php");

ob_start();
$ret = array(
    "result" => 1,
    "error" => array()
);

$session = $_POST['session'];
$baseurl = $_POST['baseurl'];

$search = array(
    'event_category' => "",
    'event_location' => "",
    'event_period' => "",
    'event_keyword' => "",
    'display' => "yes",
    'i_from' => 0,
    'i_limit' => 10
);

$events_url_prefix = '/events';

echo '<pre>$_POST = ', var_export($_POST, true), '</pre>';

$segments = 0;


if(isset($_POST['event_srch']) && isset($_POST['event_srch']['display']) && $_POST['event_srch']['display'] == "yes") {
    $domain_id = $_POST['domain_id'];
    $have_filters = true;
    $events = $_POST['event_srch'];
    foreach($search as $fld=>$val) {
        if(isset($events[$fld])) {
            $search[$fld] = $events[$fld];

        }
    }
    //echo '<pre>$search = ', var_export($search, true), '</pre>';

    /*if(isset($search['event_location']) && trim($search['event_location']) != "") {
        foreach($events_config['locations'] as $dcode=>$loc) {
            if($search['event_location'] == $dcode) {
                $events_url_prefix .= '/' . url_slug($loc['title']);

                $segments = 1;
            }
        }
    }

    if(isset($search['event_category'])) {
        $_categ_code = '';
        $_categ_title = '';
        $_categ_group_friendly = '';
        $_categ_friendly = '';
        foreach ($events_config['categories'] as $gr_id => $gr_data){
            $categ_code = $gr_id;
            if($search['event_category'] == $categ_code) {
                $_categ_code = $categ_code;
                $_categ_group_friendly = $events_config['cats_ids'][$gr_id]['friendly_url'];
                $segments = 2;
            }
            if(count($gr_data['subs']) > 0) {
                foreach($gr_data['subs'] as $cat_id=>$cat_title) {
                    $categ_code = $gr_id . '-' . $cat_id;
                    if($search['event_category'] == $categ_code) {
                        $_categ_code = $categ_code;
                        $_categ_group_friendly = $events_config['cats_ids'][$gr_id]['friendly_url'];
                        $_categ_friendly = $events_config['cats_ids'][$cat_id]['friendly_url'];
                        $segments = 3;
                    }
                }
            }
        }
        
        if($_categ_group_friendly != "") {
            $events_url_prefix .= '/' . $_categ_group_friendly;
            if($_categ_friendly != "") {
                $events_url_prefix .= '/' . $_categ_friendly;
            }
        }
    }*/

    /*if($segments == 1) {
        // I was searching only by town
        // I need to redirect to town page if exists
        $q = "SELECT l.* FROM left_content l, module m WHERE l.module_id=m.module_id AND l.catdom_id='$domain_id' AND l.type='module' AND m.url='events_ext'  ;";
        $r = @mysql_query($q);
        if($r && mysql_num_rows($r) == 1) {
            $row = mysql_fetch_assoc($r);
            $module_params = unserialize(base64_decode($row['module_params']));
            if(isset($module_params['def_breadcrumbs'])) {
                $def_breadcrumbs = $module_params['def_breadcrumbs'];
                $dcode = $search['event_location'];
                if (isset($def_breadcrumbs[$dcode])) {
                    $q1 = "SELECT * FROM domaincategories WHERE catdom_id='{$def_breadcrumbs[$dcode]}';";
                    $r1 = @mysql_query($q1);
                    if ($r1 && mysql_num_rows($r1) == 1) {
                        $row1 = mysql_fetch_assoc($r1);

                        $events_url_prefix = '/' . $row1['friendly_url'] . '.html';

                    }
                }
            }
        }
    }*/


    if($search['event_period'] == "past") {
        $events_url_prefix = '/past-events';
    }
}

$filter = base64_encode(http_build_query($search));
$link = $baseurl . $events_url_prefix . "?filter=" . $filter;

$ret['link'] = $link;

echo '<pre>$ret = ', var_export($ret, true), '</pre>';

$log = ob_get_contents();
ob_end_clean();

$ret['log'] = $log;

$f = fopen(dirname(__FILE__) . "/ajax_log.txt", "wb");
fwrite($f, $log);
fclose($f);


echo json_encode($ret);
exit();