<?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 . DIRECTORY_SEPARATOR . 'components' .
    DIRECTORY_SEPARATOR . 'com_virtuemart'.DIRECTORY_SEPARATOR.
    'helpers'.DIRECTORY_SEPARATOR.'config.php';
}
VmConfig::loadConfig();

if (!class_exists('MarketplaceControllerProducts')) {
    /**
     * MarketplaceControllerAdminproducts 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 MarketplaceControllerAdminproducts extends JControllerAdmin
    {
        public $jinput;
        /**
         * __construct function
         */
        public function __construct()
        {
            $this->jinput=JFactory::getApplication()->input;
            parent::__construct();
        }
        /**
         * AssignProductToSeller function
         *
         * @return void
         */
        public function assignProductToSeller()
        { 
            $value = $this->jinput->get('jform', 'post', 'array');
            $cid = $this->jinput->get('product_id', 'post', 'string');
            $cids = explode(',', $cid);
            $sellerid = $value['seller'];
            $option = $value['selectoption'];
            $model = $this->getModel('adminproducts');
            $vmmodel = VmModel::getModel('product');
            $count = 0;
            if ($option =='clone') {
                foreach ($cids as $cid1) {
                    $newid=$model->createClone($cid1);
                    if (isset($newid)) {
                        if ($model->copyProduct($newid, $sellerid)) {
                            $count++;
                        }
                    }
                }
                /*
                This style is added due to notices from VM
                on Load product Vmtable line : 1092
                */
                JFactory::getDocument()->addStyleDeclaration(
                    "body {
                            height:0 !important;
                            color: white;
                        }"
                );
                JFactory::getDocument()->addScriptDeclaration(
                    "window.parent.location.href='index.php?option=com_marketpla".
                    "ce&view=adminproducts&wk_redirect=1&cloned=".$count."';"
                );
            } elseif ($option=='move') {
                $count=$model->moveProduct($cids, $sellerid);
                if ($count) {
                    JFactory::getDocument()->addScriptDeclaration(
                        "window.parent.location.href='index.php?option=com_mar".
                        "ketplace&view=adminproducts&wk_redirect=1&moved="
                        .$count."';"
                    );
                }
            }
        }
        /**
         * Adminpublished function
         *
         * @return void
         */
        function adminpublished()
        {
            $_input = JFactory::getApplication()->input;
            
            $data = $_input->getArray(array('cid' =>'array'));
            $cid = $data['cid'];
            
            if (!is_array($cid) || count($cid) < 1) {
                JError::raiseError(500, JText::_('SELECT_PUBLISH'));
            }
            $model = $this->getModel('adminproducts');
            if (!$model->adminpublished($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=adminproducts',
                $msg
            );
        }
        /**
         * GetProductInfo function
         *
         * @return void
         */
        public function getProductInfo()
        {
            $jinput=JFactory::getApplication()->input;
            $cid =$jinput->get('cid', array(0), 'post', 'array');
            $this->setRedirect(
                'index.php?option=com_marketplace&view=adminp'.
                'roducts&wk_set=webkul&wk_pid='
                .urlencode(serialize($cid))
            );
        }
        /**
         * Cancel function
         *
         * @return void
         */
        public function cancel()
        {
            echo "<script> window.history.go(-1); </script>\n";
        }
        /**
         * Adminunpublished function
         *
         * @return void
         */
        function adminunpublished()
        {
            $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('adminproducts');
            if (!$model->adminunpublished($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=adminproducts',
                $msg
            );
        }
        /**
         * Deleteproduct function
         *
         * @return void
         */
        public function deleteproduct()
        {
            $jinput=JFactory::getApplication()->input;
            $ids= $jinput->get('cid', array(0), 'post', 'array');
            $virtuemart_product_model=VmModel::getModel(
                'product',
                'VirtueMartModel'
            );
            if ($virtuemart_product_model->remove($ids)) {
                $this->setRedirect(
                    'index.php?option=com_marketplace&view=adminproducts',
                    count($ids)." ".JText::_('MARKETPLACE_ADMINPRODUCTS_DELETED')
                );
            } else {
                $this->setRedirect(
                    'index.php?option=com_marketplace&view=adminproducts'
                );
            }
        }
    }
}
