diff --git a/modules/catalog/api/Catalog.php b/modules/catalog/api/Catalog.php index fe9c284..d83eca8 100644 --- a/modules/catalog/api/Catalog.php +++ b/modules/catalog/api/Catalog.php @@ -4,6 +4,7 @@ use Yii; use yii\data\ActiveDataProvider; +use yii\easyii\modules\catalog\models\ItemData; use yii\easyii\widgets\Fancybox; use yii\easyii\modules\catalog\models\Category; use yii\easyii\modules\catalog\models\Item; @@ -64,6 +65,9 @@ public function api_items($options = []) } else { $query->sortDate(); } + if(!empty($options['filters'])){ + $query = self::applyFilters($options['filters'], $query); + } $this->_adp = new ActiveDataProvider([ 'query' => $query, @@ -127,6 +131,53 @@ public function api_plugin($options = []) 'options' => $options ]); } + + public static function applyFilters($filters, $query) + { + if(is_array($filters)){ + + if(!empty($filters['price'])){ + $price = $filters['price']; + if(is_array($price) && count($price) == 2) { + if(!$price[0]){ + $query->andFilterWhere(['<=', 'price', (int)$price[1]]); + } elseif(!$price[1]) { + $query->andFilterWhere(['>=', 'price', (int)$price[0]]); + } else { + $query->andFilterWhere(['between', 'price', (int)$price[0], (int)$price[1]]); + } + } + unset($filters['price']); + } + if(count($filters)){ + $filtersApplied = 0; + $subQuery = ItemData::find()->select('item_id, COUNT(*) as filter_matched')->groupBy('item_id'); + foreach($filters as $field => $value){ + if(!is_array($value)) { + $subQuery->orFilterWhere(['and', ['name' => $field], ['value' => $value]]); + $filtersApplied++; + } elseif(count($value) == 2){ + if(!$value[0]){ + $additionalCondition = ['<=', 'value', (int)$value[1]]; + } elseif(!$value[1]) { + $additionalCondition = ['>=', 'value', (int)$value[0]]; + } else { + $additionalCondition = ['between', 'value', (int)$value[0], (int)$value[1]]; + } + $subQuery->orFilterWhere(['and', ['name' => $field], $additionalCondition]); + + $filtersApplied++; + } + } + if($filtersApplied) { + $query->join('LEFT JOIN', ['f' => $subQuery], 'f.item_id = '.Item::tableName().'.item_id'); + $query->andFilterWhere(['f.filter_matched' => $filtersApplied]); + } + } + } + return $query; + } + private function findCategory($id_slug) { diff --git a/modules/catalog/api/CategoryObject.php b/modules/catalog/api/CategoryObject.php index e43f3e2..a4255ce 100644 --- a/modules/catalog/api/CategoryObject.php +++ b/modules/catalog/api/CategoryObject.php @@ -4,7 +4,6 @@ use yii\data\ActiveDataProvider; use yii\easyii\components\API; use yii\easyii\modules\catalog\models\Item; -use yii\easyii\modules\catalog\models\ItemData; use yii\helpers\Url; use yii\widgets\LinkPager; @@ -46,48 +45,10 @@ public function items($options = []) } else { $query->sortDate(); } - - if(!empty($options['filters']) && is_array($options['filters'])){ - - if(!empty($options['filters']['price'])){ - $price = $options['filters']['price']; - if(is_array($price) && count($price) == 2) { - if(!$price[0]){ - $query->andFilterWhere(['<=', 'price', (float)$price[1]]); - } elseif(!$price[1]) { - $query->andFilterWhere(['>=', 'price', (float)$price[0]]); - } else { - $query->andFilterWhere(['between', 'price', (float)$price[0], (float)$price[1]]); - } - } - unset($options['filters']['price']); - } - if(count($options['filters'])){ - $filtersApplied = 0; - $subQuery = ItemData::find()->select('item_id, COUNT(*) as filter_matched')->groupBy('item_id'); - foreach($options['filters'] as $field => $value){ - if(!is_array($value)) { - $subQuery->orFilterWhere(['and', ['name' => $field], ['value' => $value]]); - $filtersApplied++; - } elseif(count($value) == 2){ - if(!$value[0]){ - $additionalCondition = ['<=', 'value', (float)$value[1]]; - } elseif(!$value[1]) { - $additionalCondition = ['>=', 'value', (float)$value[0]]; - } else { - $additionalCondition = ['between', 'value', (float)$value[0], (float)$value[1]]; - } - $subQuery->orFilterWhere(['and', ['name' => $field], $additionalCondition]); - - $filtersApplied++; - } - } - if($filtersApplied) { - $query->join('LEFT JOIN', ['f' => $subQuery], 'f.item_id = '.Item::tableName().'.item_id'); - $query->andFilterWhere(['f.filter_matched' => $filtersApplied]); - } - } + if(!empty($options['filters'])){ + $query = Catalog::applyFilters($options['filters'], $query); } + $this->_adp = new ActiveDataProvider([ 'query' => $query, 'pagination' => !empty($options['pagination']) ? $options['pagination'] : []