<?php
//echo __FILE__ . '<br>';
$optres['css'][] = $module_css_path . 'amodules/events_ext/css/events.css';
$optres['js'][] = $path . 'amodules/events_ext/js/events_main.js';

if(isset($def_location) && $def_location != "") {
    $event_srch['event_location'] = $def_location;
}

require_once(dirname(__FILE__) . "/events_search_form.php");


if($gads_show) {
    if ($def_ads == "yes" && $def_ad_id != "") {
        include(dirname(__FILE__) . "/../gads_content/gads_content_default.php");
    }
}

require_once(dirname(__FILE__) . "/current_events.php");

if($def_category_list == "yes") {
    // Get the category events
    $events_by_categs_key = 'events_by_categs';
    if(isset($def_location) && $def_location != "") {
        $events_by_categs_key .= "_" . $def_location;
    }
    $categs = $pfcache->get($events_by_categs_key);
    if($categs == null) {
        $today = date("Y-m-d");
        $q = "SELECT e.* FROM events e WHERE e.active='1' AND e.crawled='1' AND date_to >='$today'";
        /*if ($jdl > 0) {
            $daystolive = date("Y-m-d 00:00:00", strtotime("-$jdl days", time()));
            $q .= " AND j.updated_at>='$daystolive' ";
        }*/

        if(isset($def_location) && $def_location != "") {
            $q .= " AND e.dcode='$def_location' ";
        }

        $q .= " ORDER BY e.category_group_title ASC, e.category_title ASC;";

        //echo $q;

        $r = @mysql_query($q);
        if ($r && mysql_num_rows($r) > 0) {
            while ($row = mysql_fetch_assoc($r)) {
                $row = 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($categs[$category_group_id])) {
                    $categs[$category_group_id] = array(
                        'title' => $category_group_title,
                        'total' => 0,
                        'link'  => $category_group_link,
                        'subs'  => array()
                    );
                }


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

                if($category_id > 0) {


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


            }
        }

        $pfcache->set($events_by_categs_key, $categs, $pfcache_ttl);
    }



    $def_title_location = (isset($def_location) && $def_location != "") ? $events_config['locations'][$def_location]['title'] : "Cyprus";

    if (isset($categs) && count($categs) > 0) {
        ?>
        <div class="box box-primary category-events">
            <div class="box-header with-border">
                <h2><?=$def_title_location;?> Events By Category</h2>
            </div>
            <ul class="list-group">
                <?php
                foreach ($categs as $gr_id=>$gr_data) {
                    ?>

                    <li class="list-group-item events-group">
                        <span class="badge"><?=$gr_data['total'];?></span>
                        <a href="<?= $gr_data['link']; ?>"><?= $gr_data['title']; ?></a>
                    </li>
                    <?php
                    if(count($gr_data['subs']) > 0) {
                        foreach($gr_data['subs'] as $cat) {
                            ?>
                            <li class="list-group-item events-categ">
                                <span class="badge"><?=$cat['total'];?></span>
                                <i class="fa fa-caret-right"></i> <a href="<?= $cat['link']; ?>"><?= $cat['title']; ?></a>
                            </li>
                            <?php
                        }
                    }
                    ?>
                    <?php

                }
                ?>
            </ul>

        </div>
        <?php
    }

}

