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_Eav_Model_Resource_Entity_Attribute_Set extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('eav/attribute_set', 'attribute_set_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: protected function _afterSave(Mage_Core_Model_Abstract $object)
53: {
54: if ($object->getGroups()) {
55:
56: foreach ($object->getGroups() as $group) {
57: $group->setAttributeSetId($object->getId());
58: if ($group->itemExists() && !$group->getId()) {
59: continue;
60: }
61: $group->save();
62: }
63: }
64: if ($object->getRemoveGroups()) {
65: foreach ($object->getRemoveGroups() as $group) {
66:
67: $group->delete();
68: }
69: Mage::getResourceModel('eav/entity_attribute_group')->updateDefaultGroup($object->getId());
70: }
71: if ($object->getRemoveAttributes()) {
72: foreach ($object->getRemoveAttributes() as $attribute) {
73:
74: $attribute->deleteEntity();
75: }
76: }
77:
78: return parent::_afterSave($object);
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public function validate($object, $attributeSetName)
89: {
90:
91: $adapter = $this->_getReadAdapter();
92: $bind = array(
93: 'attribute_set_name' => trim($attributeSetName),
94: 'entity_type_id' => $object->getEntityTypeId()
95: );
96: $select = $adapter->select()
97: ->from($this->getMainTable())
98: ->where('attribute_set_name = :attribute_set_name')
99: ->where('entity_type_id = :entity_type_id');
100:
101: if ($object->getId()) {
102: $bind['attribute_set_id'] = $object->getId();
103: $select->where('attribute_set_id != :attribute_set_id');
104: }
105:
106: return !$adapter->fetchOne($select, $bind) ? true : false;
107: }
108:
109: 110: 111: 112: 113: 114: 115:
116: public function getSetInfo(array $attributeIds, $setId = null)
117: {
118: $adapter = $this->_getReadAdapter();
119: $setInfo = array();
120: $attributeToSetInfo = array();
121:
122: if (count($attributeIds) > 0) {
123: $select = $adapter->select()
124: ->from(
125: array('entity' => $this->getTable('eav/entity_attribute')),
126: array('attribute_id', 'attribute_set_id', 'attribute_group_id', 'sort_order'))
127: ->joinLeft(
128: array('attribute_group' => $this->getTable('eav/attribute_group')),
129: 'entity.attribute_group_id = attribute_group.attribute_group_id',
130: array('group_sort_order' => 'sort_order'))
131: ->where('entity.attribute_id IN (?)', $attributeIds);
132: $bind = array();
133: if (is_numeric($setId)) {
134: $bind[':attribute_set_id'] = $setId;
135: $select->where('entity.attribute_set_id = :attribute_set_id');
136: }
137: $result = $adapter->fetchAll($select, $bind);
138:
139: foreach ($result as $row) {
140: $data = array(
141: 'group_id' => $row['attribute_group_id'],
142: 'group_sort' => $row['group_sort_order'],
143: 'sort' => $row['sort_order']
144: );
145: $attributeToSetInfo[$row['attribute_id']][$row['attribute_set_id']] = $data;
146: }
147: }
148:
149: foreach ($attributeIds as $atttibuteId) {
150: $setInfo[$atttibuteId] = isset($attributeToSetInfo[$atttibuteId])
151: ? $attributeToSetInfo[$atttibuteId]
152: : array();
153: }
154:
155: return $setInfo;
156: }
157:
158: 159: 160: 161: 162: 163:
164: public function getDefaultGroupId($setId)
165: {
166: $adapter = $this->_getReadAdapter();
167: $bind = array(
168: 'attribute_set_id' => (int)$setId
169: );
170: $select = $adapter->select()
171: ->from($this->getTable('eav/attribute_group'), 'attribute_group_id')
172: ->where('attribute_set_id = :attribute_set_id')
173: ->where('default_id = 1')
174: ->limit(1);
175: return $adapter->fetchOne($select, $bind);
176: }
177: }
178: