Example #1
0
 /**
  * @return ActiveDataProvider
  */
 public function execute()
 {
     $query = clone $this->query;
     $subQuery = clone $this->subQuery;
     $this->filtersApplied = 0;
     foreach ($this->filters as $filter) {
         $attributeInfo = $this->model->getAttributesFilterBehavior()->getAttributeInfo($filter['attributeName'] . '_filter');
         if ($attributeInfo) {
             if (!$attributeInfo->multiple) {
                 /** @var ActiveRecord $modelClass */
                 $modelClass = $query->modelClass;
                 if (!$modelClass::getTableSchema()->getColumn($attributeInfo->attribute)) {
                     $subQuery->orFilterWhere(['and', ['attribute_name' => $attributeInfo->attribute], ['attribute_value' => $filter['attributeValue']]]);
                     $this->filtersApplied++;
                 } else {
                     $query->andWhere([$attributeInfo->attribute => $filter['attributeValue']]);
                 }
             } else {
                 $subQuery->orFilterWhere(['and', ['attribute_name' => $attributeInfo->attribute], ['in', 'attribute_value', is_array($filter['attributeValue']) ? $filter['attributeValue'] : [$filter['attributeValue']]]]);
                 $this->filtersApplied++;
             }
         } else {
             if (in_array($filter['attributeName'], ['sort'])) {
                 continue;
             }
             if ($filter['attributeName'] == 'price_min') {
                 if (is_numeric($filter['attributeValue']) && $filter['attributeValue'] > 0) {
                     $query->andWhere(['>=', 'price', $filter['attributeValue']]);
                 }
             } else {
                 if ($filter['attributeName'] == 'price_max') {
                     if (is_numeric($filter['attributeValue']) && $filter['attributeValue'] > 0) {
                         $query->andWhere(['<=', 'price', $filter['attributeValue']]);
                     }
                 } else {
                     if ($filter['attributeName'] == 'height') {
                         if (is_numeric($filter['attributeValue']) && $filter['attributeValue'] > 0) {
                             $query->andWhere(['<=', 'height', $filter['attributeValue']]);
                         }
                     } else {
                         if ($filter['attributeName'] == 'width') {
                             if (is_numeric($filter['attributeValue']) && $filter['attributeValue'] > 0) {
                                 $query->andWhere(['<=', 'width', $filter['attributeValue']]);
                             }
                         } else {
                             if ($filter['attributeName'] == 'length') {
                                 if (is_numeric($filter['attributeValue']) && $filter['attributeValue'] > 0) {
                                     $query->andWhere(['<=', 'length', $filter['attributeValue']]);
                                 }
                             } else {
                                 if ($filter['attributeName'] == 'search') {
                                     $query->andWhere(['like', 'title', $filter['attributeValue']]);
                                 } else {
                                     if ($filter['attributeName'] == 'title') {
                                         $query->andWhere(['title' => $filter['attributeValue']]);
                                     } else {
                                         if ($filter['attributeName'] == 'color') {
                                             $query->andWhere(['color' => $filter['attributeValue']]);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($this->filtersApplied > 0) {
         $query->join('LEFT JOIN', ['f' => $subQuery], 'f.owner_id = ' . $this->model->tableName() . '.item_id');
         $query->andFilterWhere(['f.filter_matched' => $this->filtersApplied]);
     }
     return new ActiveDataProvider(['query' => $query]);
 }