1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Model_Resource_Attribute extends Mage_Eav_Model_Resource_Entity_Attribute
36: {
37: 38: 39: 40: 41: 42:
43: protected function _beforeSave(Mage_Core_Model_Abstract $object)
44: {
45: $applyTo = $object->getApplyTo();
46: if (is_array($applyTo)) {
47: $object->setApplyTo(implode(',', $applyTo));
48: }
49: return parent::_beforeSave($object);
50: }
51:
52: 53: 54: 55: 56: 57:
58: protected function _afterSave(Mage_Core_Model_Abstract $object)
59: {
60: $this->_clearUselessAttributeValues($object);
61: return parent::_afterSave($object);
62: }
63:
64: 65: 66: 67: 68: 69:
70: protected function _clearUselessAttributeValues(Mage_Core_Model_Abstract $object)
71: {
72: $origData = $object->getOrigData();
73:
74: if ($object->isScopeGlobal()
75: && isset($origData['is_global'])
76: && Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL != $origData['is_global']
77: ) {
78: $attributeStoreIds = array_keys(Mage::app()->getStores());
79: if (!empty($attributeStoreIds)) {
80: $delCondition = array(
81: 'entity_type_id=?' => $object->getEntityTypeId(),
82: 'attribute_id = ?' => $object->getId(),
83: 'store_id IN(?)' => $attributeStoreIds
84: );
85: $this->_getWriteAdapter()->delete($object->getBackendTable(), $delCondition);
86: }
87: }
88:
89: return $this;
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function deleteEntity(Mage_Core_Model_Abstract $object)
99: {
100: if (!$object->getEntityAttributeId()) {
101: return $this;
102: }
103:
104: $select = $this->_getReadAdapter()->select()
105: ->from($this->getTable('eav/entity_attribute'))
106: ->where('entity_attribute_id = ?', (int)$object->getEntityAttributeId());
107: $result = $this->_getReadAdapter()->fetchRow($select);
108:
109: if ($result) {
110: $attribute = Mage::getSingleton('eav/config')
111: ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $result['attribute_id']);
112:
113: if ($this->isUsedBySuperProducts($attribute, $result['attribute_set_id'])) {
114: Mage::throwException(Mage::helper('eav')->__("Attribute '%s' used in configurable products", $attribute->getAttributeCode()));
115: }
116: $backendTable = $attribute->getBackend()->getTable();
117: if ($backendTable) {
118: $select = $this->_getWriteAdapter()->select()
119: ->from($attribute->getEntity()->getEntityTable(), 'entity_id')
120: ->where('attribute_set_id = ?', $result['attribute_set_id']);
121:
122: $clearCondition = array(
123: 'entity_type_id =?' => $attribute->getEntityTypeId(),
124: 'attribute_id =?' => $attribute->getId(),
125: 'entity_id IN (?)' => $select
126: );
127: $this->_getWriteAdapter()->delete($backendTable, $clearCondition);
128: }
129: }
130:
131: $condition = array('entity_attribute_id = ?' => $object->getEntityAttributeId());
132: $this->_getWriteAdapter()->delete($this->getTable('entity_attribute'), $condition);
133:
134: return $this;
135: }
136:
137: 138: 139: 140: 141: 142: 143:
144: public function isUsedBySuperProducts(Mage_Core_Model_Abstract $object, $attributeSet = null)
145: {
146: $adapter = $this->_getReadAdapter();
147: $attrTable = $this->getTable('catalog/product_super_attribute');
148: $productTable = $this->getTable('catalog/product');
149:
150: $bind = array('attribute_id' => $object->getAttributeId());
151: $select = clone $adapter->select();
152: $select->reset()
153: ->from(array('main_table' => $attrTable), array('psa_count' => 'COUNT(product_super_attribute_id)'))
154: ->join(array('entity' => $productTable), 'main_table.product_id = entity.entity_id')
155: ->where('main_table.attribute_id = :attribute_id')
156: ->group('main_table.attribute_id')
157: ->limit(1);
158:
159: if ($attributeSet !== null) {
160: $bind['attribute_set_id'] = $attributeSet;
161: $select->where('entity.attribute_set_id = :attribute_set_id');
162: }
163:
164: $helper = Mage::getResourceHelper('core');
165: $query = $helper->getQueryUsingAnalyticFunction($select);
166: return $adapter->fetchOne($query, $bind);
167: }
168: }
169: