<?php

##################################################
#
# Copyright (c) 2004-2021 OIC Group, Inc.
#
# This file is part of Exponent
#
# Exponent is free software; you can redistribute
# it and/or modify it under the terms of the GNU
# General Public License as published by the Free
# Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# GPL: http://www.gnu.org/licenses/gpl.txt
#
##################################################

if (!defined('EXPONENT'))
    exit('');

global $db, $user, $router;

$active = ECOM;
if (empty($active))
    return false;

if (!$user->isAdmin()) {
    $viewregperms = $db->selectValue(
        'userpermission',
        'uid',
        "uid=" . $user->id . " AND module=='eventregistration' AND permission!='view_registrants'"
    );
    if (!$viewregperms) {
        $groups = $user->getGroupMemberships();
        foreach ($groups as $group) {
            if (!$viewregperms) {
                $viewregperms = $db->selectValue(
                    'grouppermission',
                    'gid',
                    "gid=" . $group->id . " AND module=='eventregistration' AND permission!='view_registrants'"
                );
            } else {
                break;
            }
        }
    }
    if (!$viewregperms)
        return false;
}

if (!$db->selectValue('modstate', 'active', 'module=\'eventregistration\''))
    return false;

$ev = new eventregistration();
$allevents = $ev->find('all', 'product_type=\'eventregistration\' AND active_type=0');
$events = array();
foreach ($allevents as $event) {
    if ($event->eventenddate > time()) {
        $events[] = $event;
    }
}

$items1 = array(
    array(
        'text'      => gt('Manage Event Registrations'),
        'icon'      => 'fa-calendar-o',
        'icon5'      => 'far fa-calendar-alt',
        'iconbs'      => 'bi-calendar3',
        'classname' => 'events',
        'url'       => makeLink(
            array(
                'controller' => 'eventregistration',
                'action' => 'manage'
            )
        ),
    ),
    array(
        'text'      => gt('Add an event'),
        'icon'      => 'fa-plus-circle',
        'icon5'      => 'fas fa-plus-circle',
        'iconbs'      => 'bi-plus-circle',
        'classname' => 'add',
        'url'       => makeLink(
            array(
                'controller' => 'store',
                'action' => 'edit',
                'product_type' => 'eventregistration'
            )
        ),
        'divider' => count($events),
    )
);
$events = expSorter::sort(array('array' => $events, 'sortby' => 'eventdate', 'order' => 'ASC'));
$items2 = array();
foreach ($events as $event) {
    if (!empty($event->title)) {
        $thisitem = array();
        $thisitem['text'] = $event->title . ' (' . $event->countRegistrants(
            ) . ($event->quantity ? '/' . $event->quantity : '') . ')';
        $thisitem['url'] = $router->makeLink(
            array(
                'controller' => 'eventregistration',
                'action' => 'view_registrants', 'id' => $event->id
            )
        );
        $thisitem['classname'] = 'event';
        $thisitem['icon'] = 'fa-info';
        $thisitem['icon5'] = 'fas fa-info';
        $thisitem['iconbs'] = 'bi-info';
        $items2[] = $thisitem;
    }
}

if (bs3() || bs4() || bs5()) {
    $items = array_merge($items1, $items2);
} else {
    $items = array($items1, $items2);
}
return array(
    'text'      => ' <span class="event '.(bs4()?'badge badge-secondary':'label label-default').'">' . count($events) . '</span>',
    'icon'      => 'fa-calendar',
    'icon5'      => 'far fa-calendar-alt',
    'iconbs'      => 'bi-calendar3',
    'classname' => 'events',
    'submenu'   => array(
        'id'       => 'event2',
        'itemdata' => $items,
    )
);

?>
