<?php
//echo __FILE__ . '<br>';
//echo '<pre>', var_export($_REQUEST, true), '</pre>';
$show_events_ext_side = true;
$must_cache = false;
$module_dir = "events_ext";

$module_css_path = ($line_domain['theme'] != "" && $line_domain['theme'] != "default") ? ($path . ('assets/themes/' . $line_domain['theme'] . '/')) : $path;

$optres['css'][] = $path . 'assets/css/bootstrap_select/bootstrap-select.css';
$optres['js'][] = $path . 'assets/js/bootstrap_select/bootstrap-select.min.js';
$optres['js'][] = $path . 'amodules/events_ext/js/events_search_form.v2.js';

include(dirname(__FILE__) . "/events_ext_init.php");

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

//echo '<pre>$_REQUEST = ', var_export($_REQUEST, true), '</pre>';
/*if(isset($_REQUEST['filter'])) {
    echo '<pre>$_REQUEST[filter] = ', var_export(base64_decode($_REQUEST['filter']), true), '</pre>';
}*/

$include_file = dirname(__FILE__) . "/events_main.php";


$def_breadcrumbs = array();
if(!isset($module_params['def_breadcrumbs'])) {
    $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'];
        }
    }
}
else {
    $def_breadcrumbs = $module_params['def_breadcrumbs'];
}

if(isset($module_params)) {
    $def_ads = isset($module_params['def_ads']) ? $module_params['def_ads'] : 'no';
    $def_ad_id = isset($module_params['ws_ad_unit_id']) ? $module_params['ws_ad_unit_id'] : '';
}
else {
    $default_module_params = [];
    $q = "SELECT l.* FROM left_content l, module m WHERE l.module_id=m.module_id AND m.url='events_ext' AND l.catdom_id='$domain_id';";
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r) > 0) {
        $row = mysql_fetch_assoc($r);
        $default_module_params = unserialize(base64_decode($row['module_params']));
    }

    $def_ads = isset($default_module_params['def_ads']) ? $default_module_params['def_ads'] : 'no';
    $def_ad_id = isset($default_module_params['ws_ad_unit_id']) ? $default_module_params['ws_ad_unit_id'] : '';

    $module_params = [
        'ws_ad_unit_id' => $def_ad_id,
        'def_ads' => $def_ads
    ];
}


if(isset($_REQUEST['events_debug'])) {
    echo '<pre>$def_breadcrumbs = ' , var_export($def_breadcrumbs, true), '</pre>';
}
//echo '<pre>', var_export($def_breadcrumbs, true), '</pre>';


$events_dcode = '';
$events_town = '';
$events_category_group = '';
$events_category = '';

if(isset($_REQUEST['events_url'])) {
    $events_url = $_REQUEST['events_url'];

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

    $events_type = isset($_REQUEST['module_type']) ? $_REQUEST['module_type']: 'events';

    // Try to find out if I need to send 404 - town only urls should point to town pages
    $only_town = false;
    if($events_type == "events") {
        if ($events_url != '') {
            $canonical_link = $protocol . $ultra_domainname . '/events/' . $events_url;
            $_route = explode("/", $events_url);
            if(isset($_REQUEST['events_debug'])) {
                echo '<pre>$_route = ', var_export($_route, true), '</pre>';
            }


            if(in_array($_route[0], $events_config['towns']))  {
                $events_town = $_route['0'];
                $events_dcode = array_search($events_town, $events_config['towns']);
                $only_town = true;

                if(isset($_route[1]) && trim($_route[1]) != "") {
                    if(isset($events_config['cats_frs'][$_route[1]])) {
                        $only_town = false;
                        $events_category_group = $events_config['cats_frs'][$_route[1]];

                        if(isset($_route[2]) && trim($_route[2]) != "") {
                            if(isset($events_config['cats_frs'][$_route[2]])) {
                                $events_category = $events_config['cats_frs'][$_route[2]];
                            }
                            else {
                                $send404 = true;
                            }
                        }

                    }
                    else {
                        $send404 = true;
                    }
                }
            }
            elseif(isset($events_config['cats_frs'][$_route[0]])) {
                $events_category_group = $events_config['cats_frs'][$_route[0]];
                if(isset($_route[1]) && trim($_route[1]) != "") {
                    if(isset($events_config['cats_frs'][$_route[1]])) {
                        $events_category = $events_config['cats_frs'][$_route[1]];
                    }
                    else {
                        $send404 = true;
                    }
                }
            }
            else {
                $send404 = true;
            }

            if($only_town) {
                $send404 = true;
            }

            $include_file = dirname(__FILE__) . "/show_events_by_category.php";



            /*if ($_route[0] == "category") {
                $include_file = dirname(__FILE__) . "/show_events_by_category.php";
            } elseif (substr($_route[0], 0, 3) == 'in-') {
                $include_file = dirname(__FILE__) . "/show_events_by_category.php";
            } elseif(preg_match("/^(0[1-9]|[1-2][0-9]|3[0-1]-(0[1-9]|1[0-2])-[0-9]{4})$/",$_route[0])) {
                $canonical_link = $protocol . $ultra_domainname . '/events/' . $events_url;
                $include_file = dirname(__FILE__) . "/events_today.php";
            } elseif ($_route[0] == 'past') {
                $include_file = dirname(__FILE__) . "/past_events.php";
            }
            else {
                $send404 = true;
            }*/

        } else {
            $canonical_link = $protocol . $ultra_domainname . '/events';
            $include_file = dirname(__FILE__) . "/find_events.php";
        }
    }
    elseif($events_type == "event") {
        $canonical_link = $protocol . $ultra_domainname . '/event/' . $events_url;
        $_route = explode("/", $events_url);
        $include_file = dirname(__FILE__) . "/show_event.php";
    }
    elseif($events_type == "past-events") {
        $canonical_link = $protocol . $ultra_domainname . '/past-events';
        $include_file = dirname(__FILE__) . "/past_events_all.php";
    }
}
elseif($_REQUEST['shortName'] == "events_ext" && isset($_REQUEST['filter'])) {
    $canonical_link = $protocol . $ultra_domainname . "/events";
}

if(!isset($send404) || $send404 == false) {
    include($include_file);
    ?>
    <div class="alert alert-info">
        <p class="no-padding text-primary text-bold">
            <i class="fa fa-info-circle fa-lg"></i> The content is taken from the Cyprus Tourism Organisation's website.
        </p>
    </div>
    <?php
}
else {
    include(dirname(__FILE__) . "/../404/404.php");
}
?>
