<?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');
if (!class_exists('VmConfig')) {
    include JPATH_ADMINISTRATOR.'/components/com_virtuemart/helpers/config.php';
}
if (!class_exists('CurrencyDisplay')) {
    include JPATH_ADMINISTRATOR.
    '/components/com_virtuemart/helpers/currencydisplay.php';
}
$vmconfig=VmConfig::loadConfig();
if (!class_exists('MarketplaceModelProducts')) {
    /**
     * Marketplace Model Category 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 MarketplaceModelCategory extends JModelLegacy
    {
        /**
         * __construct function
         */
        function __construct()
        {
            parent::__construct();
        }
        /**
         * Category Product function
         *
         * @param [int]  $sellerid    var
         * @param [int]  $category_id var
         * @param string $order_by    var
         * @param string $search      var
         * 
         * @return void
         */
        public function catPro($sellerid,$category_id,$order_by="",$search="")
        {
            $vmconfig=VmConfig::loadConfig();
            if ($order_by=='p_l_to_h') {
                $order_by='e.product_price asc';
            } elseif ($order_by=='p_date') {
                $order_by='a.created_on desc';
            } elseif ($order_by=='p_name') {
                $order_by='c.product_name asc';
            } elseif ($order_by=='p_h_to_l') {
                $order_by='e.product_price desc';
            } else {
                $order_by='a.created_on asc';
            }
            $db = JFactory::getDBO();
            $url=JURI::base();
            $abc=explode('/component', $url);
            $basepath=$abc[0].'/';

            $defaultcountry_code=VmConfig::$vmlang;
            $mediaModel = VmModel::getModel('Media');
            $obj=CurrencyDisplay::getInstance();
            $wk_currency = $obj->getSymbol();
            $query = $db->getQuery(true);
            $query
                ->select(
                    array(
                        'a.*',
                        'c.product_name',
                        'd.rating',
                        'e.product_price'
                    )
                )
                ->from($db->quoteName('#__virtuemart_products', 'a'))
                ->join(
                    'LEFT',
                    $db->quoteName('#__virtuemart_product_categories', 'b') .
                    ' ON (' . $db->quoteName('a.virtuemart_product_id') .
                    ' = ' . $db->quoteName('b.virtuemart_product_id') . ')'
                )
                ->join(
                    'LEFT',
                    $db->quoteName(
                        '#__virtuemart_products_'.$defaultcountry_code,
                        'c'
                    ) .' ON (' . $db->quoteName('a.virtuemart_product_id') .
                    ' = ' . $db->quoteName('c.virtuemart_product_id') . ')'
                )
                ->join(
                    'LEFT',
                    $db->quoteName('#__virtuemart_ratings', 'd').' ON ('.
                    $db->quoteName('a.virtuemart_product_id') . ' = '
                    . $db->quoteName('d.virtuemart_product_id') . ')'
                )
                ->join(
                    'LEFT',
                    $db->quoteName('#__virtuemart_product_prices', 'e')
                    . ' ON (' . $db->quoteName('a.virtuemart_product_id') .
                    ' = ' . $db->quoteName('e.virtuemart_product_id') . ')'
                )
                ->where($db->quoteName('a.created_by') . '='.$sellerid)
                ->where($db->quoteName('a.published') . '=1')
                ->where(
                    $db->quoteName('b.virtuemart_category_id') . '='.$category_id
                )
                ->group($db->quoteName('a.virtuemart_product_id'))
                ->order($order_by);
            if (strlen($search)) {
                $query -> where('c.product_name LIKE "%'.$search.'%"');
            }
            $db ->setQuery($query);
            
            try{
                $latest_products = $db->loadObjectList();
                if (count($latest_products)) {
                    for ($i=0;$i<count($latest_products);$i++) {
                        $media= $mediaModel->getFiles(
                            "",
                            "",
                            $latest_products[$i]->virtuemart_product_id
                        );
                        $latest_products[$i]->file_url=isset(
                            $media[0]
                        )?$media[0]->file_url:$vmconfig->get(
                            "assets_general_path"
                        )."images/vmgeneral/".$vmconfig->get("no_image_set");
                    }
                }
            }catch(Exception $e){
                $latest_products=null;
            }
            return $latest_products;
        }
    }
}
