<?php
$optres['css'][] = $module_css_path . "amodules/events_ext/css/events.css";
$event_fr = mysql_real_escape_string(trim($_route[0]));
$q = "SELECT * FROM events WHERE friendly_url='$event_fr'";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) == 1) {
    $row = mysql_fetch_assoc($r);

    $event = array_map('stripslashes', $row);

    $id = $event['id'];

    $dcode = $row['dcode'];
    $location = '<span itemprop="addressRegion">' . trim($row['region']) . '</span>';
    if (trim($row['city']) != '') $location .= ' / ' . '<span itemprop="addressLocality">' . trim($row['city']) . '</span>';
    if ($location == '') {
        $location = '<span itemprop="addressCountry">Cyprus</span>';
    }
    else {
        $location .= ', <span itemprop="addressCountry">Cyprus</span>';
    }

    $category_group_id = intval($event['category_group_id']);
    $category_id = intval($event['category_id']);
    $categ_code = $category_group_id . '-' . $category_id;
    if($category_id == 0) {
        $categ_code = $category_group_id;
    }

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

    $date_from = date("d/m/Y", strtotime($event['date_from']));
    $date_to = date("d/m/Y", strtotime($event['date_to']));

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

    $event_link = 'event/' . $id . '/' . url_slug($event['title']);

    $description = $event['description'];

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


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

    //echo '<pre>', var_export($event, true), '</pre>';
    $webpage_title_en = $event['title'];
    $meta['title'] = $webpage_title_en;
    $meta['description'] = trim($row['region']) . ' Event | ' . tokenTruncate(strip_tags($description),  145);


    if(strtotime($event['date_to']) < strtotime(date('Y-m-d 00:00:00'))) {
        $breadcrumbs = array();
        $breadcrumbs [] = array(
            'link' => 'past-events',
            'title' => 'Cyprus Past Events'
        );
    }
    else {
        $breadcrumbs = array();
        $town_ = "";
        if ($dcode != "") {
            $town_ = $events_config['towns'][$dcode];
            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);
                    $crumb = array(
                        'link' => $row1['friendly_url'] . '.html',
                        'title' => str_replace("|", '', stripslashes($row1['title_el']))
                    );

                    $breadcrumbs [] = $crumb;
                }
            }
        }


        $show_links = true;
        if(strpos($ultra_domainname, 'cyprusevents.com') === false) {
            $show_links = false;
        }

        if($show_links) {
            $breadcrumbs [] = array(
                'link' => ($town_ == "") ? $category_group_link : str_replace('events/', "events/$town_/", $category_group_link),
                'title' => $category_group_link_title
            );

            if ($category_id > 0) {
                $breadcrumbs [] = array(
                    'link' => ($town_ == "") ? $category_link : str_replace('events/', "events/$town_/", $category_link),
                    'title' => $category_link_title
                );
            }
        }
    }


    ?>
    <article class="article cyprus" itemscope itemtype="http://schema.org/Event">
        <header>
            <h1 itemprop="name"><?php echo $event['title']; ?></h1>
            <!--[/SocialShareButtons/]-->
            <?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=' . $event['id'] . '" target="_blank">e</a></div>';
            }
            ?>
        </header>
        <div class="article-body cyprus event">
            <?php
            if (isset($_REQUEST['adsense_debug'])) {
                echo '$def_ad_id = ' . var_export($def_ad_id, true);
                echo '$gads_show = ' . var_export($gads_show, true);
                echo '<pre>$gads = ' . var_export($gads, true), '</pre>';
            }
            // Show Adsense
            if ($gads_show && $def_ads == "yes" && $def_ad_id != "") {
                $adsense_container_class = "no-margin";
                echo '<div style="margin-top: 9px;margin-bottom: 15px;">';
                include(dirname(__FILE__) . "/../gads_content/gads_content_default.php");
                echo '</div>';
                unset($adsense_container_class);
            }
            ?>
            <div class="container-fluid">
                <div class="row">
                    <div class="event-data" itemprop="description">
                        <?php
                        if ($image != '') {
                            echo '<div class="image-container"><img itemprop="image" class="event-image img-thumbnail" src="' . $image . '" alt="'.$event['title'].'"></div>';
                        }
                        ?>
                        <?php
                        echo $description;
                        ?>
                    </div>
                    <div class="box event-box">
                        <div class="box-body">
                            <div class="event-specs">
                                <div class="row">
                                    <div class="col col-sm-3 col-xs-3 no-wrap"><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" itemprop="location" itemscope itemtype="http://schema.org/Place">
                                        <meta content="Event Location" itemprop="name"/>
                                        <strong itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><?= $location; ?></strong>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col col-sm-3 col-xs-3 no-wrap"><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><span itemprop="startDate" content="<?=$date_from_iso?>"><?= $date_from; ?></span></b>
                                        <?php
                                        if(strcmp($date_to, $date_from) != 0) {
                                            echo  ' - ';
                                            echo '<b><span itemprop="endDate"  content="'.$date_to_iso.'">' . $date_to . '</span></b>';
                                        }
                                        ?>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col col-sm-3 col-xs-3 no-wrap">
                                        <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) {
                                            if($show_links) {
                                                ?>
                                                <a href="<?= $category_group_link; ?>"><?= $category_group_link_title; ?></a>
                                                <?php
                                                if ($category_id > 0) {
                                                    ?>
                                                    / <a href="<?= $category_link; ?>"><?= $category_link_title; ?></a>
                                                    <?php
                                                }
                                            }
                                            else {
                                                ?>
                                                <?= $category_group_link_title; ?>
                                                <?php
                                                if ($category_id > 0) {
                                                    ?>
                                                    / <?= $category_link_title; ?>
                                                    <?php
                                                }
                                            }
                                        }
                                        else {
                                            echo 'N/A';
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </article>
    <?php
}
else {
    $show404 = true;
    require_once(dirname(__FILE__) . "/../404/404.php");
}
