<?php
//echo __FILE__ . '<br>';
require_once(dirname(__FILE__) . "/../classes/Paginator.php");

$optres['css'][] = $module_css_path . "amodules/events_ext/css/paginator.css";
$optres['css'][] = $module_css_path . "amodules/events_ext/css/events.css";
$optres['js'][] = $path . "amodules/events_ext/js/events_main.js";
function namesToCapital($str) {
    $ret = array();
    $str = explode(" ", $str);
    if(is_array($str) && count($str)>0) {
        foreach ($str as $i=>$s) {
            if(trim($s) != "") {
                $ret [] = ucfirst(trim(strtolower($s)));
            }
        }
    }

    if(count($ret) > 0) return implode(" ", $ret);
    return $str;
}

$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
);

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

if(isset($event_srch) && is_array($event_srch)) {
    $event_srch = array_merge($search, $event_srch);
}
else {
    $event_srch = $search;
}


$m_location = 'Cyprus';
$m_title = "Current & Upcoming Events in <location>";
$locations = $events_config['locations'];
foreach ($locations as $dcode=>$loc) {
    if($event_srch['event_location'] != "" && $event_srch['event_location'] == $dcode) {
        $m_location = $loc['title'];
    };
}

$current_title = str_replace('<location>', $m_location, $m_title);

?>
    <section class="article cyprus">
        <header>
            <h2><?=$current_title;?></h2>
        </header>
        <div class="article-body ">
            <?php

            if($gads_show && $def_ads == "yes" && $def_ad_id != "") {
                $adsense_container_class = "no-margin";
                echo '<div style="margin-bottom: 6px;">';
                include(dirname(__FILE__) . "/../gads_content/gads_content_default.php");
                echo '</div>';
                unset($adsense_container_class);
            }

            //echo $events_search_form_html;



            if($have_filters) {

                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;";

                //echo "<br/>".$q;

                if($total_records > 0) {

                    $paginator = new Paginator(array(
                        "base_url"  => "events/?",
                        "request_parameter_name" => "filter",
                        "records_per_page" => $event_srch['i_limit'],
                        "show_all" => 0
                    ));

                    $search = array_map('stripslashes', $search);

                    $pagination = $paginator->paginate($total_records, $event_srch);
                    /*echo '<div class="clearfix">' . $pagination . '</div>';*/

                    $r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
                    if($r && mysql_num_rows($r)>0) {
                        ?>
                        <div class="container-fluid no-padding">

                            <?php
                            $cnt = 1 + $i_from;
                            while ($row = mysql_fetch_assoc($r)) {
                                $row = array_map('stripslashes', $row);
                                //$name = (($row['Genre'] == "m") ? "Mr." : "Ms.") . " " . stripslashes(trim($row['FirstName'])) . " " . stripslashes(trim($row['LastName']));
                                $event_title = trim(stripslashes($row['title']));


                                $location = trim($row['region']);
                                if(trim($row['city']) != '') $location .= ' / ' . trim($row['city']);
                                if($location == '') $location = "Cyprus";

                                $id = intval($row['id']);
                                $category_group_id = intval($row['category_group_id']);
                                $category_id = intval($row['category_id']);
                                $categ_code = $category_group_id . '-' . $category_id;

                                $category_group_title = trim($row['category_group_title']);
                                $category_title = trim($row['category_title']);
                                $date_from = date("d/m/Y", strtotime($row['date_from']));
                                $date_to = date("d/m/Y", strtotime($row['date_to']));

                                $event_link = 'event/' . $id . '/' . url_slug($event_title);

                                $description = tokenTruncate(strip_tags($row['description']),  250) . '...';
                                $description .= '<a href="'.$event_link.'" class="event-readmore">[Read more]</a>';

                                // Category
                                $category_link = '';
                                $category_link_title = '';
                                if($category_group_title != '') {
                                    $category_link_title = $category_group_title;
                                }
                                if($category_title != '') {
                                    $category_link_title .=  ' / ' . $category_title;
                                }


                                $slug = url_slug($category_group_title .'-'. $category_title);
                                if($category_id == 0) {
                                    $slug = url_slug($category_group_title);
                                }

                                $category_link_group = "events/category/" . $category_group_id . "/" . $slug;
                                $category_link = "events/category/" . $categ_code . "/" . $slug;
                                if(isset($def_location) && $def_location != "") {
                                    $location_title = $events_config['locations'][$def_location]['title'];
                                    $category_link_group = "events/" . url_slug("in $location_title") . "/category/" . $category_group_id . "/" . $slug;
                                    $category_link = "events/" . url_slug("in $location_title") . "/category/" . $categ_code . "/" . $slug;
                                }



                                $image = '';
                                if($row['image'] != "") {
                                    $image = $path . 'images/events/' . $id . '/' . $row['image'];
                                }



                                ?>
                                <div class="box event-box">
                                    <div class="box-header with-border">
                                        <a href="<?=$event_link;?>" class="event-title"><?=$event_title;?></a>
                                    </div>
                                    <div class="box-body">
                                        <div class="row">
                                            <div class="col-sm-3">
                                                <div class="image-holder">
                                                    <a href="<?=$event_link;?>" class="image-container">
                                                        <img src="<?=$image?>" class="event-image">
                                                    </a>
                                                </div>
                                            </div>
                                            <div class="col-sm-7 no-padding">
                                                <p class="event-description"><?=$description;?></p>
                                                <div class="event-details">
                                                    <div class="row">
                                                        <div class="col col-sm-3 col-xs-3"><span class="event-d"><i class="fa fa-map-marker fa-fw"></i> Location:</span></div>
                                                        <div class="col col-sm-7 col-xs-7"><strong><?=$location;?></strong></div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col col-sm-3 col-xs-3"><span class="event-d"><i class="fa fa-calendar fa-fw"></i> Date(s):</span></div>
                                                        <div class="col col-sm-7 col-xs-7"><b><?=$date_from . (strcmp($date_to,$date_from) != 0 ? ' - ' . $date_to : '')  ;?></b></div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col col-sm-3 col-xs-3"><span class="event-d"><i class="fa fa-tag fa-fw"></i>Category:</span></div>
                                                        <div class="col col-sm-7 col-xs-7"><a href="<?=$category_link;?>"><?=$category_link_title  ;?></a></div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <?php

                                if($cnt - $i_from == 1) {
                                    // Show Adsense
                                    if($gads_show && $def_ads == "yes" && $def_ad_id != "") {
                                        $adsense_container_class = "no-margin";
                                        echo '<div style="margin-top: 9px;margin-bottom: 6px; margin-left: -15px!important;margin-right: -15px!important;">';
                                        include(dirname(__FILE__) . "/../gads_content/gads_content_default.php");
                                        echo '</div>';
                                        unset($adsense_container_class);
                                    }
                                }

                                $cnt++;
                            }
                            ?>

                        </div>

                        <?php
                    }

                    echo $pagination;
                }
                else {
                    echo '<br><div class="alert alert-danger"><p class="text-white"><b><i class="fa fa-warning"></i> Sorry!</b></span><br/>There are no events.</p></div>';
                }
            }
            ?>


        </div>

    </section>
<?php
?>