<?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.controller');
if (!class_exists('VmConfig')) {
    include JPATH_ADMINISTRATOR .'/components/com_virtuemart/helpers/config.php';
}
VmConfig::loadConfig();
/**
 * MarketplaceControllerProducts 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 MarketplaceControllerProducts extends JControllerAdmin
{
    /**
     * __construct function
     */
    function __construct()
    {
        parent::__construct();
    }
    /**
     * GetModel function
     *
     * @param string $name   var
     * @param string $prefix var
     * @param array  $config var 
     * 
     * @return void
     */
    public function getModel(
        $name = 'Products',
        $prefix = 'MarketplaceModel',
        $config = array('ignore_request' => true)
    ) {
        $model = parent::getModel($name, $prefix, $config);
        return $model;
    }
    /**
     * Publish function
     *
     * @return void
     */
    function publish()
    {
        $jinput=JFactory::getApplication()->input;
        $cid = $jinput->get('cid', array(0), 'post', 'array');
        
        if (!is_array($cid) || count($cid) < 1) {
             JError::raiseError(500, JText::_('SELECT_PUBLISH'));
        }
        $model = $this->getModel('products');
        if (!$model->publish($cid)) {
            echo "<script> alert('".$model->getError().
                "'); window.history.go(-1); </script>\n";
        }
        $total = count($cid);
        $msg  = $total.' '.JText::_('ITEM_PUBLISHED');
        $this->setRedirect('index.php?option=com_marketplace&view=products', $msg);
    }
    /**
     * Unpublish function
     *
     * @return void
     */
    function unpublish()
    {
        $jinput=JFactory::getApplication()->input;
        $cid = $jinput->get('cid', array(0), 'post', 'array');
        if (!is_array($cid) || count($cid) < 1) {
            JError::raiseError(500, JText::_('SELECT_UNPUBLISH'));
        }  
        $model = $this->getModel('products');
        if (!$model->unpublish($cid)) {
            echo "<script> alert('".$model->getError().
                "'); window.history.go(-1); </script>\n";
        }  $total = count($cid);
        $msg  = $total.' '.JText::_('ITEM_UNPUBLISHED');
        $this->setRedirect('index.php?option=com_marketplace&view=products', $msg);
    } 
    /**
     * Edit function
     *
     * @return void
     */
    function edit()
    {
        $jinput=JFactory::getApplication()->input;
        $cid = $jinput->get('cid', array(0), 'post', 'array');
        if (!is_array($cid) || count($cid) < 1) {
            JError::raiseError(500, JText::_('SELECT_UNPUBLISH'));
        }  
        $this->setRedirect(
            'index.php?option=com_virtuemart&view=product&task=edit&v'.
                'irtuemart_product_id='.$cid[0]
        );
    }
    /**
     * Remove function
     *
     * @return void
     */
    function remove()
    {
        global $option;
        $jinput=JFactory::getApplication()->input;
        $cid = $jinput->get('cid', array(0), 'post', 'array');
        if (!is_array($cid) || count($cid) < 1) {
            JError::raiseError(500, JText::_('SELECT_DELETE'));
        }  
        $model = $this->getModel('products');
        $msg = $model->delete($cid);
        $cache = JFactory::getCache('com_marketplace');
        $cache->clean();
        $this->setRedirect(
            'index.php?option=com_marketplace&view=products',
            $msg
        );
    }
    /**
     * Deleteproduct function
     *
     * @return void
     */
    function deleteproduct()
    {
        $jinput=JFactory::getApplication()->input;
        $cid = $jinput->get('cid', array(0), 'post', 'array');
        $model=$this->getModel('products');
        $virtuemart_product_model=VmModel::getModel('product', 'VirtueMartModel');
        if ($virtuemart_product_model->remove($cid)) {
            if ($model->deleteproducts($cid)) {
                $this->setRedirect(
                    'index.php?option=com_marketplace&view=products',
                    JText::_('DELETED_SUCCESSFULLY')
                );
            }
        }
    }
    /**
     * Published function
     *
     * @return void
     */
    function published()
    {
        $jinput=JFactory::getApplication()->input;
        $cid = $jinput->get('cid', array(0), 'post', 'array');
        $model=$this->getModel('products');
        if ($model->published($cid)) {
            $this->setRedirect(
                'index.php?option=com_marketplace&view=products',
                JText::_("SELECTED_PUBLISHED")
            );
        }
    }
    /**
     * Unpublished function
     *
     * @return void
     */
    function unpublished()
    {
        $jinput=JFactory::getApplication()->input;
        $cid = $jinput->get('cid', array(0), 'post', 'array');
        $model=$this->getModel('products');
        if ($model->unpublished($cid)) {
            $this->setRedirect(
                'index.php?option=com_marketplace&view=products',
                JText::_("SELECTED_UNPUBLISHED")
            );
        }
    }
}
