<?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');
jimport('joomla.access.access');
if (!class_exists('VirtueMartModelUser')) {
    include JPATH_VM_ADMINISTRATOR .'/'. 'models'.'/'. 'user.php';
}
if (!class_exists('MarketplaceModelMarketplace')) {
    /**
     * MarketplaceModelSuggestedcategory 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 MarketplaceModelSuggestedcategory extends JModelList
    {
        var $_data = null;
        var $_total = null;
        var $_pagination = null;
        var $_id = null;
        /**
         * __construct function
         *
         * @param array $config var
         */
        public function __construct($config = array())
        {
            if (empty($config['filter_fields'])) {
                $config['filter_fields'] = array(
                    'category_name', 
                    'category_desc',
                    'status',
                    'additional_info',
                    'requested_by',
                    'requested_date',
                    'id',
                );
            }
            parent::__construct($config);
        }
        /**
         * GetCategoryToCreate function
         *
         * @param [type] $cid var
         * 
         * @return void
         */
        public function getCategoryToCreate($cid)
        {
            $db=$this->_db;
            $query=$db->getQuery(true);
            $query->select('*')
                ->from($db->quoteName('#__marketplace_category_request'))
                ->where($db->quoteName('id')."=".$db->quote($cid));
            try{
                $db->setQuery($query);

                return $db->loadObjectList();
            }
            catch(Exception $e){
                JFactory::getApplication()->enqueueMessage(
                    $e->getMessage(), 'error'
                );
            }
        }
        /**
         * Remove Request function
         *
         * @param [type] $cid var
         * 
         * @return void
         */
        public function removeRequest($cid)
        {
            $db=$this->_db;
            $query=$db->getQuery(true);
            $query->delete($db->quoteName('#__marketplace_category_request'))

                ->where($db->quoteName('id')."=".$db->quote($cid));
            try{
                $db->setQuery($query);

                return $db->loadObjectList();
            }
            catch(Exception $e){
                JFactory::getApplication()->enqueueMessage(
                    $e->getMessage(), 'error'
                );
            }
        }
        /**
         * GetEmail function
         *
         * @param array $cid var
         * 
         * @return void
         */
        public function getEmail($cid=array())
        {
            $db=JFactory::getDBO();
            $email=array();
            foreach ($cid as $value) {
                $query=$db->getQuery(true);
                $query->select($db->quoteName('requested_by'))
                    ->from($db->quoteName('#__marketplace_category_request'))
                    ->where($db->quoteName('id')."=".$value);
                $db->setQuery($query);
                try{
                    $email[]=$db->loadResult();
                }catch(Exception $e){
                    JFactory::getApplication()->enqueueMessage(
                        $e->getMessage(), 'error'
                    );
                    return false;
                }
            }
            return $email;
        }
        /**
         * GetListQuery function
         *
         * @return void
         */
        protected function getListQuery()
        {
            $db  = $this->getDbo();
            $query = $db->getQuery(true);
            $query->select(
                $this->getState(
                    'list.select', 'c.*', 'c.status AS status'
                )
            );
            $query->from(
                $db->quoteName('#__marketplace_category_request').' AS c'
            );
            $search = $this->getState('filter.search');
            $status=$this->getState('filter.status');
            if (is_numeric($status)) {
                $query->where('c.status != ' . (int) $status);
            }
            if ($this->getState('filter.search') !== ''
                && $this->getState('filter.search') !== null
            ) {
                $search = $db->quote(
                    '%' . str_replace(
                        ' ',
                        '%',
                        $db->escape(trim($this->getState('filter.search')), true)
                        . '%'
                    )
                );
                $searches   = array();
                $searches[] = 'c.category_name LIKE ' . $search;
                $searches[] = 'c.category_desc LIKE ' . $search;
                $query->where('(' . implode(' OR ', $searches) . ')');
            }
            $published = $this->getState('filter.published');
            if (is_numeric($published)) {
                $query->where('c.status = '.(int) $published);
            } elseif ($published === '') {
                $query->where('(c.status IN (0, 1))');
            }
            $orderCol       = $this->state->get('list.ordering', 'id');
            $orderDirn      = $this->state->get('list.direction', 'asc');
            $query->order($db->escape($orderCol.' '.$orderDirn));
            return $query;
        }
        /**
         * Reject function
         *
         * @param [type] $cid var
         * 
         * @return void
         */
        function reject($cid)
        {
            $db=JFactory::getDBO();
            $query=$db->getQuery(true);
            $query->delete($db->quoteName('#__marketplace_category_request'))
                ->where($db->quoteName('id').'='.$db->quote($cid));
            $db->setQuery($query);
            try{
                if (!$db->execute()) {
                    $this->setError($db->getErrorMsg());
                    return false;
                }
            }catch(Exception $e){
                JFactory::getApplication()->enqueueMessage(
                    $e->getMessage(), 'error'
                );
                return false;
            }
            return true;
        }
        /**
         * Approve function
         *
         * @param array   $cid  var
         * @param integer $deny var
         * 
         * @return void
         */
        function approve($cid = array(), $deny = 1)
        {
            $db=JFactory::getDBO();
            if (count($cid)) {
                $cids = implode(',', $cid);
                $query=$db->getQuery(true);
                $query->update($db->quoteName('#__marketplace_category_request'))
                    ->set($db->quoteName('status')."=".(int)$deny)
                    ->where($db->quoteName('id').' IN('.$cids.')');
                $db->setQuery($query);
                try{
                    if (!$db->execute()) {
                        $this->setError($db->getErrorMsg());
                        return false;
                    }
                }catch(Exception $e){
                    JFactory::getApplication()->enqueueMessage(
                        $e->getMessage(), 'error'
                    );
                    return false;
                }
            } 
            return true;
        }
        /**
         * PopulateState function
         *
         * @param [type] $ordering  var
         * @param [type] $direction var
         * 
         * @return void
         */
        protected function populateState($ordering = null, $direction = null)
        {
            $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);
            $published = $this->getUserStateFromRequest(
                $this->context.'.filter.published', 'filter_state', ''
            );
            $this->setState('filter.published', $published);
            parent::populateState('c.id', 'asc');
        }
        /**
         * GetSellerInfo function
         *
         * @param [type] $seller_id var
         * 
         * @return void
         */
        public function getSellerInfo($seller_id)
        {
            $db=JFactory::getDBO();
            $query=$db->getQuery(true);
            $query->select('a.*,b.email')
                ->from($db->quoteName('#__marketplace_sellerprofile', 'a'))
                ->join(
                    'LEFT', $db->quoteName('#__users', 'b') . ' ON ('
                    . $db->quoteName('a.seller_id') . ' = ' .
                    $db->quoteName('b.id') . ')'
                )
                ->where($db->quoteName('seller_id')."=".$seller_id);
            $db->setQuery($query);
            try{
                return $db->loadObjectList();
            }catch(Exception $e){
                JFactory::getApplication()->enqueueMessage(
                    $e->getMessage(), 'error'
                );
                return false;
            }
        }
        /**
         * GetForm function
         *
         * @param array   $data     var
         * @param boolean $loadData var
         * 
         * @return void
         */
        public function getForm($data = array(), $loadData = true)
        {
            $form = $this->loadForm(
                'com_marketplace.suggestedcategory',
                'sendmail_category',
                array('control' => 'jform', 'load_data' => $loadData)
            );
            if (empty($form)) {
                return false;
            }
            return $form;
        }
        /**
         * GetCountry function
         *
         * @return void
         */
        public function getCountry()
        {
            $db=JFactory::getDBO();
            $query=$db->getQuery(true);
            $query->select('*')
                ->from($db->quoteName('#__virtuemart_countries'));
            $db->setQuery($query);
            $country_result = $db->loadObjectList();
            return $country_result;
        }
    }
}
