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: 36: 37: 38: 39: 40: 41: 42: 43:
44: class Mage_Eav_Model_Entity_Attribute_Set extends Mage_Core_Model_Abstract
45: {
46: 47: 48: 49:
50: protected $_eventPrefix = 'eav_entity_attribute_set';
51:
52: 53: 54: 55:
56: protected function _construct()
57: {
58: $this->_init('eav/entity_attribute_set');
59: }
60:
61: 62: 63: 64: 65: 66:
67: public function initFromSkeleton($skeletonId)
68: {
69: $groups = Mage::getModel('eav/entity_attribute_group')
70: ->getResourceCollection()
71: ->setAttributeSetFilter($skeletonId)
72: ->load();
73:
74: $newGroups = array();
75: foreach ($groups as $group) {
76: $newGroup = clone $group;
77: $newGroup->setId(null)
78: ->setAttributeSetId($this->getId())
79: ->setDefaultId($group->getDefaultId());
80:
81: $groupAttributesCollection = Mage::getModel('eav/entity_attribute')
82: ->getResourceCollection()
83: ->setAttributeGroupFilter($group->getId())
84: ->load();
85:
86: $newAttributes = array();
87: foreach ($groupAttributesCollection as $attribute) {
88: $newAttribute = Mage::getModel('eav/entity_attribute')
89: ->setId($attribute->getId())
90:
91: ->setAttributeSetId($this->getId())
92: ->setEntityTypeId($this->getEntityTypeId())
93: ->setSortOrder($attribute->getSortOrder());
94: $newAttributes[] = $newAttribute;
95: }
96: $newGroup->setAttributes($newAttributes);
97: $newGroups[] = $newGroup;
98: }
99: $this->setGroups($newGroups);
100:
101: return $this;
102: }
103:
104: 105: 106: 107: 108: 109:
110: public function organizeData($data)
111: {
112: $modelGroupArray = array();
113: $modelAttributeArray = array();
114: $attributeIds = array();
115: if ($data['attributes']) {
116: $ids = array();
117: foreach ($data['attributes'] as $attribute) {
118: $ids[] = $attribute[0];
119: }
120: $attributeIds = Mage::getResourceSingleton('eav/entity_attribute')
121: ->getValidAttributeIds($ids);
122: }
123: if( $data['groups'] ) {
124: foreach ($data['groups'] as $group) {
125: $modelGroup = Mage::getModel('eav/entity_attribute_group');
126: $modelGroup->setId(is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null)
127: ->setAttributeGroupName($group[1])
128: ->setAttributeSetId($this->getId())
129: ->setSortOrder($group[2]);
130:
131: if( $data['attributes'] ) {
132: foreach( $data['attributes'] as $attribute ) {
133: if( $attribute[1] == $group[0] && in_array($attribute[0], $attributeIds) ) {
134: $modelAttribute = Mage::getModel('eav/entity_attribute');
135: $modelAttribute->setId($attribute[0])
136: ->setAttributeGroupId($attribute[1])
137: ->setAttributeSetId($this->getId())
138: ->setEntityTypeId($this->getEntityTypeId())
139: ->setSortOrder($attribute[2]);
140: $modelAttributeArray[] = $modelAttribute;
141: }
142: }
143: $modelGroup->setAttributes($modelAttributeArray);
144: $modelAttributeArray = array();
145: }
146: $modelGroupArray[] = $modelGroup;
147: }
148: $this->setGroups($modelGroupArray);
149: }
150:
151:
152: if( $data['not_attributes'] ) {
153: $modelAttributeArray = array();
154: foreach( $data['not_attributes'] as $attributeId ) {
155: $modelAttribute = Mage::getModel('eav/entity_attribute');
156:
157: $modelAttribute->setEntityAttributeId($attributeId);
158: $modelAttributeArray[] = $modelAttribute;
159: }
160: $this->setRemoveAttributes($modelAttributeArray);
161: }
162:
163: if( $data['removeGroups'] ) {
164: $modelGroupArray = array();
165: foreach( $data['removeGroups'] as $groupId ) {
166: $modelGroup = Mage::getModel('eav/entity_attribute_group');
167: $modelGroup->setId($groupId);
168:
169: $modelGroupArray[] = $modelGroup;
170: }
171: $this->setRemoveGroups($modelGroupArray);
172: }
173: $this->setAttributeSetName($data['attribute_set_name'])
174: ->setEntityTypeId($this->getEntityTypeId());
175:
176: return $this;
177: }
178:
179: 180: 181: 182: 183: 184: 185:
186: public function validate()
187: {
188: if (!$this->_getResource()->validate($this, $this->getAttributeSetName())) {
189: throw Mage::exception('Mage_Eav',
190: Mage::helper('eav')->__('Attribute set with the "%s" name already exists.', $this->getAttributeSetName())
191: );
192: }
193:
194: return true;
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204:
205: public function addSetInfo($entityType, array $attributes, $setId = null)
206: {
207: $attributeIds = array();
208: $config = Mage::getSingleton('eav/config');
209: $entityType = $config->getEntityType($entityType);
210: foreach ($attributes as $attribute) {
211: $attribute = $config->getAttribute($entityType, $attribute);
212: if ($setId && is_array($attribute->getAttributeSetInfo($setId))) {
213: continue;
214: }
215: if (!$attribute->getAttributeId()) {
216: continue;
217: }
218: $attributeIds[] = $attribute->getAttributeId();
219: }
220:
221: if ($attributeIds) {
222: $setInfo = $this->_getResource()
223: ->getSetInfo($attributeIds, $setId);
224:
225: foreach ($attributes as $attribute) {
226: $attribute = $config->getAttribute($entityType, $attribute);
227: if (!$attribute->getAttributeId()) {
228: continue;
229: }
230: if (!in_array($attribute->getAttributeId(), $attributeIds)) {
231: continue;
232: }
233: if (is_numeric($setId)) {
234: $attributeSetInfo = $attribute->getAttributeSetInfo();
235: if (!is_array($attributeSetInfo)) {
236: $attributeSetInfo = array();
237: }
238: if (isset($setInfo[$attribute->getAttributeId()][$setId])) {
239: $attributeSetInfo[$setId] = $setInfo[$attribute->getAttributeId()][$setId];
240: }
241: $attribute->setAttributeSetInfo($attributeSetInfo);
242: } else {
243: if (isset($setInfo[$attribute->getAttributeId()])) {
244: $attribute->setAttributeSetInfo($setInfo[$attribute->getAttributeId()]);
245: }
246: else {
247: $attribute->setAttributeSetInfo(array());
248: }
249: }
250: }
251: }
252:
253: return $this;
254: }
255:
256: 257: 258: 259: 260: 261:
262: public function getDefaultGroupId($setId = null)
263: {
264: if ($setId === null) {
265: $setId = $this->getId();
266: }
267: if ($setId) {
268: $groupId = $this->_getResource()->getDefaultGroupId($setId);
269: } else {
270: $groupId = null;
271: }
272: return $groupId;
273: }
274: }
275: