PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/kdms/sh-magento
PHP | 164 lines | 80 code | 14 blank | 70 comment | 5 complexity | 6b80fa0ea59a6f237da0477da5d9970d 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 model
  28. *
  29. * @method Mage_Catalog_Model_Resource_Product_Link _getResource()
  30. * @method Mage_Catalog_Model_Resource_Product_Link getResource()
  31. * @method int getProductId()
  32. * @method Mage_Catalog_Model_Product_Link setProductId(int $value)
  33. * @method int getLinkedProductId()
  34. * @method Mage_Catalog_Model_Product_Link setLinkedProductId(int $value)
  35. * @method int getLinkTypeId()
  36. * @method Mage_Catalog_Model_Product_Link setLinkTypeId(int $value)
  37. *
  38. * @category Mage
  39. * @package Mage_Catalog
  40. * @author Magento Core Team <core@magentocommerce.com>
  41. */
  42. class Mage_Catalog_Model_Product_Link extends Mage_Core_Model_Abstract
  43. {
  44. const LINK_TYPE_RELATED = 1;
  45. const LINK_TYPE_GROUPED = 3;
  46. const LINK_TYPE_UPSELL = 4;
  47. const LINK_TYPE_CROSSSELL = 5;
  48. protected $_attributeCollection = null;
  49. /**
  50. * Initialize resource
  51. */
  52. protected function _construct()
  53. {
  54. $this->_init('catalog/product_link');
  55. }
  56. public function useRelatedLinks()
  57. {
  58. $this->setLinkTypeId(self::LINK_TYPE_RELATED);
  59. return $this;
  60. }
  61. public function useGroupedLinks()
  62. {
  63. $this->setLinkTypeId(self::LINK_TYPE_GROUPED);
  64. return $this;
  65. }
  66. public function useUpSellLinks()
  67. {
  68. $this->setLinkTypeId(self::LINK_TYPE_UPSELL);
  69. return $this;
  70. }
  71. /**
  72. * @return Mage_Catalog_Model_Product_Link
  73. */
  74. public function useCrossSellLinks()
  75. {
  76. $this->setLinkTypeId(self::LINK_TYPE_CROSSSELL);
  77. return $this;
  78. }
  79. /**
  80. * Retrieve table name for attribute type
  81. *
  82. * @param string $type
  83. * @return string
  84. */
  85. public function getAttributeTypeTable($type)
  86. {
  87. return $this->_getResource()->getAttributeTypeTable($type);
  88. }
  89. /**
  90. * Retrieve linked product collection
  91. */
  92. public function getProductCollection()
  93. {
  94. $collection = Mage::getResourceModel('catalog/product_link_product_collection')
  95. ->setLinkModel($this);
  96. return $collection;
  97. }
  98. /**
  99. * Retrieve link collection
  100. */
  101. public function getLinkCollection()
  102. {
  103. $collection = Mage::getResourceModel('catalog/product_link_collection')
  104. ->setLinkModel($this);
  105. return $collection;
  106. }
  107. public function getAttributes($type=null)
  108. {
  109. if (is_null($type)) {
  110. $type = $this->getLinkTypeId();
  111. }
  112. return $this->_getResource()->getAttributesByType($type);
  113. }
  114. /**
  115. * Save data for product relations
  116. *
  117. * @param Mage_Catalog_Model_Product $product
  118. * @return Mage_Catalog_Model_Product_Link
  119. */
  120. public function saveProductRelations($product)
  121. {
  122. $data = $product->getRelatedLinkData();
  123. if (!is_null($data)) {
  124. $this->_getResource()->saveProductLinks($product, $data, self::LINK_TYPE_RELATED);
  125. }
  126. $data = $product->getUpSellLinkData();
  127. if (!is_null($data)) {
  128. $this->_getResource()->saveProductLinks($product, $data, self::LINK_TYPE_UPSELL);
  129. }
  130. $data = $product->getCrossSellLinkData();
  131. if (!is_null($data)) {
  132. $this->_getResource()->saveProductLinks($product, $data, self::LINK_TYPE_CROSSSELL);
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Save grouped product relation links
  138. *
  139. * @param Mage_Catalog_Model_Product $product
  140. * @return Mage_Catalog_Model_Product_Link
  141. */
  142. public function saveGroupedLinks($product)
  143. {
  144. $data = $product->getGroupedLinkData();
  145. if (!is_null($data)) {
  146. $this->_getResource()->saveGroupedLinks($product, $data, self::LINK_TYPE_GROUPED);
  147. }
  148. return $this;
  149. }
  150. }