<?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.form.form');
jimport('joomla.application.component.controller');
JHTML::_('behavior.modal');
jimport('joomla.filesystem.folder');
if (!class_exists('MarketplaceControllerSuggestedcategory')) {
    /**
     * MarketplaceControllerSuggestedcategory 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 MarketplaceControllerSuggestedcategory extends JControllerAdmin
    {
        /**
         * __construct function
         */
        function __construct()
        {
            parent::__construct();
        }
        /**
         * Approve function
         *
         * @return void
         */
        function approve()
        {
            $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('suggestedcategory');
            if (!$model->approve($cid)) {
                echo "<script> alert('".$model->getError().
                "'); window.history.go(-1); </script>\n";
            }
            $total = count($cid);
            $msg = $total.' '.JText::_('CATEGORY_ADDED');
            $this->setRedirect(
                'index.php?option=com_marketplace&view=suggestedcategory',
                $msg
            );
        }
        /**
         * Reject function
         *
         * @return void
         */
        function reject()
        {
            $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('suggestedcategory');
            if (!$model->reject($cid)) {
                echo "<script> alert('".$model->getError().
                "'); window.history.go(-1); </script>\n";
            }
            $total = count($cid);
            $msg   = $total.' '.JText::_('CATEGORY_REMOVE');
            $this->setRedirect(
                'index.php?option=com_marketplace&view=suggestedcategory',
                $msg
            );
        }
        /**
         * CreateCategory function
         *
         * @return void
         */
        function createCategory()
        {
            $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'));
            }
            $parent_category= $jinput->get(
                'product_parent_id',
                array(),
                'post',
                'array'
            );
        
            if (!count($parent_category)) {
                $parent_category=$jinput->get('selected_cat');
            }
            VmConfig::loadConfig();
            $i=0;
            foreach ($cid as $key => $value) {
                $model=$this->getModel('suggestedcategory');
                $records=$model->getCategoryToCreate($value);
                $data = array(
                    "category_name" =>$records[0]->category_name,
                    "published" => "1",
                    "category_description" =>$records[0]->category_desc,
                    'ordering' => '0',
                    'category_parent_id' => $parent_category[$key]
                );
                $catmodel=VmModel::getModel('category');
                $table = $catmodel->getTable('categories');
                if (!array_key_exists('category_template', $data)) {
                    $data['category_template']
                        = $data['category_layout']
                            = $data['category_product_layout'] = 0;
                }
                if (VmConfig::get('categorytemplate')==$data['category_template']) {
                    $data['category_template'] = 0;
                }

                if (VmConfig::get('categorylayout') == $data['category_layout']) {
                    $data['category_layout'] = 0;
                }
                $pro_layout = VmConfig::get('productlayout');
                if ($pro_layout==$data['category_product_layout']) {
                    $data['category_product_layout'] = 0;
                }

                $table->bindChecknStore($data);

                if (!empty($data['virtuemart_category_id'])) {
                    $xdata['category_child_id']
                        = (int)$data['virtuemart_category_id'];
                    $xdata['category_parent_id']= empty($data['category_parent_id'])
                        ? 0:(int)$data['category_parent_id'];
                    $xdata['ordering']
                        = empty($data['ordering'])? 0: (int)$data['ordering'];
                    $table = $catmodel->getTable('category_categories');
                    $table->bindChecknStore($xdata);
                }

                $mediaModel = VmModel::getModel('Media');
                $file_id = $mediaModel->storeMedia($data, 'category');
                $catmodel->clearCategoryRelatedCaches();
                $i++;
                $mailer=JFactory::getMailer();
                $config=JFactory::getConfig();
                $sender=$config->get('mailfrom', '');
                $mailer->addRecipient($records[0]->requested_by);
                $mailer->setSender($sender);
                $mailer->isHtml(true);
                $mailer->setSubject(JText::_('HELLO_SELLER_APPROVED'));
                $mailer->setBody(
                    JText::_("YOUR_REQUEST_HAS_APPROVED_ADMIN")." ".
                    $records[0]->category_name.JText::_("HAS_BEEN_APPROVED")
                );
                $mailer->send();
                $model->removeRequest($value);
            }
            $msg=$i.JText::_("CATEGORY_CREATED_SUCCCESS");
            $this->setRedirect(
                'index.php?option=com_marketplace&view=suggestedcategory',
                $msg
            );
        }
        /**
         * DeteteCategory function
         *
         * @return void
         */
        function deteteCategory()
        {
            $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('suggestedcategory');
            $i=0;
            foreach ($cid as $key => $value) {
                $model=$this->getModel('suggestedcategory');
                $records=$model->getCategoryToCreate($value);
                $model->reject($value);
                $i++;
                $mailer=JFactory::getMailer();
                $config=JFactory::getConfig();
                $sender=$config->get('mailfrom', '');
                $mailer->addRecipient($records[0]->requested_by);
                $mailer->setSender($sender);
                $mailer->isHtml(true);
                $mailer->setSubject(
                    JText::_('HELLO_SELLER_REJECTED').$records[0]->category_name
                );
                $mailer->setBody(
                    JText::_("YOUR_REQUEST_HAS_REJECTED_ADMIN")." ".
                    $records[0]->category_name.JText::_("HAS_BEEN_REJECTED")
                );
                $mailer->send();
            }      
            $msg=$i.JText::_("CATEGORY_REJECTED_SUCCCESS");
            $this->setRedirect(
                'index.php?option=com_marketplace&view=suggestedcategory',
                $msg
            );
        }
        /**
         * Mailtoseller function
         *
         * @return void
         */
        public function mailtoseller()
        {
            $jinput=JFactory::getApplication()->input;
            $cid = $jinput->get('cid', array(0), 'post', 'array');
            $model = $this->getModel('suggestedcategory');
            $emails=$model->getEmail($cid);
            $jinput->set('view', 'suggestedcategory');
            $jinput->set('layout', 'sendmail');
            $this->setRedirect(
                'index.php?option=com_marketplace&view=suggestedcategory&w'.
                    'k_set=webkul&emails='.urlencode(serialize($emails))
            );
        }
        /**
         * Sendmail function
         *
         * @return void
         */
        public function sendmail()
        {
            $jinput=JFactory::getApplication()->input;
            $postvar = $jinput->get('jform', array(), 'array');
            
            $body=$postvar['description'];
            $subject=$postvar['subject'];
            $email=$postvar['reciepient'];
            $emails=explode(',', $email);
            
            foreach ($emails as $mail) {
                $mailer=JFactory::getMailer();
                $config=JFactory::getConfig();
                $adminadd=$config->get('mailfrom');
                $mailer->setSender($adminadd);
                $mailer->addRecipient($mail);
                $mailer->setSubject($subject);
                $mailer->setBody($body);
                $mailer->isHTML(true);
                $mailer->send();
            }
            JFactory::getDocument()->addScriptDeclaration(
                "window.parent.location.href='index.php?option=com_marke".
                    "tplace&view=suggestedcategory&wk_redirect=1';"
            );
        }
    }
}
