Skip to content

Commit

Permalink
filters rework
Browse files Browse the repository at this point in the history
  • Loading branch information
noumo committed May 23, 2015
1 parent b870eb2 commit f2575d9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
51 changes: 51 additions & 0 deletions modules/catalog/api/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
{
Expand Down
45 changes: 3 additions & 42 deletions modules/catalog/api/CategoryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'] : []
Expand Down

0 comments on commit f2575d9

Please sign in to comment.