/app/code/core/Mage/Catalog/Model/Product/Attribute/Set/Api.php

https://bitbucket.org/kdms/sh-magento · PHP · 287 lines · 163 code · 15 blank · 109 comment · 18 complexity · 60d9d2e803ea41f8a0ba3ed5e3257bc8 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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_Catalog
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Catalog product attribute set api
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Attribute_Set_Api extends Mage_Api_Model_Resource_Abstract
  34. {
  35. /**
  36. * Retrieve attribute set list
  37. *
  38. * @return array
  39. */
  40. public function items()
  41. {
  42. $entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
  43. $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
  44. ->setEntityTypeFilter($entityType->getId());
  45. $result = array();
  46. foreach ($collection as $attributeSet) {
  47. $result[] = array(
  48. 'set_id' => $attributeSet->getId(),
  49. 'name' => $attributeSet->getAttributeSetName()
  50. );
  51. }
  52. return $result;
  53. }
  54. /**
  55. * Create new attribute set based on another set
  56. *
  57. * @param string $attributeSetName
  58. * @param string $skeletonSetId
  59. * @return integer
  60. */
  61. public function create($attributeSetName, $skeletonSetId)
  62. {
  63. // check if set with requested $skeletonSetId exists
  64. if (!Mage::getModel('eav/entity_attribute_set')->load($skeletonSetId)->getId()){
  65. $this->_fault('invalid_skeleton_set_id');
  66. }
  67. // get catalog product entity type id
  68. $entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
  69. /** @var $attributeSet Mage_Eav_Model_Entity_Attribute_Set */
  70. $attributeSet = Mage::getModel('eav/entity_attribute_set')
  71. ->setEntityTypeId($entityTypeId)
  72. ->setAttributeSetName($attributeSetName);
  73. try {
  74. // check if name is valid
  75. $attributeSet->validate();
  76. // copy parameters to new set from skeleton set
  77. $attributeSet->save();
  78. $attributeSet->initFromSkeleton($skeletonSetId)->save();
  79. } catch (Mage_Eav_Exception $e) {
  80. $this->_fault('invalid_data', $e->getMessage());
  81. } catch (Exception $e) {
  82. $this->_fault('create_attribute_set_error', $e->getMessage());
  83. }
  84. return (int)$attributeSet->getId();
  85. }
  86. /**
  87. * Remove attribute set
  88. *
  89. * @param string $attributeSetId
  90. * @param bool $forceProductsRemove
  91. * @return bool
  92. */
  93. public function remove($attributeSetId, $forceProductsRemove = false)
  94. {
  95. // if attribute set has related goods and $forceProductsRemove is not set throw exception
  96. if (!$forceProductsRemove) {
  97. /** @var $catalogProductsCollection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
  98. $catalogProductsCollection = Mage::getModel('catalog/product')->getCollection()
  99. ->addFieldToFilter('attribute_set_id', $attributeSetId);
  100. if (count($catalogProductsCollection)) {
  101. $this->_fault('attribute_set_has_related_products');
  102. }
  103. }
  104. $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($attributeSetId);
  105. // check if set with requested id exists
  106. if (!$attributeSet->getId()){
  107. $this->_fault('invalid_attribute_set_id');
  108. }
  109. try {
  110. $attributeSet->delete();
  111. } catch (Exception $e) {
  112. $this->_fault('remove_attribute_set_error', $e->getMessage());
  113. }
  114. return true;
  115. }
  116. /**
  117. * Add attribute to attribute set
  118. *
  119. * @param string $attributeId
  120. * @param string $attributeSetId
  121. * @param string|null $attributeGroupId
  122. * @param string $sortOrder
  123. * @return bool
  124. */
  125. public function attributeAdd($attributeId, $attributeSetId, $attributeGroupId = null, $sortOrder = '0')
  126. {
  127. // check if attribute with requested id exists
  128. /** @var $attribute Mage_Eav_Model_Entity_Attribute */
  129. $attribute = Mage::getModel('eav/entity_attribute')->load($attributeId);
  130. if (!$attribute->getId()) {
  131. $this->_fault('invalid_attribute_id');
  132. }
  133. // check if attribute set with requested id exists
  134. /** @var $attributeSet Mage_Eav_Model_Entity_Attribute_Set */
  135. $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($attributeSetId);
  136. if (!$attributeSet->getId()) {
  137. $this->_fault('invalid_attribute_set_id');
  138. }
  139. if (!empty($attributeGroupId)) {
  140. // check if attribute group with requested id exists
  141. if (!Mage::getModel('eav/entity_attribute_group')->load($attributeGroupId)->getId()) {
  142. $this->_fault('invalid_attribute_group_id');
  143. }
  144. } else {
  145. // define default attribute group id for current attribute set
  146. $attributeGroupId = $attributeSet->getDefaultGroupId();
  147. }
  148. $attribute->setAttributeSetId($attributeSet->getId())->loadEntityAttributeIdBySet();
  149. if ($attribute->getEntityAttributeId()) {
  150. $this->_fault('attribute_is_already_in_set');
  151. }
  152. try {
  153. $attribute->setEntityTypeId($attributeSet->getEntityTypeId())
  154. ->setAttributeSetId($attributeSetId)
  155. ->setAttributeGroupId($attributeGroupId)
  156. ->setSortOrder($sortOrder)
  157. ->save();
  158. } catch (Exception $e) {
  159. $this->_fault('add_attribute_error', $e->getMessage());
  160. }
  161. return true;
  162. }
  163. /**
  164. * Remove attribute from attribute set
  165. *
  166. * @param string $attributeId
  167. * @param string $attributeSetId
  168. * @return bool
  169. */
  170. public function attributeRemove($attributeId, $attributeSetId)
  171. {
  172. // check if attribute with requested id exists
  173. /** @var $attribute Mage_Eav_Model_Entity_Attribute */
  174. $attribute = Mage::getModel('eav/entity_attribute')->load($attributeId);
  175. if (!$attribute->getId()) {
  176. $this->_fault('invalid_attribute_id');
  177. }
  178. // check if attribute set with requested id exists
  179. /** @var $attributeSet Mage_Eav_Model_Entity_Attribute_Set */
  180. $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($attributeSetId);
  181. if (!$attributeSet->getId()) {
  182. $this->_fault('invalid_attribute_set_id');
  183. }
  184. // check if attribute is in set
  185. $attribute->setAttributeSetId($attributeSet->getId())->loadEntityAttributeIdBySet();
  186. if (!$attribute->getEntityAttributeId()) {
  187. $this->_fault('attribute_is_not_in_set');
  188. }
  189. try {
  190. // delete record from eav_entity_attribute
  191. // using entity_attribute_id loaded by loadEntityAttributeIdBySet()
  192. $attribute->deleteEntity();
  193. } catch (Exception $e) {
  194. $this->_fault('remove_attribute_error', $e->getMessage());
  195. }
  196. return true;
  197. }
  198. /**
  199. * Create group within existing attribute set
  200. *
  201. * @param string|int $attributeSetId
  202. * @param string $groupName
  203. * @return int
  204. */
  205. public function groupAdd($attributeSetId, $groupName)
  206. {
  207. /** @var $group Mage_Eav_Model_Entity_Attribute_Group */
  208. $group = Mage::getModel('eav/entity_attribute_group');
  209. $group->setAttributeSetId($attributeSetId)
  210. ->setAttributeGroupName(
  211. Mage::helper('catalog')->stripTags($groupName)
  212. );
  213. if ($group->itemExists()) {
  214. $this->_fault('group_already_exists');
  215. }
  216. try {
  217. $group->save();
  218. } catch (Exception $e) {
  219. $this->_fault('group_add_error', $e->getMessage());
  220. }
  221. return (int)$group->getId();
  222. }
  223. /**
  224. * Rename existing group
  225. *
  226. * @param string|int $groupId
  227. * @param string $groupName
  228. * @return boolean
  229. */
  230. public function groupRename($groupId, $groupName)
  231. {
  232. $model = Mage::getModel('eav/entity_attribute_group')->load($groupId);
  233. if (!$model->getAttributeGroupName()) {
  234. $this->_fault('invalid_attribute_group_id');
  235. }
  236. $model->setAttributeGroupName(
  237. Mage::helper('catalog')->stripTags($groupName)
  238. );
  239. try {
  240. $model->save();
  241. } catch (Exception $e) {
  242. $this->_fault('group_rename_error', $e->getMessage());
  243. }
  244. return true;
  245. }
  246. /**
  247. * Remove group from existing attribute set
  248. *
  249. * @param string|int $attributeGroupId
  250. * @return bool
  251. */
  252. public function groupRemove($attributeGroupId)
  253. {
  254. /** @var $group Mage_Catalog_Model_Product_Attribute_Group */
  255. $group = Mage::getModel('catalog/product_attribute_group')->load($attributeGroupId);
  256. if (!$group->getId()) {
  257. $this->_fault('invalid_attribute_group_id');
  258. }
  259. if ($group->hasConfigurableAttributes()) {
  260. $this->_fault('group_has_configurable_attributes');
  261. }
  262. if ($group->hasSystemAttributes()) {
  263. $this->_fault('group_has_system_attributes');
  264. }
  265. try {
  266. $group->delete();
  267. } catch (Exception $e) {
  268. $this->_fault('group_remove_error', $e->getMessage());
  269. }
  270. return true;
  271. }
  272. } // Class Mage_Catalog_Model_Product_Attribute_Set_Api End