예제 #1
0
 /**
  * @param FilterInterface $filter
  * @param bool $isNegation
  * @param string $query
  * @return string
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 private function processQueryWithField(FilterInterface $filter, $isNegation, $query)
 {
     $currentStoreId = $this->scopeResolver->getScope()->getId();
     $attribute = $this->config->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $filter->getField());
     $select = $this->getSelect();
     $table = $attribute->getBackendTable();
     if ($filter->getField() == 'price') {
         $query = str_replace('price', 'min_price', $query);
         $select->from(['main_table' => $this->resource->getTableName('catalog_product_index_price')], 'entity_id')->where($query);
     } elseif ($filter->getField() == 'category_ids') {
         return 'category_index.category_id = ' . $filter->getValue();
     } else {
         if ($attribute->isStatic()) {
             $select->from(['main_table' => $table], 'entity_id')->where($query);
         } else {
             if ($filter->getType() == FilterInterface::TYPE_TERM) {
                 $field = $filter->getField();
                 $mapper = function ($value) use($field, $isNegation) {
                     return ($isNegation ? '-' : '') . $this->attributePrefix . $field . '_' . $value;
                 };
                 if (is_array($filter->getValue())) {
                     $value = implode(' ', array_map($mapper, $filter->getValue()));
                 } else {
                     $value = $mapper($filter->getValue());
                 }
                 return 'MATCH (data_index) AGAINST (' . $this->getConnection()->quote($value) . ' IN BOOLEAN MODE)';
             }
             $ifNullCondition = $this->getConnection()->getIfNullSql('current_store.value', 'main_table.value');
             $select->from(['main_table' => $table], 'entity_id')->joinLeft(['current_store' => $table], 'current_store.attribute_id = main_table.attribute_id AND current_store.store_id = ' . $currentStoreId, null)->columns([$filter->getField() => $ifNullCondition])->where('main_table.attribute_id = ?', $attribute->getAttributeId())->where('main_table.store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID)->having($query);
         }
     }
     return 'search_index.product_id IN (
         select entity_id from  ' . $this->conditionManager->wrapBrackets($select) . ' as filter
         )';
 }
예제 #2
0
 /**
  * @param FilterInterface $filter
  * @param bool $isNegation
  * @param string $query
  * @param QueryContainer $queryContainer
  * @return string
  */
 private function processQueryWithField(FilterInterface $filter, $isNegation, $query, QueryContainer $queryContainer)
 {
     $currentStoreId = $this->scopeResolver->getScope()->getId();
     $attribute = $this->config->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $filter->getField());
     $select = $this->getConnection()->select();
     $table = $attribute->getBackendTable();
     if ($filter->getField() == 'price') {
         $query = str_replace('price', 'min_price', $query);
         $select->from(['main_table' => $this->resource->getTableName('catalog_product_index_price')], 'entity_id')->where($query);
     } elseif ($filter->getField() == 'category_ids') {
         return 'category_index.category_id = ' . $filter->getValue();
     } else {
         if ($attribute->isStatic()) {
             $select->from(['main_table' => $table], 'entity_id')->where($query);
         } else {
             if ($filter->getType() == FilterInterface::TYPE_TERM) {
                 if (is_array($filter->getValue())) {
                     $value = sprintf('%s IN (%s)', $isNegation ? 'NOT' : '', implode(',', $filter->getValue()));
                 } else {
                     $value = ($isNegation ? '!' : '') . '= ' . $filter->getValue();
                 }
                 $filterQuery = sprintf('cpie.store_id = %d AND cpie.attribute_id = %d AND cpie.value %s', $this->scopeResolver->getScope()->getId(), $attribute->getId(), $value);
                 $queryContainer->addFilter($filterQuery);
                 return '';
             }
             $ifNullCondition = $this->getConnection()->getIfNullSql('current_store.value', 'main_table.value');
             $select->from(['main_table' => $table], 'entity_id')->joinLeft(['current_store' => $table], 'current_store.attribute_id = main_table.attribute_id AND current_store.store_id = ' . $currentStoreId, null)->columns([$filter->getField() => $ifNullCondition])->where('main_table.attribute_id = ?', $attribute->getAttributeId())->where('main_table.store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID)->having($query);
         }
     }
     return 'search_index.entity_id IN (
         select entity_id from  ' . $this->conditionManager->wrapBrackets($select) . ' as filter
         )';
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function build(RequestFilterInterface $filter)
 {
     switch ($filter->getType()) {
         case RequestFilterInterface::TYPE_BOOL:
             /** @var \Magento\Framework\Search\Request\Filter\Bool $filter */
             $queries = [];
             $must = $this->buildFilters($filter->getMust(), Select::SQL_AND);
             if (!empty($must)) {
                 $queries[] = $must;
             }
             $should = $this->buildFilters($filter->getShould(), Select::SQL_OR);
             if (!empty($should)) {
                 $queries[] = $this->wrapBrackets($should);
             }
             $mustNot = $this->buildFilters($filter->getMustNot(), Select::SQL_AND);
             if (!empty($mustNot)) {
                 $queries[] = '!' . $this->wrapBrackets($mustNot);
             }
             $query = $this->generateQuery($queries, Select::SQL_AND);
             break;
         case RequestFilterInterface::TYPE_TERM:
             /** @var \Magento\Framework\Search\Request\Filter\Term $filter */
             $query = $this->term->buildFilter($filter);
             break;
         case RequestFilterInterface::TYPE_RANGE:
             /** @var \Magento\Framework\Search\Request\Filter\Range $filter */
             $query = $this->range->buildFilter($filter);
             break;
         default:
             throw new \InvalidArgumentException(sprintf('Unknown filter type \'%s\'', $filter->getType()));
     }
     return $this->wrapBrackets($query);
 }
