<?php
/**
 * @package DJ-Events
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 * @developer Szymon Woronowski - szymon.woronowski@design-joomla.eu
 *
 */

defined('_JEXEC') or die('Restricted access');

require_once JPath::clean(JPATH_ADMINISTRATOR . '/components/com_djevents/models/events.php');

jimport('joomla.application.component.helper');
jimport('joomla.html.pagination');

class DJEventsModelEventsList extends DJEventsModelEvents
{
    public function __construct($config = array())
    {
        parent::__construct($config);
    }

    public function populateState($ordering = null, $direction = null)
    {

        // This is ignored when calling a model from API scope

        // List state information.
        parent::populateState('t.start', 'asc');

        $app = JFactory::getApplication();
        $params = $app->getParams('com_djevents');

        $search = $app->input->getString('search');
        $this->setState('filter.search', $search);

        $category = $app->input->getInt('cid');
        $this->setState('filter.category', $category);

        $city = $app->input->getInt('city');
        $this->setState('filter.city', $city);

        $location = $app->input->getInt('location');
        $this->setState('filter.location', $location);

        // Online
        $onlyOnline = $app->input->getInt('online');
        $this->setState('filter.online', $onlyOnline);

        $from = $app->input->getString('from');
        $to = $app->input->getString('to');

        $timeline = $app->input->getString('timeline', 'upcoming');
        switch ($timeline) {
            case 'custom' :
                if (!$from) $from = $app->input->get('date_from');
                if (!$to) $to = $app->input->get('date_to');
                break;
            case 'past' :
                if (!$to) $to = date("Y-m-d");
            case 'upcoming' :
                if (!$from) $from = date("Y-m-d");
                break;
        }


        $this->setState('filter.to', $to);
        $this->setState('filter.from', $from);

        $start = $app->input->getInt('limitstart', 0);
        $limit = $app->input->getInt('limit', $params->get('evlist_limit', 10));

        $this->setState('list.start', $start);
        $this->setState('list.limit', $limit);

        $this->setState('filter.published', 1);
    }

    function getTag()
    {

        $app = JFactory::getApplication();
        $params = JComponentHelper::getParams('com_djevents');

        $tag = $app->input->get('tag');

        if (!empty($tag) && intval($tag) > 0) {

            if ($params->get('tags_type', 'djevents') == 'joomla') {
                $this->_db->setQuery('SELECT *, title as name FROM #__tags WHERE id=' . (int)$tag);
            } else {
                $this->_db->setQuery('SELECT * FROM #__djev_tags WHERE id=' . (int)$tag);
            }

            $tag = $this->_db->loadObject();

            return $tag;
        }

        return false;
    }

    function getStoreKey()
    {
        return $this->getStoreId();
    }

    public function getCategory()
    {
        $app = JFactory::getApplication();
        $cid = $app->input->get('cid');
        if ($cid) {
            $db = JFactory::getDbo();
            $db->setQuery('SELECT * FROM #__djev_cats WHERE id = ' . (int)$cid);

            return $db->loadObject();
        }

    }

    public function getCategories($asFilter = true)
    {

        if (!$asFilter) {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $query
                ->select(array(
                    'c.id as value',
                    'c.name as text'
                ))
                ->from($db->quoteName('#__djev_cats', 'c'))
                ->order('c.name ASC');
            $this->_db->setQuery($query);
            return $this->_db->loadObjectList();
        } else {
            $this->setState('list.select', 'c.id as value, c.name as text');
            $this->setState('list.ordering', 'c.name');

            $query = $this->getListQuery();

            $query->group('c.id');
            $query->order('c.name ASC');


            $this->_db->setQuery($query);
            return $this->_db->loadObjectList();
        }
    }


    public function getCities($locationId = 0, $asFilter = true)
    {

        if (!$asFilter) {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $query
                ->select(array(
                    'city.id as value',
                    'city.name as text'
                ))
                ->from($db->quoteName('#__djev_cities', 'city'))
                ->order('city.id');
            $db->setQuery($query);

            return $db->loadObjectList();
        } else {
            $app = JFactory::getApplication();
            $category = $app->input->get('cid');
            $city = $app->input->get('city');
            $location = $app->input->get('location');

            $this->setState('list.select', 'city.id as value, city.name as text');
            $this->setState('list.ordering', 'city.id');

            if ($category && (!$city && !$location)) {

                $this->setState('filter.city', false);
            }

            $query = $this->getListQuery();
            $query->join('INNER', $this->_db->quoteName('#__djev_cities', 'city') . ' ON  l.city_id = city.id OR a.city_id = city.id');

            $query->group('city.id');

            $this->_db->setQuery($query);
            $cities = $this->_db->loadObjectList();
            if ($category && (!$city && !$location)) {
                $this->setState('filter.city', false);
            }


            return $cities;
        }

    }

    public function getLocations($cityId = 0, $asFilter = true)
    {
        if (!$asFilter) {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $query
                ->select(array(
                    'loc.id as value',
                    'loc.name as text',
                    'CONCAT(loc.id, "-", loc.alias) AS location_sef'
                ))
                ->from($db->quoteName('#__djev_locations', 'loc'))
                ->order('loc.name ASC');
        } else {
            $app = JFactory::getApplication();
            $city = $app->input->getInt('city');

            $this->setState('list.select', 'loc.id as value, loc.name as text');
            $this->setState('list.ordering', 'loc.name');


            $query = $this->getListQuery();
            $query->select('CONCAT(loc.id, "-", loc.alias) AS location_sef');
            $query->join('INNER', $this->_db->quoteName('#__djev_locations', 'loc') . ' ON loc.id = a.location_id' . (($city) ? ' AND loc.city_id = ' . $city : ''));



        }


        $this->_db->setQuery($query);

        return $this->_db->loadObjectList('value');
    }

    public function getLocation($id)
    {
        if (!isset($this->locations[$id])) {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true);

            $query
                ->select('l.*')
                ->from($db->quoteName('#__djev_locations', 'l'))
                ->where('l.published = 1')
                ->where('l.id = ' . (int)$id);
            $db->setQuery($query);
            $this->locations[$id] = $db->loadObject();
        }
        return $this->locations[$id];

    }

}
