PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/CatalogSearch/Model/Mysql4/Advanced.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 267 lines | 135 code | 32 blank | 100 comment | 37 complexity | ca7c07d8253c526d3559d8bc77946792 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 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. * Advanced Catalog Search resource model
  28. *
  29. * @category Mage
  30. * @package Mage_CatalogSearch
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_CatalogSearch_Model_Mysql4_Advanced extends Mage_Core_Model_Mysql4_Abstract
  34. {
  35. /**
  36. * Initialize connection and define catalog product table as main table
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('catalog/product', 'entity_id');
  42. }
  43. /**
  44. * Prepare response object and dispatch prepare price event
  45. *
  46. * Return response object
  47. *
  48. * @param Varien_Db_Select $select
  49. * @return Varien_Object
  50. */
  51. protected function _dispatchPreparePriceEvent($select)
  52. {
  53. // prepare response object for event
  54. $response = new Varien_Object();
  55. $response->setAdditionalCalculations(array());
  56. // prepare event arguments
  57. $eventArgs = array(
  58. 'select' => $select,
  59. 'table' => 'price_index',
  60. 'store_id' => Mage::app()->getStore()->getId(),
  61. 'response_object' => $response
  62. );
  63. /**
  64. * @deprecated since 1.3.2.2
  65. */
  66. Mage::dispatchEvent('catalogindex_prepare_price_select', $eventArgs);
  67. /**
  68. * @since 1.4
  69. */
  70. Mage::dispatchEvent('catalog_prepare_price_select', $eventArgs);
  71. return $response;
  72. }
  73. /**
  74. * Prepare search condition for attribute
  75. *
  76. * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
  77. * @param string|array $value
  78. * @param Mage_CatalogSearch_Model_Mysql4_Advanced_Collection $collection
  79. *
  80. * @return mixed
  81. */
  82. public function prepareCondition($attribute, $value, $collection)
  83. {
  84. $condition = false;
  85. if (is_array($value)) {
  86. if (!empty($value['from']) || !empty($value['to'])) { // range
  87. $condition = $value;
  88. } else if ($attribute->getBackendType() == 'varchar') { // multiselect
  89. $condition = array('in_set' => $value);
  90. } else if (!isset($value['from']) && !isset($value['to'])) { // select
  91. $condition = array('in' => $value);
  92. }
  93. } else {
  94. if (strlen($value) > 0) {
  95. if (in_array($attribute->getBackendType(), array('varchar', 'text', 'static'))) {
  96. $condition = array('like' => '%' . $value . '%'); // text search
  97. } else {
  98. $condition = $value;
  99. }
  100. }
  101. }
  102. return $condition;
  103. }
  104. /**
  105. * Add filter by attribute price
  106. *
  107. * @deprecated after 1.4.1.0 - use $this->addRatedPriceFilter()
  108. *
  109. * @param Mage_CatalogSearch_Model_Advanced $object
  110. * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
  111. * @param string|array $value
  112. *
  113. * @return bool
  114. */
  115. public function addPriceFilter($object, $attribute, $value)
  116. {
  117. if (empty($value['from']) && empty($value['to'])) {
  118. return false;
  119. }
  120. $adapter = $this->_getReadAdapter();
  121. $conditions = array();
  122. if (strlen($value['from']) > 0) {
  123. $conditions[] = $adapter->quoteInto('price_index.min_price %s * %s >= ?', $value['from']);
  124. }
  125. if (strlen($value['to']) > 0) {
  126. $conditions[] = $adapter->quoteInto('price_index.min_price %s * %s <= ?', $value['to']);
  127. }
  128. if (!$conditions) {
  129. return false;
  130. }
  131. $object->getProductCollection()->addPriceData();
  132. $select = $object->getProductCollection()->getSelect();
  133. $response = $this->_dispatchPreparePriceEvent($select);
  134. $additional = join('', $response->getAdditionalCalculations());
  135. $rate = 1;
  136. if (!empty($value['currency'])) {
  137. $rate = Mage::app()->getStore()->getBaseCurrency()->getRate($value['currency']);
  138. }
  139. foreach ($conditions as $condition) {
  140. $select->where(sprintf($condition, $additional, $rate));
  141. }
  142. return true;
  143. }
  144. /**
  145. * Add filter by attribute rated price
  146. *
  147. * @param Mage_CatalogSearch_Model_Mysql4_Advanced_Collection $collection
  148. * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
  149. * @param string|array $value
  150. * @param int $rate
  151. *
  152. * @return bool
  153. */
  154. public function addRatedPriceFilter($collection, $attribute, $value, $rate = 1)
  155. {
  156. $adapter = $this->_getReadAdapter();
  157. $conditions = array();
  158. if (strlen($value['from']) > 0) {
  159. $conditions[] = $adapter->quoteInto('price_index.min_price %s * %s >= ?', $value['from']);
  160. }
  161. if (strlen($value['to']) > 0) {
  162. $conditions[] = $adapter->quoteInto('price_index.min_price %s * %s <= ?', $value['to']);
  163. }
  164. if (!$conditions) {
  165. return false;
  166. }
  167. $collection->addPriceData();
  168. $select = $collection->getSelect();
  169. $response = $this->_dispatchPreparePriceEvent($select);
  170. $additional = join('', $response->getAdditionalCalculations());
  171. foreach ($conditions as $condition) {
  172. $select->where(sprintf($condition, $additional, $rate));
  173. }
  174. return true;
  175. }
  176. /**
  177. * Add filter by indexable attribute
  178. *
  179. * @deprecated after 1.4.1.0 - use $this->addIndexableAttributeModifiedFilter()
  180. *
  181. * @param Mage_CatalogSearch_Model_Advanced $object
  182. * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
  183. * @param string|array $value
  184. *
  185. * @return bool
  186. */
  187. public function addIndexableAttributeFilter($object, $attribute, $value)
  188. {
  189. return $this->addIndexableAttributeModifiedFilter($object, $attribute, $value);
  190. }
  191. /**
  192. *
  193. * @param Mage_CatalogSearch_Model_Mysql4_Advanced_Collection $collection
  194. * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
  195. * @param string|array $value
  196. *
  197. * @return bool
  198. */
  199. public function addIndexableAttributeModifiedFilter($collection, $attribute, $value)
  200. {
  201. if ($attribute->getIndexType() == 'decimal') {
  202. $table = $this->getTable('catalog/product_index_eav_decimal');
  203. } else {
  204. $table = $this->getTable('catalog/product_index_eav');
  205. }
  206. $tableAlias = 'ast_' . $attribute->getAttributeCode();
  207. $storeId = Mage::app()->getStore()->getId();
  208. $select = $collection->getSelect();
  209. if (is_array($value)) {
  210. if (isset($value['from']) && isset($value['to'])) {
  211. if (empty($value['from']) && empty($value['to'])) {
  212. return false;
  213. }
  214. }
  215. }
  216. $select->distinct(true);
  217. $select->join(
  218. array($tableAlias => $table),
  219. "e.entity_id={$tableAlias}.entity_id AND {$tableAlias}.attribute_id={$attribute->getAttributeId()}"
  220. . " AND {$tableAlias}.store_id={$storeId}",
  221. array()
  222. );
  223. if (is_array($value) && (isset($value['from']) || isset($value['to']))) {
  224. if (isset($value['from']) && !empty($value['from'])) {
  225. $select->where("{$tableAlias}.`value` >= ?", $value['from']);
  226. }
  227. if (isset($value['to']) && !empty($value['to'])) {
  228. $select->where("{$tableAlias}.`value` <= ?", $value['to']);
  229. }
  230. return true;
  231. }
  232. $select->where("{$tableAlias}.`value` IN(?)", $value);
  233. return true;
  234. }
  235. }