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_CatalogSearch_Model_Resource_Advanced extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('catalog/product', 'entity_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: protected function _dispatchPreparePriceEvent($select)
54: {
55:
56: $response = new Varien_Object();
57: $response->setAdditionalCalculations(array());
58:
59:
60: $eventArgs = array(
61: 'select' => $select,
62: 'table' => 'price_index',
63: 'store_id' => Mage::app()->getStore()->getId(),
64: 'response_object' => $response
65: );
66:
67: Mage::dispatchEvent('catalog_prepare_price_select', $eventArgs);
68:
69: return $response;
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79:
80: public function prepareCondition($attribute, $value, $collection)
81: {
82: $condition = false;
83:
84: if (is_array($value)) {
85: if (!empty($value['from']) || !empty($value['to'])) {
86: $condition = $value;
87: } else if ($attribute->getBackendType() == 'varchar') {
88: $condition = array('in_set' => $value);
89: } else if (!isset($value['from']) && !isset($value['to'])) {
90: $condition = array('in' => $value);
91: }
92: } else {
93: if (strlen($value) > 0) {
94: if (in_array($attribute->getBackendType(), array('varchar', 'text', 'static'))) {
95: $condition = array('like' => '%' . $value . '%');
96: } else {
97: $condition = $value;
98: }
99: }
100: }
101:
102: return $condition;
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113:
114: public function addRatedPriceFilter($collection, $attribute, $value, $rate = 1)
115: {
116: $adapter = $this->_getReadAdapter();
117:
118: $conditions = array();
119: if (strlen($value['from']) > 0) {
120: $conditions[] = $adapter->quoteInto(
121: 'price_index.min_price %s * %s >= ?', $value['from'], Zend_Db::FLOAT_TYPE);
122: }
123: if (strlen($value['to']) > 0) {
124: $conditions[] = $adapter->quoteInto(
125: 'price_index.min_price %s * %s <= ?', $value['to'], Zend_Db::FLOAT_TYPE);
126: }
127:
128: if (!$conditions) {
129: return false;
130: }
131:
132: $collection->addPriceData();
133: $select = $collection->getSelect();
134: $response = $this->_dispatchPreparePriceEvent($select);
135: $additional = join('', $response->getAdditionalCalculations());
136:
137: foreach ($conditions as $condition) {
138: $select->where(sprintf($condition, $additional, $rate));
139: }
140:
141: return true;
142: }
143:
144: 145: 146: 147: 148: 149: 150: 151:
152: public function addIndexableAttributeModifiedFilter($collection, $attribute, $value)
153: {
154: if ($attribute->getIndexType() == 'decimal') {
155: $table = $this->getTable('catalog/product_index_eav_decimal');
156: } else {
157: $table = $this->getTable('catalog/product_index_eav');
158: }
159:
160: $tableAlias = 'a_' . $attribute->getAttributeId();
161: $storeId = Mage::app()->getStore()->getId();
162: $select = $collection->getSelect();
163:
164: if (is_array($value)) {
165: if (isset($value['from']) && isset($value['to'])) {
166: if (empty($value['from']) && empty($value['to'])) {
167: return false;
168: }
169: }
170: }
171:
172: $select->distinct(true);
173: $select->join(
174: array($tableAlias => $table),
175: "e.entity_id={$tableAlias}.entity_id "
176: . " AND {$tableAlias}.attribute_id={$attribute->getAttributeId()}"
177: . " AND {$tableAlias}.store_id={$storeId}",
178: array()
179: );
180:
181: if (is_array($value) && (isset($value['from']) || isset($value['to']))) {
182: if (isset($value['from']) && !empty($value['from'])) {
183: $select->where("{$tableAlias}.value >= ?", $value['from']);
184: }
185: if (isset($value['to']) && !empty($value['to'])) {
186: $select->where("{$tableAlias}.value <= ?", $value['to']);
187: }
188: return true;
189: }
190:
191: $select->where("{$tableAlias}.value IN(?)", $value);
192:
193: return true;
194: }
195: }
196: