1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_CatalogIndex_Model_Resource_Data_Abstract extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_attributeCodeIds = array();
43:
44: 45: 46: 47: 48:
49: protected $_linkSelect = null;
50:
51: 52: 53: 54: 55: 56:
57: protected function _setLinkSelect($select)
58: {
59: $this->_linkSelect = $select;
60: return $this;
61: }
62:
63: 64: 65: 66: 67:
68: protected function _getLinkSelect()
69: {
70: return $this->_linkSelect;
71: }
72:
73: 74: 75: 76:
77: protected function _construct()
78: {
79: $this->_init('catalog/product', 'entity_id');
80: }
81:
82: 83: 84: 85: 86: 87: 88: 89:
90: public function getAttributeData($products, $attributes, $store)
91: {
92: $suffixes = array('decimal', 'varchar', 'int', 'text', 'datetime');
93: if (!is_array($products)) {
94: $products = new Zend_Db_Expr($products);
95: }
96: $result = array();
97: foreach ($suffixes as $suffix) {
98: $tableName = "{$this->getTable('catalog/product')}_{$suffix}";
99: $condition = "product.entity_id = c.entity_id AND c.store_id = {$store} AND c.attribute_id = d.attribute_id";
100: $defaultCondition = "product.entity_id = d.entity_id AND d.store_id = 0";
101: $fields = array(
102: 'entity_id',
103: 'type_id',
104: 'attribute_id' => 'IF(c.value_id > 0, c.attribute_id, d.attribute_id)',
105: 'value' => 'IF(c.value_id > 0, c.value, d.value)'
106: );
107:
108: $select = $this->_getReadAdapter()->select()
109: ->from(array('product'=>$this->getTable('catalog/product')), $fields)
110: ->where('product.entity_id in (?)', $products)
111: ->joinRight(array('d'=>$tableName), $defaultCondition, array())
112: ->joinLeft(array('c'=>$tableName), $condition, array())
113: ->where('c.attribute_id IN (?) OR d.attribute_id IN (?)', $attributes);
114: $part = $this->_getReadAdapter()->fetchAll($select);
115:
116: if (is_array($part)) {
117: $result = array_merge($result, $part);
118: }
119: }
120:
121: return $result;
122: }
123:
124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134:
135: public function fetchLinkInformation($store, $table, $idField, $whereField, $id, $additionalWheres = array())
136: {
137: $idsConditionSymbol = "= ?";
138: if (is_array($id)) {
139: $idsConditionSymbol = "in (?)";
140: }
141:
142: $select = $this->_getReadAdapter()->select();
143: $select->from(array('l'=>$this->getTable($table)), array("l.{$idField}"))
144: ->where("l.{$whereField} {$idsConditionSymbol}", $id);
145: foreach ($additionalWheres as $field=>$condition) {
146: $select->where("l.$field = ?", $condition);
147: }
148:
149:
150: $this->_addAttributeFilter($select, 'status', 'l', $idField, $store,
151: Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
152:
153: if ($websiteId = Mage::app()->getStore($store)->getWebsiteId()) {
154: $select->join(
155: array('w' => $this->getTable('catalog/product_website')),
156: "l.{$idField}=w.product_id AND w.website_id={$websiteId}",
157: array()
158: );
159: }
160:
161: $this->_setLinkSelect($select);
162: $this->_prepareLinkFetchSelect($store, $table, $idField, $whereField, $id, $additionalWheres);
163:
164: return $this->_getWriteAdapter()->fetchCol($this->_getLinkSelect());
165: }
166:
167: 168: 169: 170: 171: 172: 173: 174: 175: 176:
177: protected function _prepareLinkFetchSelect($store, $table, $idField, $whereField, $id, $additionalWheres = array())
178: {
179:
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189:
190: public function getMinimalPrice($products, $priceAttributes, $store)
191: {
192: $website = Mage::app()->getStore($store)->getWebsiteId();
193:
194: $fields = array('customer_group_id', 'minimal_value'=>'MIN(value)');
195: $select = $this->_getReadAdapter()->select()
196: ->from(array('base'=>$this->getTable('catalogindex/price')), $fields)
197: ->where('base.entity_id in (?)', $products)
198: ->where('base.attribute_id in (?)', $priceAttributes)
199: ->where('base.website_id = ?', $website)
200: ->group('base.customer_group_id');
201: return $this->_getReadAdapter()->fetchAll($select);
202: }
203:
204: 205: 206: 207: 208: 209: 210:
211: public function getTierPrices($products, $website)
212: {
213: $fields = array(
214: 'entity_id',
215: 'type_id',
216: 'c.customer_group_id',
217: 'c.qty',
218: 'c.value',
219: 'c.all_groups',
220: );
221: $condition = "product.entity_id = c.entity_id";
222:
223: $select = $this->_getReadAdapter()->select()
224: ->from(array('product'=>$this->getTable('catalog/product')), $fields)
225: ->joinLeft(array('c'=>"{$this->getTable('catalog/product')}_tier_price"), $condition, array())
226: ->where('product.entity_id in (?)', $products);
227: if (Mage::helper('catalog')->isPriceGlobal())
228: {
229: $select->where('c.website_id=?', 0);
230: }
231: elseif (Mage::app()->getWebsite($website)->getBaseCurrencyCode() != Mage::app()->getBaseCurrencyCode()) {
232: $select->where('c.website_id=?', $website);
233: }
234: else {
235: $select->where('c.website_id IN(?)', array(0, $website));
236: }
237:
238: return $this->_getReadAdapter()->fetchAll($select);
239: }
240:
241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251:
252: protected function _addAttributeFilter(Varien_Db_Select $select, $attributeCode, $table, $field, $store, $value)
253: {
254: $adapter = $this->_getReadAdapter();
255: $attribute = Mage::getSingleton('eav/config')
256: ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
257:
258: $attributeTable = $attribute->getBackend()->getTable();
259: if ($attribute->getBackendType() == 'static') {
260: $tableAlias = sprintf('t_%s', $attribute->getAttributeCode());
261: $joinCond = join(' AND ', array(
262: sprintf('`%s`.`%s`=`%s`.`entity_id`', $table, $field, $tableAlias)
263: ));
264: $select
265: ->join(
266: array($tableAlias => $attributeTable),
267: $joinCond,
268: array())
269: ->where(sprintf('%s.%s IN(?)', $tableAlias, $attribute->getAttributeCode()), $value);
270: }
271: elseif ($attribute->isScopeGlobal()) {
272: $tableAlias = sprintf('t_%s', $attribute->getAttributeCode());
273: $joinCond = join(' AND ', array(
274: sprintf('`%s`.`%s`=`%s`.`entity_id`', $table, $field, $tableAlias),
275: $adapter->quoteInto(sprintf('`%s`.`attribute_id`=?', $tableAlias), $attribute->getAttributeId()),
276: $adapter->quoteInto(sprintf('`%s`.`store_id`=?', $tableAlias), 0)
277: ));
278: $select
279: ->join(
280: array($tableAlias => $attributeTable),
281: $joinCond,
282: array())
283: ->where(sprintf('%s.value IN(?)', $tableAlias), $value);
284: }
285: else {
286: $tableGlobal = sprintf('t_global_%s', $attribute->getAttributeCode());
287: $tableStore = sprintf('t_store_%s', $attribute->getAttributeCode());
288: $joinCondGlobal = join(' AND ', array(
289: sprintf('`%s`.`%s`=`%s`.`entity_id`', $table, $field, $tableGlobal),
290: $adapter->quoteInto(sprintf('`%s`.`attribute_id`=?', $tableGlobal), $attribute->getAttributeId()),
291: $adapter->quoteInto(sprintf('`%s`.`store_id`=?', $tableGlobal), 0)
292: ));
293: $joinCondStore = join(' AND ', array(
294: sprintf('`%s`.`entity_id`=`%s`.`entity_id`', $tableGlobal, $tableStore),
295: sprintf('`%s`.`attribute_id`=`%s`.`attribute_id`', $tableGlobal, $tableStore),
296: $adapter->quoteInto(sprintf('`%s`.`store_id`=?', $tableStore), $store)
297: ));
298: $whereCond = sprintf('IF(`%s`.`value_id`>0, `%s`.`value`, `%s`.`value`) IN(?)',
299: $tableStore, $tableStore, $tableGlobal);
300:
301: $select
302: ->join(
303: array($tableGlobal => $attributeTable),
304: $joinCondGlobal,
305: array())
306: ->joinLeft(
307: array($tableStore => $attributeTable),
308: $joinCondStore,
309: array())
310: ->where($whereCond, $value);
311: }
312:
313: return $this;
314: }
315: }
316: