00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Eav_Model_Entity_Attribute_Set extends Mage_Core_Model_Abstract
00036 {
00037
00038
00039
00040
00041 protected function _construct()
00042 {
00043 $this->_init('eav/entity_attribute_set');
00044 }
00045
00046
00047
00048
00049
00050
00051
00052 public function initFromSkeleton($skeletonId)
00053 {
00054 $groups = Mage::getModel('eav/entity_attribute_group')
00055 ->getResourceCollection()
00056 ->setAttributeSetFilter($skeletonId)
00057 ->load();
00058
00059 $newGroups = array();
00060 foreach( $groups as $group ) {
00061 $newGroup = clone $group;
00062 $newGroup->setId(null)
00063 ->setAttributeSetId($this->getId())
00064 ->setDefaultId($group->getDefaultId());
00065
00066 $groupAttributesCollection = Mage::getModel('eav/entity_attribute')
00067 ->getResourceCollection()
00068 ->setAttributeGroupFilter($group->getId())
00069 ->load();
00070
00071 $newAttributes = array();
00072 foreach( $groupAttributesCollection as $attribute ) {
00073 $newAttribute = Mage::getModel('eav/entity_attribute')
00074 ->setId($attribute->getId())
00075
00076 ->setAttributeSetId($this->getId())
00077 ->setEntityTypeId($this->getEntityTypeId())
00078 ->setSortOrder($attribute->getSortOrder());
00079 $newAttributes[] = $newAttribute;
00080 }
00081 $newGroup->setAttributes($newAttributes);
00082 $newGroups[] = $newGroup;
00083 }
00084 $this->setGroups($newGroups);
00085 return $this;
00086 }
00087
00088
00089
00090
00091
00092
00093 public function organizeData($data)
00094 {
00095 $modelGroupArray = array();
00096 $modelAttributeArray = array();
00097 if( $data['groups'] ) {
00098 foreach( $data['groups'] as $group ) {
00099 $modelGroup = Mage::getModel('eav/entity_attribute_group');
00100 $modelGroup->setId(is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null)
00101 ->setAttributeGroupName($group[1])
00102 ->setAttributeSetId($this->getId())
00103 ->setSortOrder($group[2]);
00104
00105 if( $data['attributes'] ) {
00106 foreach( $data['attributes'] as $attribute ) {
00107 if( $attribute[1] == $group[0] ) {
00108 $modelAttribute = Mage::getModel('eav/entity_attribute');
00109 $modelAttribute->setId($attribute[0])
00110 ->setAttributeGroupId($attribute[1])
00111 ->setAttributeSetId($this->getId())
00112 ->setEntityTypeId($this->getEntityTypeId())
00113 ->setSortOrder($attribute[2]);
00114 $modelAttributeArray[] = $modelAttribute;
00115 }
00116 }
00117 $modelGroup->setAttributes($modelAttributeArray);
00118 $modelAttributeArray = array();
00119 }
00120 $modelGroupArray[] = $modelGroup;
00121 }
00122 $this->setGroups($modelGroupArray);
00123 }
00124
00125
00126 if( $data['not_attributes'] ) {
00127 $modelAttributeArray = array();
00128 foreach( $data['not_attributes'] as $attributeId ) {
00129 $modelAttribute = Mage::getModel('eav/entity_attribute');
00130
00131 $modelAttribute->setEntityAttributeId($attributeId);
00132 $modelAttributeArray[] = $modelAttribute;
00133 }
00134 $this->setRemoveAttributes($modelAttributeArray);
00135 }
00136
00137 if( $data['removeGroups'] ) {
00138 $modelGroupArray = array();
00139 foreach( $data['removeGroups'] as $groupId ) {
00140 $modelGroup = Mage::getModel('eav/entity_attribute_group');
00141 $modelGroup->setId($groupId);
00142
00143 $modelGroupArray[] = $modelGroup;
00144 }
00145 $this->setRemoveGroups($modelGroupArray);
00146 }
00147 $this->setAttributeSetName($data['attribute_set_name'])
00148 ->setEntityTypeId($this->getEntityTypeId());
00149 }
00150
00151
00152
00153
00154
00155
00156
00157 public function validate($name)
00158 {
00159 if (!$this->_getResource()->validate($this, $name)) {
00160 Mage::throwException(Mage::helper('eav')->__('Attribute set with the "%s" name already exists',$name));
00161 }
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 public function addSetInfo($entityType, array $attributes, $setId = null)
00173 {
00174 $attributeIds = array();
00175 $config = Mage::getSingleton('eav/config');
00176 $entityType = $config->getEntityType($entityType);
00177 foreach ($attributes as $attribute) {
00178 $attribute = $config->getAttribute($entityType, $attribute);
00179 if ($setId && is_array($attribute->getAttributeSetInfo($setId))) {
00180 continue;
00181 }
00182 if (!$attribute->getAttributeId()) {
00183 continue;
00184 }
00185 $attributeIds[] = $attribute->getAttributeId();
00186 }
00187
00188 if ($attributeIds) {
00189 $setInfo = $this->_getResource()
00190 ->getSetInfo($attributeIds, $setId);
00191
00192 foreach ($attributes as $attribute) {
00193 $attribute = $config->getAttribute($entityType, $attribute);
00194 if (!$attribute->getAttributeId()) {
00195 continue;
00196 }
00197 if (!in_array($attribute->getAttributeId(), $attributeIds)) {
00198 continue;
00199 }
00200 if (is_numeric($setId)) {
00201 $attributeSetInfo = $attribute->getAttributeSetInfo();
00202 if (!is_array($attributeSetInfo)) {
00203 $attributeSetInfo = array();
00204 }
00205 if (isset($setInfo[$attribute->getAttributeId()][$setId])) {
00206 $attributeSetInfo[$setId] = $setInfo[$attribute->getAttributeId()][$setId];
00207 }
00208 $attribute->setAttributeSetInfo($attributeSetInfo);
00209 }
00210 else {
00211 if (isset($setInfo[$attribute->getAttributeId()])) {
00212 $attribute->setAttributeSetInfo($setInfo[$attribute->getAttributeId()]);
00213 }
00214 else {
00215 $attribute->setAttributeSetInfo(array());
00216 }
00217 }
00218 }
00219 }
00220
00221 return $this;
00222 }
00223 }