<?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('MarketplaceModelFeedback')) {
    /**
     * MarketplaceModelFeedback 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 MarketplaceModelFeedback 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(
                    'g.name',
                    'a.feed_value',
                    'a.feed_price',
                    'a.feed_quality',
                    'a.feed_summary',
                    'a.feed_review',
                    'a.create_date',
                    'a.status',
                    'a.feed_id'
                );
            }
            parent::__construct($config);
        }
        /**
         * GetListQuery function
         *
         * @return void
         */
        protected function getListQuery()
        {
            $db  = $this->getDbo();
            $query = $db->getQuery(true);
            $query->select(
                $this->getState(
                    'list.select', 'a.*'
                )
            );

            $query->from($db->quoteName('#__marketplace_feedback').' AS a');
            $search = $this->getState('filter.search');
            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[] = 'a.feed_summary  LIKE ' . $search;
                $searches[] = 'a.feed_review LIKE ' . $search;
                $searches[] = 'g.name LIKE ' . $search;
                $searches[] = 'u.first_name LIKE ' . $search;
                $query->where('(' . implode(' OR ', $searches) . ')');
            }
            $published = $this->getState('filter.published');
            if (is_numeric($published)) {
                    $query->where('a.status = '.(int) $published);
            } elseif ($published === '') {
                    $query->where('(a.status IN (-1, 1))');
            }
            $query->select('g.name AS b_name');
            $query->join('LEFT', '#__users AS g ON g.id = a.buyer_id');

            // Join over the categories.
            $query->select(
                'u.first_name as s_first_name, u.last_name as s_last_name'
            );
            $query->join(
                'LEFT',
                '#__marketplace_sellerprofile AS u ON u.seller_id = a.seller_id'
            ); 
        
            // Add the list ordering clause.
            $orderCol       = $this->state->get('list.ordering', 'u.first_name');
            $orderDirn      = $this->state->get('list.direction', 'asc');
            if ($orderCol == 'a.ordering' || $orderCol == 'first_name') {
                $orderCol = 'u.first_name'.$orderDirn.', u.first_name';
            }
            $query->order($db->escape($orderCol.' '.$orderDirn));
            $query->group($db->quoteName('feed_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);
            $params = JComponentHelper::getParams('com_marketplace');
            $this->setState('params', $params);
            parent::populateState('a.feed_id', 'asc');
        }

        /**
         * Approvefeedback function
         *
         * @param array   $cid var
         * @param integer $add var
         * 
         * @return void
         */
        function approvefeedback($cid = array(), $add = 1)
        {
            if (count($cid)) {
                $query=$this->_db->getQuery(true);
                $fields=array($this->_db->quoteName('status')."=".(int)$add."");
                $cids = implode(',', $cid);
                $query->update($this->_db->quoteName('#__marketplace_feedback'))
                    ->set($fields)
                    ->where($this->_db->quoteName('feed_id')." IN(".$cids.")");
                    
                $this->_db->setQuery($query);
                try{
                    if (!$this->_db->execute()) {
                        $this->setError($this->_db->getErrorMsg());
                        return false;
                    }
                }catch(Exception $e){
                    JFactory::getApplication()->enqueueMessage(
                        $e->getMessage(), 'error'
                    );
                    return false;
                }
            } 
            return true;
        }
        /**
         * Rejectfeedback function
         *
         * @param array   $cid  var
         * @param integer $deny var
         * 
         * @return void
         */
        function rejectfeedback($cid = array(), $deny = -1)
        {
            if (count($cid)) {
                $cids = implode(',', $cid);
                $query=$this->_db->getQuery(true);
                $fields=array($this->_db->quoteName('status')."=".(int)$deny."");
                $cids = implode(',', $cid);
                $query->update($this->_db->quoteName('#__marketplace_feedback'))
                    ->set($fields)
                    ->where($this->_db->quoteName('feed_id')." IN(".$cids.")");
                $this->_db->setQuery($query);
                try{
                    if (!$this->_db->execute()) {
                        $this->setError($this->_db->getErrorMsg());
                        return false;
                    }
                }catch(Exception $e){
                    JFactory::getApplication()->enqueueMessage(
                        $e->getMessage(), 'error'
                    );
                    return false;
                }
            } 
            return true;
        }
        /**
         * Delete function
         *
         * @param array $cid var
         * 
         * @return void
         */
        function delete($cid = array())
        {
            if (count($cid)) {
                $cids = implode(',', $cid);
                $query=$this->_db->getQuery(true);
                $fields=array($this->_db->quoteName('status')."=".(int)$deny."");
                $cids = implode(',', $cid);
                $query->delete($this->_db->quoteName('#__marketplace_feedback'))
                    ->where($this->_db->quoteName('feed_id')." IN(".$cids.")");

                $this->_db->setQuery($query);
                try{
                    if (!$this->_db->query()) {
                        $this->setError($this->_db->getErrorMsg());
                        return false;
                    }
                }catch(Exception $e){
                    JFactory::getApplication()->enqueueMessage(
                        $e->getMessage(), 'error'
                    );
                    return false;
                }
            }
            $total  = count($cid);
            $msg  = $total.' '.JText::_('FEEDBACK_DELETE');
            return $msg;
        }
    }
}
