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_CatalogIndex
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: * Attribute index resource model
30: *
31: * @category Mage
32: * @package Mage_CatalogIndex
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_CatalogIndex_Model_Resource_Attribute extends Mage_CatalogIndex_Model_Resource_Abstract
36: {
37: /**
38: * Enter description here ...
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('catalogindex/eav', 'index_id');
44: }
45:
46: /**
47: * Enter description here ...
48: *
49: * @param unknown_type $attribute
50: * @param unknown_type $filter
51: * @param unknown_type $entityFilter
52: * @return unknown
53: */
54: public function getFilteredEntities($attribute, $filter, $entityFilter)
55: {
56: $select = $this->_getReadAdapter()->select();
57:
58: $select
59: ->from($this->getMainTable(), 'entity_id')
60: ->distinct(true)
61: ->where('entity_id in (?)', $entityFilter)
62: ->where('store_id = ?', $this->getStoreId())
63: ->where('attribute_id = ?', $attribute->getId())
64: ->where('value = ?', $filter);
65:
66: return $this->_getReadAdapter()->fetchCol($select);
67: }
68:
69: /**
70: * Enter description here ...
71: *
72: * @param unknown_type $attribute
73: * @param unknown_type $entitySelect
74: * @return unknown
75: */
76: public function getCount($attribute, $entitySelect)
77: {
78: $select = clone $entitySelect;
79: $select->reset(Zend_Db_Select::COLUMNS);
80: $select->reset(Zend_Db_Select::ORDER);
81: $select->reset(Zend_Db_Select::LIMIT_COUNT);
82: $select->reset(Zend_Db_Select::LIMIT_OFFSET);
83:
84: $fields = array('count'=>'COUNT(index.entity_id)', 'index.value');
85:
86: $select->columns($fields)
87: ->join(array('index'=>$this->getMainTable()), 'index.entity_id=e.entity_id', array())
88: ->where('index.store_id = ?', $this->getStoreId())
89: ->where('index.attribute_id = ?', $attribute->getId())
90: ->group('index.value');
91:
92: $select = $select->__toString();
93: // $alias = $this->_getReadAdapter()->quoteTableAs($this->getMainTable(), 'index');
94: $result = $this->_getReadAdapter()->fetchAll($select);
95:
96: $counts = array();
97: foreach ($result as $row) {
98: $counts[$row['value']] = $row['count'];
99: }
100: return $counts;
101: }
102:
103: /**
104: * Enter description here ...
105: *
106: * @param unknown_type $collection
107: * @param unknown_type $attribute
108: * @param unknown_type $value
109: * @return Mage_CatalogIndex_Model_Resource_Attribute
110: */
111: public function applyFilterToCollection($collection, $attribute, $value)
112: {
113: /**
114: * Will be used after SQL review
115: */
116: // if ($collection->isEnabledFlat()) {
117: // $collection->getSelect()->where("e.{$attribute->getAttributeCode()}=?", $value);
118: // return $this;
119: // }
120:
121: $alias = 'attr_index_'.$attribute->getId();
122: $collection->getSelect()->join(
123: array($alias => $this->getMainTable()),
124: $alias.'.entity_id=e.entity_id',
125: array()
126: )
127: ->where($alias.'.store_id = ?', $this->getStoreId())
128: ->where($alias.'.attribute_id = ?', $attribute->getId())
129: ->where($alias.'.value = ?', $value);
130: return $this;
131: }
132: }
133: