/app/code/core/Mage/Eav/Model/Entity/Attribute/Set.php

https://github.com/gryzz/crystal_magento · PHP · 236 lines · 156 code · 18 blank · 62 comment · 22 complexity · 5e0e76613c03dd2bb75a490b38c462b3 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Eav
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Eav attribute set model
  28. *
  29. * @category Mage
  30. * @package Mage_Eav
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Eav_Model_Entity_Attribute_Set extends Mage_Core_Model_Abstract
  34. {
  35. /**
  36. * Initialize resource model
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('eav/entity_attribute_set');
  42. }
  43. /**
  44. * Init attribute set from skeleton (another attribute set)
  45. *
  46. * @param int $skeletonId
  47. * @return Mage_Eav_Model_Entity_Attribute_Set
  48. */
  49. public function initFromSkeleton($skeletonId)
  50. {
  51. $groups = Mage::getModel('eav/entity_attribute_group')
  52. ->getResourceCollection()
  53. ->setAttributeSetFilter($skeletonId)
  54. ->load();
  55. $newGroups = array();
  56. foreach( $groups as $group ) {
  57. $newGroup = clone $group;
  58. $newGroup->setId(null)
  59. ->setAttributeSetId($this->getId())
  60. ->setDefaultId($group->getDefaultId());
  61. $groupAttributesCollection = Mage::getModel('eav/entity_attribute')
  62. ->getResourceCollection()
  63. ->setAttributeGroupFilter($group->getId())
  64. ->load();
  65. $newAttributes = array();
  66. foreach( $groupAttributesCollection as $attribute ) {
  67. $newAttribute = Mage::getModel('eav/entity_attribute')
  68. ->setId($attribute->getId())
  69. //->setAttributeGroupId($newGroup->getId())
  70. ->setAttributeSetId($this->getId())
  71. ->setEntityTypeId($this->getEntityTypeId())
  72. ->setSortOrder($attribute->getSortOrder());
  73. $newAttributes[] = $newAttribute;
  74. }
  75. $newGroup->setAttributes($newAttributes);
  76. $newGroups[] = $newGroup;
  77. }
  78. $this->setGroups($newGroups);
  79. return $this;
  80. }
  81. /**
  82. * Collect data for save
  83. *
  84. * @param array $data
  85. */
  86. public function organizeData($data)
  87. {
  88. $modelGroupArray = array();
  89. $modelAttributeArray = array();
  90. $attributeIds = array();
  91. if ($data['attributes']) {
  92. $ids = array();
  93. foreach ($data['attributes'] as $attribute) {
  94. $ids[] = $attribute[0];
  95. }
  96. $attributeIds = Mage::getResourceSingleton('eav/entity_attribute')
  97. ->getValidAttributeIds($ids);
  98. }
  99. if( $data['groups'] ) {
  100. foreach( $data['groups'] as $group ) {
  101. $modelGroup = Mage::getModel('eav/entity_attribute_group');
  102. $modelGroup->setId(is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null)
  103. ->setAttributeGroupName($group[1])
  104. ->setAttributeSetId($this->getId())
  105. ->setSortOrder($group[2]);
  106. if( $data['attributes'] ) {
  107. foreach( $data['attributes'] as $attribute ) {
  108. if( $attribute[1] == $group[0] && in_array($attribute[0], $attributeIds) ) {
  109. $modelAttribute = Mage::getModel('eav/entity_attribute');
  110. $modelAttribute->setId($attribute[0])
  111. ->setAttributeGroupId($attribute[1])
  112. ->setAttributeSetId($this->getId())
  113. ->setEntityTypeId($this->getEntityTypeId())
  114. ->setSortOrder($attribute[2]);
  115. $modelAttributeArray[] = $modelAttribute;
  116. }
  117. }
  118. $modelGroup->setAttributes($modelAttributeArray);
  119. $modelAttributeArray = array();
  120. }
  121. $modelGroupArray[] = $modelGroup;
  122. }
  123. $this->setGroups($modelGroupArray);
  124. }
  125. if( $data['not_attributes'] ) {
  126. $modelAttributeArray = array();
  127. foreach( $data['not_attributes'] as $attributeId ) {
  128. $modelAttribute = Mage::getModel('eav/entity_attribute');
  129. $modelAttribute->setEntityAttributeId($attributeId);
  130. $modelAttributeArray[] = $modelAttribute;
  131. }
  132. $this->setRemoveAttributes($modelAttributeArray);
  133. }
  134. if( $data['removeGroups'] ) {
  135. $modelGroupArray = array();
  136. foreach( $data['removeGroups'] as $groupId ) {
  137. $modelGroup = Mage::getModel('eav/entity_attribute_group');
  138. $modelGroup->setId($groupId);
  139. $modelGroupArray[] = $modelGroup;
  140. }
  141. $this->setRemoveGroups($modelGroupArray);
  142. }
  143. $this->setAttributeSetName($data['attribute_set_name'])
  144. ->setEntityTypeId($this->getEntityTypeId());
  145. }
  146. /**
  147. * Validate attribute set name
  148. *
  149. * @param string $name
  150. * @throws Mage_Core_Exception
  151. * @return bool
  152. */
  153. public function validate()
  154. {
  155. if (!$this->_getResource()->validate($this, $this->getAttributeSetName())) {
  156. Mage::throwException(
  157. Mage::helper('eav')->__('Attribute set with the "%s" name already exists.', $this->getAttributeSetName())
  158. );
  159. }
  160. return true;
  161. }
  162. /**
  163. * Add set info to attributes
  164. *
  165. * @param string|Mage_Eav_Model_Entity_Type $entityType
  166. * @param array $attributes
  167. * @param int $setId
  168. * @return Mage_Eav_Model_Entity_Attribute_Set
  169. */
  170. public function addSetInfo($entityType, array $attributes, $setId = null)
  171. {
  172. $attributeIds = array();
  173. $config = Mage::getSingleton('eav/config');
  174. $entityType = $config->getEntityType($entityType);
  175. foreach ($attributes as $attribute) {
  176. $attribute = $config->getAttribute($entityType, $attribute);
  177. if ($setId && is_array($attribute->getAttributeSetInfo($setId))) {
  178. continue;
  179. }
  180. if (!$attribute->getAttributeId()) {
  181. continue;
  182. }
  183. $attributeIds[] = $attribute->getAttributeId();
  184. }
  185. if ($attributeIds) {
  186. $setInfo = $this->_getResource()
  187. ->getSetInfo($attributeIds, $setId);
  188. foreach ($attributes as $attribute) {
  189. $attribute = $config->getAttribute($entityType, $attribute);
  190. if (!$attribute->getAttributeId()) {
  191. continue;
  192. }
  193. if (!in_array($attribute->getAttributeId(), $attributeIds)) {
  194. continue;
  195. }
  196. if (is_numeric($setId)) {
  197. $attributeSetInfo = $attribute->getAttributeSetInfo();
  198. if (!is_array($attributeSetInfo)) {
  199. $attributeSetInfo = array();
  200. }
  201. if (isset($setInfo[$attribute->getAttributeId()][$setId])) {
  202. $attributeSetInfo[$setId] = $setInfo[$attribute->getAttributeId()][$setId];
  203. }
  204. $attribute->setAttributeSetInfo($attributeSetInfo);
  205. }
  206. else {
  207. if (isset($setInfo[$attribute->getAttributeId()])) {
  208. $attribute->setAttributeSetInfo($setInfo[$attribute->getAttributeId()]);
  209. }
  210. else {
  211. $attribute->setAttributeSetInfo(array());
  212. }
  213. }
  214. }
  215. }
  216. return $this;
  217. }
  218. }