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