Skip to content

Commit

Permalink
new filter
Browse files Browse the repository at this point in the history
  • Loading branch information
andrtechno committed Aug 21, 2021
1 parent 8200ccb commit 69cb9f8
Show file tree
Hide file tree
Showing 18 changed files with 1,644 additions and 48 deletions.
10 changes: 6 additions & 4 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Module extends WebModule implements BootstrapInterface
public $filterViewCurrent = '@shop/widgets/filtersnew/views/current';
public $reviewsCount = 0;
public $viewList = ['grid','list'];

/*
public function getImage($dirtyAlias)
{
//Get params
Expand Down Expand Up @@ -61,7 +63,7 @@ public function parseImageAlias($parameterized)
return ['alias' => $alias, 'size' => $size];
}
}*/


/**
Expand All @@ -71,7 +73,7 @@ public function parseImageAlias($parameterized)
*
* @param $notParsedSize
* @return array|null
*/
public function parseSize($notParsedSize)
{
$sizeParts = explode('x', $notParsedSize);
Expand Down Expand Up @@ -103,14 +105,14 @@ public function parseSize($notParsedSize)
}
return $size;
}
}*/

/**
* @inheritdoc
*/
public function bootstrap($app)
{

$rules['filter'] = 'shop/ajax/filter';
$rules['catalog'] = 'shop/default/index';
$rules['search/ajax'] = 'shop/search/ajax';
$rules['notify/<id:\d+>'] = 'shop/notify/index';
Expand Down
29 changes: 29 additions & 0 deletions commands/DevController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use panix\mod\shop\models\Currency;
use panix\mod\shop\models\Product;
use panix\mod\shop\models\ProductCategoryRef;
use panix\mod\shop\models\ProductFilter;
use Yii;
use panix\engine\console\controllers\ConsoleController;
use yii\base\Exception;
use yii\helpers\Console;
use yii\httpclient\Client;

Expand Down Expand Up @@ -138,7 +140,34 @@ public function actionIndex()
}


public function actionNewFilter()
{
$products = Product::find()->all();
foreach ($products as $p) {

foreach ($p->getEavAttributes() as $eavName => $eavOption) {
$data[]=[
$p->id,
$eavOption
];

/*$f = new ProductFilter();
$f->product_id = $p->id;
$f->option_id = $eavOption;
try {
$f->save(false);
} catch (Exception $exception) {
}*/
}

//print_r($p->getEavAttributes());die;
}
ProductFilter::getDb()->createCommand()->batchInsert(ProductFilter::tableName(), [
'product_id',
'option_id',
], $data)->execute();
}

