<?php
/**
 * Marketplace - Component Marketplace 
 * ******************************************************************
 *
 * PHP version 7.0
 *
 * @category   Component
 * @package    Joomla
 * @author     WebKul software private limited <support@webkul.com>
 * @copyright  2010 WebKul software private limited
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @version    GIT:5.2
 * @filesource http://store.webkul.com
 * @link       Technical Support:  webkul.uvdesk.com
 * ******************************************************************
 */
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');
// import the tables

if (!class_exists('MarketplaceModelMessages')) {
    /**
     * MarketplaceModelMessages class
     * 
     * @category Component
     * @package  Joomla
     * @author   WebKul software private limited <support@webkul.com>
     * @license  http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
     * @link     Technical Support:  webkul.uvdesk.com
     */
    class MarketplaceModelMessages extends JModelList
    {
        /**
         * __construct function
         *
         * @param array $config var
         */
        public function __construct($config = array())
        {
            if (empty($config['filter_fields'])) {
                $config['filter_fields'] = array(
                    'a.id','u.username',
                    'a.subject','a.time'
                );
            }
            parent::__construct($config);
        }
        /**
         * GetListQuery function
         *
         * @return void
         */
        protected function getListQuery()
        {
            $db  = $this->getDbo();
            $query = $db->getQuery(true);
            $query->select(
                $this->getState(
                    'list.select', 'a.*'
                )
            );
            $search = $this->getState('filter.search');
            if ($search != "") {
                $query->where(
                    '(a.subject LIKE '.$db->quote("%".$search.'%').
                    ' OR u.username LIKE '.$db->quote("%".$search.'%').")"
                );
            }
            $query->from($db->quoteName('#__marketplace_ask_admin').' AS a');
            $query->select(
                'u.username'
            );
            $query->join(
                'LEFT',
                '#__users AS u ON a.seller_id'.
                ' = u.id'
            );
            $sellerId = $this->getState('filter.seller_id');
            
            if (is_numeric($sellerId)) {
                $query->where('a.seller_id = '.(int)$sellerId);
            }
            $orderCol       = $this->state->get('list.ordering', 'a.id');
            $orderDirn      = $this->state->get('list.direction', 'asc');
            
            $query->order($db->escape($orderCol.' '.$orderDirn));
            $query->group('a.id');
            
            return $query;
        }
        /**
         * PopulateState function
         *
         * @param [type] $ordering  var
         * @param [type] $direction var
         * 
         * @return void
         */
        protected function populateState($ordering = null, $direction = null)
        {
            $app = JFactory::getApplication('administrator');
            $search = $this->getUserStateFromRequest(
                $this->context.'.filter.search', 'filter_search'
            );
            $this->setState('filter.search', $search);
            $state = $this->getUserStateFromRequest(
                $this->context.'.filter.state', 'filter_state', '', 'string'
            );
            $this->setState('filter.state', $state);
            $seller = $this->getUserStateFromRequest(
                $this->context . '.filter.seller_id', 'filter_seller_id'
            );
            $this->setState('filter.seller_id', $seller);
            $params = JComponentHelper::getParams('com_marketplace');
            $this->setState('params', $params);
            parent::populateState('a.id', 'asc');
        }
    }
}