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 Indexer Abstract Resource Model
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: abstract class Mage_Catalog_Model_Resource_Product_Indexer_Abstract extends Mage_Index_Model_Resource_Abstract
36: {
37: /**
38: * Retrieve catalog_product attribute instance by attribute code
39: *
40: * @param string $attributeCode
41: * @return Mage_Catalog_Model_Resource_Eav_Attribute
42: */
43: protected function _getAttribute($attributeCode)
44: {
45: return Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
46: }
47:
48: /**
49: * Add attribute join condition to select and return Zend_Db_Expr
50: * attribute value definition
51: * If $condition is not empty apply limitation for select
52: *
53: * @param Varien_Db_Select $select
54: * @param string $attrCode the attribute code
55: * @param string|Zend_Db_Expr $entity the entity field or expression for condition
56: * @param string|Zend_Db_Expr $store the store field or expression for condition
57: * @param Zend_Db_Expr $condition the limitation condition
58: * @param bool $required if required or has condition used INNER join, else - LEFT
59: * @return Zend_Db_Expr the attribute value expression
60: */
61: protected function _addAttributeToSelect($select, $attrCode, $entity, $store, $condition = null, $required = false)
62: {
63: $attribute = $this->_getAttribute($attrCode);
64: $attributeId = $attribute->getAttributeId();
65: $attributeTable = $attribute->getBackend()->getTable();
66: $adapter = $this->_getReadAdapter();
67: $joinType = !is_null($condition) || $required ? 'join' : 'joinLeft';
68:
69: if ($attribute->isScopeGlobal()) {
70: $alias = 'ta_' . $attrCode;
71: $select->$joinType(
72: array($alias => $attributeTable),
73: "{$alias}.entity_id = {$entity} AND {$alias}.attribute_id = {$attributeId}"
74: . " AND {$alias}.store_id = 0",
75: array()
76: );
77: $expression = new Zend_Db_Expr("{$alias}.value");
78: } else {
79: $dAlias = 'tad_' . $attrCode;
80: $sAlias = 'tas_' . $attrCode;
81:
82: $select->$joinType(
83: array($dAlias => $attributeTable),
84: "{$dAlias}.entity_id = {$entity} AND {$dAlias}.attribute_id = {$attributeId}"
85: . " AND {$dAlias}.store_id = 0",
86: array()
87: );
88: $select->joinLeft(
89: array($sAlias => $attributeTable),
90: "{$sAlias}.entity_id = {$entity} AND {$sAlias}.attribute_id = {$attributeId}"
91: . " AND {$sAlias}.store_id = {$store}",
92: array()
93: );
94: $expression = $adapter->getCheckSql($adapter->getIfNullSql("{$sAlias}.value_id", -1) . ' > 0',
95: "{$sAlias}.value", "{$dAlias}.value");
96: }
97:
98: if (!is_null($condition)) {
99: $select->where("{$expression}{$condition}");
100: }
101:
102: return $expression;
103: }
104:
105: /**
106: * Add website data join to select
107: * If add default store join also limitation of only has default store website
108: * Joined table has aliases
109: * cw for website table,
110: * csg for store group table (joined by website default group)
111: * cs for store table (joined by website default store)
112: *
113: * @param Varien_Db_Select $select the select object
114: * @param bool $store add default store join
115: * @param string|Zend_Db_Expr $joinCondition the limitation for website_id
116: * @return Mage_Catalog_Model_Resource_Product_Indexer_Abstract
117: */
118: protected function _addWebsiteJoinToSelect($select, $store = true, $joinCondition = null)
119: {
120: if (!is_null($joinCondition)) {
121: $joinCondition = 'cw.website_id = ' . $joinCondition;
122: }
123:
124: $select->join(
125: array('cw' => $this->getTable('core/website')),
126: $joinCondition,
127: array()
128: );
129:
130: if ($store) {
131: $select->join(
132: array('csg' => $this->getTable('core/store_group')),
133: 'csg.group_id = cw.default_group_id',
134: array())
135: ->join(
136: array('cs' => $this->getTable('core/store')),
137: 'cs.store_id = csg.default_store_id',
138: array());
139: }
140:
141: return $this;
142: }
143:
144: /**
145: * Add join for catalog/product_website table
146: * Joined table has alias pw
147: *
148: * @param Varien_Db_Select $select the select object
149: * @param string|Zend_Db_Expr $website the limitation of website_id
150: * @param string|Zend_Db_Expr $product the limitation of product_id
151: * @return Mage_Catalog_Model_Resource_Product_Indexer_Abstract
152: */
153: protected function _addProductWebsiteJoinToSelect($select, $website, $product)
154: {
155: $select->join(
156: array('pw' => $this->getTable('catalog/product_website')),
157: "pw.product_id = {$product} AND pw.website_id = {$website}",
158: array()
159: );
160:
161: return $this;
162: }
163:
164: /**
165: * Retrieve product relations by children
166: *
167: * @param int|array $childIds
168: * @return array
169: */
170: public function getRelationsByChild($childIds)
171: {
172: $write = $this->_getWriteAdapter();
173: $select = $write->select()
174: ->from($this->getTable('catalog/product_relation'), 'parent_id')
175: ->where('child_id IN(?)', $childIds);
176:
177: return $write->fetchCol($select);
178: }
179:
180: /**
181: * Retrieve product relations by parents
182: *
183: * @param int|array $parentIds
184: * @return array
185: */
186: public function getRelationsByParent($parentIds)
187: {
188: if (!is_array($parentIds)) {
189: $parentIds = array($parentIds);
190: }
191:
192: $result = array();
193: if (!empty($parentIds)) {
194: $write = $this->_getWriteAdapter();
195: $select = $write->select()
196: ->from($this->getTable('catalog/product_relation'), 'child_id')
197: ->where('parent_id IN(?)', $parentIds);
198: $result = $write->fetchCol($select);
199: }
200:
201: return $result;
202: }
203: }
204: