Example #1
0
 public function changeOptionsStatus(Varien_Object $group)
 {
     $optionIds = $this->getResource()->getOptionIds($group->getId());
     if ($optionIds) {
         foreach ($optionIds as $id) {
             $this->getResource()->setOptionsStatus($id, $group->getIsActive());
         }
     }
 }
 public function saveAction()
 {
     $data = $this->getRequest()->getParams();
     $uploader = new Mage_Core_Model_File_Uploader('file');
     $uploader->setAllowedExtensions(array('csv'));
     $uploader->setAllowRenameFiles(true);
     $path = Mage::getBaseDir('var') . DS . 'import';
     if (!file_exists($path)) {
         mkdir($path, 0777);
     }
     try {
         $result = $uploader->save($path);
         $fullPath = $result['path'] . DS . $result['file'];
         $csv = new Varien_File_Csv();
         $data = $csv->getData($fullPath);
         $items = array();
         if (count($data) > 1) {
             for ($i = 1; $i < count($data); $i++) {
                 $item = array();
                 for ($j = 0; $j < count($data[0]); $j++) {
                     if (isset($data[$i][$j]) && trim($data[$i][$j]) != '') {
                         $item[strtolower($data[0][$j])] = $data[$i][$j];
                     }
                 }
                 $items[] = $item;
             }
         }
         $resource = Mage::getSingleton('core/resource');
         $writeConnection = $resource->getConnection('core_write');
         $table = $resource->getTableName('seo/redirect');
         $table2 = $resource->getTableName('seo/redirect_store');
         $i = 0;
         foreach ($items as $item) {
             pr($item);
             if (!isset($item['url_from']) || !isset($item['url_to'])) {
                 continue;
             }
             $item = new Varien_Object($item);
             $query = "REPLACE {$table} SET\n                    url_from = '" . addslashes($item->getUrlFrom()) . "',\n                    url_to = '" . addslashes($item->getUrlTo()) . "',\n                    is_redirect_only_error_page = '" . addslashes($item->getIsRedirectOnlyErrorPage()) . "',\n                    comments = '" . addslashes($item->getComments()) . "',\n                    is_active = '" . addslashes($item->getIsActive()) . "';\n                    REPLACE {$table2} SET\n                        store_id = 0,\n                        redirect_id = LAST_INSERT_ID();\n                     ";
             $writeConnection->query($query);
             $i++;
         }
         Mage::getSingleton('adminhtml/session')->addSuccess('' . $i . ' records were inserted or updated');
     } catch (Exception $e) {
         Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
     }
     $this->_redirect('*/*/');
 }
 public function saveAction()
 {
     $data = $this->getRequest()->getParams();
     $uploader = new Mage_Core_Model_File_Uploader('file');
     $uploader->setAllowedExtensions(array('csv'));
     $uploader->setAllowRenameFiles(true);
     $path = Mage::getBaseDir('var') . DS . 'import';
     if (!file_exists($path)) {
         mkdir($path, 0777);
     }
     try {
         $result = $uploader->save($path);
         $fullPath = $result['path'] . DS . $result['file'];
         $csv = new Varien_File_Csv();
         $data = $csv->getData($fullPath);
         $items = array();
         if (count($data) > 1) {
             for ($i = 1; $i < count($data); $i++) {
                 $item = array();
                 for ($j = 0; $j < count($data[0]); $j++) {
                     if (isset($data[$i][$j]) && trim($data[$i][$j]) != '') {
                         $item[strtolower($data[0][$j])] = $data[$i][$j];
                     }
                 }
                 $items[] = $item;
             }
         }
         $resource = Mage::getSingleton('core/resource');
         $writeConnection = $resource->getConnection('core_write');
         $table = $resource->getTableName('seoautolink/link');
         $table2 = $resource->getTableName('seoautolink/link_store');
         $i = 0;
         foreach ($items as $item) {
             if (!isset($item['keyword'])) {
                 continue;
             }
             $item = new Varien_Object($item);
             $query = "REPLACE {$table} SET\n                    keyword = '" . addslashes($item->getKeyword()) . "',\n                    url = '" . addslashes($item->getUrl()) . "',\n                    url_title = '" . addslashes($item->getUrlTitle()) . "',\n                    url_target = '" . addslashes($item->getUrlTarget()) . "',\n                    is_nofollow = '" . (int) $item->getIsNofollow() . "',\n                    max_replacements = '" . (int) $item->getMaxReplacements() . "',\n                    sort_order = '" . (int) $item->getSortOrder() . "',\n                    occurence = '" . (int) $item->getOccurence() . "',\n                    is_active = '" . (int) $item->getIsActive() . "',\n                    created_at = '" . now() . "',\n                    updated_at = '" . now() . "';\n                    REPLACE {$table2} SET\n                        store_id = '" . (int) $item->getStoreId() . "',\n                        link_id = LAST_INSERT_ID();\n                     ";
             $writeConnection->query($query);
             $i++;
         }
         Mage::getSingleton('adminhtml/session')->addSuccess('' . $i . ' records were inserted or updated');
     } catch (Exception $e) {
         Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
     }
     $this->_redirect('*/*/');
 }
