getData('attribute'); $attributeId = $attribute->getData('attribute_id'); $options = $attribute->getOption(); $this->_deleteAttributeImages($options); $this->_saveAttribute($attributeId, $attribute->getData('orange35_is_colorpicker')); $this->_saveOptions($attributeId, $attribute->getOption()); return $this; } public function onDeleteAttribute(Varien_Event_Observer $observer) { $attribute = $observer->getData('attribute'); $attributeId = $attribute->getData('attribute_id'); $items = $this->getColorsOptions($attributeId); foreach ($items as $item) { Mage::helper('orange35_colorpicker/image')->delete($item['option_id']); } Mage::helper('orange35_colorpicker/image')->deleteAttributeDirectory($attributeId); return $this; } public function addCustomLayoutConfigHandle(Varien_Event_Observer $observer) { /** @var $layout Mage_Core_Model_Layout */ $event = $observer->getEvent(); $controllerAction = $event->getAction(); $layout = $event->getLayout(); if ($controllerAction && $layout && $controllerAction instanceof Mage_Adminhtml_System_ConfigController) { if ($controllerAction->getRequest()->getParam('section') == self::COLORPICKER_SECTION) { $layout->getUpdate()->addHandle('adminhtml_system_config_edit_orange35_colorpicker'); } } return $this; } /** * Retrieve the product model * * @return Mage_Catalog_Model_Product $product */ public function getProduct() { return Mage::registry('product'); } /** * @param $attributeId * @return mixed */ protected function getColorsOptions($attributeId) { $modelOption = Mage::getModel('orange35_colorpicker/option')->getCollection(); $modelOption->addFieldToFilter('attribute_id', $attributeId); return $modelOption->getItems(); } /** * Shortcut to getRequest * */ protected function _getRequest() { return Mage::app()->getRequest(); } /** * @param $attributeId * @param $colorPickerValue */ protected function _saveAttribute($attributeId, $colorPickerValue) { $tagInfoModel = Mage::getModel('orange35_colorpicker/attribute'); $tagInfoModel->addData(array('attribute_id' => $attributeId, 'is_colorpicker' => $colorPickerValue ? 1 : 0)); $tagInfoModel->save(); } /** * @param $attributeId * @param $options * @return $this|bool */ protected function _saveOptions($attributeId, $options) { $modelOption = Mage::getModel('orange35_colorpicker/option'); if (empty($options['orange35_color'])) { return false; } foreach ($options['orange35_color'] as $id => $option) { $item = array( 'attribute_id' => $attributeId, 'option_id' => $id, 'color' => $options['orange35_color'][$id] ); if (!empty($options['orange35_image'][$id])) { $item['image'] = $options['orange35_image'][$id]; } $modelOption->addData($item); $modelOption->save(); } return $this; } protected function _deleteAttributeImages($options) { if (empty($options) || empty($options['delete'])) { return $this; } foreach ($options['delete'] as $optionId => $isDelete) { if (!empty($isDelete)) { Mage::helper('orange35_colorpicker/image')->delete($optionId); } } return $this; } /** * * If product has "Multicolor" as the only Attribute Set * and number of all attributes sets for configurable product equals one * then change the view of product template view on custom * It is updating of xml used */ public function oneAttributeChangeProductViewTemplate($observer) { $product = Mage::registry('current_product'); /** * Return if it is not product page */ if (!($product instanceof Mage_Catalog_Model_Product)) { return; } if (Mage::getStoreConfig('colorpicker_section/product_view_group/product_module_enable')) { $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($product->getAttributeSetId()); /** * Convert attribute set name to alphanumeric + underscore string */ $attributeSetName = str_replace('-', '_', $product->formatUrlKey($attributeSet->getAttributeSetName())); // Collect options applicable to this configurable product if (!$product->isConfigurable()) { return; } else { $productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product); //$attributeOptions = array(); if (sizeof($productAttributeOptions) == 1 && $productAttributeOptions[0]['attribute_code'] == "multicolor") { /* @var $update Mage_Core_Model_Layout_Update */ $update = $observer->getEvent()->getLayout()->getUpdate(); $handles = $update->getHandles(); // Store all handles in a variable $update->resetHandles(); // Remove all handles /** * Rearrange layout handles to ensure * handle is added last */ foreach ($handles as $handle) { $update->addHandle($handle); } $update->addHandle('multicolor_display_options_template'); } } } } }