<?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/eventsmodule/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' => "today",
    'event_keyword' => "",
    'display' => "yes",
    'i_from' => 0,
    'i_limit' => 10
);


//ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

//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;
}

if($have_filters) {

    $today_only = array();
    $today_start = array();
    $upcoming = array();
    $ongoing = array();

    $today_value = date("d/m/Y", time());
    $today_text = 'Today';
    $today_date_str = ' (' . date("F jS, Y", time()) . ')';

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

                $today_value = date("d/m/Y", strtotime($tomorrow));
                $today_text = 'Tomorrow';
                $today_date_str = ' (' . date("F jS, Y", strtotime($tomorrow)) . ')';

                $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, date_to ASC ";

    //echo $q;

    $r = @mysql_query($q);
    if($r) {
        $total_records = mysql_num_rows($r);
        if($total_records > 0) {

            while($row = mysql_fetch_assoc($r)) {
                $date_from_timestamp = strtotime($row['date_from']);
                $date_to_timestamp = strtotime($row['date_to']);
                $date_from = date("d/m/Y", $date_from_timestamp);
                $date_to = date("d/m/Y", $date_to_timestamp);


                if($date_from == $today_value && $date_to == $today_value) {
                    $today_only [$row['id']] = array_map('stripslashes', $row);
                }
                elseif($date_from == $today_value) {
                    $today_start[$row['id']] = array_map('stripslashes', $row);
                }
                elseif($date_from_timestamp > time()) {
                    $upcoming[$row['id']] = array_map('stripslashes', $row);
                }
                else {
                    $ongoing[$row['id']] = array_map('stripslashes', $row);
                }

                // Use this to build category side list
                $category_group_id = intval($row['category_group_id']);
                $category_id = intval($row['category_id']);

                $category_group_title = trim($row['category_group_title']);
                $category_title = trim($row['category_title']);

                $category_group_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                if($category_id > 0) {
                    $category_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                }
                if($event_srch['event_location'] != "") {
                    $location_title = $events_config['locations'][$event_srch['event_location']]['title'];
                    $category_group_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                    if($category_id > 0) {
                        $category_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                    }
                }



                if(!isset($events_side_categs[$category_group_id])) {
                    $events_side_categs[$category_group_id] = array(
                        'title' => $category_group_title,
                        'total' => 0,
                        'link'  => $category_group_link,
                        'subs'  => array()
                    );
                }


                $events_side_categs[$category_group_id]['total'] += 1;

                if($category_id > 0) {


                    if (!isset($events_side_categs[$category_group_id]['subs'][$category_id])) {
                        $events_side_categs[$category_group_id]['subs'][$category_id] = array(
                            'title' => $category_title,
                            'total' => 1,
                            'link' => $category_link
                        );
                    } else {
                        $events_side_categs[$category_group_id]['subs'][$category_id]['total'] += 1;
                    }
                }



            }


            /*ob_start();
            require_once(dirname(__FILE__) . "/events_category_list.php");
            $inject_found_events_category_list = ob_get_contents();
            ob_end_clean();

            $_inject [] = array(
                "key" => 'inject_found_events_category_list',
                "value" => $inject_found_events_category_list
            );*/


            if(count($ongoing) > 0) {
                $ongoing = array_reverse($ongoing, true);
            }
        }
    }

}

