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_CatalogSearch
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: * Collection Advanced
30: *
31: * @category Mage
32: * @package Mage_CatalogSearch
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_CatalogSearch_Model_Resource_Advanced_Collection extends Mage_Catalog_Model_Resource_Product_Collection
36: {
37: /**
38: * Add not indexable fields to search
39: *
40: * @param array $fields
41: * @return Mage_CatalogSearch_Model_Resource_Advanced_Collection
42: */
43: public function addFieldsToFilter($fields)
44: {
45: if ($fields) {
46: $previousSelect = null;
47: $conn = $this->getConnection();
48: foreach ($fields as $table => $conditions) {
49: foreach ($conditions as $attributeId => $conditionValue) {
50: $select = $conn->select();
51: $select->from(array('t1' => $table), 'entity_id');
52: $conditionData = array();
53:
54: if (!is_numeric($attributeId)) {
55: $field = 't1.'.$attributeId;
56: }
57: else {
58: $storeId = $this->getStoreId();
59: $onCondition = 't1.entity_id = t2.entity_id'
60: . ' AND t1.attribute_id = t2.attribute_id'
61: . ' AND t2.store_id=?';
62:
63: $select->joinLeft(
64: array('t2' => $table),
65: $conn->quoteInto($onCondition, $storeId),
66: array()
67: );
68: $select->where('t1.store_id = ?', 0);
69: $select->where('t1.attribute_id = ?', $attributeId);
70:
71: if (array_key_exists('price_index', $this->getSelect()->getPart(Varien_Db_Select::FROM))) {
72: $select->where('t1.entity_id = price_index.entity_id');
73: }
74:
75: $field = $this->getConnection()->getCheckSql('t2.value_id>0', 't2.value', 't1.value');
76:
77: }
78:
79: if (is_array($conditionValue)) {
80: if (isset($conditionValue['in'])){
81: $conditionData[] = array('in' => $conditionValue['in']);
82: }
83: elseif (isset($conditionValue['in_set'])) {
84: $conditionParts = array();
85: foreach ($conditionValue['in_set'] as $value) {
86: $conditionParts[] = array('finset' => $value);
87: }
88: $conditionData[] = $conditionParts;
89: }
90: elseif (isset($conditionValue['like'])) {
91: $conditionData[] = array ('like' => $conditionValue['like']);
92: }
93: elseif (isset($conditionValue['from']) && isset($conditionValue['to'])) {
94: $invalidDateMessage = Mage::helper('catalogsearch')->__('Specified date is invalid.');
95: if ($conditionValue['from']) {
96: if (!Zend_Date::isDate($conditionValue['from'])) {
97: Mage::throwException($invalidDateMessage);
98: }
99: if (!is_numeric($conditionValue['from'])){
100: $conditionValue['from'] = Mage::getSingleton('core/date')
101: ->gmtDate(null, $conditionValue['from']);
102: if (!$conditionValue['from']) {
103: $conditionValue['from'] = Mage::getSingleton('core/date')->gmtDate();
104: }
105: }
106: $conditionData[] = array('gteq' => $conditionValue['from']);
107: }
108: if ($conditionValue['to']) {
109: if (!Zend_Date::isDate($conditionValue['to'])) {
110: Mage::throwException($invalidDateMessage);
111: }
112: if (!is_numeric($conditionValue['to'])){
113: $conditionValue['to'] = Mage::getSingleton('core/date')
114: ->gmtDate(null, $conditionValue['to']);
115: if (!$conditionValue['to']) {
116: $conditionValue['to'] = Mage::getSingleton('core/date')->gmtDate();
117: }
118: }
119: $conditionData[] = array('lteq' => $conditionValue['to']);
120: }
121:
122: }
123: } else {
124: $conditionData[] = array('eq' => $conditionValue);
125: }
126:
127:
128: foreach ($conditionData as $data) {
129: $select->where($conn->prepareSqlCondition($field, $data));
130: }
131:
132: if (!is_null($previousSelect)) {
133: $select->where('t1.entity_id IN (?)', new Zend_Db_Expr($previousSelect));
134: }
135: $previousSelect = $select;
136: }
137: }
138: $this->addFieldToFilter('entity_id', array('in' => new Zend_Db_Expr($select)));
139: }
140:
141: return $this;
142: }
143: }
144: