PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/kdms/sh-magento
PHP | 279 lines | 160 code | 25 blank | 94 comment | 15 complexity | 8e2930cd833d32bdbb4f685f19c778d0 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 link resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Resource_Product_Link extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Product Link Attributes Table
  37. *
  38. * @var string
  39. */
  40. protected $_attributesTable;
  41. /**
  42. * Define main table name and attributes table
  43. */
  44. protected function _construct()
  45. {
  46. $this->_init('catalog/product_link', 'link_id');
  47. $this->_attributesTable = $this->getTable('catalog/product_link_attribute');
  48. }
  49. /**
  50. * Save Product Links process
  51. *
  52. * @param Mage_Catalog_Model_Product $product
  53. * @param array $data
  54. * @param int $typeId
  55. * @return Mage_Catalog_Model_Resource_Product_Link
  56. */
  57. public function saveProductLinks($product, $data, $typeId)
  58. {
  59. if (!is_array($data)) {
  60. $data = array();
  61. }
  62. $attributes = $this->getAttributesByType($typeId);
  63. $adapter = $this->_getWriteAdapter();
  64. $bind = array(
  65. ':product_id' => (int)$product->getId(),
  66. ':link_type_id' => (int)$typeId
  67. );
  68. $select = $adapter->select()
  69. ->from($this->getMainTable(), array('linked_product_id', 'link_id'))
  70. ->where('product_id = :product_id')
  71. ->where('link_type_id = :link_type_id');
  72. $links = $adapter->fetchPairs($select, $bind);
  73. $deleteIds = array();
  74. foreach($links as $linkedProductId => $linkId) {
  75. if (!isset($data[$linkedProductId])) {
  76. $deleteIds[] = (int)$linkId;
  77. }
  78. }
  79. if (!empty($deleteIds)) {
  80. $adapter->delete($this->getMainTable(), array(
  81. 'link_id IN (?)' => $deleteIds,
  82. ));
  83. }
  84. foreach ($data as $linkedProductId => $linkInfo) {
  85. $linkId = null;
  86. if (isset($links[$linkedProductId])) {
  87. $linkId = $links[$linkedProductId];
  88. unset($links[$linkedProductId]);
  89. } else {
  90. $bind = array(
  91. 'product_id' => $product->getId(),
  92. 'linked_product_id' => $linkedProductId,
  93. 'link_type_id' => $typeId
  94. );
  95. $adapter->insert($this->getMainTable(), $bind);
  96. $linkId = $adapter->lastInsertId($this->getMainTable());
  97. }
  98. foreach ($attributes as $attributeInfo) {
  99. $attributeTable = $this->getAttributeTypeTable($attributeInfo['type']);
  100. if ($attributeTable) {
  101. if (isset($linkInfo[$attributeInfo['code']])) {
  102. $value = $this->_prepareAttributeValue($attributeInfo['type'],
  103. $linkInfo[$attributeInfo['code']]);
  104. $bind = array(
  105. 'product_link_attribute_id' => $attributeInfo['id'],
  106. 'link_id' => $linkId,
  107. 'value' => $value
  108. );
  109. $adapter->insertOnDuplicate($attributeTable, $bind, array('value'));
  110. } else {
  111. $adapter->delete($attributeTable, array(
  112. 'link_id = ?' => $linkId,
  113. 'product_link_attribute_id = ?' => $attributeInfo['id']
  114. ));
  115. }
  116. }
  117. }
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Prepare link attribute value by attribute type
  123. *
  124. * @param string $type
  125. * @param mixed $value
  126. * @return mixed
  127. */
  128. protected function _prepareAttributeValue($type, $value)
  129. {
  130. if ($type == 'int') {
  131. $value = (int)$value;
  132. } elseif ($type == 'decimal') {
  133. $value = (float)sprintf('%F', $value);
  134. }
  135. return $value;
  136. }
  137. /**
  138. * Retrieve product link attributes by link type
  139. *
  140. * @param int $typeId
  141. * @return array
  142. */
  143. public function getAttributesByType($typeId)
  144. {
  145. $adapter = $this->_getReadAdapter();
  146. $select = $adapter->select()
  147. ->from($this->_attributesTable, array(
  148. 'id' => 'product_link_attribute_id',
  149. 'code' => 'product_link_attribute_code',
  150. 'type' => 'data_type'
  151. ))
  152. ->where('link_type_id = ?', $typeId);
  153. return $adapter->fetchAll($select);
  154. }
  155. /**
  156. * Returns table for link attribute by attribute type
  157. *
  158. * @param string $type
  159. * @return string
  160. */
  161. public function getAttributeTypeTable($type)
  162. {
  163. return $this->getTable('catalog/product_link_attribute_' . $type);
  164. }
  165. /**
  166. * Retrieve Required children ids
  167. * Return grouped array, ex array(
  168. * group => array(ids)
  169. * )
  170. *
  171. * @param int $parentId
  172. * @param int $typeId
  173. * @return array
  174. */
  175. public function getChildrenIds($parentId, $typeId)
  176. {
  177. $adapter = $this->_getReadAdapter();
  178. $childrenIds = array();
  179. $bind = array(
  180. ':product_id' => (int)$parentId,
  181. ':link_type_id' => (int)$typeId
  182. );
  183. $select = $adapter->select()
  184. ->from(array('l' => $this->getMainTable()), array('linked_product_id'))
  185. ->where('product_id = :product_id')
  186. ->where('link_type_id = :link_type_id');
  187. if ($typeId == Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED) {
  188. $select->join(
  189. array('e' => $this->getTable('catalog/product')),
  190. 'e.entity_id = l.linked_product_id AND e.required_options = 0',
  191. array()
  192. );
  193. }
  194. $childrenIds[$typeId] = array();
  195. $result = $adapter->fetchAll($select, $bind);
  196. foreach ($result as $row) {
  197. $childrenIds[$typeId][$row['linked_product_id']] = $row['linked_product_id'];
  198. }
  199. return $childrenIds;
  200. }
  201. /**
  202. * Retrieve parent ids array by required child
  203. *
  204. * @param int|array $childId
  205. * @param int $typeId
  206. * @return array
  207. */
  208. public function getParentIdsByChild($childId, $typeId)
  209. {
  210. $parentIds = array();
  211. $adapter = $this->_getReadAdapter();
  212. $select = $adapter->select()
  213. ->from($this->getMainTable(), array('product_id', 'linked_product_id'))
  214. ->where('linked_product_id IN(?)', $childId)
  215. ->where('link_type_id = ?', $typeId);
  216. $result = $adapter->fetchAll($select);
  217. foreach ($result as $row) {
  218. $parentIds[] = $row['product_id'];
  219. }
  220. return $parentIds;
  221. }
  222. /**
  223. * Save grouped product relations
  224. *
  225. * @param Mage_Catalog_Model_Product $product
  226. * @param array $data
  227. * @param int $typeId
  228. * @return Mage_Catalog_Model_Resource_Product_Link
  229. */
  230. public function saveGroupedLinks($product, $data, $typeId)
  231. {
  232. $adapter = $this->_getWriteAdapter();
  233. // check for change relations
  234. $bind = array(
  235. 'product_id' => (int)$product->getId(),
  236. 'link_type_id' => (int)$typeId
  237. );
  238. $select = $adapter->select()
  239. ->from($this->getMainTable(), array('linked_product_id'))
  240. ->where('product_id = :product_id')
  241. ->where('link_type_id = :link_type_id');
  242. $old = $adapter->fetchCol($select, $bind);
  243. $new = array_keys($data);
  244. if (array_diff($old, $new) || array_diff($new, $old)) {
  245. $product->setIsRelationsChanged(true);
  246. }
  247. // save product links attributes
  248. $this->saveProductLinks($product, $data, $typeId);
  249. // Grouped product relations should be added to relation table
  250. Mage::getResourceSingleton('catalog/product_relation')
  251. ->processRelations($product->getId(), $new);
  252. return $this;
  253. }
  254. }