if($total_records > 0) {
    ?>
    <div class="container-fluid no-padding">
        <?php
        if(count($today_only) > 0) {
            ?>
            <h2 class="events-subtitle today"><?=$today_text . ' Only' . $today_date_str;?></h2>

            <?php
            $cnt = 1;
            foreach ($today_only as $event_id => $row) {
                $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']));
                $friendly_url = $row['friendly_url'];


                $location_str = '<span itemprop="location" itemscope itemtype="http://schema.org/Place">';
                    $location = trim($row['region']);
                    if($location != "") {
                        $location_str .= '<meta itemprop="name" content="'.$location.'">';
                        $location_str .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';
                        if (trim($row['city']) != '') {
                            $location_str .= '<span itemprop="addressLocality">' . trim($row['city']) . '</span>, ';
                        }
                        $location_str .= '<span itemprop="addressRegion">' . $location . '</span>';
                    }
                    else {
                        $location_str .= '<meta itemprop="name" content="Cyprus">';
                        $location_str .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';

                    }

                    $location_str .= '<meta itemprop="addressCountry" content="Cyprus">';
                    $location_str .= '</span>';
                $location_str .= '</span>';

                $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;
                if($category_id == 0) {
                    $categ_code = $category_group_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']));

                $date_from_iso = date('c', strtotime($row['date_from']));
                $date_to_iso = date('c', strtotime($row['date_to']));

                $event_link = 'event/' . $friendly_url;

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

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

                $category_group_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                if($category_id > 0) {
                    $category_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                }
                if (isset($def_location) && $def_location != "") {
                    $location_title = $events_config['locations'][$def_location]['title'];
                    $category_group_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                    if($category_id > 0) {
                        $category_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                    }
                }


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


                ?>
                <div class="box event-box today" itemscope itemtype="http://schema.org/Event">
                    <div class="box-header with-border">
                        <a href="<?= $event_link; ?>" itemprop="url" class="event-title">
                            <span itemprop="name"><?= $event_title; ?></span>
                        </a>
                        <?php
                        if(isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
                            echo '<div class="user-action-container event-action"><a class="user-action-inline" href="/admin/index.php?plugin=events&amp;action=events_item&amp;id=' . $id . '" target="_blank">e</a></div>';
                        }
                        ?>
                    </div>
                    <div class="box-body">
                        <div class="row">
                            <div class="col-sm-3">
                                <div class="image-holder">
                                    <div class="image-container" itemprop="image" itemscope itemtype="http://schema.org/ImageObject">
                                        <img src="<?= $image ?>" itemprop="url" alt="<?=$event_title;?>" class="event-image">
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-7 no-padding">
                                <p class="event-description" itemprop="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_str; ?></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><time itemprop="startDate" datetime="<?=$date_from_iso;?>"><?=$date_from;?></time></b>
                                            <?php
                                            if(strcmp($date_to, $date_from) != 0) {
                                                echo ' - ';
                                                echo '<b><time itemprop="endDate" datetime="' . $date_to_iso . '">' . $date_to . '</time></b>';
                                            }
                                            else {
                                                echo '<meta itemprop="endDate" content="' . $date_to_iso . '">';
                                            }
                                            ?>
                                        </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">
                                            <?php
                                            if($category_group_id > 0) {
                                                /*<a href="<?= $category_group_link; ?>"><?= $category_group_link_title; ?></a>*/
                                                ?>
                                                <?= $category_group_link_title; ?>
                                                <?php
                                                if($category_id > 0) {
                                                    /*<a href="<?= $category_link; ?>"><?= $category_link_title; ?></a>*/
                                                    ?>
                                                    / <?= $category_link_title; ?>
                                                    <?php
                                                }
                                            }
                                            else {
                                                echo 'N/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++;
            }
        }

        // Start today and continue

        if(count($today_start) > 0) {
            ?>
            <h2 class="events-subtitle starting">Events Starting Today</h2>

            <?php
            $cnt = 1;
            foreach ($today_start as $event_id => $row) {
                $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']));
                $friendly_url = $row['friendly_url'];


                $location_str = '<span itemprop="location" itemscope itemtype="http://schema.org/Place">';
                $location = trim($row['region']);
                if($location != "") {
                    $location_str .= '<meta itemprop="name" content="'.$location.'">';
                    $location_str .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';
                    if (trim($row['city']) != '') {
                        $location_str .= '<span itemprop="addressLocality">' . trim($row['city']) . '</span>, ';
                    }
                    $location_str .= '<span itemprop="addressRegion">' . $location . '</span>';
                }
                else {
                    $location_str .= '<meta itemprop="name" content="Cyprus">';
                    $location_str .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';

                }

                $location_str .= '<meta itemprop="addressCountry" content="Cyprus">';
                $location_str .= '</span>';
                $location_str .= '</span>';

                $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;
                if($category_id == 0) {
                    $categ_code = $category_group_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']));

                $date_from_iso = date('c', strtotime($row['date_from']));
                $date_to_iso = date('c', strtotime($row['date_to']));

                $event_link = 'event/' . $friendly_url;

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

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

                $category_group_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                if($category_id > 0) {
                    $category_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                }
                if (isset($def_location) && $def_location != "") {
                    $location_title = $events_config['locations'][$def_location]['title'];
                    $category_group_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                    if($category_id > 0) {
                        $category_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                    }
                }


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


                ?>
                <div class="box event-box starting" itemscope itemtype="http://schema.org/Event">
                    <div class="box-header with-border">
                        <a href="<?= $event_link; ?>" itemprop="url" class="event-title">
                            <span itemprop="name"><?= $event_title; ?></span>
                        </a>
                        <?php
                        if(isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
                            echo '<div class="user-action-container event-action"><a class="user-action-inline" href="/admin/index.php?plugin=events&amp;action=events_item&amp;id=' . $id . '" target="_blank">e</a></div>';
                        }
                        ?>
                    </div>
                    <div class="box-body">
                        <div class="row">
                            <div class="col-sm-3">
                                <div class="image-holder">
                                    <div class="image-container" itemprop="image" itemscope itemtype="http://schema.org/ImageObject">
                                        <img src="<?= $image ?>" itemprop="url" alt="<?=$event_title;?>" class="event-image">
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-7 no-padding">
                                <p class="event-description" itemprop="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_str; ?></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><time itemprop="startDate" datetime="<?=$date_from_iso;?>"><?=$date_from;?></time></b>
                                            <?php
                                            if(strcmp($date_to, $date_from) != 0) {
                                                echo ' - ';
                                                echo '<b><time itemprop="endDate" datetime="' . $date_to_iso . '">' . $date_to . '</time></b>';
                                            }
                                            else {
                                                echo '<meta itemprop="endDate" content="' . $date_to_iso . '">';
                                            }
                                            ?>
                                        </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">
                                            <?php
                                            if($category_group_id > 0) {
                                                /*<a href="<?= $category_group_link; ?>"><?= $category_group_link_title; ?></a>*/
                                                ?>
                                                <?= $category_group_link_title; ?>
                                                <?php
                                                if($category_id > 0) {
                                                    /*<a href="<?= $category_link; ?>"><?= $category_link_title; ?></a>*/
                                                    ?>
                                                    / <?= $category_link_title; ?>
                                                    <?php
                                                }
                                            }
                                            else {
                                                echo 'N/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++;
            }
        }


        // Upcoming Events

        if(count($upcoming) > 0) {
            ?>
            <h2 class="events-subtitle upcoming">Upcoming Events</h2>

            <?php
            $cnt = 1;
            foreach ($upcoming as $event_id => $row) {
                $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']));
                $friendly_url = $row['friendly_url'];


                $location_str = '<span itemprop="location" itemscope itemtype="http://schema.org/Place">';
                $location = trim($row['region']);
                if($location != "") {
                    $location_str .= '<meta itemprop="name" content="'.$location.'">';
                    $location_str .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';
                    if (trim($row['city']) != '') {
                        $location_str .= '<span itemprop="addressLocality">' . trim($row['city']) . '</span>, ';
                    }
                    $location_str .= '<span itemprop="addressRegion">' . $location . '</span>';
                }
                else {
                    $location_str .= '<meta itemprop="name" content="Cyprus">';
                    $location_str .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';

                }

                $location_str .= '<meta itemprop="addressCountry" content="Cyprus">';
                $location_str .= '</span>';
                $location_str .= '</span>';

                $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;
                if($category_id == 0) {
                    $categ_code = $category_group_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']));

                $date_from_iso = date('c', strtotime($row['date_from']));
                $date_to_iso = date('c', strtotime($row['date_to']));

                $event_link = 'event/' . $friendly_url;

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

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

                $category_group_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                if($category_id > 0) {
                    $category_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                }
                if (isset($def_location) && $def_location != "") {
                    $location_title = $events_config['locations'][$def_location]['title'];
                    $category_group_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                    if($category_id > 0) {
                        $category_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                    }
                }


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

                ?>
                <div class="box event-box upcoming" itemscope itemtype="http://schema.org/Event">
                    <div class="box-header with-border">
                        <a href="<?= $event_link; ?>" itemprop="url" class="event-title">
                            <span itemprop="name"><?= $event_title; ?></span>
                        </a>
                        <?php
                        if(isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
                            echo '<div class="user-action-container event-action"><a class="user-action-inline" href="/admin/index.php?plugin=events&amp;action=events_item&amp;id=' . $id . '" target="_blank">e</a></div>';
                        }
                        ?>
                    </div>
                    <div class="box-body">
                        <div class="row">
                            <div class="col-sm-3">
                                <div class="image-holder">
                                    <div class="image-container" itemprop="image" itemscope itemtype="http://schema.org/ImageObject">
                                        <img src="<?= $image ?>" itemprop="url" alt="<?=$event_title;?>" class="event-image">
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-7 no-padding">
                                <p class="event-description" itemprop="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_str; ?></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><time itemprop="startDate" datetime="<?=$date_from_iso;?>"><?=$date_from;?></time></b>
                                            <?php
                                            if(strcmp($date_to, $date_from) != 0) {
                                                echo ' - ';
                                                echo '<b><time itemprop="endDate" datetime="' . $date_to_iso . '">' . $date_to . '</time></b>';
                                            }
                                            else {
                                                echo '<meta itemprop="endDate" content="' . $date_to_iso . '">';
                                            }
                                            ?>
                                        </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">
                                            <?php
                                            if($category_group_id > 0) {
                                                /*<a href="<?= $category_group_link; ?>"><?= $category_group_link_title; ?></a>*/
                                                ?>
                                                <?= $category_group_link_title; ?>
                                                <?php
                                                if($category_id > 0) {
                                                    /*<a href="<?= $category_link; ?>"><?= $category_link_title; ?></a>*/
                                                    ?>
                                                    / <?= $category_link_title; ?>
                                                    <?php
                                                }
                                            }
                                            else {
                                                echo 'N/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++;
            }
        }

        // Started before today and ongoing

        if(count($ongoing) > 0) {
            ?>
            <h2 class="events-subtitle ongoing">Events Already Started Before Today And Continue</h2>

            <?php
            $cnt = 1;
            foreach ($ongoing as $event_id => $row) {
                $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']));
                $friendly_url = $row['friendly_url'];


                $location_str = '<span itemprop="location" itemscope itemtype="http://schema.org/Place">';
                $location = trim($row['region']);
                if($location != "") {
                    $location_str .= '<meta itemprop="name" content="'.$location.'">';
                    $location_str .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';
                    if (trim($row['city']) != '') {
                        $location_str .= '<span itemprop="addressLocality">' . trim($row['city']) . '</span>, ';
                    }
                    $location_str .= '<span itemprop="addressRegion">' . $location . '</span>';
                }
                else {
                    $location_str .= '<meta itemprop="name" content="Cyprus">';
                    $location_str .= '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';

                }

                $location_str .= '<meta itemprop="addressCountry" content="Cyprus">';
                $location_str .= '</span>';
                $location_str .= '</span>';

                $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;
                if($category_id == 0) {
                    $categ_code = $category_group_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']));

                $date_from_iso = date('c', strtotime($row['date_from']));
                $date_to_iso = date('c', strtotime($row['date_to']));

                $event_link = 'event/' . $friendly_url;

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

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

                $category_group_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                if($category_id > 0) {
                    $category_link = "events/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                }
                if (isset($def_location) && $def_location != "") {
                    $location_title = $events_config['locations'][$def_location]['title'];
                    $category_group_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'];
                    if($category_id > 0) {
                        $category_link = "events/" . url_slug("$location_title") . "/" . $events_config['cats_ids'][$category_group_id]['friendly_url'] . '/' . $events_config['cats_ids'][$category_id]['friendly_url'];
                    }
                }


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

                ?>
                <div class="box event-box ongoing" itemscope itemtype="http://schema.org/Event">
                    <div class="box-header with-border">
                        <a href="<?= $event_link; ?>" itemprop="url" class="event-title">
                            <span itemprop="name"><?= $event_title; ?></span>
                        </a>
                        <?php
                        if(isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
                            echo '<div class="user-action-container event-action"><a class="user-action-inline" href="/admin/index.php?plugin=events&amp;action=events_item&amp;id=' . $id . '" target="_blank">e</a></div>';
                        }
                        ?>
                    </div>
                    <div class="box-body">
                        <div class="row">
                            <div class="col-sm-3">
                                <div class="image-holder">
                                    <div class="image-container" itemprop="image" itemscope itemtype="http://schema.org/ImageObject">
                                        <img src="<?= $image ?>" itemprop="url" alt="<?=$event_title;?>" class="event-image">
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-7 no-padding">
                                <p class="event-description" itemprop="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_str; ?></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><time itemprop="startDate" datetime="<?=$date_from_iso;?>"><?=$date_from;?></time></b>
                                            <?php
                                            if(strcmp($date_to, $date_from) != 0) {
                                                echo ' - ';
                                                echo '<b><time itemprop="endDate" datetime="' . $date_to_iso . '">' . $date_to . '</time></b>';
                                            }
                                            else {
                                                echo '<meta itemprop="endDate" content="' . $date_to_iso . '">';
                                            }
                                            ?>
                                        </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">
                                            <?php
                                            if($category_group_id > 0) {
                                                /*<a href="<?= $category_group_link; ?>"><?= $category_group_link_title; ?></a>*/
                                                ?>
                                                <?= $category_group_link_title; ?>
                                                <?php
                                                if($category_id > 0) {
                                                    /*<a href="<?= $category_link; ?>"><?= $category_link_title; ?></a>*/
                                                    ?>
                                                    / <?= $category_link_title; ?>
                                                    <?php
                                                }
                                            }
                                            else {
                                                echo 'N/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
}
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 matching your criteria.</p></div>';
}