Mage_CatalogSearch_Model_Advanced Class Reference

Inheritance diagram for Mage_CatalogSearch_Model_Advanced:

Varien_Object

List of all members.

Public Member Functions

 getAttributes ()
 addFilters ($values)
 getSearchCriterias ()
 getProductCollection ()

Protected Member Functions

 _addSearchCriteria ($attribute, $value)


Detailed Description

Definition at line 34 of file Advanced.php.


Member Function Documentation

_addSearchCriteria ( attribute,
value 
) [protected]

Add data about search criteria to object state

Parameters:
Mage_Eav_Model_Entity_Attribute $attribute
mixed $value
Returns:
Mage_CatalogSearch_Model_Advanced

Definition at line 163 of file Advanced.php.

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                 // and more
00181                 $value = Mage::helper('catalogsearch')->__('%s and greater', ($currencyModel ? $from : $value['from']));
00182             } elseif (strlen($value['to']) > 0) {
00183                 // to
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     }

addFilters ( values  ) 

Add advanced search filters to product collection

Parameters:
array $values
Returns:
Mage_CatalogSearch_Model_Advanced

Definition at line 69 of file Advanced.php.

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     }

getAttributes (  ) 

Definition at line 43 of file Advanced.php.

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                 //->addIsSearchableFilter()
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     }

getProductCollection (  ) 

Definition at line 215 of file Advanced.php.

00215                                           {
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     }

getSearchCriterias (  ) 

Definition at line 210 of file Advanced.php.

00211     {
00212         return $this->_searchCriterias;
00213     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:49 2009 for Magento by  doxygen 1.5.8