Example #4
0
 public function render(Varien_Object $row)
 {
     $status = $row->getStatus();
     $isActive = $row->getIsActive();
     $label = __('Disabled');
     $class = 'grid-severity-major';
     if ($isActive) {
         if ($status == 1) {
             $class = 'grid-severity-notice';
             $label = __('Ready');
         } else {
             $class = 'grid-severity-critical';
             $label = __('Reindex Required');
         }
     }
     return $formatString = "<span class='{$class}'><span>{$label}</span></span>";
 }
Example #5
0
 public function render(Varien_Object $feed)
 {
     $html = '';
     $state = $feed->getGenerator()->getState();
     if ($feed->getIsActive()) {
         if ($state->isReady()) {
             if ($feed->getUrl()) {
                 $html = $this->getStatusHtml('notice', 'Ready');
             } else {
                 $html = $this->getStatusHtml('critical', 'Not generated');
             }
         } elseif ($state->isError()) {
             $html = $this->getStatusHtml('critical', 'Error');
         } elseif ($state->isProcessing()) {
             $html = $this->getStatusHtml('major', 'Processing', $state->toHtml());
         }
     } else {
         $html = $this->getStatusHtml('minor', 'Disabled');
     }
     return $html;
 }
Example #6
0
 protected function _prepareForm()
 {
     $_form = new Varien_Data_Form();
     $this->setForm($_form);
     $_data = Mage::getSingleton('adminhtml/session')->getData(AW_Featured_Helper_Data::FORM_DATA_KEY);
     if (!is_object($_data)) {
         $_data = new Varien_Object($_data);
     }
     $_fieldset = $_form->addFieldset('block_fieldset', array('legend' => $this->__('General')));
     $_fieldset->addField('block_name', 'text', array('name' => 'block_name', 'label' => $this->__('Name'), 'required' => TRUE));
     $_fieldset->addField('block_id', 'text', array('name' => 'block_id', 'label' => $this->__('Block ID'), 'required' => TRUE));
     if (is_null($_data->getIsActive())) {
         $_data->setIsActive(TRUE);
     }
     $_fieldset->addField('is_active', 'select', array('name' => 'is_active', 'label' => $this->__('Status'), 'required' => TRUE, 'values' => Mage::getModel('awfeatured/source_status')->toOptionArray()));
     if (!Mage::app()->isSingleStoreMode()) {
         $_fieldset->addField('store', 'multiselect', array('name' => 'store[]', 'label' => $this->__('Store View'), 'required' => TRUE, 'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(FALSE, TRUE)));
     } else {
         if ($_data->getStore() && is_array($_data->getStore())) {
             $_stores = $_data->getStore();
             if (isset($_stores[0]) && $_stores[0] != '') {
                 $_stores = $_stores[0];
             } else {
                 $_stores = 0;
             }
             $_data->setStore($_stores);
         }
         $_fieldset->addField('store', 'hidden', array('name' => 'store[]'));
     }
     if (!$_data->getStore()) {
         $_data->setStore(0);
     }
     $_fieldset->addField('type', 'select', array('name' => 'type', 'label' => $this->__('Representation'), 'values' => Mage::getModel('awfeatured/source_representation')->toOptionArray()));
     $_fieldset->addField('autoposition', 'select', array('name' => 'autoposition', 'label' => $this->__('Automatic layout position'), 'values' => Mage::getModel('awfeatured/source_autoposition')->toOptionArray()));
     $_fieldset = $_form->addFieldset('types_data_fset', array('legend' => $this->__('Representation settings')));
     $_fieldset->setFieldsetContainerId('awf_types_settings');
     $_fieldset->addField('types_data', 'note', array('text' => $this->__('No representation has been selected')));
     $_form->setValues($_data);
 }
Example #7
0
 public function checkCustomerGroupAccess(Varien_Object $item)
 {
     $access = true;
     if ($item->getIsActive() == self::STATUS_DISABLED) {
         return false;
     }
     $groups = $item->getCustomerGroups();
     if (is_null($groups) && $item->getAllowGuests()) {
         $groups = '0';
     }
     if (!empty($groups) && is_string($groups)) {
         $groups = explode(',', $groups);
     }
     if (empty($groups)) {
         $access = true;
     } else {
         if (is_array($groups) && !in_array(Mage::getSingleton('customer/session')->getCustomerGroupId(), $groups)) {
             $access = false;
         }
     }
     $limit = $item->getDownloadsLimit();
     if ($limit) {
         if ($item->getDownloads() >= $limit) {
             $access = false;
         }
     }
     return $access;
 }
 public function saveProductOptions($newOptions, array $prevOptions, array $productIds, Varien_Object $group, $prevGroupIsActive = 1, $place = 'apo', $prevStoreOptionsData = array())
 {
     if (isset($productIds) && is_array($productIds) && count($productIds) > 0) {
         $relation = Mage::getResourceSingleton('customoptions/relation');
         $templateId = $group->getId();
         $groupIsActive = $group->getIsActive();
         $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
         $tablePrefix = (string) Mage::getConfig()->getTablePrefix();
         $condition = '';
         if ($place == 'product') {
             $condition = ' AND product_id IN (' . implode(',', $productIds) . ')';
         }
         // get and prepare $optionRelations
         $select = $connection->select()->from($tablePrefix . 'custom_options_relation')->where('group_id = ' . $templateId . $condition);
         $optionRelations = $connection->fetchAll($select);
         if (is_array($optionRelations) && count($optionRelations) > 0) {
             $tmp = array();
             foreach ($optionRelations as $option) {
                 $tmp[$option['product_id']][$option['option_id']] = $option;
             }
             $optionRelations = $tmp;
         } else {
             $optionRelations = array();
         }
         if (isset($newOptions) && is_array($newOptions)) {
             $newOptions = $this->_prepareOptions($newOptions, $templateId, $groupIsActive);
             $prevOptions = $this->_prepareOptions($prevOptions, $templateId, $prevGroupIsActive);
             // comparison arrays
             $diffOptions = $this->comparisonArrays($newOptions, $prevOptions);
             // get all store options
             $select = $connection->select()->from($tablePrefix . 'custom_options_group_store')->where('group_id = ' . $templateId);
             $allStoreOptions = $connection->fetchAll($select);
             foreach ($allStoreOptions as $key => $storeOptions) {
                 if ($storeOptions['hash_options']) {
                     $hashOptions = unserialize($storeOptions['hash_options']);
                 } else {
                     $hashOptions = array();
                 }
                 if (isset($prevStoreOptionsData['store_id']) && $storeOptions['store_id'] == $prevStoreOptionsData['store_id']) {
                     if ($prevStoreOptionsData['hash_options']) {
                         $prevHashOptions = unserialize($prevStoreOptionsData['hash_options']);
                     } else {
                         $prevHashOptions = array();
                     }
                     // link to reset no deault!!
                     foreach ($prevHashOptions as $optionId => $option) {
                         // add to check remove store option
                         if (!isset($hashOptions[$optionId])) {
                             $hashOptions[$optionId] = array('option_id' => $option['option_id'], 'type' => $option['type']);
                         }
                         if (isset($option['values'])) {
                             foreach ($option['values'] as $valueId => $value) {
                                 // add to check remove store option value
                                 if (!isset($hashOptions[$optionId]['values'][$valueId])) {
                                     $hashOptions[$optionId]['values'][$valueId]['option_type_id'] = $value['option_type_id'];
                                 } else {
                                     // add prev specials data
                                     if (isset($value['specials'])) {
                                         $hashOptions[$optionId]['values'][$valueId]['prev_specials'] = $value['specials'];
                                     }
                                     // add prev tiers data
                                     if (isset($value['tiers'])) {
                                         $hashOptions[$optionId]['values'][$valueId]['prev_tiers'] = $value['tiers'];
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $allStoreOptions[$key]['hash_options'] = $hashOptions;
             }
             //                print_r($newOptions);
             //                print_r($prevOptions);
             //                print_r($diffOptions);
             //                exit;
             foreach ($productIds as $productId) {
                 $realOptionIds = array();
                 $options = $newOptions;
                 // work copy options
                 // $optionRelations
                 // update options
                 if (isset($optionRelations[$productId])) {
                     foreach ($optionRelations[$productId] as $optionId => $prevOption) {
                         //$optionId = $prevOption['option_id'];
                         $prevOption = Mage::getModel('catalog/product_option')->load($optionId);
                         if (isset($options['IGI' . $prevOption->getInGroupId()]) && (!isset($options['IGI' . $prevOption->getInGroupId()]['is_delete']) || $options['IGI' . $prevOption->getInGroupId()]['is_delete'] != 1)) {
                             $option = $options['IGI' . $prevOption->getInGroupId()];
                             if (isset($diffOptions['IGI' . $prevOption->getInGroupId()])) {
                                 $diffOption = $diffOptions['IGI' . $prevOption->getInGroupId()];
                             } else {
                                 $diffOption = array();
                             }
                             $this->saveOption($productId, $diffOption, $optionId, 0, $option['type']);
                             $realOptionIds[$option['option_id']]['value'] = $optionId;
                             if ($this->getGroupByType($option['type']) == self::OPTION_GROUP_SELECT) {
                                 $select = $connection->select()->from($tablePrefix . 'catalog_product_option_type_value')->where('option_id = ' . $optionId);
                                 $prevValues = $connection->fetchAll($select);
                                 if (is_array($prevValues) && count($prevValues) > 0) {
                                     foreach ($prevValues as $prValue) {
                                         if (isset($option['values']['IGI' . $prValue['in_group_id']]) && (!isset($option['values']['IGI' . $prValue['in_group_id']]['is_delete']) || $option['values']['IGI' . $prValue['in_group_id']]['is_delete'] != 1)) {
                                             // update option value
                                             if (isset($prevOptions['IGI' . $prevOption->getInGroupId()]['values']['IGI' . $prValue['in_group_id']])) {
                                                 $prevValue = $prevOptions['IGI' . $prevOption->getInGroupId()]['values']['IGI' . $prValue['in_group_id']];
                                             } else {
                                                 $prevValue = array();
                                             }
                                             if (isset($newOptions['IGI' . $prevOption->getInGroupId()]['values']['IGI' . $prValue['in_group_id']])) {
                                                 $newValue = $newOptions['IGI' . $prevOption->getInGroupId()]['values']['IGI' . $prValue['in_group_id']];
                                             } else {
                                                 $newValue = array();
                                             }
                                             if (isset($diffOption['values']['IGI' . $prValue['in_group_id']])) {
                                                 $diffValue = $diffOption['values']['IGI' . $prValue['in_group_id']];
                                             } else {
                                                 $diffValue = array();
                                             }
                                             if (isset($diffValue['customoptions_qty']) && !$group->getUpdateInventory()) {
                                                 unset($diffValue['customoptions_qty']);
                                             }
                                             $this->saveOptionValue($optionId, $diffValue, $prevValue, $newValue, $prValue['option_type_id'], 0);
                                             $realOptionIds[$option['option_id']][$option['values']['IGI' . $prValue['in_group_id']]['option_type_id']] = $prValue['option_type_id'];
                                             unset($option['values']['IGI' . $prValue['in_group_id']]);
                                         } else {
                                             // delete option value
                                             $connection->delete($tablePrefix . 'catalog_product_option_type_value', 'option_type_id = ' . $prValue['option_type_id']);
                                         }
                                     }
                                 }
                                 // insert option values
                                 if (count($option['values']) > 0) {
                                     foreach ($option['values'] as $value) {
                                         if (isset($value['is_delete']) && $value['is_delete'] == 1) {
                                             continue;
                                         }
                                         if ($group->getOnlyUpdate() && !isset($diffOptions['IGI' . $option['in_group_id']]['values']['IGI' . $value['in_group_id']]['in_group_id'])) {
                                             continue;
                                         }
                                         $this->saveOptionValue($optionId, $value, array(), $value, false, 0);
                                     }
                                 }
                             }
                             unset($options['IGI' . $prevOption->getInGroupId()]);
                         } else {
                             if (isset($prevOption['option_id'])) {
                                 // delete option
                                 $connection->delete($tablePrefix . 'catalog_product_option', 'option_id = ' . $prevOption['option_id']);
                                 $connection->delete($tablePrefix . 'catalog_product_option_type_value', 'option_id = ' . $prevOption['option_id']);
                                 $connection->delete($tablePrefix . 'custom_options_option_view_mode', 'option_id = ' . $prevOption['option_id']);
                                 $connection->delete($tablePrefix . 'custom_options_option_description', 'option_id = ' . $prevOption['option_id']);
                                 $connection->delete($tablePrefix . 'custom_options_option_default', 'option_id = ' . $prevOption['option_id']);
                                 $connection->delete($tablePrefix . 'custom_options_relation', 'group_id = ' . $templateId . ' AND product_id = ' . $productId . ' AND option_id = ' . $prevOption['option_id']);
                             }
                         }
                     }
                 }
                 // insert default options
                 foreach ($options as $option) {
                     if (isset($option['is_delete']) && $option['is_delete'] == 1) {
                         continue;
                     }
                     if ($group->getOnlyUpdate() && isset($optionRelations[$productId]) && !isset($diffOptions['IGI' . $option['in_group_id']])) {
                         continue;
                     }
                     $optionId = $this->saveOption($productId, $option, false, 0, $option['type']);
                     $realOptionIds[$option['option_id']]['value'] = $optionId;
                     $optionRelation = array('option_id' => $optionId, 'group_id' => $templateId, 'product_id' => $productId);
                     $connection->insert($tablePrefix . 'custom_options_relation', $optionRelation);
                     // insert option values
                     if ($this->getGroupByType($option['type']) == self::OPTION_GROUP_SELECT && count($option['values']) > 0) {
                         foreach ($option['values'] as $value) {
                             if (isset($value['is_delete']) && $value['is_delete'] == 1) {
                                 continue;
                             }
                             if ($group->getOnlyUpdate() && isset($optionRelations[$productId]) && !isset($diffOptions['IGI' . $option['in_group_id']]['values']['IGI' . $value['in_group_id']]['in_group_id'])) {
                                 continue;
                             }
                             $optionTypeId = $this->saveOptionValue($optionId, $value, array(), $value, false, 0);
                             $realOptionIds[$option['option_id']][$value['option_type_id']] = $optionTypeId;
                         }
                     }
                 }
                 // insert all store options
                 //print_r($allStoreOptions); exit;
                 foreach ($allStoreOptions as $storeOptions) {
                     foreach ($storeOptions['hash_options'] as $option) {
                         if (isset($realOptionIds[$option['option_id']]['value']) && $realOptionIds[$option['option_id']]['value']) {
                             $optionId = $this->saveOption($productId, $option, $realOptionIds[$option['option_id']]['value'], $storeOptions['store_id'], $option['type']);
                         } else {
                             $optionId = false;
                         }
                         // insert option values
                         if ($optionId && $this->getGroupByType($option['type']) == self::OPTION_GROUP_SELECT && count($option['values']) > 0) {
                             foreach ($option['values'] as $value) {
                                 if (isset($realOptionIds[$option['option_id']][$value['option_type_id']]) && $realOptionIds[$option['option_id']][$value['option_type_id']]) {
                                     $prevValue = $value;
                                     if (isset($value['prev_tiers'])) {
                                         $prevValue['tiers'] = $value['prev_tiers'];
                                     }
                                     if (isset($value['prev_specials'])) {
                                         $prevValue['specials'] = $value['prev_specials'];
                                     }
                                     $this->saveOptionValue($optionId, $value, $prevValue, $value, $realOptionIds[$option['option_id']][$value['option_type_id']], $storeOptions['store_id']);
                                 }
                             }
                         }
                     }
                 }
                 if (isset($optionRelations[$productId])) {
                     unset($optionRelations[$productId]);
                 }
                 $this->updateProductFlags($productId, $group->getAbsolutePrice(), $group->getAbsoluteWeight(), $group->getSkuPolicy());
             }
         }
         // remnants of the options that must be removed
         if (count($optionRelations) > 0) {
             foreach ($optionRelations as $productId => $prevOptions) {
                 if (count($prevOptions) > 0 && !in_array($productId, $productIds)) {
                     foreach ($prevOptions as $prevOption) {
                         $connection->delete($tablePrefix . 'catalog_product_option', 'option_id = ' . $prevOption['option_id']);
                         $connection->delete($tablePrefix . 'custom_options_option_view_mode', 'option_id = ' . $prevOption['option_id']);
                         $connection->delete($tablePrefix . 'custom_options_option_description', 'option_id = ' . $prevOption['option_id']);
                         $connection->delete($tablePrefix . 'custom_options_option_default', 'option_id = ' . $prevOption['option_id']);
                         $connection->delete($tablePrefix . 'custom_options_relation', 'group_id = ' . $templateId . ' AND product_id = ' . $productId . ' AND option_id = ' . $prevOption['option_id']);
                     }
                     $this->updateProductFlags($productId, $group->getAbsolutePrice(), $group->getAbsoluteWeight(), $group->getSkuPolicy());
                 }
             }
         }
     }
 }
Example #9
0
 public function saveProductOptions(array $newOptions, array $prevOptions, array $productIds, Varien_Object $group, $prevGroupIsActive = 1, $remove = true, $place = 'apo', $prevStoreOptionsData = array())
 {
     if (isset($productIds) && is_array($productIds)) {
         $relation = Mage::getResourceSingleton('customoptions/relation');
         if ($remove) {
             $this->removeProductOptions($group->getId());
             $relation->deleteGroup($group->getId());
         }
         $groupId = $group->getId();
         $groupIsActive = $group->getIsActive();
         $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
         $tablePrefix = (string) Mage::getConfig()->getTablePrefix();
         $condition = '';
         if ($place == 'product') {
             $condition = $place == 'product' ? ' AND product_id = ' . current($productIds) : '';
         }
         // get and prepare $optionRelations
         $select = $connection->select()->from($tablePrefix . 'custom_options_relation')->where('group_id = ' . $groupId . $condition);
         $optionRelations = $connection->fetchAll($select);
         if (is_array($optionRelations) && count($optionRelations) > 0) {
             $tmp = array();
             foreach ($optionRelations as $option) {
                 $tmp[$option['product_id']][$option['option_id']] = $option;
             }
             $optionRelations = $tmp;
         } else {
             $optionRelations = array();
         }
         $newOptions = $this->_prepareOptions($newOptions, $groupId, $groupIsActive);
         $prevOptions = $this->_prepareOptions($prevOptions, $groupId, $prevGroupIsActive);
         // comparison arrays - quadruple nesting
         $diffOptions = $this->comparisonArrays($newOptions, $prevOptions);
         // get all store options
         $select = $connection->select()->from($tablePrefix . 'custom_options_group_store')->where('group_id = ' . $groupId);
         $allStoreOptions = $connection->fetchAll($select);
         foreach ($allStoreOptions as $key => $storeOptions) {
             if ($storeOptions['hash_options']) {
                 $hashOptions = unserialize($storeOptions['hash_options']);
             } else {
                 $hashOptions = array();
             }
             if (isset($prevStoreOptionsData['store_id']) && $storeOptions['store_id'] == $prevStoreOptionsData['store_id']) {
                 if ($prevStoreOptionsData['hash_options']) {
                     $prevHashOptions = unserialize($prevStoreOptionsData['hash_options']);
                 } else {
                     $prevHashOptions = array();
                 }
                 // соединить чтобы сбросить недефолт!!
                 foreach ($prevHashOptions as $optionId => $option) {
                     //
                     if (isset($hashOptions[$optionId])) {
                         if (isset($option['values'])) {
                             foreach ($option['values'] as $valueId => $value) {
                                 // add to check remove store option value
                                 if (!isset($hashOptions[$optionId]['values'][$valueId])) {
                                     $hashOptions[$optionId]['values'][$valueId]['option_type_id'] = $value['option_type_id'];
                                 }
                             }
                         }
                     } else {
                         // add to check remove store option
                         $hashOptions[$optionId] = array('option_id' => $option['option_id'], 'type' => $option['type']);
                     }
                 }
             }
             $allStoreOptions[$key]['hash_options'] = $hashOptions;
         }
         //            print_r($newOptions); exit;
         //            print_r($prevOptions);
         //            print_r($diffOptions);
         //            exit;
         if (isset($newOptions) && is_array($newOptions)) {
             foreach ($productIds as $productId) {
                 $realOptionIds = array();
                 $options = $newOptions;
                 // work copy options
                 // $optionRelations
                 // update options
                 if (isset($optionRelations[$productId])) {
                     foreach ($optionRelations[$productId] as $optionId => $prevOption) {
                         //$optionId = $prevOption['option_id'];
                         $prevOption = Mage::getModel('catalog/product_option')->load($optionId);
                         if (isset($options['IGI' . $prevOption->getInGroupId()]) && (!isset($options['IGI' . $prevOption->getInGroupId()]['is_delete']) || $options['IGI' . $prevOption->getInGroupId()]['is_delete'] != 1)) {
                             $option = $options['IGI' . $prevOption->getInGroupId()];
                             if (isset($diffOptions['IGI' . $prevOption->getInGroupId()])) {
                                 $diffOption = $diffOptions['IGI' . $prevOption->getInGroupId()];
                             } else {
                                 $diffOption = array();
                             }
                             $this->saveOption($productId, $diffOption, $optionId, 0, $option['type']);
                             $realOptionIds[$option['option_id']]['value'] = $optionId;
                             if ($option['type'] == 'drop_down' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'multiple') {
                                 $select = $connection->select()->from($tablePrefix . 'catalog_product_option_type_value')->where('option_id = ' . $optionId);
                                 $prevValues = $connection->fetchAll($select);
                                 if (is_array($prevValues) && count($prevValues) > 0) {
                                     foreach ($prevValues as $prValue) {
                                         if (isset($option['values']['IGI' . $prValue['in_group_id']]) && (!isset($option['values']['IGI' . $prValue['in_group_id']]['is_delete']) || $option['values']['IGI' . $prValue['in_group_id']]['is_delete'] != 1)) {
                                             // update option value
                                             if (isset($diffOption['values']['IGI' . $prValue['in_group_id']])) {
                                                 $diffValue = $diffOption['values']['IGI' . $prValue['in_group_id']];
                                             } else {
                                                 $diffValue = array();
                                             }
                                             $this->saveOptionValue($optionId, $diffValue, $prValue['option_type_id'], 0);
                                             $realOptionIds[$option['option_id']][$option['values']['IGI' . $prValue['in_group_id']]['option_type_id']] = $prValue['option_type_id'];
                                             unset($option['values']['IGI' . $prValue['in_group_id']]);
                                         } else {
                                             // delete option value
                                             $connection->delete($tablePrefix . 'catalog_product_option_type_value', 'option_type_id = ' . $prValue['option_type_id']);
                                         }
                                     }
                                 }
                                 // insert option values
                                 if (count($option['values']) > 0) {
                                     foreach ($option['values'] as $value) {
                                         if (isset($value['is_delete']) && $value['is_delete'] == 1) {
                                             continue;
                                         }
                                         $this->saveOptionValue($optionId, $value, false, 0);
                                     }
                                 }
                             }
                             unset($options['IGI' . $prevOption->getInGroupId()]);
                         } else {
                             if (isset($prevOption['option_id'])) {
                                 // delete option
                                 $connection->delete($tablePrefix . 'catalog_product_option', 'option_id = ' . $prevOption['option_id']);
                                 $connection->delete($tablePrefix . 'custom_options_relation', 'group_id = ' . $groupId . ' AND product_id = ' . $productId . ' AND option_id = ' . $prevOption['option_id']);
                             }
                         }
                     }
                     unset($optionRelations[$productId]);
                 }
                 // insert default options
                 foreach ($options as $option) {
                     if (isset($option['is_delete']) && $option['is_delete'] == 1) {
                         continue;
                     }
                     $optionId = $this->saveOption($productId, $option, false, 0, $option['type']);
                     $realOptionIds[$option['option_id']]['value'] = $optionId;
                     $optionRelation = array('option_id' => $optionId, 'group_id' => $groupId, 'product_id' => $productId);
                     $connection->insert($tablePrefix . 'custom_options_relation', $optionRelation);
                     // insert option values
                     if (($option['type'] == 'drop_down' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'multiple') && count($option['values']) > 0) {
                         foreach ($option['values'] as $value) {
                             if (isset($value['is_delete']) && $value['is_delete'] == 1) {
                                 continue;
                             }
                             $optionTypeId = $this->saveOptionValue($optionId, $value, false, 0);
                             $realOptionIds[$option['option_id']][$value['option_type_id']] = $optionTypeId;
                         }
                     }
                 }
                 // insert all store options
                 //print_r($allStoreOptions); exit;
                 foreach ($allStoreOptions as $storeOptions) {
                     foreach ($storeOptions['hash_options'] as $option) {
                         if (isset($realOptionIds[$option['option_id']]['value']) && $realOptionIds[$option['option_id']]['value']) {
                             $optionId = $this->saveOption($productId, $option, $realOptionIds[$option['option_id']]['value'], $storeOptions['store_id'], $option['type']);
                         } else {
                             $optionId = false;
                         }
                         // insert option values
                         if ($optionId && ($option['type'] == 'drop_down' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'multiple') && count($option['values']) > 0) {
                             foreach ($option['values'] as $value) {
                                 if (isset($realOptionIds[$option['option_id']][$value['option_type_id']]) && $realOptionIds[$option['option_id']][$value['option_type_id']]) {
                                     $this->saveOptionValue($optionId, $value, $realOptionIds[$option['option_id']][$value['option_type_id']], $storeOptions['store_id']);
                                 }
                             }
                         }
                     }
                 }
                 $this->checkHasAndRequiredOptions($productId);
             }
         }
         // remnants of the options that must be removed
         if (count($optionRelations) > 0) {
             foreach ($optionRelations as $productId => $prevOptions) {
                 if (count($prevOptions) > 0) {
                     foreach ($prevOptions as $prevOption) {
                         $connection->delete($tablePrefix . 'catalog_product_option', 'option_id = ' . $prevOption['option_id']);
                         $connection->delete($tablePrefix . 'custom_options_relation', 'group_id = ' . $groupId . ' AND product_id = ' . $productId . ' AND option_id = ' . $prevOption['option_id']);
                     }
                 }
                 $this->checkHasAndRequiredOptions($productId);
             }
         }
     } else {
         if ($remove) {
             $this->removeProductOptions($group->getId());
             $relation = Mage::getResourceSingleton('customoptions/relation');
             $relation->deleteGroup($group->getId());
         }
     }
 }