<?php

defined('_JEXEC') or die('resticted aceess');

AddonParser::addAddon('sp_ob_events', 'sp_ob_events_addon');

function sp_ob_events_addon($atts, $content)
{

    $class = $number = '';

    extract(spAddonAtts(array(
        'number' => '',
        "class" => '',
    ), $atts));
    ob_start();
    jimport('joomla.application.component.model');
    JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_icagenda/models');
    $eventModel = JModelLegacy::getInstance( 'Event', 'iCagendaModel' );
    if (!class_exists('icagendaEvent'))
        require(JPATH_SITE.'/administrator/components/com_icagenda/utilities/event/event.php');
    if (!class_exists('icagendaGooglemaps'))
        require(JPATH_SITE.'/administrator/components/com_icagenda/utilities/googlemaps/googlemaps.php');
    if (!class_exists('icagendaManager'))
        require(JPATH_SITE.'/administrator/components/com_icagenda/utilities/manager/manager.php');
    if (!class_exists('icagendaRegistration'))
        require(JPATH_SITE.'/administrator/components/com_icagenda/utilities/registration/registration.php');
    if (!class_exists('icagendaAddtocal'))
        require(JPATH_SITE.'/administrator/components/com_icagenda/utilities/addtocal/addtocal.php');

    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('id');
    $query->from('#__icagenda_events');
    $query->where('startdate>now()');
    $query->order('startdate');
    $db->setQuery($query);
    $ids= $db->loadColumn();
    $count = (count($ids)<=$number) ? count($ids) : $number;
    ?>

    <div class="recent_events">
        <?php
        for($i=0;$i<$count;$i++) {
            $item = $eventModel->getItem($ids[$i]);
            ?>
            <div class="item">
                <div class="clear">
                    <div class="date">
                        <div class="date_n"><?php echo date("d", strtotime($item->startdate));?></div>
                        <div class="date_t"><?php echo date("M", strtotime($item->startdate));?></div>
                    </div>
                    <div class="info">
                        <h3>
                            <a href="<?php echo icagendaEvent::url($item->id, $item->alias);?>" class="title-post"><?php echo $item->title;?></a>
                        </h3>
                        <div class="excerpt-post">
                            <?php echo substr(strip_tags($item->desc),0,60)?>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        }
        ?>
    </div>

    <?php
    $html = ob_get_clean();
    return $html;
}