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 links collection
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Catalog_Model_Resource_Product_Link_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Product object
39: *
40: * @var Mage_Catalog_Model_Product
41: */
42: protected $_product;
43:
44: /**
45: * Product Link model class
46: *
47: * @var Mage_Catalog_Model_Product_Link
48: */
49: protected $_linkModel;
50:
51: /**
52: * Product Link Type identifier
53: *
54: * @var Mage_Catalog_Model_Product_Type
55: */
56: protected $_linkTypeId;
57:
58: /**
59: * Resource initialization
60: */
61: protected function _construct()
62: {
63: $this->_init('catalog/product_link');
64: }
65:
66: /**
67: * Declare link model and initialize type attributes join
68: *
69: * @param Mage_Catalog_Model_Product_Link $linkModel
70: * @return Mage_Catalog_Model_Resource_Product_Link_Collection
71: */
72: public function setLinkModel(Mage_Catalog_Model_Product_Link $linkModel)
73: {
74: $this->_linkModel = $linkModel;
75: if ($linkModel->hasLinkTypeId()) {
76: $this->_linkTypeId = $linkModel->getLinkTypeId();
77: }
78: return $this;
79: }
80:
81: /**
82: * Retrieve collection link model
83: *
84: * @return Mage_Catalog_Model_Product_Link
85: */
86: public function getLinkModel()
87: {
88: return $this->_linkModel;
89: }
90:
91: /**
92: * Initialize collection parent product and add limitation join
93: *
94: * @param Mage_Catalog_Model_Product $product
95: * @return Mage_Catalog_Model_Resource_Product_Link_Collection
96: */
97: public function setProduct(Mage_Catalog_Model_Product $product)
98: {
99: $this->_product = $product;
100: return $this;
101: }
102:
103: /**
104: * Retrieve collection base product object
105: *
106: * @return Mage_Catalog_Model_Product
107: */
108: public function getProduct()
109: {
110: return $this->_product;
111: }
112:
113: /**
114: * Add link's type to filter
115: *
116: * @return Mage_Catalog_Model_Resource_Product_Link_Collection
117: */
118: public function addLinkTypeIdFilter()
119: {
120: if ($this->_linkTypeId) {
121: $this->addFieldToFilter('link_type_id', array('eq' => $this->_linkTypeId));
122: }
123: return $this;
124: }
125:
126: /**
127: * Add product to filter
128: *
129: * @return Mage_Catalog_Model_Resource_Product_Link_Collection
130: */
131: public function addProductIdFilter()
132: {
133: if ($this->getProduct() && $this->getProduct()->getId()) {
134: $this->addFieldToFilter('product_id', array('eq' => $this->getProduct()->getId()));
135: }
136: return $this;
137: }
138:
139: /**
140: * Join attributes
141: *
142: * @return Mage_Catalog_Model_Resource_Product_Link_Collection
143: */
144: public function joinAttributes()
145: {
146: if (!$this->getLinkModel()) {
147: return $this;
148: }
149: $attributes = $this->getLinkModel()->getAttributes();
150: $adapter = $this->getConnection();
151: foreach ($attributes as $attribute) {
152: $table = $this->getLinkModel()->getAttributeTypeTable($attribute['type']);
153: $alias = sprintf('link_attribute_%s_%s', $attribute['code'], $attribute['type']);
154:
155: $aliasInCondition = $adapter->quoteColumnAs($alias, null);
156: $this->getSelect()->joinLeft(
157: array($alias => $table),
158: $aliasInCondition . '.link_id = main_table.link_id AND '
159: . $aliasInCondition . '.product_link_attribute_id = ' . (int) $attribute['id'],
160: array($attribute['code'] => 'value')
161: );
162: }
163:
164: return $this;
165: }
166: }
167: