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: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52:
53: class Mage_CatalogSearch_Model_Advanced extends Mage_Core_Model_Abstract
54: {
55: 56: 57: 58: 59:
60: protected $_searchCriterias = array();
61:
62: 63: 64: 65: 66:
67: protected $_engine = null;
68:
69: 70: 71: 72:
73: protected function _construct()
74: {
75: $this->_getEngine();
76: $this->_init('catalogsearch/advanced');
77: }
78:
79: protected function _getEngine()
80: {
81: if ($this->_engine == null) {
82: $this->_engine = Mage::helper('catalogsearch')->getEngine();
83: }
84:
85: return $this->_engine;
86: }
87:
88: 89: 90: 91: 92:
93: protected function _getResource()
94: {
95: $resourceName = $this->_engine->getResourceName();
96: if ($resourceName) {
97: $this->_resourceName = $resourceName;
98: }
99: return parent::_getResource();
100: }
101:
102: 103: 104: 105: 106:
107: public function getAttributes()
108: {
109:
110: $attributes = $this->getData('attributes');
111: if (is_null($attributes)) {
112: $product = Mage::getModel('catalog/product');
113: $attributes = Mage::getResourceModel('catalog/product_attribute_collection')
114: ->addHasOptionsFilter()
115: ->addDisplayInAdvancedSearchFilter()
116: ->addStoreLabel(Mage::app()->getStore()->getId())
117: ->setOrder('main_table.attribute_id', 'asc')
118: ->load();
119: foreach ($attributes as $attribute) {
120: $attribute->setEntity($product->getResource());
121: }
122: $this->setData('attributes', $attributes);
123: }
124: return $attributes;
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134: 135:
136: protected function _prepareCondition($attribute, $value)
137: {
138: return $this->_getResource()->prepareCondition($attribute, $value, $this->getProductCollection());
139: }
140:
141: 142: 143: 144: 145: 146:
147: public function addFilters($values)
148: {
149: $attributes = $this->getAttributes();
150: $hasConditions = false;
151: $allConditions = array();
152:
153: foreach ($attributes as $attribute) {
154:
155: if (!isset($values[$attribute->getAttributeCode()])) {
156: continue;
157: }
158: $value = $values[$attribute->getAttributeCode()];
159:
160: if ($attribute->getAttributeCode() == 'price') {
161: $value['from'] = isset($value['from']) ? trim($value['from']) : '';
162: $value['to'] = isset($value['to']) ? trim($value['to']) : '';
163: if (is_numeric($value['from']) || is_numeric($value['to'])) {
164: if (!empty($value['currency'])) {
165: $rate = Mage::app()->getStore()->getBaseCurrency()->getRate($value['currency']);
166: } else {
167: $rate = 1;
168: }
169: if ($this->_getResource()->addRatedPriceFilter(
170: $this->getProductCollection(), $attribute, $value, $rate)
171: ) {
172: $hasConditions = true;
173: $this->_addSearchCriteria($attribute, $value);
174: }
175: }
176: } else if ($attribute->isIndexable()) {
177: if (!is_string($value) || strlen($value) != 0) {
178: if ($this->_getResource()->addIndexableAttributeModifiedFilter(
179: $this->getProductCollection(), $attribute, $value)) {
180: $hasConditions = true;
181: $this->_addSearchCriteria($attribute, $value);
182: }
183: }
184: } else {
185: $condition = $this->_prepareCondition($attribute, $value);
186: if ($condition === false) {
187: continue;
188: }
189:
190: $this->_addSearchCriteria($attribute, $value);
191:
192: $table = $attribute->getBackend()->getTable();
193: if ($attribute->getBackendType() == 'static'){
194: $attributeId = $attribute->getAttributeCode();
195: } else {
196: $attributeId = $attribute->getId();
197: }
198: $allConditions[$table][$attributeId] = $condition;
199: }
200: }
201: if ($allConditions) {
202: $this->getProductCollection()->addFieldsToFilter($allConditions);
203: } else if (!$hasConditions) {
204: Mage::throwException(Mage::helper('catalogsearch')->__('Please specify at least one search term.'));
205: }
206:
207: return $this;
208: }
209:
210: 211: 212: 213: 214: 215: 216:
217: protected function _addSearchCriteria($attribute, $value)
218: {
219: $name = $attribute->getStoreLabel();
220:
221: if (is_array($value)) {
222: if (isset($value['from']) && isset($value['to'])) {
223: if (!empty($value['from']) || !empty($value['to'])) {
224: if (isset($value['currency'])) {
225: $currencyModel = Mage::getModel('directory/currency')->load($value['currency']);
226: $from = $currencyModel->format($value['from'], array(), false);
227: $to = $currencyModel->format($value['to'], array(), false);
228: } else {
229: $currencyModel = null;
230: }
231:
232: if (strlen($value['from']) > 0 && strlen($value['to']) > 0) {
233:
234: $value = sprintf('%s - %s',
235: ($currencyModel ? $from : $value['from']), ($currencyModel ? $to : $value['to']));
236: } elseif (strlen($value['from']) > 0) {
237:
238: $value = Mage::helper('catalogsearch')->__('%s and greater', ($currencyModel ? $from : $value['from']));
239: } elseif (strlen($value['to']) > 0) {
240:
241: $value = Mage::helper('catalogsearch')->__('up to %s', ($currencyModel ? $to : $value['to']));
242: }
243: } else {
244: return $this;
245: }
246: }
247: }
248:
249: if (($attribute->getFrontendInput() == 'select' || $attribute->getFrontendInput() == 'multiselect')
250: && is_array($value)
251: ) {
252: foreach ($value as $key => $val){
253: $value[$key] = $attribute->getSource()->getOptionText($val);
254:
255: if (is_array($value[$key])) {
256: $value[$key] = $value[$key]['label'];
257: }
258: }
259: $value = implode(', ', $value);
260: } else if ($attribute->getFrontendInput() == 'select' || $attribute->getFrontendInput() == 'multiselect') {
261: $value = $attribute->getSource()->getOptionText($value);
262: if (is_array($value))
263: $value = $value['label'];
264: } else if ($attribute->getFrontendInput() == 'boolean') {
265: $value = $value == 1
266: ? Mage::helper('catalogsearch')->__('Yes')
267: : Mage::helper('catalogsearch')->__('No');
268: }
269:
270: $this->_searchCriterias[] = array('name' => $name, 'value' => $value);
271: return $this;
272: }
273:
274: 275: 276: 277: 278:
279: public function getSearchCriterias()
280: {
281: return $this->_searchCriterias;
282: }
283:
284: 285: 286: 287: 288:
289: public function getProductCollection(){
290: if (is_null($this->_productCollection)) {
291: $collection = $this->_engine->getAdvancedResultCollection();
292: $this->prepareProductCollection($collection);
293: if (!$collection) {
294: return $collection;
295: }
296: $this->_productCollection = $collection;
297: }
298:
299: return $this->_productCollection;
300: }
301:
302: 303: 304: 305: 306: 307:
308: public function prepareProductCollection($collection)
309: {
310: $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
311: ->setStore(Mage::app()->getStore())
312: ->addMinimalPrice()
313: ->addTaxPercents()
314: ->addStoreFilter();
315:
316: Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
317: Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
318: return $this;
319: }
320: }
321: