PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Product/Link/Api.php

https://bitbucket.org/jokusafet/magento2
PHP | 349 lines | 179 code | 60 blank | 110 comment | 14 complexity | 0ba2071906759552d5c18e071f26e226 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_Catalog
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog product link api
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Link_Api extends Mage_Catalog_Model_Api_Resource
  34. {
  35. /**
  36. * Product link type mapping, used for references and validation
  37. *
  38. * @var array
  39. */
  40. protected $_typeMap = array(
  41. 'related' => Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED,
  42. 'up_sell' => Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL,
  43. 'cross_sell' => Mage_Catalog_Model_Product_Link::LINK_TYPE_CROSSSELL,
  44. 'grouped' => Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED
  45. );
  46. public function __construct()
  47. {
  48. $this->_storeIdSessionField = 'product_store_id';
  49. }
  50. /**
  51. * Retrieve product link associations
  52. *
  53. * @param string $type
  54. * @param int|sku $productId
  55. * @param string $identifierType
  56. * @return array
  57. */
  58. public function items($type, $productId, $identifierType = null)
  59. {
  60. $typeId = $this->_getTypeId($type);
  61. $product = $this->_initProduct($productId, $identifierType);
  62. $link = $product->getLinkInstance()
  63. ->setLinkTypeId($typeId);
  64. $collection = $this->_initCollection($link, $product);
  65. $result = array();
  66. foreach ($collection as $linkedProduct) {
  67. $row = array(
  68. 'product_id' => $linkedProduct->getId(),
  69. 'type' => $linkedProduct->getTypeId(),
  70. 'set' => $linkedProduct->getAttributeSetId(),
  71. 'sku' => $linkedProduct->getSku()
  72. );
  73. foreach ($link->getAttributes() as $attribute) {
  74. $row[$attribute['code']] = $linkedProduct->getData($attribute['code']);
  75. }
  76. $result[] = $row;
  77. }
  78. return $result;
  79. }
  80. /**
  81. * Add product link association
  82. *
  83. * @param string $type
  84. * @param int|string $productId
  85. * @param int|string $linkedProductId
  86. * @param array $data
  87. * @param string $identifierType
  88. * @return boolean
  89. */
  90. public function assign($type, $productId, $linkedProductId, $data = array(), $identifierType = null)
  91. {
  92. $typeId = $this->_getTypeId($type);
  93. $product = $this->_initProduct($productId, $identifierType);
  94. $link = $product->getLinkInstance()
  95. ->setLinkTypeId($typeId);
  96. $collection = $this->_initCollection($link, $product);
  97. $idBySku = $product->getIdBySku($linkedProductId);
  98. if ($idBySku) {
  99. $linkedProductId = $idBySku;
  100. }
  101. $links = $this->_collectionToEditableArray($collection);
  102. $links[(int)$linkedProductId] = array();
  103. foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
  104. if (isset($data[$attribute['code']])) {
  105. $links[(int)$linkedProductId][$attribute['code']] = $data[$attribute['code']];
  106. }
  107. }
  108. try {
  109. if ($type == 'grouped') {
  110. $link->getResource()->saveGroupedLinks($product, $links, $typeId);
  111. } else {
  112. $link->getResource()->saveProductLinks($product, $links, $typeId);
  113. }
  114. $_linkInstance = Mage::getSingleton('Mage_Catalog_Model_Product_Link');
  115. $_linkInstance->saveProductRelations($product);
  116. $indexerStock = Mage::getModel('Mage_CatalogInventory_Model_Stock_Status');
  117. $indexerStock->updateStatus($productId);
  118. $indexerPrice = Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Indexer_Price');
  119. $indexerPrice->reindexProductIds($productId);
  120. } catch (Exception $e) {
  121. $this->_fault('data_invalid', Mage::helper('Mage_Catalog_Helper_Data')->__('Link product does not exist.'));
  122. }
  123. return true;
  124. }
  125. /**
  126. * Update product link association info
  127. *
  128. * @param string $type
  129. * @param int|string $productId
  130. * @param int|string $linkedProductId
  131. * @param array $data
  132. * @param string $identifierType
  133. * @return boolean
  134. */
  135. public function update($type, $productId, $linkedProductId, $data = array(), $identifierType = null)
  136. {
  137. $typeId = $this->_getTypeId($type);
  138. $product = $this->_initProduct($productId, $identifierType);
  139. $link = $product->getLinkInstance()
  140. ->setLinkTypeId($typeId);
  141. $collection = $this->_initCollection($link, $product);
  142. $links = $this->_collectionToEditableArray($collection);
  143. $idBySku = $product->getIdBySku($linkedProductId);
  144. if ($idBySku) {
  145. $linkedProductId = $idBySku;
  146. }
  147. foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
  148. if (isset($data[$attribute['code']])) {
  149. $links[(int)$linkedProductId][$attribute['code']] = $data[$attribute['code']];
  150. }
  151. }
  152. try {
  153. if ($type == 'grouped') {
  154. $link->getResource()->saveGroupedLinks($product, $links, $typeId);
  155. } else {
  156. $link->getResource()->saveProductLinks($product, $links, $typeId);
  157. }
  158. $_linkInstance = Mage::getSingleton('Mage_Catalog_Model_Product_Link');
  159. $_linkInstance->saveProductRelations($product);
  160. $indexerStock = Mage::getModel('Mage_CatalogInventory_Model_Stock_Status');
  161. $indexerStock->updateStatus($productId);
  162. $indexerPrice = Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Indexer_Price');
  163. $indexerPrice->reindexProductIds($productId);
  164. } catch (Exception $e) {
  165. $this->_fault('data_invalid', Mage::helper('Mage_Catalog_Helper_Data')->__('Link product does not exist.'));
  166. }
  167. return true;
  168. }
  169. /**
  170. * Remove product link association
  171. *
  172. * @param string $type
  173. * @param int|string $productId
  174. * @param int|string $linkedProductId
  175. * @param string $identifierType
  176. * @return boolean
  177. */
  178. public function remove($type, $productId, $linkedProductId, $identifierType = null)
  179. {
  180. $typeId = $this->_getTypeId($type);
  181. $product = $this->_initProduct($productId, $identifierType);
  182. $link = $product->getLinkInstance()
  183. ->setLinkTypeId($typeId);
  184. $collection = $this->_initCollection($link, $product);
  185. $idBySku = $product->getIdBySku($linkedProductId);
  186. if ($idBySku) {
  187. $linkedProductId = $idBySku;
  188. }
  189. $links = $this->_collectionToEditableArray($collection);
  190. if (isset($links[$linkedProductId])) {
  191. unset($links[$linkedProductId]);
  192. }
  193. try {
  194. $link->getResource()->saveProductLinks($product, $links, $typeId);
  195. } catch (Exception $e) {
  196. $this->_fault('not_removed');
  197. }
  198. return true;
  199. }
  200. /**
  201. * Retrieve attribute list for specified type
  202. *
  203. * @param string $type
  204. * @return array
  205. */
  206. public function attributes($type)
  207. {
  208. $typeId = $this->_getTypeId($type);
  209. $attributes = Mage::getModel('Mage_Catalog_Model_Product_Link')
  210. ->getAttributes($typeId);
  211. $result = array();
  212. foreach ($attributes as $attribute) {
  213. $result[] = array(
  214. 'code' => $attribute['code'],
  215. 'type' => $attribute['type']
  216. );
  217. }
  218. return $result;
  219. }
  220. /**
  221. * Retrieve link types
  222. *
  223. * @return array
  224. */
  225. public function types()
  226. {
  227. return array_keys($this->_typeMap);
  228. }
  229. /**
  230. * Retrieve link type id by code
  231. *
  232. * @param string $type
  233. * @return int
  234. */
  235. protected function _getTypeId($type)
  236. {
  237. if (!isset($this->_typeMap[$type])) {
  238. $this->_fault('type_not_exists');
  239. }
  240. return $this->_typeMap[$type];
  241. }
  242. /**
  243. * Initialize and return product model
  244. *
  245. * @param int $productId
  246. * @param string $identifierType
  247. * @return Mage_Catalog_Model_Product
  248. */
  249. protected function _initProduct($productId, $identifierType = null)
  250. {
  251. $product = Mage::helper('Mage_Catalog_Helper_Product')->getProduct($productId, null, $identifierType);
  252. if (!$product->getId()) {
  253. $this->_fault('product_not_exists');
  254. }
  255. return $product;
  256. }
  257. /**
  258. * Initialize and return linked products collection
  259. *
  260. * @param Mage_Catalog_Model_Product_Link $link
  261. * @param Mage_Catalog_Model_Product $product
  262. * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
  263. */
  264. protected function _initCollection($link, $product)
  265. {
  266. $collection = $link
  267. ->getProductCollection()
  268. ->setIsStrongMode()
  269. ->setProduct($product);
  270. return $collection;
  271. }
  272. /**
  273. * Export collection to editable array
  274. *
  275. * @param Mage_Catalog_Model_Resource_Product_Link_Product_Collection $collection
  276. * @return array
  277. */
  278. protected function _collectionToEditableArray($collection)
  279. {
  280. $result = array();
  281. foreach ($collection as $linkedProduct) {
  282. $result[$linkedProduct->getId()] = array();
  283. foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
  284. $result[$linkedProduct->getId()][$attribute['code']] = $linkedProduct->getData($attribute['code']);
  285. }
  286. }
  287. return $result;
  288. }
  289. } // Class Mage_Catalog_Model_Product_Link_Api End