diff --git a/.htaccess b/.htaccess index 0989a50..bb5adae 100755 --- a/.htaccess +++ b/.htaccess @@ -144,12 +144,7 @@ AddDefaultCharset utf-8 RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{THE_REQUEST} \s/+(.+?)/+[?\s] - RewriteRule /$ /%1 [R,L] - - - - RewriteCond %{THE_REQUEST} \s/+(.+?)/+[?\s] - RewriteRule /$ /%1 [R,L] + RewriteRule /$ /%1 [R=301,L] RewriteCond %{HTTP_HOST} ^([^www].*)$ diff --git a/common/models/FilterCache.php b/common/models/FilterCache.php new file mode 100644 index 0000000..81ee210 --- /dev/null +++ b/common/models/FilterCache.php @@ -0,0 +1,88 @@ + 'ID', + 'options_key' => 'Options Key', + 'category_id' => 'Category ID', + 'depth' => 'Depth', + 'count' => 'Count', + ]; + } + + /** + * @param array $optionsIdArray + * @return string + */ + static function createCacheKey($optionsIdArray){ + if(!empty($optionsIdArray)){ + sort($optionsIdArray); + $string = implode('',$optionsIdArray); + $key = md5($string); + return $key; + } else { + return 0; + } + } + + + /** + * @param $groups + * @param $params + * @return array + */ + + static function convertAliasToId($groups,$params){ + $optionsAlias = ArrayHelper::index($groups, 'option_alias'); + $convertedParams = []; + foreach($params as $key => $options){ + foreach($options as $option){ + if(isset($optionsAlias[$option]) && !in_array($optionsAlias[$option]['tax_option_id'],$convertedParams)){ + $convertedParams[] = $optionsAlias[$option]['tax_option_id']; + } + } + } + return $convertedParams; + + } + +} diff --git a/common/modules/product/helpers/FilterHelper.php b/common/modules/product/helpers/FilterHelper.php index 9f4a573..ff8e734 100755 --- a/common/modules/product/helpers/FilterHelper.php +++ b/common/modules/product/helpers/FilterHelper.php @@ -3,10 +3,16 @@ namespace common\modules\product\helpers; +use common\modules\product\models\Brand; +use common\modules\product\models\Category; +use common\modules\product\models\Product; +use common\modules\product\models\ProductVariant; use common\modules\rubrication\models\TaxGroup; use yii\base\Object; use Yii; +use yii\db\Query; use yii\db\QueryInterface; +use yii\db\ActiveQuery; use yii\helpers\ArrayHelper; class FilterHelper extends Object { @@ -24,7 +30,7 @@ class FilterHelper extends Object { } - /* + /** * Return custom filter-option link * @var array $filter * @var array $options @@ -84,4 +90,157 @@ class FilterHelper extends Object { + /** + * @param ActiveQuery $query + * @param array $params + */ + public static function setNewQueryParams($query, $params) + { + $last_query = null; + foreach ($params as $key => $param) { + switch ($key) { + case 'special': + self::filterSpecial($param, $query); + break; + case 'brands': + self::filterBrands($param, $query); + break; + case 'keywords': + self::filterKeywords($param, $query); + break; + case 'prices': + self::filterPrices($param, $query); + break; + default: + $last_query = self::filterOptions($param, $last_query); + break; + } + } + if(!empty($last_query)) { + $query->andWhere(['product.product_id' => $last_query]); + } + } + + private static function filterOptions(array $params, Query $last_query = null) + { + $variant_query = ( new Query() )->distinct() + ->select('product_variant.product_id as products') + ->from('product_variant_option') + ->innerJoin( + 'product_variant', + 'product_variant_option.product_variant_id = product_variant.product_variant_id' + ) + ->innerJoin('tax_option', 'tax_option.tax_option_id = product_variant_option.option_id') + ->where([ 'tax_option.alias' => $params ]); + $product_query = ( new Query() )->distinct() + ->select('product_option.product_id as products') + ->from('product_option') + ->innerJoin('tax_option', 'product_option.option_id = tax_option.tax_option_id') + ->where( + [ 'tax_option.alias' => $params ] + )->union($variant_query); + $query = (new Query())->select('products')->from(['result_table' => $product_query]); + if (!empty( $last_query )) { + $query->andWhere([ 'product.product_id' => $last_query ]); + } + return $query; + } + + private static function filterSpecial(array $params, ActiveQuery $query) + { + $conditions = []; + /** + * @var string $key + */ + foreach ($params as $key => $param) { + $conditions[] = [ + '=', + Product::tableName() . '.' . $key, + $param, + ]; + } + /* If 2 or more special conditions get all that satisfy at least one of them. */ + if (count($conditions) > 1) { + array_unshift($conditions, 'or'); + } else { + $conditions = $conditions[ 0 ]; + } + $query->andFilterWhere($conditions); + } + + private static function filterBrands(array $param, ActiveQuery $query) + { + $query->andFilterWhere([ Product::tableName() . '.brand_id' => $param ]); + } + + private static function filterKeywords(array $params, ActiveQuery $query) + { + $conditions = []; + if (!empty( $params )) { + if (!is_array($params)) { + $params = [ $params ]; + } + /** + * @var string $param Inputed keyword + */ + foreach ($params as $param) { + $conditions[] = [ + 'or', + [ + 'ilike', + Product::tableName() . '.name', + $param, + ], + [ + 'ilike', + Brand::tableName() . '.name', + $param, + ], + [ + 'ilike', + Category::tableName() . '.name', + $param, + ], + [ + 'ilike', + ProductVariant::tableName() . '.sku', + $param, + ], + ]; + } + } + if (count($conditions) > 1) { + array_unshift($conditions, 'or'); + } else { + $conditions = $conditions[ 0 ]; + } + $query->andFilterWhere($conditions); + } + + private static function filterPrices(array $params, ActiveQuery $query) + { + $conditions = []; + if (!empty( $params[ 'min' ] ) && $params[ 'min' ] > 0) { + $conditions[] = [ + '>=', + ProductVariant::tableName() . '.price', + $params[ 'min' ], + ]; + } + if (!empty( $params[ 'max' ] ) && $params[ 'max' ] > 0) { + $conditions[] = [ + '<=', + ProductVariant::tableName() . '.price', + $params[ 'max' ], + ]; + } + if (count($conditions) > 1) { + array_unshift($conditions, 'and'); + } else { + $conditions = $conditions[ 0 ]; + } + $query->andFilterWhere($conditions); + } + + } \ No newline at end of file diff --git a/common/modules/product/helpers/ProductHelper.php b/common/modules/product/helpers/ProductHelper.php index 97ebc85..1935100 100755 --- a/common/modules/product/helpers/ProductHelper.php +++ b/common/modules/product/helpers/ProductHelper.php @@ -31,7 +31,7 @@ return Brand::find(); // ->with('brandName') } - /* + /** * Return custom filter-option link * @var array $filter * @var array $options @@ -269,7 +269,49 @@ } } - + + /** + * @param ActiveQuery $query + * @param $params + * @param bool $setPriceLimits + */ + public static function setCacheQueryParams(&$query, $params) + { + + + foreach($params as $key => $param) { + + + $query->andWhere( + Product::tableName() . '.product_id IN ( + SELECT DISTINCT products + FROM ( + SELECT product_id AS products FROM product WHERE product_id IN( + SELECT product_id FROM product_option + INNER JOIN tax_option ON tax_option.tax_option_id = product_option.option_id + INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id + WHERE tax_group.alias = \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\')) + OR product_id IN ( + (SELECT product_id AS products + FROM product_variant_option + INNER JOIN product_variant ON product_variant_option.product_variant_id = product_variant.product_variant_id + INNER JOIN tax_option ON tax_option.tax_option_id = product_variant_option.option_id + INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id + WHERE tax_group.alias = \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\')) + ) + ) AS table_name + )' + ); + } + + + } + + + + + + public static function productCountQuery($category = NULL, $params, $excludeKeys = [ ]) { $p = [ ]; diff --git a/common/modules/product/models/Category.php b/common/modules/product/models/Category.php index 9cff907..0c9870a 100755 --- a/common/modules/product/models/Category.php +++ b/common/modules/product/models/Category.php @@ -5,7 +5,8 @@ namespace common\modules\product\models; use common\components\artboxtree\ArtboxTreeBehavior; use common\components\artboxtree\ArtboxTreeHelper; - +use common\modules\rubrication\models\TaxOption; +use yii\db\ActiveQuery; use common\modules\rubrication\models\TaxGroup; use Yii; @@ -35,6 +36,7 @@ use common\behaviors\Slug; * @property ProductUnit $productUnit * @property CategoryName[] $categoryNames * @property ProductCategory[] $productCategories + * @property TaxGroup $TaxGroup */ class Category extends \yii\db\ActiveRecord { @@ -195,56 +197,99 @@ class Category extends \yii\db\ActiveRecord ProductCategory::deleteAll(['category_id' => $this->category_id]); return true; } + + /** + * @param array $product_id + * @param array $product_variant_id + * @return ActiveQuery + */ + + public function getFilterQuery( $product_id = [], $product_variant_id = []){ + $query1 = (new Query()) + ->distinct() + ->select([ + 'option_id' + ]) + ->from('tax_option') + ->innerJoin('product_variant_option', 'tax_option.tax_option_id = product_variant_option.option_id') + ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') + ->innerJoin('product_variant', 'product_variant.product_variant_id = product_variant_option.product_variant_id') + ->innerJoin('product', 'product.product_id = product_variant.product_id') + ->innerJoin('product_category', 'product_category.product_id = product.product_id') + ->innerJoin('tax_group_to_category', 'tax_group.tax_group_id = tax_group_to_category.tax_group_id') + ->where(['product_category.category_id' => $this->category_id, + 'tax_group.is_filter' => TRUE, + 'tax_group_to_category.category_id'=>$this->category_id, + + ]) + ->filterWhere([ + 'product_variant_option.product_variant_id' => $product_variant_id + ]) + ->andWhere(['!=', 'product_variant.status', 1]); + + $query2 = (new Query()) + ->distinct() + ->select([ + 'option_id' + ]) + ->from('tax_option') + ->innerJoin('product_option', 'tax_option.tax_option_id = product_option.option_id') + ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') + ->innerJoin('product', 'product.product_id = product_option.product_id') + ->innerJoin('product_category', 'product_category.product_id = product.product_id') + ->innerJoin('product_variant', 'product_variant.product_id = product.product_id') + ->innerJoin('tax_group_to_category', 'tax_group.tax_group_id = tax_group_to_category.tax_group_id') + ->where(['product_category.category_id' => $this->category_id, + 'tax_group.is_filter' => TRUE, + 'tax_group_to_category.category_id'=>$this->category_id, + ]) + ->filterWhere([ + 'product_option.product_id' => $product_id + ]) + ->andWhere(['!=', 'product_variant.status', 1]); + $query3 = (new Query()) + ->select([ + 'tax_option.*', + 'tax_group.*', + 'tax_option.alias as option_alias', + 'tax_group.alias as group_alias', + 'tax_option.name as value', + 'tax_option.sort AS tax_option_sort', + 'tax_group.sort AS tax_group_sort', + ]) + ->from(['tax_option' ]) + ->where(['tax_option.tax_option_id'=>$query1->union($query2)]) + + ->innerJoin('tax_group','tax_group.tax_group_id = tax_option.tax_group_id') + ->orderBy('tax_option.sort, tax_group.sort'); + return $query3; + } + + /** + * @return mixed + * @throws \Exception + */ + + public function getActiveFilters() { return Category::getDb()->cache(function(){ - $query1 = (new Query()) - ->distinct() - ->select([ - 'option_id' - ]) - ->from('tax_option') - ->innerJoin('product_variant_option', 'tax_option.tax_option_id = product_variant_option.option_id') - ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') - ->innerJoin('product_variant', 'product_variant.product_variant_id = product_variant_option.product_variant_id') - ->innerJoin('product', 'product.product_id = product_variant.product_id') - ->innerJoin('product_category', 'product_category.product_id = product.product_id') - ->innerJoin('tax_group_to_category', 'tax_group.tax_group_id = tax_group_to_category.tax_group_id') - ->where(['product_category.category_id' => $this->category_id, 'tax_group.is_filter' => TRUE,'tax_group_to_category.category_id'=>$this->category_id]) - ->andWhere(['!=', 'product_variant.status', 1]); - - $query2 = (new Query()) - ->distinct() - ->select([ - 'option_id' - ]) - ->from('tax_option') - ->innerJoin('product_option', 'tax_option.tax_option_id = product_option.option_id') - ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') - ->innerJoin('product', 'product.product_id = product_option.product_id') - ->innerJoin('product_category', 'product_category.product_id = product.product_id') - ->innerJoin('product_variant', 'product_variant.product_id = product.product_id') - ->innerJoin('tax_group_to_category', 'tax_group.tax_group_id = tax_group_to_category.tax_group_id') - ->where(['product_category.category_id' => $this->category_id, 'tax_group.is_filter' => TRUE,'tax_group_to_category.category_id'=>$this->category_id]) - ->andWhere(['!=', 'product_variant.status', 1]); - $query3 = (new Query()) - ->select([ - 'tax_option.*', - 'tax_group.*', - 'tax_option.alias as option_alias', - 'tax_group.alias as group_alias', - 'tax_option.name as value', - 'tax_option.sort AS tax_option_sort', - 'tax_group.sort AS tax_group_sort', - ]) - ->from(['tax_option' ]) - ->where(['tax_option.tax_option_id'=>$query1->union($query2)]) - ->innerJoin('tax_group','tax_group.tax_group_id = tax_option.tax_group_id') - ->orderBy('tax_option.sort, tax_group.sort'); - return $query3->all(); + return $this->getFilterQuery()->all(); }, 3600); } + /** + * @param array $product_id + * @param array $product_variant_id + * @return mixed + */ + public function getSelectFilters($product_id = [], $product_variant_id = []) { + + return $this->getFilterQuery($product_id, $product_variant_id)->select('tax_option_id')->column(); + + } + + public function getTaxGroupsForMenu() { @@ -282,4 +327,5 @@ class Category extends \yii\db\ActiveRecord return $this->hasMany(TaxGroup::className(), ['tax_group_id' => 'tax_group_id']) ->viaTable('tax_group_to_category', ['category_id' => 'category_id']); } + } diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index a97dd22..57b69c9 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -504,4 +504,5 @@ class Product extends \yii\db\ActiveRecord ->viaTable('product_option',[ 'product_id'=> 'product_id']) ->joinWith('taxGroup'); } + } diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php index 4090e14..1bc052d 100755 --- a/common/modules/product/models/ProductVariant.php +++ b/common/modules/product/models/ProductVariant.php @@ -215,6 +215,10 @@ class ProductVariant extends \yii\db\ActiveRecord return $this->hasMany(TaxOption::className(), ['tax_option_id' => 'option_id'])->viaTable('product_variant_option', ['product_variant_id' => 'product_variant_id']); } + public function setOptions($value){ + $this->_options = $value; + } + public function getProperties() { $groups = $options = []; foreach ($this->options as $option) { diff --git a/common/widgets/views/order.php b/common/widgets/views/order.php index 0ad3cff..5d566f9 100755 --- a/common/widgets/views/order.php +++ b/common/widgets/views/order.php @@ -3,7 +3,7 @@
-