*/ class Mage_Catalog_Model_Resource_Eav_Attribute extends Mage_Eav_Model_Entity_Attribute { const SCOPE_STORE = 0; const SCOPE_GLOBAL = 1; const SCOPE_WEBSITE = 2; const MODULE_NAME = 'Mage_Catalog'; const ENTITY = 'catalog_eav_attribute'; /** * Event prefix * * @var string */ protected $_eventPrefix = 'catalog_entity_attribute'; /** * Event object name * * @var string */ protected $_eventObject = 'attribute'; /** * Array with labels * * @var array */ static protected $_labels = null; protected function _construct() { $this->_init('catalog/attribute'); } /** * Processing object before save data * * @throws Mage_Core_Exception * @return Mage_Core_Model_Abstract */ protected function _beforeSave() { $this->setData('modulePrefix', self::MODULE_NAME); if (isset($this->_origData['is_global'])) { if (!isset($this->_data['is_global'])) { $this->_data['is_global'] = self::SCOPE_GLOBAL; } if (($this->_data['is_global'] != $this->_origData['is_global']) && $this->_getResource()->isUsedBySuperProducts($this)) { Mage::throwException(Mage::helper('catalog')->__('Scope must not be changed, because the attribute is used in configurable products.')); } } if ($this->getFrontendInput() == 'price') { if (!$this->getBackendModel()) { $this->setBackendModel('catalog/product_attribute_backend_price'); } } if ($this->getFrontendInput() == 'textarea') { if ($this->getIsWysiwygEnabled()) { $this->setIsHtmlAllowedOnFront(1); } } return parent::_beforeSave(); } /** * Processing object after save data * * @return Mage_Core_Model_Abstract */ protected function _afterSave() { /** * Fix saving attribute in admin */ Mage::getSingleton('eav/config')->clear(); Mage::getSingleton('index/indexer')->processEntityAction( $this, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE ); return parent::_afterSave(); } /** * Register indexing event before delete catalog eav attribute * * @return Mage_Catalog_Model_Resource_Eav_Attribute */ protected function _beforeDelete() { if ($this->_getResource()->isUsedBySuperProducts($this)) { Mage::throwException(Mage::helper('catalog')->__('This attribute is used in configurable products.')); } Mage::getSingleton('index/indexer')->logEvent( $this, self::ENTITY, Mage_Index_Model_Event::TYPE_DELETE ); return parent::_beforeDelete(); } /** * Init indexing process after catalog eav attribute delete commit * * @return Mage_Catalog_Model_Resource_Eav_Attribute */ protected function _afterDeleteCommit() { parent::_afterDeleteCommit(); Mage::getSingleton('index/indexer')->indexEvents( self::ENTITY, Mage_Index_Model_Event::TYPE_DELETE ); return $this; } /** * Return is attribute global * * @return integer */ public function getIsGlobal() { return $this->_getData('is_global'); } /** * Retrieve attribute is global scope flag * * @return bool */ public function isScopeGlobal() { return $this->getIsGlobal() == self::SCOPE_GLOBAL; } /** * Retrieve attribute is website scope website * * @return bool */ public function isScopeWebsite() { return $this->getIsGlobal() == self::SCOPE_WEBSITE; } /** * Retrieve attribute is store scope flag * * @return bool */ public function isScopeStore() { return !$this->isScopeGlobal() && !$this->isScopeWebsite(); } /** * Retrieve store id * * @return int */ public function getStoreId() { $dataObject = $this->getDataObject(); if ($dataObject) { return $dataObject->getStoreId(); } return $this->getData('store_id'); } /** * Retrieve apply to products array * Return empty array if applied to all products * * @return array */ public function getApplyTo() { if ($this->getData('apply_to')) { if (is_array($this->getData('apply_to'))) { return $this->getData('apply_to'); } return explode(',', $this->getData('apply_to')); } else { return array(); } } /** * Retrieve source model * * @return Mage_Eav_Model_Entity_Attribute_Source_Abstract */ public function getSourceModel() { $model = $this->getData('source_model'); if (empty($model)) { if ($this->getBackendType() == 'int' && $this->getFrontendInput() == 'select') { return $this->_getDefaultSourceModel(); } } return $model; } /** * Check is allow for rule condition * * @return bool */ public function isAllowedForRuleCondition() { $allowedInputTypes = array('text', 'multiselect', 'textarea', 'date', 'datetime', 'select', 'boolean', 'price'); return $this->getIsVisible() && in_array($this->getFrontendInput(), $allowedInputTypes); } /** * Retrieve don't translated frontend label * * @return string */ public function getFrontendLabel() { return $this->_getData('frontend_label'); } /** * Get Attribute translated label for store * * @deprecated * @return string */ protected function _getLabelForStore() { return $this->getFrontendLabel(); } /** * Initialize store Labels for attributes * * @deprecated * @param int $storeId */ public static function initLabels($storeId = null) { if (is_null(self::$_labels)) { if (is_null($storeId)) { $storeId = Mage::app()->getStore()->getId(); } $attributeLabels = array(); $attributes = Mage::getResourceSingleton('catalog/product')->getAttributesByCode(); foreach ($attributes as $attribute) { if (strlen($attribute->getData('frontend_label')) > 0) { $attributeLabels[] = $attribute->getData('frontend_label'); } } self::$_labels = Mage::app()->getTranslator()->getResource() ->getTranslationArrayByStrings($attributeLabels, $storeId); } } /** * Get default attribute source model * * @return string */ public function _getDefaultSourceModel() { return 'eav/entity_attribute_source_table'; } /** * Check is an attribute used in EAV index * * @return bool */ public function isIndexable() { // exclude price attribute if ($this->getAttributeCode() == 'price') { return false; } if (!$this->getIsFilterableInSearch() && !$this->getIsVisibleInAdvancedSearch() && !$this->getIsFilterable()) { return false; } $backendType = $this->getBackendType(); $frontendInput = $this->getFrontendInput(); if ($backendType == 'int' && $frontendInput == 'select') { return true; } else if ($backendType == 'varchar' && $frontendInput == 'multiselect') { return true; } else if ($backendType == 'decimal') { return true; } return false; } /** * Retrieve index type for indexable attribute * * @return string|false */ public function getIndexType() { if (!$this->isIndexable()) { return false; } if ($this->getBackendType() == 'decimal') { return 'decimal'; } return 'source'; } }