<?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');

use Joomla\Utilities\ArrayHelper;

jimport('joomla.application.component.modellist');

class DJEventsModelCities extends JModelList
{
    public function __construct($config = array())
    {
        if (empty($config['filter_fields'])) {
            $config['filter_fields'] = array(
                'a.id',
                'a.name',
                'a.created',
                'a.published'
            );
        }

        parent::__construct($config);
    }
    protected function populateState($ordering = null, $direction = null)
    {
        // List state information.
        parent::populateState('a.name', 'asc');

        // Initialise variables.
        $app = JFactory::getApplication();
        $session = JFactory::getSession();

        $search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
        $this->setState('filter.search', $search);

        $state = $this->getUserStateFromRequest($this->context.'.filter.published', 'filter_published');
        $this->setState('filter.published', $state);

        // Load the parameters.
        $params = JComponentHelper::getParams('com_djevents');
        $this->setState('params', $params);
    }

    protected function getStoreId($id = '')
    {
        // Compile the store id.
        $id	.= ':'.$this->getState('filter.search');
        $id	.= ':'.$this->getState('filter.published');

        return parent::getStoreId($id);
    }

    protected function getListQuery()
    {
        // Create a new query object.
        $app = JFactory::getApplication();
        $user = JFactory::getUser();
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $params = JComponentHelper::getParams('com_djevents');

        // Select the required fields from the table.
        $select_default = 'a.*';

        $query->select($this->getState('list.select', $select_default));

        $query->from('#__djev_cities AS a');

        // Join over the users for the checked out user.
        $query->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');

        // Filter by search in title.
        $search = $this->getState('filter.search');
        if (!empty($search)) {
            if (stripos($search, 'id:') === 0) {
                $query->where('a.id = '.(int) substr($search, 3));
            }
            else {
                $search = $db->quote('%'.$db->escape($search, true).'%');
                $searchCols = array('a.name', 'a.alias','a.description');
                $query->where('('.implode(' LIKE '.$search.' OR ', $searchCols).' LIKE '.$search.')');
            }
        }

        $state = $this->getState('filter.published', false);
        if (is_numeric($state)) {
            $query->where('a.published='.(int)$state);
        } else if ($state == '') {
            $query->where('(a.published = 0 OR a.published = 1)');
        }


        // Add the list ordering clause.
        $orderCol	= $this->state->get('list.ordering', 'a.id');
        $orderDirn	= $this->state->get('list.direction', 'asc');

        $query->order($db->escape($orderCol .' '.$orderDirn));

        return $query;
    }

}
