diff --git a/backend/controllers/ArticlesController.php b/backend/controllers/ArticlesController.php index 15b6dd4..f2ef542 100755 --- a/backend/controllers/ArticlesController.php +++ b/backend/controllers/ArticlesController.php @@ -81,12 +81,17 @@ class ArticlesController extends Controller if ($model->load(Yii::$app->request->post())) { - if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + if ( ($image = UploadedFile::getInstance($model, 'image')) ) { $model->image = $image->name; } + $imgDir = Yii::getAlias('@storage/articles/'); + + if(!is_dir($imgDir)) { + mkdir($imgDir, 0755, true); + } if ($model->save() && $image) { - $image->saveAs(Yii::getAlias('@imagesDir/articles/' . $image->name)); + $image->saveAs(Yii::getAlias('@storage/articles/' . $image->name)); } return $this->redirect(['view', 'id' => $model->id]); @@ -108,13 +113,17 @@ class ArticlesController extends Controller $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { - - if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + if ( ($image = UploadedFile::getInstance($model, 'image')) ) { $model->image = $image->name; } + $imgDir = Yii::getAlias('@storage/articles/'); + + if(!is_dir($imgDir)) { + mkdir($imgDir, 0755, true); + } if ($model->save() && $image) { - $image->saveAs(Yii::getAlias('@imagesDir/articles/' . $image->name)); + $image->saveAs(Yii::getAlias('@storage/articles/' . $image->name)); } return $this->redirect(['view', 'id' => $model->id]); diff --git a/backend/views/articles/_form.php b/backend/views/articles/_form.php index f7f997a..f5eb6f8 100755 --- a/backend/views/articles/_form.php +++ b/backend/views/articles/_form.php @@ -37,7 +37,7 @@ use yii\jui\DatePicker; ]) ?> - field($model, 'imageUpload')->widget(\kartik\file\FileInput::classname(), [ + field($model, 'image')->widget(\kartik\file\FileInput::classname(), [ 'language' => 'ru', 'options' => [ 'accept' => 'image/*', diff --git a/common/config/bootstrap.php b/common/config/bootstrap.php index 16e7a8c..8a3c9bc 100755 --- a/common/config/bootstrap.php +++ b/common/config/bootstrap.php @@ -5,5 +5,5 @@ Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend'); Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console'); Yii::setAlias('storage', dirname(dirname(__DIR__)) . '/storage'); -Yii::setAlias('@imagesDir', dirname(dirname(__DIR__)) . '/frontend/web/images'); -Yii::setAlias('@imagesUrl', '/images'); +Yii::setAlias('@imagesDir', dirname(dirname(__DIR__)) . '/storage'); +Yii::setAlias('@imagesUrl', '/storage'); diff --git a/common/models/Articles.php b/common/models/Articles.php index 50e7139..7b88a0b 100755 --- a/common/models/Articles.php +++ b/common/models/Articles.php @@ -31,6 +31,23 @@ class Articles extends \yii\db\ActiveRecord return 'articles'; } + + + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + 'slug' => [ + 'class' => 'common\behaviors\Slug', + 'in_attribute' => 'title', + 'out_attribute' => 'translit', + 'translit' => true + ] + ]; + } + /** * @inheritdoc */ @@ -38,7 +55,7 @@ class Articles extends \yii\db\ActiveRecord { return [ [['date'], 'safe'], - [['title', 'body', 'image', 'translit'], 'required'], + [['title', 'body'], 'required'], [['body', 'seo_text'], 'string'], [['title', 'image', 'translit', 'meta_title', 'meta_keywords', 'meta_description', 'h1'], 'string', 'max' => 255], [['imageUpload'], 'safe'], diff --git a/common/modules/product/CatalogUrlManager.php b/common/modules/product/CatalogUrlManager.php index 6bbcdea..8e3211f 100755 --- a/common/modules/product/CatalogUrlManager.php +++ b/common/modules/product/CatalogUrlManager.php @@ -2,6 +2,7 @@ namespace common\modules\product; +use common\modules\product\helpers\FilterHelper; use common\modules\product\models\Brand; use common\modules\product\models\BrandSearch; use common\modules\product\models\CategorySearch; @@ -45,9 +46,15 @@ class CatalogUrlManager implements UrlRuleInterface { } if (!empty($paths[2])) { // Filter - if (strpos($paths[2], 'filter:') === 0) { + + if (strpos($paths[2], 'filters:') === 0) { $this->parseFilter($paths[2], $params); } + else if(strpos($paths[2], 'filter:') === 0){ + $this->parseOldFilter($paths[2], $params); + //['catalog/category', 'category' => $category, 'filters' =>$params['filter']] + Yii::$app->response->redirect(['catalog/category', 'category' => $category, 'filters' =>$params['filter']],301); + } else { throw new HttpException(404 ,'Page not found'); } @@ -72,7 +79,7 @@ class CatalogUrlManager implements UrlRuleInterface { $params['brand'] = $brand; if (!empty($paths[2])) { // Filter - if (strpos($paths[2], 'filter:') === 0) { + if (strpos($paths[2], 'filters:') === 0) { $this->parseFilter($paths[2], $params); } } @@ -158,24 +165,24 @@ class CatalogUrlManager implements UrlRuleInterface { private function setFilterUrl(&$params, &$url) { $filter = []; - if (!empty($params['filter'])) { - foreach ($params['filter'] as $key => $values) { + if (!empty($params['filters'])) { + foreach ($params['filters'] as $key => $values) { switch($key) { case 'prices': $filter[] = $key .'='. implode(':', $values); break; - case 'options': - foreach($values as $group => &$value_items) { - foreach($value_items as &$value_item) { - $value_item = $this->option_value_encode($value_item); - if (empty($value_item)) { - unset($value_item); - } - } - $filter[] = $this->option_prefix. $group .'='. implode(',', $value_items); - } - break; +// case 'options': +// foreach($values as $group => &$value_items) { +// foreach($value_items as &$value_item) { +// $value_item = $this->option_value_encode($value_item); +// if (empty($value_item)) { +// unset($value_item); +// } +// } +// $filter[] = $this->option_prefix. $group .'='. implode(',', $value_items); +// } +// break; default: foreach($values as &$value) { @@ -189,13 +196,37 @@ class CatalogUrlManager implements UrlRuleInterface { } } if (!empty($filter)) { - $url .= 'filter:'. implode(';', $filter); + $url .= 'filters:'. implode(';', $filter); } - unset($params['filter']); + unset($params['filters']); } } private function parseFilter($paths, &$params) { + $params['filters'] = []; + $filter_str = substr($paths, 8); + $filter_options = explode(';', $filter_str); + foreach ($filter_options as $filter_option) { + if (empty($filter_option)) { + continue; + } + list($filter_key, $filter_option) = explode('=', $filter_option); + if($filter_key == 'prices') { // price-interval section + $prices = explode(':', $filter_option); + $params['filters'][$filter_key] = [ + 'min' => floatval($prices[0]), + 'max' => floatval($prices[1]), + ]; + } + else { // brands and other sections + $params['filters'][$filter_key] = explode(',', $filter_option); + } + } + } + + + + private function parseOldFilter($paths, &$params) { $params['filter'] = []; $filter_str = substr($paths, 7); $filter_options = explode(';', $filter_str); @@ -210,9 +241,11 @@ class CatalogUrlManager implements UrlRuleInterface { 'min' => floatval($prices[0]), 'max' => floatval($prices[1]), ]; - } elseif (strpos($filter_key, $this->option_prefix) === 0) { // options section - $params['filter']['options'][substr($filter_key, 2)] = explode(',', $filter_option); - } else { // brands and other sections + } + elseif (strpos($filter_key, $this->option_prefix) === 0) { // options section + $params['filter'][substr($filter_key, 2)] = explode(',', $filter_option); + } + else { // brands and other sections $params['filter'][$filter_key] = explode(',', $filter_option); } } diff --git a/common/modules/product/controllers/VariantController.php b/common/modules/product/controllers/VariantController.php index 9abdd3e..0b9cfa7 100755 --- a/common/modules/product/controllers/VariantController.php +++ b/common/modules/product/controllers/VariantController.php @@ -150,31 +150,42 @@ class VariantController extends Controller $ProductStocks = Yii::$app->request->post('ProductStock'); - foreach($ProductStocks as $index => $ProductStock){ - $stock = Stock::find()->where(['name'=>$ProductStock['name']])->one(); - if(!$stock instanceof Stock){ - $stock = new Stock(); - $stock->name = $ProductStock['name']; - $stock->save(); - } + $quantity = 0; + + if(is_array($ProductStocks)){ + + foreach($ProductStocks as $index => $ProductStock){ + $stock = Stock::find()->where(['name'=>$ProductStock['name']])->one(); + if(!$stock instanceof Stock){ + $stock = new Stock(); + $stock->name = $ProductStock['name']; + $stock->save(); + } + + $psModel = ProductStock::find()->where(['product_variant_id'=>$model->product_variant_id, 'stock_id' =>$stock->stock_id ])->one(); + if(!$psModel instanceof ProductStock){ + $psModel = new ProductStock; + $psModel->load(['ProductStock'=>$ProductStock]); + $psModel->stock_id = $stock->stock_id; + $psModel->product_variant_id = $model->product_variant_id; + $psModel->product_id = $model->product->product_id; + $quantity = $quantity + $psModel->quantity; + $psModel->validate(); + $psModel->save(); + } else { + $psModel->load(['ProductStock'=>$ProductStock]); + $quantity = $quantity + $psModel->quantity; + $psModel->save(); + } - $psModel = ProductStock::find()->where(['product_variant_id'=>$model->product_variant_id, 'stock_id' =>$stock->stock_id ]); - if(!$psModel instanceof ProductStock){ - $psModel = new ProductStock; - $psModel->load(['ProductStock'=>$ProductStock]); - $psModel->stock_id = $stock->stock_id; - $psModel->product_variant_id = $model->product_variant_id; - $psModel->product_id = $model->product->product_id; - $psModel->save(); - } else { - $psModel->load(['ProductStock'=>$ProductStock]); - $psModel->save(); } - + } else { + $model->unlinkAll('stocks',true); } - + $model->stock = $quantity; + $model->save(); } return $this->redirect(['index', 'product_id'=>$product_id]); diff --git a/common/modules/product/helpers/FilterHelper.php b/common/modules/product/helpers/FilterHelper.php new file mode 100755 index 0000000..e460691 --- /dev/null +++ b/common/modules/product/helpers/FilterHelper.php @@ -0,0 +1,89 @@ +where(['is_filter' => 'TRUE'])->all(),'alias'); + } + else + { + return static::$optionsList; + } + + } + + /* + * Return custom filter-option link + * @var array $filter + * @var array $options + * @return array + */ + public static function getFilterForOption($filter, $key, $value, $remove = false) { + + $optionsTemplate = self::optionsTemplate(); + array_unshift($optionsTemplate, "special", "brands"); + + $result = $filter; + + if (is_array($value)) { + foreach($value as $value_key => $value_items) { + if (!is_array($value_items)) { + $value_items = [$value_items]; + } + foreach($value_items as $value_item) { + if ($remove && isset($result[$key]) && ($i = array_search($value_item, $result[$key][$value_key])) !== FALSE) { + unset($result[$key][$value_key][$i]); + if (empty($result[$key][$value_key])) { + unset($result[$key][$value_key]); + } + } else { + if (!isset($result[$key][$value_key]) || array_search($value_item, $result[$key][$value_key]) === FALSE) { + $result[$key][$value_key][] = $value_item; + } + } + } + } + } else { + if ($remove && isset($result[$key]) && ($i = array_search($value, $result[$key])) !== FALSE) { + unset($result[$key][$i]); + if (empty($result[$key])) { + unset($result[$key]); + } + } else { + if (!isset($result[$key]) || array_search($value, $result[$key]) === FALSE) { + $result[$key][] = $value; + } + } + } + + + $filterView = []; + + foreach($optionsTemplate as $optionKey){ + if(isset($result[$optionKey])){ + $filterView[$optionKey] = $result[$optionKey]; + } + + + } + + return $filterView; + } + + + +} \ No newline at end of file diff --git a/common/modules/product/helpers/ProductHelper.php b/common/modules/product/helpers/ProductHelper.php index a5eb723..96b3f9f 100755 --- a/common/modules/product/helpers/ProductHelper.php +++ b/common/modules/product/helpers/ProductHelper.php @@ -104,7 +104,7 @@ class ProductHelper extends Object { public static function getSimilarProducts($product, $count = 10) { if (!is_object($product)) { - $product = Product::findOne($product); + $product = Product::find()->where(['product_id'=>$product])->with('enabledVariants')->one(); } if (!$product->properties) { @@ -143,6 +143,8 @@ class ProductHelper extends Object { return $products; } + + public static function _setQueryParams(&$query, $params, $setPriceLimits = true) { if (!empty($params['keywords'])) { if (!is_array($params['keywords'])) { @@ -152,35 +154,39 @@ class ProductHelper extends Object { $query->orFilterWhere(['ilike', Product::tableName() .'.name', $keyword]); $query->orFilterWhere(['ilike', BrandName::tableName() .'.value', $keyword]); $query->orFilterWhere(['ilike', CategoryName::tableName() .'.value', $keyword]); - } - } - if (!empty($params['special'])) { - foreach($params['special'] as $key => $value) { - $query->orFilterWhere([Product::tableName() .'.'. $key => $value]); - } - } - if (!empty($params['brands'])) { - $query->andFilterWhere([Product::tableName() .'.brand_id' => $params['brands']]); - } - if (!empty($params['options'])) { - foreach ($params['options'] as $group => $options) { - foreach ($options as &$option) { - $option = "'$option'"; - } - $query->andWhere( - Product::tableName() . '.product_id IN (SELECT product_id AS products 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 LIKE \''. $group .'\' AND tax_option.alias IN (' . implode(',', $options) . '))' - ); + $query->orFilterWhere(['ilike', ProductVariant::tableName() .'.sku', $keyword]); } } - if ($setPriceLimits && !empty($params['prices'])) { - if ($params['prices']['min'] > 0) { - $query->andWhere(['>=', ProductVariant::tableName() .'.price', $params['prices']['min']]); - } - if ($params['prices']['max'] > 0) { - $query->andWhere(['<=', ProductVariant::tableName() .'.price', $params['prices']['max']]); + foreach($params as $key => $param){ + + switch ($key) { + case 'special': + foreach($param as $key => $value) { + $query->orFilterWhere([Product::tableName() .'.'. $key => $value]); + } + break; + case 'brands': + $query->andFilterWhere([Product::tableName() .'.brand_id' => $param]); + break; + case 'keywords': + break; + case 'prices': + if ($param['min'] > 0) { + $query->andWhere(['>=', ProductVariant::tableName() .'.price', $param['min']]); + } + if ($param['max'] > 0) { + $query->andWhere(['<=', ProductVariant::tableName() .'.price', $param['max']]); + } + break; + default: + $query->andWhere( + Product::tableName() . '.product_id IN (SELECT product_id AS products 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 LIKE \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\'))' + ); } + } + } public static function productCountQuery($category = null, $params, $excludeKeys = []) { diff --git a/common/modules/product/models/BrandSearch.php b/common/modules/product/models/BrandSearch.php index 14b0eda..f77d91f 100755 --- a/common/modules/product/models/BrandSearch.php +++ b/common/modules/product/models/BrandSearch.php @@ -114,25 +114,25 @@ class BrandSearch extends Brand ->with(['brandName']); - $queryCount = Product::find() - ->select(['COUNT(product.product_id)']) - ->where('product.brand_id = brand.brand_id'); - $queryCount->andWhere('(SELECT COUNT(pv.product_variant_id) FROM "product_variant" "pv" WHERE pv.stock != 0 AND pv.product_id = product.product_id) > 0'); - if (!empty($category)) { - $queryCount->andWhere('(SELECT COUNT(pc.product_id) FROM product_category pc WHERE pc.product_id = product.product_id AND pc.category_id = '. $category->category_id .') > 0'); - } - if (!empty($params['options'])) { - $queryCount->innerJoin('product_option', 'product_option.product_id = product.product_id'); - $queryCount->innerJoin('tax_option', 'tax_option.tax_option_id = product_option.option_id'); - $queryCount->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id'); - foreach ($params['options'] as $group => $options) { - $queryCount->andWhere([ - 'tax_group.alias' => $group, - 'tax_option.alias' => $options - ]); - } -// $query->addSelect("(SELECT COUNT(product_option.product_id) AS products 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 LIKE '$group' AND tax_option.alias IN (" . implode(',', $options) . ")) AS _items_count"); - } +// $queryCount = Product::find() +// ->select(['COUNT(product.product_id)']) +// ->where('product.brand_id = brand.brand_id'); +// $queryCount->andWhere('(SELECT COUNT(pv.product_variant_id) FROM "product_variant" "pv" WHERE pv.stock != 0 AND pv.product_id = product.product_id) > 0'); +// if (!empty($category)) { +// $queryCount->andWhere('(SELECT COUNT(pc.product_id) FROM product_category pc WHERE pc.product_id = product.product_id AND pc.category_id = '. $category->category_id .') > 0'); +// } +// if (!empty($params['options'])) { +// $queryCount->innerJoin('product_option', 'product_option.product_id = product.product_id'); +// $queryCount->innerJoin('tax_option', 'tax_option.tax_option_id = product_option.option_id'); +// $queryCount->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id'); +// foreach ($params['options'] as $group => $options) { +// $queryCount->andWhere([ +// 'tax_group.alias' => $group, +// 'tax_option.alias' => $options +// ]); +// } +//// $query->addSelect("(SELECT COUNT(product_option.product_id) AS products 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 LIKE '$group' AND tax_option.alias IN (" . implode(',', $options) . ")) AS _items_count"); +// } // $query->addSelect(['_items_count' => $queryCount]); // if ($productQuery) { diff --git a/common/modules/product/models/Export.php b/common/modules/product/models/Export.php index aa29e87..eeb9c99 100755 --- a/common/modules/product/models/Export.php +++ b/common/modules/product/models/Export.php @@ -31,7 +31,7 @@ class Export extends Model { }*/ $mods = []; - $filterString = $this->convertFilterToString($product->getFilters()); + //$filterString = $this->convertFilterToString($product->getFilters()); foreach ($product->variantsWithFilters as $variant) { @@ -45,6 +45,7 @@ class Export extends Model { '=' . $variant->stock; } + $fotos = []; // foreach ($product->images as $image) @@ -69,7 +70,7 @@ class Export extends Model { $product->name, '', ((! empty($product->description)) ? $product->description : ''), - $filterString, + '',//$filterString, (!empty($product->variant)) ? $product->variant->price_old : '', (!empty($product->variant)) ? $product->variant->price : '', intval($product->akciya), diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index efa99e3..37d2061 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -185,7 +185,7 @@ class Product extends \yii\db\ActiveRecord public function getEnabledVariants() { - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0]); + return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0])->joinWith('image'); } /* diff --git a/common/modules/product/models/ProductImage.php b/common/modules/product/models/ProductImage.php index e7ece0f..a69a9f2 100755 --- a/common/modules/product/models/ProductImage.php +++ b/common/modules/product/models/ProductImage.php @@ -81,14 +81,14 @@ class ProductImage extends \yii\db\ActiveRecord return $this->hasOne(Product::className(), ['product_variant_id' => 'product_variant_id']); } - /** - * @inheritdoc - * @return ProductImageQuery the active query used by this AR class. - */ - public static function find() - { - return new ProductImageQuery(get_called_class()); - } +// /** +// * @inheritdoc +// * @return ProductImageQuery the active query used by this AR class. +// */ +// public static function find() +// { +// return new ProductImageQuery(get_called_class()); +// } /** * fetch stored image file name with complete path diff --git a/common/modules/rubrication/models/TaxGroup.php b/common/modules/rubrication/models/TaxGroup.php index 3679faa..0f1ec51 100755 --- a/common/modules/rubrication/models/TaxGroup.php +++ b/common/modules/rubrication/models/TaxGroup.php @@ -51,7 +51,7 @@ class TaxGroup extends \yii\db\ActiveRecord */ public static function tableName() { - return '{{%tax_group}}'; + return 'tax_group'; } /** diff --git a/frontend/controllers/CatalogController.php b/frontend/controllers/CatalogController.php index 1fa4c4e..64a9c06 100755 --- a/frontend/controllers/CatalogController.php +++ b/frontend/controllers/CatalogController.php @@ -23,6 +23,7 @@ use yii\data\ActiveDataProvider; use yii\data\Pagination; use yii\data\Sort; use yii\db\ActiveQuery; +use yii\helpers\ArrayHelper; use yii\web\HttpException; class CatalogController extends \yii\web\Controller @@ -35,7 +36,7 @@ class CatalogController extends \yii\web\Controller { /** @var Category $category */ $category = Yii::$app->request->get('category'); - $filter = Yii::$app->request->get('filter', []); + $filter = Yii::$app->request->get('filters', []); if (empty($category->category_id) && empty($word)) { return $this->render( @@ -46,6 +47,8 @@ class CatalogController extends \yii\web\Controller $params = []; + $optionsList = ArrayHelper::getColumn(TaxGroup::find()->where(['is_filter' => 'TRUE'])->all(),'alias'); + if ( !empty($filter['brands']) ) { $brands = Brand::find()->select('brand_id')->where(['in', 'alias', $filter['brands']])->all(); $params['brands'] = []; @@ -69,16 +72,27 @@ class CatalogController extends \yii\web\Controller } } - if ( !empty($filter['options']) ) { - $params['options'] = $filter['options']; + +// if ( !empty($filter['options']) ) { +// $params['options'] = $filter['options']; +// } + + + foreach($optionsList as $optionList){ + + if(isset($filter[$optionList])){ + $params[$optionList] = $filter[$optionList]; + } + } + if ( !empty($filter['prices']) ) { $params['prices'] = $filter['prices']; } $productModel = new ProductFrontendSearch(); - $productQuery = $productModel->getSearchQuery($category, $params); + //$productQuery = $productModel->getSearchQuery($category, $params); $productProvider = $productModel->search($category, $params); $brandModel = new BrandSearch(); @@ -86,6 +100,8 @@ class CatalogController extends \yii\web\Controller $optionsQuery = $productModel->optionsForCategory($category, $params); $groups = []; + + foreach ($optionsQuery->all() as $option) { if (!isset($groups[$option->tax_group_id])) { $groups[$option->tax_group_id] = $option->taxGroup; diff --git a/frontend/controllers/SearchController.php b/frontend/controllers/SearchController.php index b46fff5..a60e80c 100644 --- a/frontend/controllers/SearchController.php +++ b/frontend/controllers/SearchController.php @@ -18,28 +18,45 @@ class SearchController extends \yii\web\Controller if (!empty($word)) { - $params = []; - $params['keywords'] = explode(' ', preg_replace("|[\s,.!:&?~();-]|i", " ", $word)); - foreach($params['keywords'] as $i => &$keyword) { - $keyword = trim($keyword); - if (empty($keyword)) { - unset($params['keywords'][$i]); + + if( preg_match('/^\+?\d+$/', $word) && (iconv_strlen($word) > 4)){ + + $params['keywords'][] = $word; + + $categoriesQuery = Category::find() + ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.category_id = '. Category::tableName() .'.category_id') + ->innerJoin(Product::tableName(), Product::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id') + ->innerJoin(ProductVariant::tableName(), ProductVariant::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id'); + $categoriesQuery->andWhere(['ilike', 'product.name', $params['keywords'][0]]); + $categories = $categoriesQuery->all(); + + } else { + + $params['keywords'] = explode(' ', preg_replace("|[\s,.!:&?~();-]|i", " ", $word)); + + foreach($params['keywords'] as $i => &$keyword) { + $keyword = trim($keyword); + if (empty($keyword)) { + unset($params['keywords'][$i]); + } + } + array_unshift($params['keywords'], $word); + + $categoriesQuery = Category::find() + ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.category_id = '. Category::tableName() .'.category_id') + ->innerJoin(Product::tableName(), Product::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id') + ->innerJoin(ProductVariant::tableName(), ProductVariant::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id'); + foreach ($params['keywords'] as $keyword) { + $categoriesQuery->andWhere(['ilike', 'product.name', $keyword]); } + $categoriesQuery->andWhere(['!=', ProductVariant::tableName() .'.stock', 0]); + $categories = $categoriesQuery->all(); } $productModel = new ProductFrontendSearch(); $productProvider = $productModel->search(null, $params); - $categoriesQuery = Category::find() - ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.category_id = '. Category::tableName() .'.category_id') - ->innerJoin(Product::tableName(), Product::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id') - ->innerJoin(ProductVariant::tableName(), ProductVariant::tableName() .'.product_id = '. ProductCategory::tableName() .'.product_id'); - foreach ($params['keywords'] as $keyword) { - $categoriesQuery->andWhere(['ilike', 'product.name', $keyword]); - } - $categoriesQuery->andWhere(['!=', ProductVariant::tableName() .'.stock', 0]); - $categories = $categoriesQuery->all(); return $this->render( 'index', diff --git a/frontend/models/ProductFrontendSearch.php b/frontend/models/ProductFrontendSearch.php index 1f25ecc..d7ef7cf 100755 --- a/frontend/models/ProductFrontendSearch.php +++ b/frontend/models/ProductFrontendSearch.php @@ -91,7 +91,7 @@ class ProductFrontendSearch extends Product { $query = Product::find(); } $query->select(['product.*']); - $query->joinWith(['variant', 'image', 'brand', 'brand.brandName', 'category', 'category.categoryName']); + $query->joinWith(['enabledVariants','brand', 'brand.brandName', 'category', 'category.categoryName']); $query->groupBy(['product.product_id', 'product_variant.price']); @@ -108,7 +108,7 @@ class ProductFrontendSearch extends Product { TaxOption::tableName() .'.*', ]) ->leftJoin(ProductOption::tableName(), ProductOption::tableName() .'.option_id='. TaxOption::tableName() .'.tax_option_id') - ->innerJoin(TaxGroup::tableName(), TaxGroup::tableName() .'.tax_group_id='. TaxOption::tableName() .'.tax_group_id') + ->joinWith('taxGroup') ->where([TaxGroup::tableName() .'.is_filter' => true]); $query->innerJoin('product_variant', 'product_variant.product_id = '. ProductOption::tableName() .'.product_id'); @@ -124,35 +124,35 @@ class ProductFrontendSearch extends Product { $query->orderBy(TaxOption::tableName() .'.sort', SORT_ASC); $query->limit(null); - $queryCount = ProductOption::find() - ->select(['COUNT('. ProductOption::tableName() .'.product_id)']) - ->where(ProductOption::tableName() .'.option_id = '. TaxOption::tableName() .'.tax_option_id'); - $queryCount->andWhere('(SELECT COUNT(pv.product_variant_id) FROM "product_variant" "pv" WHERE pv.stock != 0 AND pv.product_id = '. ProductOption::tableName() .'.product_id) > 0'); - if (!empty($category)) { - $queryCount->andWhere('(SELECT COUNT(pc.product_id) FROM product_category pc WHERE pc.product_id = '. ProductOption::tableName() .'.product_id AND pc.category_id = '. $category->category_id .') > 0'); - } - if (!empty($params['options'])) { - $queryCount->innerJoin('tax_option', 'tax_option.tax_option_id = product_option.option_id'); - $queryCount->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id'); - foreach ($params['options'] as $group => $options) { - $queryCount->andWhere([ - 'tax_group.alias' => $group, - 'tax_option.alias' => $options - ]); - } - } - if (!empty($params['brands'])) { - $queryCount->innerJoin(Product::tableName(), 'product.product_id='. ProductOption::tableName() .'.product_id'); - $queryCount->andWhere(['product.brand_id' => $params['brands']]); - } - if (!empty($params['prices'])) { - if ($params['prices']['min'] > 0) { - $queryCount->andWhere(['>=', 'pv.price', $params['prices']['min']]); - } - if ($params['prices']['max'] > 0) { - $queryCount->andWhere(['<=', 'pv.price', $params['prices']['max']]); - } - } +// $queryCount = ProductOption::find() +// ->select(['COUNT('. ProductOption::tableName() .'.product_id)']) +// ->where(ProductOption::tableName() .'.option_id = '. TaxOption::tableName() .'.tax_option_id'); +// $queryCount->andWhere('(SELECT COUNT(pv.product_variant_id) FROM "product_variant" "pv" WHERE pv.stock != 0 AND pv.product_id = '. ProductOption::tableName() .'.product_id) > 0'); +// if (!empty($category)) { +// $queryCount->andWhere('(SELECT COUNT(pc.product_id) FROM product_category pc WHERE pc.product_id = '. ProductOption::tableName() .'.product_id AND pc.category_id = '. $category->category_id .') > 0'); +// } +// if (!empty($params['options'])) { +// $queryCount->innerJoin('tax_option', 'tax_option.tax_option_id = product_option.option_id'); +// $queryCount->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id'); +// foreach ($params['options'] as $group => $options) { +// $queryCount->andWhere([ +// 'tax_group.alias' => $group, +// 'tax_option.alias' => $options +// ]); +// } +// } +// if (!empty($params['brands'])) { +// $queryCount->innerJoin(Product::tableName(), 'product.product_id='. ProductOption::tableName() .'.product_id'); +// $queryCount->andWhere(['product.brand_id' => $params['brands']]); +// } +// if (!empty($params['prices'])) { +// if ($params['prices']['min'] > 0) { +// $queryCount->andWhere(['>=', 'pv.price', $params['prices']['min']]); +// } +// if ($params['prices']['max'] > 0) { +// $queryCount->andWhere(['<=', 'pv.price', $params['prices']['max']]); +// } +// } // $query->addSelect(['_items_count' => $queryCount]); return $query; diff --git a/frontend/views/catalog/products.php b/frontend/views/catalog/products.php index c2b5057..0433063 100755 --- a/frontend/views/catalog/products.php +++ b/frontend/views/catalog/products.php @@ -8,6 +8,7 @@ use yii\helpers\Url; use yii\widgets\Breadcrumbs; use yii\web\View; use common\modules\product\helpers\ProductHelper; +use common\modules\product\helpers\FilterHelper; use frontend\widgets\Seo; //$this->title = $category->categoryName->value; $this->params['seo']['title'] = !empty($category->meta_title) ? $category->meta_title : ''; @@ -52,7 +53,7 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[ 'max' => '{to}', ]; ?> - var url = " $category, 'filter' => $filterWhitoutPrice])?>"; + var url = " $category, 'filters' => $filterWhitoutPrice])?>"; var from = e.from; var to = e.to; document.location = url.replace('{from}', from).replace('{to}', to); @@ -83,7 +84,7 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[
  • $category, 'filter' => ProductHelper::getFilterForOption($filter, 'special', 'new', $checked)]); + $option_url = Url::to(['catalog/category', 'category' => $category, 'filters' => FilterHelper::getFilterForOption($filter, 'special', 'new', $checked)]); ?> onchange="document.location=''" /> @@ -91,7 +92,7 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[
  • $category, 'filter' => ProductHelper::getFilterForOption($filter, 'special', 'top', $checked)]); + $option_url = Url::to(['catalog/category', 'category' => $category, 'filters' => FilterHelper::getFilterForOption($filter, 'special', 'top', $checked)]); ?> onchange="document.location=''" /> @@ -99,7 +100,7 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[
  • $category, 'filter' => ProductHelper::getFilterForOption($filter, 'special', 'promo', $checked)]); + $option_url = Url::to(['catalog/category', 'category' => $category, 'filters' => FilterHelper::getFilterForOption($filter, 'special', 'promo', $checked)]); ?> onchange="document.location=''" /> @@ -119,7 +120,8 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[