00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_CatalogSearch_Model_Advanced extends Varien_Object
00035 {
00036
00037
00038
00039
00040
00041 private $_searchCriterias = array();
00042
00043 public function getAttributes()
00044 {
00045 $attributes = $this->getData('attributes');
00046 if (is_null($attributes)) {
00047 $product = Mage::getModel('catalog/product');
00048 $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
00049 ->setEntityTypeFilter($product->getResource()->getTypeId())
00050
00051 ->addHasOptionsFilter()
00052 ->addDisplayInAdvancedSearchFilter()
00053 ->setOrder('attribute_id', 'asc')
00054 ->load();
00055 foreach ($attributes as $attribute) {
00056 $attribute->setEntity($product->getResource());
00057 }
00058 $this->setData('attributes', $attributes);
00059 }
00060 return $attributes;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069 public function addFilters($values)
00070 {
00071 $attributes = $this->getAttributes();
00072 $allConditions = array();
00073 $filteredAttributes = array();
00074 $indexFilters = Mage::getModel('catalogindex/indexer')->buildEntityFilter(
00075 $attributes,
00076 $values,
00077 $filteredAttributes,
00078 $this->getProductCollection()
00079 );
00080
00081 foreach ($indexFilters as $filter) {
00082 $this->getProductCollection()->addFieldToFilter('entity_id', array('in'=>new Zend_Db_Expr($filter)));
00083 }
00084
00085 $priceFilters = Mage::getModel('catalogindex/indexer')->buildEntityPriceFilter(
00086 $attributes,
00087 $values,
00088 $filteredAttributes,
00089 $this->getProductCollection()
00090 );
00091
00092 foreach ($priceFilters as $code=>$filter) {
00093 $this->getProductCollection()->getSelect()->joinInner(
00094 array("_price_filter_{$code}"=>$filter),
00095 "`_price_filter_{$code}`.`entity_id` = `e`.`entity_id`",
00096 array()
00097 );
00098 }
00099
00100 foreach ($attributes as $attribute) {
00101 $code = $attribute->getAttributeCode();
00102 $condition = false;
00103
00104 if (isset($values[$code])) {
00105 $value = $values[$code];
00106
00107 if (is_array($value)) {
00108 if ((isset($value['from']) && strlen($value['from']) > 0)
00109 || (isset($value['to']) && strlen($value['to']) > 0)) {
00110 $condition = $value;
00111 }
00112 elseif ($attribute->getBackend()->getType() == 'varchar') {
00113 $condition = array('in_set'=>$value);
00114 }
00115 elseif (!isset($value['from']) && !isset($value['to'])) {
00116 $condition = array('in'=>$value);
00117 }
00118 } else {
00119 if (strlen($value)>0) {
00120 if (in_array($attribute->getBackend()->getType(), array('varchar', 'text'))) {
00121 $condition = array('like'=>'%'.$value.'%');
00122 } elseif ($attribute->getFrontendInput() == 'boolean') {
00123 $condition = array('in' => array('0','1'));
00124 } else {
00125 $condition = $value;
00126 }
00127 }
00128 }
00129 }
00130
00131 if (false !== $condition) {
00132 $this->_addSearchCriteria($attribute, $value);
00133
00134 if (in_array($code, $filteredAttributes))
00135 continue;
00136
00137 $table = $attribute->getBackend()->getTable();
00138 $attributeId = $attribute->getId();
00139 if ($attribute->getBackendType() == 'static'){
00140 $attributeId = $attribute->getAttributeCode();
00141 $condition = array('like'=>"%{$condition}%");
00142 }
00143
00144 $allConditions[$table][$attributeId] = $condition;
00145 }
00146 }
00147 if ($allConditions) {
00148 $this->getProductCollection()->addFieldsToFilter($allConditions);
00149 } else if (!count($filteredAttributes)) {
00150 Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
00151 }
00152
00153 return $this;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163 protected function _addSearchCriteria($attribute, $value)
00164 {
00165 $name = $attribute->getFrontend()->getLabel();
00166
00167 if (is_array($value) && (isset($value['from']) || isset($value['to']))){
00168 if (isset($value['currency'])) {
00169 $currencyModel = Mage::getModel('directory/currency')->load($value['currency']);
00170 $from = $currencyModel->format($value['from'], array(), false);
00171 $to = $currencyModel->format($value['to'], array(), false);
00172 } else {
00173 $currencyModel = null;
00174 }
00175
00176 if (strlen($value['from']) > 0 && strlen($value['to']) > 0) {
00177
00178 $value = sprintf('%s - %s', ($currencyModel ? $from : $value['from']), ($currencyModel ? $to : $value['to']));
00179 } elseif (strlen($value['from']) > 0) {
00180
00181 $value = Mage::helper('catalogsearch')->__('%s and greater', ($currencyModel ? $from : $value['from']));
00182 } elseif (strlen($value['to']) > 0) {
00183
00184 $value = Mage::helper('catalogsearch')->__('up to %s', ($currencyModel ? $to : $value['to']));
00185 }
00186 }
00187
00188 if (($attribute->getFrontendInput() == 'select' || $attribute->getFrontendInput() == 'multiselect') && is_array($value)) {
00189 foreach ($value as $k=>$v){
00190 $value[$k] = $attribute->getSource()->getOptionText($v);
00191
00192 if (is_array($value[$k]))
00193 $value[$k] = $value[$k]['label'];
00194 }
00195 $value = implode(', ', $value);
00196 } else if ($attribute->getFrontendInput() == 'select' || $attribute->getFrontendInput() == 'multiselect') {
00197 $value = $attribute->getSource()->getOptionText($value);
00198 if (is_array($value))
00199 $value = $value['label'];
00200 } else if ($attribute->getFrontendInput() == 'boolean') {
00201 $value = $value == 1
00202 ? Mage::helper('catalogsearch')->__('Yes')
00203 : Mage::helper('catalogsearch')->__('No');
00204 }
00205
00206 $this->_searchCriterias[] = array('name'=>$name, 'value'=>$value);
00207 return $this;
00208 }
00209
00210 public function getSearchCriterias()
00211 {
00212 return $this->_searchCriterias;
00213 }
00214
00215 public function getProductCollection(){
00216 if (is_null($this->_productCollection)) {
00217 $this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
00218 ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
00219 ->addMinimalPrice()
00220 ->addTaxPercents()
00221 ->addStoreFilter();
00222 Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
00223 Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
00224 }
00225
00226 return $this->_productCollection;
00227 }
00228 }