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: abstract class Mage_Eav_Model_Resource_Attribute extends Mage_Eav_Model_Resource_Entity_Attribute
36: {
37: 38: 39: 40: 41: 42: 43: 44:
45: abstract protected function _getEavWebsiteTable();
46:
47: 48: 49: 50: 51: 52: 53:
54: abstract protected function _getFormAttributeTable();
55:
56: 57: 58: 59: 60: 61:
62: protected function _beforeSave(Mage_Core_Model_Abstract $object)
63: {
64: $validateRules = $object->getData('validate_rules');
65: if (is_array($validateRules)) {
66: $object->setData('validate_rules', serialize($validateRules));
67: }
68: return parent::_beforeSave($object);
69: }
70:
71: 72: 73: 74: 75: 76: 77: 78:
79: protected function _getLoadSelect($field, $value, $object)
80: {
81: $select = parent::_getLoadSelect($field, $value, $object);
82: $websiteId = (int)$object->getWebsite()->getId();
83: if ($websiteId) {
84: $adapter = $this->_getReadAdapter();
85: $columns = array();
86: $scopeTable = $this->_getEavWebsiteTable();
87: $describe = $adapter->describeTable($scopeTable);
88: unset($describe['attribute_id']);
89: foreach (array_keys($describe) as $columnName) {
90: $columns['scope_' . $columnName] = $columnName;
91: }
92: $conditionSql = $adapter->quoteInto(
93: $this->getMainTable() . '.attribute_id = scope_table.attribute_id AND scope_table.website_id =?',
94: $websiteId);
95: $select->joinLeft(
96: array('scope_table' => $scopeTable),
97: $conditionSql,
98: $columns
99: );
100: }
101:
102: return $select;
103: }
104:
105: 106: 107: 108: 109: 110:
111: protected function _afterSave(Mage_Core_Model_Abstract $object)
112: {
113: $forms = $object->getData('used_in_forms');
114: $adapter = $this->_getWriteAdapter();
115: if (is_array($forms)) {
116: $where = array('attribute_id=?' => $object->getId());
117: $adapter->delete($this->_getFormAttributeTable(), $where);
118:
119: $data = array();
120: foreach ($forms as $formCode) {
121: $data[] = array(
122: 'form_code' => $formCode,
123: 'attribute_id' => (int)$object->getId()
124: );
125: }
126:
127: if ($data) {
128: $adapter->insertMultiple($this->_getFormAttributeTable(), $data);
129: }
130: }
131:
132:
133: if (!$object->isObjectNew() && $object->dataHasChangedFor('sort_order')) {
134: $data = array('sort_order' => $object->getSortOrder());
135: $where = array('attribute_id=?' => (int)$object->getId());
136: $adapter->update($this->getTable('eav/entity_attribute'), $data, $where);
137: }
138:
139:
140: $websiteId = (int)$object->getWebsite()->getId();
141: if ($websiteId) {
142: $table = $this->_getEavWebsiteTable();
143: $describe = $this->_getReadAdapter()->describeTable($table);
144: $data = array();
145: if (!$object->getScopeWebsiteId() || $object->getScopeWebsiteId() != $websiteId) {
146: $data = $this->getScopeValues($object);
147: }
148:
149: $data['attribute_id'] = (int)$object->getId();
150: $data['website_id'] = (int)$websiteId;
151: unset($describe['attribute_id']);
152: unset($describe['website_id']);
153:
154: $updateColumns = array();
155: foreach (array_keys($describe) as $columnName) {
156: $data[$columnName] = $object->getData('scope_' . $columnName);
157: $updateColumns[] = $columnName;
158: }
159:
160: $adapter->insertOnDuplicate($table, $data, $updateColumns);
161: }
162:
163: return parent::_afterSave($object);
164: }
165:
166: 167: 168: 169: 170: 171:
172: public function getScopeValues(Mage_Eav_Model_Attribute $object)
173: {
174: $adapter = $this->_getReadAdapter();
175: $bind = array(
176: 'attribute_id' => (int)$object->getId(),
177: 'website_id' => (int)$object->getWebsite()->getId()
178: );
179: $select = $adapter->select()
180: ->from($this->_getEavWebsiteTable())
181: ->where('attribute_id = :attribute_id')
182: ->where('website_id = :website_id')
183: ->limit(1);
184: $result = $adapter->fetchRow($select, $bind);
185:
186: if (!$result) {
187: $result = array();
188: }
189:
190: return $result;
191: }
192:
193: 194: 195: 196: 197: 198:
199: public function getUsedInForms(Mage_Core_Model_Abstract $object)
200: {
201: $adapter = $this->_getReadAdapter();
202: $bind = array('attribute_id' => (int)$object->getId());
203: $select = $adapter->select()
204: ->from($this->_getFormAttributeTable(), 'form_code')
205: ->where('attribute_id = :attribute_id');
206:
207: return $adapter->fetchCol($select, $bind);
208: }
209: }
210: