<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
//$Id: AttributeController.php, v 1.2 2012/12/24 17:50:11
//
class ProductoptionController extends Controller {
    //Lists Attributes
    public function indexAction() {
        $ObjCustom       = new CustomOptionModel();
        $languageIDs     = $this->ObjView->languageIDs;
        $sortkey         = '';
        $sortkey1        = '';
        $sortkey2        = '';
        $attributeParams = array();
        $enablePriority  = 0;
        $link            = 'productoption/index/';
        //Search
        if (isset($_POST['submitSearch'])) {
            if ($_POST['customOptionName'] != '') {
                $link .= 'name/' . $this->encodeUrlString($_POST['customOptionName']) . '/';
            }
            if ($_POST['status'] != '') {
                $link .= 'status/' . $this->encodeUrlString($_POST['status']) . '/';
            }
            $this->setRedirect($link);
        }
        // bulk status change			
        if (isset($_POST['changeStatus']) && $_POST['changeStatus'] != '') {
            $statusUpdate = $ObjCustom->updateOptionStatus($_POST);
            if ($_POST['changeStatus'] == "D") {
                $this->setMessage("language.msgCustomOptionDeleted", 3);
            } else {
                $this->setMessage("language.msgCustomOptionStatus", 3);
            }
            $this->setRedirect($this->ObjCommon->selfURL(), "No");
            exit;
        }
        $attributeParams['customOptionName'] = $this->escape($this->decodeUrlString($this->getRequestValue('name')));
        $attributeParams['status']           = $this->escape($this->decodeUrlString($this->getRequestValue('status')));
        $sortby                              = $this->escape($this->decodeUrlString($this->getRequestValue('sortby')));
        $orderby                             = $this->escape($this->decodeUrlString($this->getRequestValue('orderby')));
        $attributeParams['sortBy']           = $sortby;
        $attributeParams['orderBy']          = $orderby;
        //Attribute Code Sorting
        if (($sortby == "customOptionName") && ($orderby == "asc")) {
            $sortkey = "desc";
        }
        if (($sortby == "customOptionName") && ($orderby == "desc")) {
            $sortkey = "asc";
        }
        //Set Link	
        $link .= (($attributeParams['customOptionName'] != "") ? "name/" . $attributeParams['customOptionName'] . "/" : "");
        $link .= (($attributeParams['status'] != "") ? "status/" . $attributeParams['status'] . "/" : "");
        $languageIDs['Current']      = $languageIDs['Content'];
        $attributeParams['isGlobal'] = '1';
        $resListCustomOptions        = $ObjCustom->listCustomOptions($languageIDs, $attributeParams);
        $resAtrributeInputTypes      = $ObjCustom->listAtrributeInputTypes($this->ObjView->_languageXML, 'Yes');
        $resAttributeGroup           = isset($resAttributeGroup) ? $resAttributeGroup : '';
        $this->ObjView->assign('sortkey', $sortkey);
        $this->ObjView->assign('sortkey1', $sortkey1);
        $this->ObjView->assign('sortkey2', $sortkey2);
        $this->ObjView->assign("link", $link);
        $this->ObjView->assign('resAtrributeInputTypes', $resAtrributeInputTypes);
        $this->ObjView->assign('resAttributeGroup', $resAttributeGroup);
        $this->ObjView->assign('resListCustomOptions', $resListCustomOptions);
        $this->ObjView->assign('customOptionName', $attributeParams['customOptionName']);
        if ($attributeParams['status'] == 'i')
            $attributeParams['status'] = 'I';
        elseif ($attributeParams['status'] == 'a')
            $attributeParams['status'] = 'A';
        $this->ObjView->assign('status', $attributeParams['status']);
        $deleteprivilage = $this->ObjCommon->hasPrivilege('productoption/delete/');
        $editprivilage   = $this->ObjCommon->hasPrivilege('productoption/edit/');
        $this->ObjView->assign("deleteprivilage", $deleteprivilage);
        $this->ObjView->assign("editprivilage", $editprivilage);
        $this->ObjView->display('ProductOptionList.tpl');
    }
    //Add CustomOption
    public function newAction($productID = '') {
        $ObjCustom        = new CustomOptionModel();
        $languageIDs      = $this->ObjView->languageIDs;
        $ObjProduct       = new ProductModel();
        $productRequestID = $this->escape($this->getRequestValue("productRequestID"));
        $validationErrors = 1;
        if (isset($_POST['submitAttributeAdd']) && $this->ObjForm->isValid('formAttributeAdd', 'submitAttributeAdd')) {
            $validationErrors = 0;
        } elseif (isset($_POST['submitAttributeAddNew']) && $this->ObjForm->isValid('formAttributeAdd', 'submitAttributeAddNew')) {
            $validationErrors = 0;
        }
        if ($validationErrors == 0) {
            
            if ($productID > 0 || $productRequestID > 0) {
               
                $_POST['productID']        = $productID;
                $_POST['productRequestID'] = $productRequestID;
                $customOptionID            = $ObjCustom->saveCustomOptions($languageIDs, $_POST);
                if (isset($_POST['submitAttributeAddNew'])) {
                    $this->setMessage("language.msgOptionAdd", 3);
                    $this->setRedirect('productoption/ajaxproductoptionadd/productID/' . $productID . '/');
                } else {
                    echo '<script type="text/javascript">parent.$.colorbox.close();parent.reloadCustomOptions(1);</script>';
                }
                exit;
            } else {
                
                $customOptionID = $ObjCustom->saveGlobalOption($languageIDs, $_POST);
                if ($customOptionID) {
                    $this->setMessage("language.msgOptionAdd", 3);
                    if (isset($_POST['submitAttributeAddNew'])) {
                        $this->setRedirect('productoption/new/');
                    } else {
                        $this->setRedirect('productoption/index/');
                        exit;
                    }
                    exit;
                }
            }
        } elseif (isset($_POST['attrOptionAddCount'])) {
            $resAttributeEdit['inputType'] = $_POST['inputType'];
            $this->ObjView->assign('resAttributeOptions', $_POST['attrOptionsGroup']);
            $this->ObjView->assign('resAttributeEdit', $resAttributeEdit);
            $this->ObjView->assign('postCallback', '1');
        }
        $resAtrributeInputTypes      = $ObjCustom->listAtrributeInputTypes($this->ObjView->_languageXML);
        $resAtrributeValidationRules = $ObjCustom->listAtrributeValidationRules($this->ObjView->_languageXML);
        if (($productID > 0 || $productRequestID > 0)) {
            $this->ObjView->assign('enableCustomOption', '1');
        } else {
            $this->ObjView->assign('enableCustomOption', '0');
        }
        $resAttributeGroup = isset($resAttributeGroup) ? $resAttributeGroup : '';
        $this->ObjView->assign('productID', $productID);
        $this->ObjView->assign('productRequestID', $productRequestID);
        $this->ObjView->assign('resAtrributeValidationRules', $resAtrributeValidationRules);
        $this->ObjView->assign('resAtrributeInputTypes', $resAtrributeInputTypes);
        $this->ObjView->assign('resAttributeGroup', $resAttributeGroup);
        $this->ObjView->assign('FILE_UPLOAD_LIMIT', $this->getFileUploadLimit());
        $this->ObjView->assign('VALID_IMAGE_FILES', VALID_IMAGE_FILES);
        $this->ObjView->assign('currentAction', "new");
        $this->ObjView->display('CustomOptionAdd.tpl');
    }
    //Edit Attribute
    public function editAction($productID = '') {
        $ObjCustom              = new CustomOptionModel();
        $ObjProduct             = new ProductModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $customOptionID         = (int) $this->getRequestValue('id');
        $productRequestID       = $this->escape($this->getRequestValue("productRequestID"));
        if ($this->ObjForm->isValid('formAttributeAdd', 'submitAttributeAdd') && ($customOptionID > 0)) {
            $_POST['customOptionID']   = $customOptionID;
            $_POST['productID']        = $productID;
            $_POST['productRequestID'] = $productRequestID;
            
            $customOptionID            = $ObjCustom->saveGlobalOption($languageIDs, $_POST);
            if ($customOptionID) {
                if ($productID > 0 || $productRequestID > 0) {
                    echo '<script type="text/javascript">parent.$.colorbox.close();parent.reloadCustomOptions(2);</script>';
                } else {
                    $this->setMessage("language.msgCustomOptionUpdate", 3);
                    $this->setRedirect('productoption/');
                }
                exit;
            }
        }
        if ($customOptionID > 0) {
            $paramArray['customOptionID'] = $customOptionID;
            $resAttributeEdit             = $ObjCustom->listCustomOptions($languageIDs, $paramArray);
            $resAttributeEdit             = $resAttributeEdit[0];
            if (empty($resAttributeEdit)) {
                $this->setRedirect('productoption/');
                exit;
            }
            $resAttributeOptions = $ObjCustom->listOptionVariant($languageIDs, $paramArray);
            $this->ObjView->assign('resAttributeOptions', $resAttributeOptions);
            $this->ObjView->assign('resAttributeEdit', $resAttributeEdit);
            $this->ObjView->assign('customOptionID', $customOptionID);
            $this->ObjView->assign('enableCustomOption', '0');
            if ($productID > 0 || $productRequestID > 0) {
                $this->ObjView->assign('enableCustomOption', '1');
                $this->ObjView->assign('productID', $productID);
                $this->ObjView->assign('productRequestID', $productRequestID);
            }
        } else {
            $this->setRedirect('productoption/');
            exit;
        }
        if (isset($_POST['attrOptionAddCount']) && ($_POST['attrOptionAddCount'] > 0)) {
            $resAttributeEdit['inputType'] = $_POST['inputType'];
            $this->ObjView->assign('resAttributeOptions', $_POST['attrOptionsGroup']);
            $this->ObjView->assign('resAttributeEdit', $resAttributeEdit);
            $this->ObjView->assign('postCallback', '1');
        }
        $resAtrributeInputTypes      = $ObjCustom->listAtrributeInputTypes($this->ObjView->_languageXML);
        $resAtrributeValidationRules = $ObjCustom->listAtrributeValidationRules($this->ObjView->_languageXML);
        $this->ObjView->assign('resAtrributeValidationRules', $resAtrributeValidationRules);
        $this->ObjView->assign('resAtrributeInputTypes', $resAtrributeInputTypes);
        $this->ObjView->assign('resAttributeGroup', isset($resAttributeGroup) ? $resAttributeGroup : '');
        $this->ObjView->assign('FILE_UPLOAD_LIMIT', $this->getFileUploadLimit());
        $this->ObjView->assign('VALID_IMAGE_FILES', VALID_IMAGE_FILES);
        $this->ObjView->display('CustomOptionAdd.tpl');
    }
    //load Attribute Option tpl
    public function ajaxLoadOptionsVariantTplAction() {
        $attrOptionCount = $this->escape($_POST["attrOptionCount"]);
        $inputType       = $this->escape($_POST["inputType"]);
        $optionType      = $this->escape($_POST["optionType"]);
        $this->ObjView->assign('inputType', $inputType);
        $this->ObjView->assign('enableCustomOption', $optionType);
        $this->ObjView->assign('attrOptionCount', $attrOptionCount + 1);
        $this->ObjView->display('CustomOptionAddVariantAjax.tpl');
    }
    //Attribute Status Change
    public function AjaxChangeOptStatusAction() {
        if ($_POST['from'] == "productAdd" || $this->ObjCommon->hasPrivilege('productoption/edit/')) {
            $ObjCustom             = new CustomOptionModel();
            $_POST['id']           = $_POST['customOptionID'];
            $_POST['changeStatus'] = $_POST['status'];
            $statusUpdate          = $ObjCustom->updateOptionStatus($_POST);
            if ($statusUpdate == 1)
                $this->setMessage("language.msgCustomOptionStatus", 3);
            elseif ($statusUpdate == 2)
                $this->setMessage("language.msgInactiveCustomOption", "message_error");
            echo $statusUpdate;
            exit;
        }
        $this->setRedirect('productoption/index/');
        exit;
    }
    //Attribute Delete
    public function AjaxDestroyOptionAction() {
        if ($this->ObjCommon->hasPrivilege('productoption/delete/')) {
            $ObjCustom    = new CustomOptionModel();
            $customOption = $_POST['id'];
            if ($customOption > 0) {
                $attrArray['id']           = $customOption;
                $attrArray['changeStatus'] = 'D';
                if (strtolower($_POST['type']) == 'select') {
                    $comboData  = $ObjCustom->getcombinationData('', '', "global");
                    $vaiantData = $ObjCustom->getvariantData($customOption);
                    if (count(array_intersect($vaiantData, $comboData)) > 0) {
                        $destroyAttribute = "1";
                    } else
                        $destroyAttribute = $ObjCustom->updateOptionStatus($attrArray);
                } else {
                    $destroyAttribute = $ObjCustom->updateOptionStatus($attrArray);
                }
                if ($destroyAttribute === "1") // ===	 to check variable type
                    {
                    $this->setMessage("language.msgdeleteCustomOption", "message_error");
                } else {
                    $this->setMessage("language.msgCustomOptionDeleted", 3);
                }
                exit;
            }
        }
        $this->setRedirect('productoption/index/');
        exit;
    }
    public function AjaxDestroyProVariantAction() {
        $ObjCustom = new CustomOptionModel();
        $variantID = $_POST['variantID'];
        $section   = $_POST['section'];
        $comboData = $ObjCustom->getcombinationData($_POST['productID'], $_POST['productRequestID'], $section);
        if (in_array($variantID, $comboData))
            echo "3";
        else
            echo "1";
        exit;
    }
    //Custom Attribute Delete
    public function AjaxDestroyProOptionAction() {
        $ObjCustom                 = new CustomOptionModel();
        $_POST['id']               = $_POST['customOptionID'];
        $_POST['productID']        = $_POST['productID'];
        $_POST['productRequestID'] = (!empty($_POST['productRequestID'])) ? $_POST['productRequestID'] : '';
        $_POST['isGlobal']         = $_POST['isGlobal'];
        if ($_POST['type'] == 'select') {
            $comboData  = $ObjCustom->getcombinationData($_POST['productID']);
            $vaiantData = $ObjCustom->getvariantData($_POST['id']);
            if (count(array_intersect($vaiantData, $comboData)) > 0) {
                echo "3";
                exit;
            } else
                $statusUpdate = $ObjCustom->updateProOptionStatus($_POST);
        } else
            $statusUpdate = $ObjCustom->updateProOptionStatus($_POST);
    }
    public function showAction() {
        $ObjCustom              = new CustomOptionModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $customOptionID         = (int) $this->getRequestValue('id');
        $productID              = $this->escape($this->getRequestValue("productID"));
        $productRequestID       = $this->escape($this->getRequestValue("productRequestID"));
        if ($customOptionID > 0) {
            $paramArray['customOptionID'] = $customOptionID;
            $resAttributeEdit             = $ObjCustom->listCustomOptions($languageIDs, $paramArray);
            $resAttributeEdit             = $resAttributeEdit[0];
            if (empty($resAttributeEdit)) {
                $this->setRedirect('productoption/');
                exit;
            }
            $resAttributeOptions = $ObjCustom->listOptionVariant($languageIDs, $paramArray);
            $this->ObjView->assign('resAttributeOptions', $resAttributeOptions);
            $this->ObjView->assign('resAttributeEdit', $resAttributeEdit);
            $this->ObjView->assign('customOptionID', $customOptionID);
            $this->ObjView->assign('enableCustomOption', '0');
            if ($productID > 0 || $productRequestID > 0) {
                $this->ObjView->assign('enableCustomOption', '1');
                $this->ObjView->assign('productID', $productID);
                $this->ObjView->assign('productRequestID', $productRequestID);
            }
            if ($resAttributeEdit['isGlobal'] == 1) {
                $this->ObjView->assign('hideStatus', 1);
            }
        } else {
            $this->setRedirect('productoption/');
            exit;
        }
        if ($_POST['attrOptionAddCount'] > 0) {
            $resAttributeEdit['inputType'] = $_POST['inputType'];
            $this->ObjView->assign('resAttributeOptions', $_POST['attrOptionsGroup']);
            $this->ObjView->assign('resAttributeEdit', $resAttributeEdit);
            $this->ObjView->assign('postCallback', '1');
        }
        $resAtrributeInputTypes = $ObjCustom->listAtrributeInputTypes($this->ObjView->_languageXML);
        $this->ObjView->assign('resAtrributeValidationRules', $resAtrributeValidationRules);
        $this->ObjView->assign('resAtrributeInputTypes', $resAtrributeInputTypes);
        $this->ObjView->assign('resAttributeGroup', $resAttributeGroup);
        $this->ObjView->display('CustomOptionDetails.tpl');
    }
    //product option add
    public function ajaxProductOptionAddAction() {
        $productID = $this->escape($this->getRequestValue("productID"));
        $this->newAction($productID);
    }
    //product option edit
    public function ajaxProductOptionEditAction() {
        $productID = $this->escape($this->getRequestValue("productID"));
        $this->editAction($productID);
    }
}