private function array_random(array $array, int $n = 1): array
{
Expand Down
95 changes: 79 additions & 16 deletions components/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function getActiveFilters()
public function getCategoryAttributesCallback()
{
$data = [];

$active = $this->activeAttributes;
foreach ($this->_eavAttributes as $attribute) {
$data[$attribute->name] = [
'title' => $attribute->title,
Expand All @@ -290,13 +290,30 @@ public function getCategoryAttributesCallback()
];
$totalCount = 0;
$filtersCount = 0;
foreach ($attribute->getOptions()->cache($this->cacheDuration)->all() as $option) {
foreach ($attribute->getOptions()->all() as $option) {
$count = $this->countAttributeProductsCallback($attribute, $option);
//$count=1;
//if ($count > 1) {


$ss = array_key_first($active);
//print_r($ss);
$countText = $count;
if(isset($active[$attribute->name])){

//if($active[$attribute->name] != $attribute->name){
// $countText = '+'.$count;
//}

if($ss == $attribute->name && $count){
$countText = '+'.$count;
}
}

$data[$attribute->name]['filters'][] = [
'title' => $option->value,
'count' => (int)$count,
'count_text' => $countText,
'data' => unserialize($option->data),
'abbreviation' => ($attribute->abbreviation) ? $attribute->abbreviation : null,
'key' => $attribute->name,
Expand All @@ -318,6 +335,9 @@ public function getCategoryAttributesCallback()
public function getCategoryAttributes()
{
$data = [];
$active = $this->activeAttributes;

$first = array_key_first($active);

foreach ($this->_eavAttributes as $attribute) {
$data[$attribute->name] = [
Expand All @@ -332,10 +352,22 @@ public function getCategoryAttributes()
foreach ($attribute->getOptions()->cache($this->cacheDuration)->all() as $option) {
$count = $this->countAttributeProducts($attribute, $option);
//$count=1;


//print_r($ss);
$countText = $count;
if(isset($active[$attribute->name])){

// if($first == $attribute->name){
$countText = '+'.$count;
// }
}

//if ($count > 1) {
$data[$attribute->name]['filters'][] = [
'title' => $option->value,
'count' => (int)$count,
'count_text' => $countText,
'data' => unserialize($option->data),
'abbreviation' => ($attribute->abbreviation) ? $attribute->abbreviation : null,
'key' => $attribute->name,
Expand Down Expand Up @@ -367,19 +399,38 @@ public function countAttributeProducts($attribute, $option)

$model->select('COUNT(*)');
$newData = [];

$newData[$attribute->name][] = $option->id;

$newData2=[];
$ss = array_key_first($this->activeAttributes);

foreach ($this->activeAttributes as $key=>$p){
if($key!=$attribute->name) {
$newData[$key] = $p;

}
}



//print_r($newData);die;
//$newData = ArrayHelper::merge($newData,$this->activeAttributes);

// echo $model->createCommand()->rawSql;
// echo '<br><br><br>';
/** @var EavQueryTrait|ActiveQuery $model */
//$model->withEavAttributes($newData);

// $model->withEavAttributes($newData);
//print_r($newData);die;
$model->getFindByEavAttributes2($newData);



//$model->applyAttributes($newData2);

// echo $model->createCommand()->rawSql;die;
$model->groupBy = false;
// $model->groupBy(''.Product::tableName().'.`id`');
// $model->distinct(true);
// $model->cache($this->cacheDuration);
//$res->distinct(false);
// print_r($newData);die;
Expand All @@ -395,7 +446,7 @@ public function countAttributeProducts($attribute, $option)
// $count = Attribute::getDb()->cache(function () use ($model) {
// return $model->count();
// }, 1, $dependency);
if ($attribute->name == 'obem') {
if ($attribute->name == 'tip') {
//echo $model->createCommand()->rawSql;die;
}
return $model->createCommand()->queryScalar();
Expand All @@ -411,19 +462,30 @@ public function countAttributeProductsCallback($attribute, $option)
$model->select('COUNT(*)');
$newData = [];
$newData[$attribute->name][] = $option->id;
$firstItem = array_key_first($this->activeAttributes);
// print_r($this->activeAttributes);die;

if ($attribute->name == $firstItem) {
$newData = ArrayHelper::merge($newData, $this->activeAttributes);
$newData2=[];
foreach ($this->activeAttributes as $key=>$p){
if($key!=$attribute->name) {
$newData[$key] = $p;
}
}




//$newData = ArrayHelper::merge($newData,$this->activeAttributes);

// echo $model->createCommand()->rawSql;
// echo '<br><br><br>';
/** @var EavQueryTrait|ActiveQuery $model */
//$model->withEavAttributes($newData);
$model->getFindByEavAttributes3($newData);
// $model->withEavAttributes($newData);

$model->groupBy = false;
$model->cache($this->cacheDuration);
//$res->distinct(false);
$model->getFindByEavAttributes2($newData);

// $model->groupBy = false;
//$model->cache($this->cacheDuration);
// $res->distinct(true);
// print_r($newData);die;


Expand All @@ -437,8 +499,9 @@ public function countAttributeProductsCallback($attribute, $option)
// $count = Attribute::getDb()->cache(function () use ($model) {
// return $model->count();
// }, 1, $dependency);
if ($attribute->name == 'obem') {
//echo $model->createCommand()->rawSql;die;
Yii::info($model->createCommand()->rawSql);
if ($attribute->name == 'tip') {
// echo $model->createCommand()->rawSql;die;
}
return $model->createCommand()->queryScalar();
}
Expand Down
6 changes: 3 additions & 3 deletions components/FilterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ class FilterController extends WebController
public $per_page;
public $filter;

public function actionFilterCallback($category_id)
public function actionFilterCallback()
{

/** @var Product $productModel */
$productModel = Yii::$app->getModule('shop')->model('Product');
$query = $productModel::find();
$query->published();


/*
if ($category_id) {
$category = Category::findOne($category_id);
if (!$category)
$this->error404();
$query->applyCategories($category);
}
}*/



Expand Down
45 changes: 45 additions & 0 deletions controllers/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace panix\mod\shop\controllers;

use panix\mod\shop\components\Filter;
use panix\mod\shop\models\Category;
use Yii;
use panix\engine\controllers\WebController;
use yii\helpers\ArrayHelper;

class AjaxController extends WebController
{
Expand All @@ -21,4 +24,46 @@ public function actionCurrency($id)
return $this->goHome();
}
}


public function actionFilter()
{
$productModel = Yii::$app->getModule('shop')->model('Product');
$query = $productModel::find();
$query->published();
$category_id=9;
if ($category_id) {
$category = Category::findOne($category_id);
if (!$category)
$this->error404();
$query->applyCategories($category);
}
$filterPost = Yii::$app->request->post('filter');
if ($filterPost){
if(isset($filterPost['brand'])){
$query->applyBrands($filterPost['brand']);
}
//unset(Yii::$app->request->post('filter')['brand']);
// $query->getFindByEavAttributes3($filterPost);
}
//echo $query->createCommand()->rawSql;die;

$filter = new Filter($query, $category);


//print_r($f->getPostActiveAttributes());die;
$attributes = $filter->getCategoryAttributesCallback();
$brands = $filter->getCategoryBrandsCallback();
$total = 0;
$results = ArrayHelper::merge($attributes, ['brand' => $brands]);



// $total = $filter->count();
return $this->asJson([
'textTotal'=>"Показать ".Yii::t('shop/default','PRODUCTS_COUNTER',$total),
'totalCount' => $total,
'filters' => $results
]);
}
}
7 changes: 6 additions & 1 deletion controllers/CatalogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class CatalogController extends FilterController

public $provider;
public $currentUrl;

public function actionFilterCallback()
{
return $this->asJson(['ok' => true]);
}
public function beforeAction($action)
{
if (Yii::$app->request->headers->has('filter-ajax')) {
Expand All @@ -51,6 +54,8 @@ public function actionView()
$this->query->published();




// $cr->with = array('brandActive');
// Скрывать товары если бренд скрыт.
//TODO: если у товара не выбран бренд то он тоже скрывается!! need fix
Expand Down
Loading

0 comments on commit 69cb9f8

Please sign in to comment.