Advanced.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\ResourceModel;
  7. /**
  8. * Advanced Catalog Search resource model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Advanced extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * Core event manager proxy
  17. *
  18. * @var \Magento\Framework\Event\ManagerInterface
  19. */
  20. protected $_eventManager = null;
  21. /**
  22. * Store manager
  23. *
  24. * @var \Magento\Store\Model\StoreManagerInterface
  25. */
  26. protected $_storeManager;
  27. /**
  28. * Construct
  29. *
  30. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  31. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  32. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  33. * @param string $connectionName
  34. */
  35. public function __construct(
  36. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  37. \Magento\Store\Model\StoreManagerInterface $storeManager,
  38. \Magento\Framework\Event\ManagerInterface $eventManager,
  39. $connectionName = null
  40. ) {
  41. $this->_storeManager = $storeManager;
  42. $this->_eventManager = $eventManager;
  43. parent::__construct($context, $connectionName);
  44. }
  45. /**
  46. * Initialize connection and define catalog product table as main table
  47. *
  48. * @return void
  49. */
  50. protected function _construct()
  51. {
  52. $this->_init('catalog_product_entity', 'entity_id');
  53. }
  54. /**
  55. * Prepare search condition for attribute
  56. *
  57. * @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute
  58. * @param string|array $value
  59. * @return string|array
  60. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  61. */
  62. public function prepareCondition($attribute, $value)
  63. {
  64. $condition = false;
  65. if (is_array($value)) {
  66. if ($attribute->getBackendType() == 'varchar') { // multiselect
  67. // multiselect
  68. $condition = ['in_set' => $value];
  69. } elseif (!isset($value['from']) && !isset($value['to'])) { // select
  70. // select
  71. $condition = ['in' => $value];
  72. } elseif (isset($value['from']) && '' !== $value['from'] || isset($value['to']) && '' !== $value['to']) {
  73. // range
  74. $condition = $value;
  75. }
  76. } else {
  77. if (strlen($value) > 0) {
  78. if (in_array($attribute->getBackendType(), ['varchar', 'text', 'static'])) {
  79. $condition = ['like' => $value]; // text search
  80. } else {
  81. $condition = $value;
  82. }
  83. }
  84. }
  85. return $condition;
  86. }
  87. }