Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_CatalogSearch_AdvancedController
  • Mage_CatalogSearch_AjaxController
  • Mage_CatalogSearch_Block_Advanced_Form
  • Mage_CatalogSearch_Block_Advanced_Result
  • Mage_CatalogSearch_Block_Autocomplete
  • Mage_CatalogSearch_Block_Layer
  • Mage_CatalogSearch_Block_Layer_Filter_Attribute
  • Mage_CatalogSearch_Block_Result
  • Mage_CatalogSearch_Block_Term
  • Mage_CatalogSearch_Helper_Data
  • Mage_CatalogSearch_Model_Advanced
  • Mage_CatalogSearch_Model_Fulltext
  • Mage_CatalogSearch_Model_Fulltext_Observer
  • Mage_CatalogSearch_Model_Indexer_Fulltext
  • Mage_CatalogSearch_Model_Layer
  • Mage_CatalogSearch_Model_Layer_Filter_Attribute
  • Mage_CatalogSearch_Model_Mysql4_Advanced
  • Mage_CatalogSearch_Model_Mysql4_Advanced_Collection
  • Mage_CatalogSearch_Model_Mysql4_Fulltext
  • Mage_CatalogSearch_Model_Mysql4_Fulltext_Collection
  • Mage_CatalogSearch_Model_Mysql4_Fulltext_Engine
  • Mage_CatalogSearch_Model_Mysql4_Indexer_Fulltext
  • Mage_CatalogSearch_Model_Mysql4_Query
  • Mage_CatalogSearch_Model_Mysql4_Query_Collection
  • Mage_CatalogSearch_Model_Mysql4_Search_Collection
  • Mage_CatalogSearch_Model_Query
  • Mage_CatalogSearch_Model_Resource_Advanced
  • Mage_CatalogSearch_Model_Resource_Advanced_Collection
  • Mage_CatalogSearch_Model_Resource_Fulltext
  • Mage_CatalogSearch_Model_Resource_Fulltext_Collection
  • Mage_CatalogSearch_Model_Resource_Fulltext_Engine
  • Mage_CatalogSearch_Model_Resource_Indexer_Fulltext
  • Mage_CatalogSearch_Model_Resource_Query
  • Mage_CatalogSearch_Model_Resource_Query_Collection
  • Mage_CatalogSearch_Model_Resource_Search_Collection
  • Mage_CatalogSearch_Model_Session
  • Mage_Catalogsearch_Model_System_Config_Backend_Sitemap
  • Mage_CatalogSearch_ResultController
  • Mage_CatalogSearch_TermController
  • Overview
  • Package
  • Class
  • Tree
  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:  * Catalog advanced search model
 29:  *
 30:  * @method Mage_CatalogSearch_Model_Resource_Advanced _getResource()
 31:  * @method Mage_CatalogSearch_Model_Resource_Advanced getResource()
 32:  * @method int getEntityTypeId()
 33:  * @method Mage_CatalogSearch_Model_Advanced setEntityTypeId(int $value)
 34:  * @method int getAttributeSetId()
 35:  * @method Mage_CatalogSearch_Model_Advanced setAttributeSetId(int $value)
 36:  * @method string getTypeId()
 37:  * @method Mage_CatalogSearch_Model_Advanced setTypeId(string $value)
 38:  * @method string getSku()
 39:  * @method Mage_CatalogSearch_Model_Advanced setSku(string $value)
 40:  * @method int getHasOptions()
 41:  * @method Mage_CatalogSearch_Model_Advanced setHasOptions(int $value)
 42:  * @method int getRequiredOptions()
 43:  * @method Mage_CatalogSearch_Model_Advanced setRequiredOptions(int $value)
 44:  * @method string getCreatedAt()
 45:  * @method Mage_CatalogSearch_Model_Advanced setCreatedAt(string $value)
 46:  * @method string getUpdatedAt()
 47:  * @method Mage_CatalogSearch_Model_Advanced setUpdatedAt(string $value)
 48:  *
 49:  * @category    Mage
 50:  * @package     Mage_CatalogSearch
 51:  * @author      Magento Core Team <core@magentocommerce.com>
 52:  */
 53: class Mage_CatalogSearch_Model_Advanced extends Mage_Core_Model_Abstract
 54: {
 55:     /**
 56:      * User friendly search criteria list
 57:      *
 58:      * @var array
 59:      */
 60:     protected $_searchCriterias = array();
 61: 
 62:     /**
 63:      * Current search engine
 64:      *
 65:      * @var object | Mage_CatalogSearch_Model_Resource_Fulltext_Engine
 66:      */
 67:     protected $_engine = null;
 68: 
 69:     /**
 70:      * Initialize resource model
 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:      * Retrieve resource instance wrapper
 90:      *
 91:      * @return Mage_CatalogSearch_Model_Resource_Advanced
 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:      * Retrieve array of attributes used in advanced search
104:      *
105:      * @return array
106:      */
107:     public function getAttributes()
108:     {
109:         /* @var $attributes Mage_Catalog_Model_Resource_Eav_Resource_Product_Attribute_Collection */
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:      * Prepare search condition for attribute
129:      *
130:      * @deprecated after 1.4.1.0 - use Mage_CatalogSearch_Model_Resource_Advanced->_prepareCondition()
131:      *
132:      * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
133:      * @param string|array $value
134:      * @return mixed
135:      */
136:     protected function _prepareCondition($attribute, $value)
137:     {
138:         return $this->_getResource()->prepareCondition($attribute, $value, $this->getProductCollection());
139:     }
140: 
141:     /**
142:      * Add advanced search filters to product collection
143:      *
144:      * @param   array $values
145:      * @return  Mage_CatalogSearch_Model_Advanced
146:      */
147:     public function addFilters($values)
148:     {
149:         $attributes     = $this->getAttributes();
150:         $hasConditions  = false;
151:         $allConditions  = array();
152: 
153:         foreach ($attributes as $attribute) {
154:             /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
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:      * Add data about search criteria to object state
212:      *
213:      * @param   Mage_Eav_Model_Entity_Attribute $attribute
214:      * @param   mixed $value
215:      * @return  Mage_CatalogSearch_Model_Advanced
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:                         // and more
238:                         $value = Mage::helper('catalogsearch')->__('%s and greater', ($currencyModel ? $from : $value['from']));
239:                     } elseif (strlen($value['to']) > 0) {
240:                         // to
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:      * Returns prepared search criterias in text
276:      *
277:      * @return array
278:      */
279:     public function getSearchCriterias()
280:     {
281:         return $this->_searchCriterias;
282:     }
283: 
284:     /**
285:      * Retrieve advanced search product collection
286:      *
287:      * @return Mage_CatalogSearch_Model_Resource_Advanced_Collection
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:      * Prepare product collection
304:      *
305:      * @param Mage_CatalogSearch_Model_Resource_Advanced_Collection $collection
306:      * @return Mage_Catalog_Model_Layer
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: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0