예제 #4
0
 protected function processFilter(RequestFilterInterface $filter, DataObject $params, $conditionType)
 {
     if ($filter->getType() == RequestFilterInterface::TYPE_TERM) {
         $filters = $params->hasFilters() ? $params->getFilters() : [];
         $filters[$filter->getField()] = $filter->getValue();
         $params->setFilters($filters);
     }
     /* ignore otherwise */
 }
예제 #5
0
 /**
  * @param RequestFilterInterface $filter
  * @param bool $isNegation
  * @return string
  */
 private function processFilter(RequestFilterInterface $filter, $isNegation)
 {
     if ($filter->getType() == RequestFilterInterface::TYPE_BOOL) {
         $query = $this->processBoolFilter($filter, $isNegation);
         $query = $this->conditionManager->wrapBrackets($query);
     } else {
         if (!isset($this->filters[$filter->getType()])) {
             throw new \InvalidArgumentException('Unknown filter type ' . $filter->getType());
         }
         $query = $this->filters[$filter->getType()]->buildFilter($filter, $isNegation);
         $query = $this->preprocessor->process($filter, $isNegation, $query);
     }
     return $query;
 }
예제 #6
0
 /**
  * Returns mapping data for field in format: [
  *  'table_alias',
  *  'table',
  *  'join_condition',
  *  ['fields']
  * ]
  * @param FilterInterface $filter
  * @return array
  */
 private function getMappingData(FilterInterface $filter)
 {
     $alias = null;
     $table = null;
     $mapOn = null;
     $mappedFields = null;
     $field = $filter->getField();
     $fieldToTableMap = $this->getFieldToTableMap($field);
     if ($fieldToTableMap) {
         list($alias, $table, $mapOn, $mappedFields) = $fieldToTableMap;
         $table = $this->resource->getTableName($table);
     } elseif ($attribute = $this->getAttributeByCode($field)) {
         if ($filter->getType() === FilterInterface::TYPE_TERM && in_array($attribute->getFrontendInput(), ['select', 'multiselect'], true)) {
             $table = $this->resource->getTableName('catalog_product_index_eav');
             $alias = $field . '_filter';
             $mapOn = sprintf('search_index.entity_id = %1$s.entity_id AND %1$s.attribute_id = %2$d AND %1$s.store_id = %3$d', $alias, $attribute->getId(), $this->getStoreId());
             $mappedFields = [];
         } elseif ($attribute->getBackendType() === AbstractAttribute::TYPE_STATIC) {
             $table = $attribute->getBackendTable();
             $alias = $field . '_filter';
             $mapOn = 'search_index.entity_id = ' . $alias . '.entity_id';
             $mappedFields = null;
         }
     }
     return [$alias, $table, $mapOn, $mappedFields];
 }