/**
  * {@inheritdoc}
  */
 public function generateCondition(ConditionInterface $condition, QueryBuilder $query, ShopContextInterface $context)
 {
     /** @var ProductAttributeCondition $condition */
     if (!$condition->getField()) {
         throw new \Exception('ProductAttributeCondition class requires a defined attribute field!');
     }
     if (!$condition->getOperator()) {
         throw new \Exception('ProductAttributeCondition class requires a defined operator!');
     }
     $placeholder = ':' . $condition->getField();
     $field = 'productAttribute.' . $condition->getField();
     switch (true) {
         case $condition->getValue() === null:
             if ($condition->getOperator() === ProductAttributeCondition::OPERATOR_EQ) {
                 $query->andWhere($field . ' IS NULL');
             } else {
                 $query->andWhere($field . ' IS NOT NULL');
             }
             break;
         case $condition->getOperator() === ProductAttributeCondition::OPERATOR_IN:
             $query->andWhere($field . ' IN (' . $placeholder . ')');
             $query->setParameter($placeholder, $condition->getValue(), Connection::PARAM_STR_ARRAY);
             break;
         case $condition->getOperator() === ProductAttributeCondition::OPERATOR_CONTAINS:
             $query->andWhere($field . ' LIKE ' . $placeholder);
             $query->setParameter($placeholder, '%' . $condition->getValue() . '%');
             break;
         case $condition->getOperator() === ProductAttributeCondition::OPERATOR_BETWEEN:
             $value = $condition->getValue();
             if (isset($value['min'])) {
                 $query->andWhere($field . ' >= ' . $placeholder . 'Min')->setParameter($placeholder . 'Min', $value['min']);
             }
             if (isset($value['max'])) {
                 $query->andWhere($field . ' <= ' . $placeholder . 'Max')->setParameter($placeholder . 'Max', $value['max']);
             }
             break;
         case $condition->getOperator() === ProductAttributeCondition::OPERATOR_STARTS_WITH:
             $query->andWhere($field . ' LIKE ' . $placeholder);
             $query->setParameter($placeholder, $condition->getValue() . '%');
             break;
         case $condition->getOperator() === ProductAttributeCondition::OPERATOR_ENDS_WITH:
             $query->andWhere($field . ' LIKE ' . $placeholder);
             $query->setParameter($placeholder, '%' . $condition->getValue());
             break;
         default:
             $query->andWhere($field . ' ' . $condition->getOperator() . ' ' . $placeholder);
             $query->setParameter($placeholder, $condition->getValue());
             break;
     }
 }
 /**
  * @param SortingInterface $sorting
  * @param QueryBuilder $query
  * @param ShopContextInterface $context
  * @throws \Exception
  */
 public function generateSorting(SortingInterface $sorting, QueryBuilder $query, ShopContextInterface $context)
 {
     /** @var Listing $categoryComponent */
     $categoryComponent = Shopware()->Container()->get('swagcustomsort.listing_component');
     $categoryId = Shopware()->Front()->Request()->getParam('sCategory');
     $linkedCategoryId = $categoryComponent->getLinkedCategoryId($categoryId);
     $hasCustomSort = $categoryComponent->hasCustomSort($categoryId);
     $baseSort = $categoryComponent->getCategoryBaseSort($categoryId);
     if ($hasCustomSort || $baseSort > 0) {
         $baseSorting = $categoryComponent->getCategoryBaseSort($categoryId);
     } else {
         $baseSorting = Shopware()->Config()->get('defaultListingSorting');
     }
     //apply 'plugin' order
     if ($linkedCategoryId) {
         $query->leftJoin('productCategory', 's_articles_sort', 'customSort', 'customSort.articleId = productCategory.articleID AND (customSort.categoryId = :sortCategoryId OR customSort.categoryId IS NULL)');
         $query->setParameter('sortCategoryId', $linkedCategoryId);
     } else {
         $query->leftJoin('productCategory', 's_articles_sort', 'customSort', 'customSort.articleId = productCategory.articleID AND (customSort.categoryId = productCategory.categoryID OR customSort.categoryId IS NULL)');
     }
     //exclude passed products ids from result
     $sortedProductsIds = $this->sortingComponent->getSortedProductsIds();
     if ($sortedProductsIds) {
         $query->andWhere($query->expr()->notIn("product.id", $sortedProductsIds));
     }
     //for records with no 'plugin' order data use the default shopware order
     $handlerData = $this->getDefaultData($baseSorting);
     if ($handlerData) {
         $sorting->setDirection($handlerData['direction']);
         $handlerData['handler']->generateSorting($sorting, $query, $context);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function generateCondition(ConditionInterface $condition, QueryBuilder $query, ShopContextInterface $context)
 {
     $query->innerJoin('product', 's_articles_supplier', 'manufacturer', 'manufacturer.id = product.supplierID
          AND product.supplierID IN (:manufacturer)');
     /** @var ManufacturerCondition $condition */
     $query->setParameter(':manufacturer', $condition->getManufacturerIds(), Connection::PARAM_INT_ARRAY);
 }
 /**
  * {@inheritdoc}
  */
 public function generateCondition(ConditionInterface $condition, QueryBuilder $query, ShopContextInterface $context)
 {
     $query->leftJoin('product', 's_articles_avoid_customergroups', 'avoidCustomerGroup', 'avoidCustomerGroup.articleID = product.id
          AND avoidCustomerGroup.customerGroupId IN (:customerGroupIds)');
     /** @var CustomerGroupCondition $condition */
     $query->setParameter(':customerGroupIds', $condition->getCustomerGroupIds(), Connection::PARAM_INT_ARRAY);
     $query->andWhere('avoidCustomerGroup.articleID IS NULL');
 }
 /**
  * {@inheritdoc}
  */
 public function generateCondition(ConditionInterface $condition, QueryBuilder $query, ShopContextInterface $context)
 {
     if (!$query->hasState(self::STATE_INCLUDES_TOPSELLER_TABLE)) {
         $query->leftJoin('product', 's_articles_top_seller_ro', 'topSeller', 'topSeller.article_id = product.id');
         $query->addState(self::STATE_INCLUDES_TOPSELLER_TABLE);
     }
     $query->andWhere('topSeller.sales > :sales');
     /** @var SalesCondition $condition */
     $query->setParameter('sales', $condition->getMinSales());
 }
 /**
  * {@inheritdoc}
  */
 public function generateCondition(ConditionInterface $condition, QueryBuilder $query, ShopContextInterface $context)
 {
     if ($this->counter++ === 0) {
         $suffix = '';
     } else {
         $suffix = $this->counter;
     }
     $query->innerJoin('product', 's_articles_categories_ro', "productCategory{$suffix}", "productCategory{$suffix}.articleID = product.id\n            AND productCategory{$suffix}.categoryID IN (:category{$suffix})");
     /** @var CategoryCondition $condition */
     $query->setParameter(":category{$suffix}", $condition->getCategoryIds(), Connection::PARAM_INT_ARRAY);
 }
    /**
     * {@inheritdoc}
     */
    public function generateCondition(ConditionInterface $condition, QueryBuilder $query, ShopContextInterface $context)
    {
        $table = '
SELECT SUM(vote.points) / COUNT(vote.id) AS average, vote.articleID AS product_id
FROM s_articles_vote vote
GROUP BY vote.articleID';
        $query->innerJoin('product', '(' . $table . ')', 'voteAverage', 'voteAverage.product_id = product.id
             AND voteAverage.average >= :average');
        /** @var VoteAverageCondition $condition */
        $query->setParameter(':average', (double) $condition->getAverage());
        $query->addSelect('voteAverage.average');
        $query->addState(VoteAverageCondition::STATE_INCLUDES_VOTE_TABLE);
    }
 /**
  * {@inheritdoc}
  */
 public function generateCondition(ConditionInterface $condition, QueryBuilder $query, ShopContextInterface $context)
 {
     $tableKey = $condition->getName();
     $where = [];
     /** @var PropertyCondition $condition */
     foreach ($condition->getValueIds() as $valueId) {
         $valueKey = $tableKey . '_' . $valueId;
         $where[] = $tableKey . '.valueID = :' . $valueKey;
         $query->setParameter(':' . $valueKey, $valueId);
     }
     $where = implode(' OR ', $where);
     $query->innerJoin('product', 's_filter_articles', $tableKey, 'product.id = ' . $tableKey . '.articleID
          AND (' . $where . ')');
 }
 /**
  * {@inheritdoc}
  */
 public function generateCondition(ConditionInterface $condition, QueryBuilder $query, ShopContextInterface $context)
 {
     $date = new \DateTime();
     /** @var ReleaseDateCondition $condition */
     $intervalSpec = 'P' . $condition->getDays() . 'D';
     $interval = new \DateInterval($intervalSpec);
     $dateNow = new \DateTime();
     switch ($condition->getDirection()) {
         case ReleaseDateCondition::DIRECTION_FUTURE:
             $date->add($interval);
             $query->andWhere('variant.releasedate <= :releaseDateFrom');
             $query->andWhere('variant.releasedate > :dateNow');
             $query->setParameter(':releaseDateFrom', $date->format('Y-m-d'));
             $query->setParameter(':dateNow', $dateNow->format('Y-m-d'));
             break;
         case ReleaseDateCondition::DIRECTION_PAST:
             $date->sub($interval);
             $query->andWhere('variant.releasedate >= :releaseDateTo');
             $query->andWhere('variant.releasedate <= :dateNow');
             $query->setParameter(':releaseDateTo', $date->format('Y-m-d'));
             $query->setParameter(':dateNow', $dateNow->format('Y-m-d'));
             break;
     }
 }
 /**
  * @param QueryBuilder $query
  * @param Struct\ShopContextInterface $context
  */
 private function addTranslations($query, $context)
 {
     if ($context->getShop()->isDefault()) {
         return;
     }
     $query->addSelect('attributeTranslations.objectdata as __attribute_translation')->leftJoin('product', 's_core_translations', 'attributeTranslations', 'attributeTranslations.objectkey = product.id AND attributeTranslations.objecttype = "article" AND attributeTranslations.objectlanguage = :language');
     $query->setParameter(':language', $context->getShop()->getId());
     if (!$context->getShop()->getFallbackId() || $context->getShop()->getFallbackId() === $context->getShop()->getId()) {
         return;
     }
     $query->addSelect('attributeTranslations_fallback.objectdata as __attribute_translation_fallback')->leftJoin('product', 's_core_translations', 'attributeTranslations_fallback', 'attributeTranslations_fallback.objectkey = product.id AND attributeTranslations_fallback.objecttype = "article" AND attributeTranslations_fallback.objectlanguage = :languageFallback');
     $query->setParameter(':languageFallback', $context->getShop()->getFallbackId());
 }
 /**
  * {@inheritdoc}
  */
 public function generateCondition(ConditionInterface $condition, QueryBuilder $query, ShopContextInterface $context)
 {
     $query->andWhere('variant.ordernumber IN (:ordernumbers)');
     /** @var OrdernumberCondition $condition */
     $query->setParameter(':ordernumbers', $condition->getOrdernumbers(), Connection::PARAM_STR_ARRAY);
 }