Commit d407befd09c59ac3d9a27a48d24f85e5ce0d7ced
merge generator commit
Merge branch 'master' of gitlab.artweb.com.ua:root/new_rukzachok
Showing
12 changed files
with
660 additions
and
416 deletions
Show diff stats
common/modules/product/helpers/ProductHelper.php
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -namespace common\modules\product\helpers; | |
| 4 | - | |
| 5 | -use common\modules\product\models\Brand; | |
| 6 | -use common\modules\product\models\Category; | |
| 7 | -use common\modules\product\models\Product; | |
| 8 | -use common\modules\product\models\ProductVariant; | |
| 9 | -use common\modules\product\models\BrandName; | |
| 10 | -use common\modules\product\models\CategoryName; | |
| 11 | -use yii\base\Object; | |
| 12 | -use Yii; | |
| 13 | - | |
| 14 | -class ProductHelper extends Object { | |
| 15 | - const PRODUCT_TAX_GROUP_ID_TARGET = 20; | |
| 16 | - const PRODUCT_TAX_GROUP_ID_YEAR = 21; | |
| 17 | - const PRODUCT_TAX_GROUP_ID_SEX = 22; | |
| 18 | - | |
| 19 | - const PRODUCT_VARIANT_TYPE_COLOR = 1; | |
| 20 | - const PRODUCT_VARIANT_TYPE_SIZE = 2; | |
| 21 | - | |
| 22 | - public static function getCategories() { | |
| 23 | - return Category::find()->getTree(); // with('categoryName')-> | |
| 24 | - } | |
| 25 | - | |
| 26 | - public static function getBrands() { | |
| 27 | - return Brand::find(); // ->with('brandName') | |
| 28 | - } | |
| 29 | - | |
| 30 | - /* | |
| 31 | - * Return custom filter-option link | |
| 32 | - * @var array $filter | |
| 33 | - * @var array $options | |
| 34 | - * @return array | |
| 35 | - */ | |
| 36 | - public static function getFilterForOption($filter, $key, $value, $remove = false) { | |
| 37 | - $result = $filter; | |
| 38 | - if (is_array($value)) { | |
| 39 | - foreach($value as $value_key => $value_items) { | |
| 40 | - if (!is_array($value_items)) { | |
| 41 | - $value_items = [$value_items]; | |
| 42 | - } | |
| 43 | - foreach($value_items as $value_item) { | |
| 44 | - if ($remove && isset($result[$key]) && ($i = array_search($value_item, $result[$key][$value_key])) !== FALSE) { | |
| 45 | - unset($result[$key][$value_key][$i]); | |
| 46 | - if (empty($result[$key][$value_key])) { | |
| 47 | - unset($result[$key][$value_key]); | |
| 48 | - } | |
| 49 | - } else { | |
| 50 | - if (!isset($result[$key][$value_key]) || array_search($value_item, $result[$key][$value_key]) === FALSE) { | |
| 51 | - $result[$key][$value_key][] = $value_item; | |
| 2 | + | |
| 3 | + namespace common\modules\product\helpers; | |
| 4 | + | |
| 5 | + use common\modules\product\models\Brand; | |
| 6 | + use common\modules\product\models\Category; | |
| 7 | + use common\modules\product\models\Product; | |
| 8 | + use common\modules\product\models\ProductVariant; | |
| 9 | + use common\modules\product\models\BrandName; | |
| 10 | + use common\modules\product\models\CategoryName; | |
| 11 | + use yii\base\Object; | |
| 12 | + use Yii; | |
| 13 | + use yii\db\ActiveQuery; | |
| 14 | + | |
| 15 | + class ProductHelper extends Object | |
| 16 | + { | |
| 17 | + | |
| 18 | + const PRODUCT_TAX_GROUP_ID_TARGET = 20; | |
| 19 | + const PRODUCT_TAX_GROUP_ID_YEAR = 21; | |
| 20 | + const PRODUCT_TAX_GROUP_ID_SEX = 22; | |
| 21 | + | |
| 22 | + const PRODUCT_VARIANT_TYPE_COLOR = 1; | |
| 23 | + const PRODUCT_VARIANT_TYPE_SIZE = 2; | |
| 24 | + | |
| 25 | + public static function getCategories() | |
| 26 | + { | |
| 27 | + return Category::find() | |
| 28 | + ->getTree(); // with('categoryName')-> | |
| 29 | + } | |
| 30 | + | |
| 31 | + public static function getBrands() | |
| 32 | + { | |
| 33 | + return Brand::find(); // ->with('brandName') | |
| 34 | + } | |
| 35 | + | |
| 36 | + /* | |
| 37 | + * Return custom filter-option link | |
| 38 | + * @var array $filter | |
| 39 | + * @var array $options | |
| 40 | + * @return array | |
| 41 | + */ | |
| 42 | + public static function getFilterForOption($filter, $key, $value, $remove = false) | |
| 43 | + { | |
| 44 | + $result = $filter; | |
| 45 | + if(is_array($value)) { | |
| 46 | + foreach($value as $value_key => $value_items) { | |
| 47 | + if(!is_array($value_items)) { | |
| 48 | + $value_items = [ $value_items ]; | |
| 49 | + } | |
| 50 | + foreach($value_items as $value_item) { | |
| 51 | + if($remove && isset( $result[ $key ] ) && ( $i = array_search($value_item, $result[ $key ][ $value_key ]) ) !== false) { | |
| 52 | + unset( $result[ $key ][ $value_key ][ $i ] ); | |
| 53 | + if(empty( $result[ $key ][ $value_key ] )) { | |
| 54 | + unset( $result[ $key ][ $value_key ] ); | |
| 55 | + } | |
| 56 | + } else { | |
| 57 | + if(!isset( $result[ $key ][ $value_key ] ) || array_search($value_item, $result[ $key ][ $value_key ]) === false) { | |
| 58 | + $result[ $key ][ $value_key ][] = $value_item; | |
| 59 | + } | |
| 52 | 60 | } |
| 53 | 61 | } |
| 54 | 62 | } |
| 55 | - } | |
| 56 | - } else { | |
| 57 | - if ($remove && isset($result[$key]) && ($i = array_search($value, $result[$key])) !== FALSE) { | |
| 58 | - unset($result[$key][$i]); | |
| 59 | - if (empty($result[$key])) { | |
| 60 | - unset($result[$key]); | |
| 61 | - } | |
| 62 | 63 | } else { |
| 63 | - if (!isset($result[$key]) || array_search($value, $result[$key]) === FALSE) { | |
| 64 | - $result[$key][] = $value; | |
| 64 | + if($remove && isset( $result[ $key ] ) && ( $i = array_search($value, $result[ $key ]) ) !== false) { | |
| 65 | + unset( $result[ $key ][ $i ] ); | |
| 66 | + if(empty( $result[ $key ] )) { | |
| 67 | + unset( $result[ $key ] ); | |
| 68 | + } | |
| 69 | + } else { | |
| 70 | + if(!isset( $result[ $key ] ) || array_search($value, $result[ $key ]) === false) { | |
| 71 | + $result[ $key ][] = $value; | |
| 72 | + } | |
| 65 | 73 | } |
| 66 | 74 | } |
| 75 | + return $result; | |
| 67 | 76 | } |
| 68 | - return $result; | |
| 69 | - } | |
| 70 | - | |
| 71 | - public static function addLastProsucts($product_id) { | |
| 72 | - $last_products = self::getLastProducts(); | |
| 73 | - if (!in_array($product_id, $last_products)) { | |
| 74 | - $last_products[] = intval($product_id); | |
| 75 | - if (count($last_products) > 16) { | |
| 76 | - array_shift($last_products); | |
| 77 | + | |
| 78 | + public static function addLastProsucts($product_id) | |
| 79 | + { | |
| 80 | + $last_products = self::getLastProducts(); | |
| 81 | + if(!in_array($product_id, $last_products)) { | |
| 82 | + $last_products[] = intval($product_id); | |
| 83 | + if(count($last_products) > 16) { | |
| 84 | + array_shift($last_products); | |
| 85 | + } | |
| 86 | + Yii::$app->session->set('last_products', $last_products); | |
| 77 | 87 | } |
| 78 | - Yii::$app->session->set('last_products', $last_products); | |
| 79 | - } | |
| 80 | - } | |
| 81 | - | |
| 82 | - public static function getLastProducts($as_object = false) { | |
| 83 | - $last_products = Yii::$app->session->get('last_products', []); | |
| 84 | - if ($as_object) { | |
| 85 | - $last_products = Product::find()->joinWith(['variant'])->where([Product::tableName() .'.product_id' => $last_products])->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])->all(); | |
| 86 | 88 | } |
| 87 | - return array_reverse($last_products); | |
| 88 | - } | |
| 89 | - | |
| 90 | - public static function getSpecialProducts($type, $count, $sort = null) { | |
| 91 | - switch($type) { | |
| 92 | - case 'top': | |
| 93 | - $data = ['is_top' => true]; | |
| 94 | - break; | |
| 95 | - case 'new': | |
| 96 | - $data = ['is_new' => true]; | |
| 97 | - break; | |
| 98 | - case 'promo': | |
| 99 | - $data = ['akciya' => true]; | |
| 100 | - break; | |
| 101 | - } | |
| 102 | - return Product::find()->joinWith('variants')->where($data)->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])->limit($count)/*->orderBy($sort)*/->all(); | |
| 103 | - } | |
| 104 | - | |
| 105 | - public static function getSimilarProducts($product, $count = 10) { | |
| 106 | - if (!is_object($product)) { | |
| 107 | - $product = Product::find()->where(['product_id'=>$product])->with('enabledVariants')->one(); | |
| 108 | - } | |
| 109 | - | |
| 110 | - if (!$product->properties) { | |
| 111 | - return []; | |
| 89 | + | |
| 90 | + public static function getLastProducts($as_object = false) | |
| 91 | + { | |
| 92 | + $last_products = Yii::$app->session->get('last_products', [ ]); | |
| 93 | + if($as_object) { | |
| 94 | + $last_products = Product::find() | |
| 95 | + ->joinWith([ 'variant' ]) | |
| 96 | + ->where([ Product::tableName() . '.product_id' => $last_products ]) | |
| 97 | + ->andWhere([ | |
| 98 | + '!=', | |
| 99 | + ProductVariant::tableName() . '.stock', | |
| 100 | + 0, | |
| 101 | + ]) | |
| 102 | + ->all(); | |
| 103 | + } | |
| 104 | + return array_reverse($last_products); | |
| 112 | 105 | } |
| 113 | - $product_categories = []; | |
| 114 | - foreach ($product->categories as $category) { | |
| 115 | - $product_categories[] = $category->category_id; | |
| 106 | + | |
| 107 | + public static function getSpecialProducts($type, $count, $sort = NULL) | |
| 108 | + { | |
| 109 | + switch($type) { | |
| 110 | + case 'top': | |
| 111 | + $data = [ 'is_top' => true ]; | |
| 112 | + break; | |
| 113 | + case 'new': | |
| 114 | + $data = [ 'is_new' => true ]; | |
| 115 | + break; | |
| 116 | + case 'promo': | |
| 117 | + $data = [ 'akciya' => true ]; | |
| 118 | + break; | |
| 119 | + } | |
| 120 | + return Product::find() | |
| 121 | + ->joinWith('variants') | |
| 122 | + ->where($data) | |
| 123 | + ->andWhere([ | |
| 124 | + '!=', | |
| 125 | + ProductVariant::tableName() . '.stock', | |
| 126 | + 0, | |
| 127 | + ]) | |
| 128 | + ->limit($count)/*->orderBy($sort)*/ | |
| 129 | + ->all(); | |
| 116 | 130 | } |
| 117 | - $query = Product::find() | |
| 118 | - ->select('product.product_id') | |
| 119 | - ->innerJoinWith('variant') | |
| 120 | - ->joinWith('category') | |
| 121 | - ->where(['!=', 'product_variant.stock', 0]) | |
| 122 | - ->andWhere(['product_category.category_id' => $product_categories]); | |
| 123 | -// $query->andWhere(['>=', 'product_variant.price', $product->enabledVariant->price * 0.7]); | |
| 124 | -// $query->andWhere(['<=', 'product_variant.price', $product->enabledVariant->price * 1.3]); | |
| 125 | - foreach($product->properties as $group) { | |
| 126 | - $where = []; | |
| 127 | - foreach ($group->_options as $option) { | |
| 128 | - $where[] = $option->tax_option_id; | |
| 131 | + | |
| 132 | + public static function getSimilarProducts($product, $count = 10) | |
| 133 | + { | |
| 134 | + if(!is_object($product)) { | |
| 135 | + $product = Product::find() | |
| 136 | + ->where([ 'product_id' => $product ]) | |
| 137 | + ->with('enabledVariants') | |
| 138 | + ->one(); | |
| 129 | 139 | } |
| 130 | - if (!$where) { | |
| 131 | - continue; | |
| 140 | + | |
| 141 | + if(!$product->properties) { | |
| 142 | + return [ ]; | |
| 132 | 143 | } |
| 133 | - $query->innerJoin('product_option to'. $group->tax_group_id, 'to'. $group->tax_group_id .'.product_id = product.product_id'); | |
| 134 | - $query->andWhere(['to'. $group->tax_group_id .'.option_id' => $where]); | |
| 135 | - } | |
| 136 | - $query->andWhere(['!=', 'product.product_id', $product->product_id]); | |
| 137 | - $query->groupBy('product.product_id'); | |
| 138 | - $query->limit($count); | |
| 139 | - $products = $query->asArray()->all(); | |
| 140 | - foreach ($products as &$_product) { | |
| 141 | - $_product = Product::findOne($_product['product_id']); | |
| 142 | - } | |
| 143 | - return $products; | |
| 144 | - } | |
| 145 | - | |
| 146 | - | |
| 147 | - | |
| 148 | - public static function _setQueryParams(&$query, $params, $setPriceLimits = true) { | |
| 149 | - if (!empty($params['keywords'])) { | |
| 150 | - if (!is_array($params['keywords'])) { | |
| 151 | - $params['keywords'] = [$params['keywords']]; | |
| 144 | + $product_categories = [ ]; | |
| 145 | + foreach($product->categories as $category) { | |
| 146 | + $product_categories[] = $category->category_id; | |
| 152 | 147 | } |
| 153 | - foreach ($params['keywords'] as $keyword) { | |
| 154 | - $query->orFilterWhere(['ilike', Product::tableName() .'.name', $keyword]); | |
| 155 | - $query->orFilterWhere(['ilike', BrandName::tableName() .'.value', $keyword]); | |
| 156 | - $query->orFilterWhere(['ilike', CategoryName::tableName() .'.value', $keyword]); | |
| 157 | - $query->orFilterWhere(['ilike', ProductVariant::tableName() .'.sku', $keyword]); | |
| 148 | + $query = Product::find() | |
| 149 | + ->select('product.product_id') | |
| 150 | + ->innerJoinWith('variant') | |
| 151 | + ->joinWith('category') | |
| 152 | + ->where([ | |
| 153 | + '!=', | |
| 154 | + 'product_variant.stock', | |
| 155 | + 0, | |
| 156 | + ]) | |
| 157 | + ->andWhere([ 'product_category.category_id' => $product_categories ]); | |
| 158 | + // $query->andWhere(['>=', 'product_variant.price', $product->enabledVariant->price * 0.7]); | |
| 159 | + // $query->andWhere(['<=', 'product_variant.price', $product->enabledVariant->price * 1.3]); | |
| 160 | + foreach($product->properties as $group) { | |
| 161 | + $where = [ ]; | |
| 162 | + foreach($group->_options as $option) { | |
| 163 | + $where[] = $option->tax_option_id; | |
| 164 | + } | |
| 165 | + if(!$where) { | |
| 166 | + continue; | |
| 167 | + } | |
| 168 | + $query->innerJoin('product_option to' . $group->tax_group_id, 'to' . $group->tax_group_id . '.product_id = product.product_id'); | |
| 169 | + $query->andWhere([ 'to' . $group->tax_group_id . '.option_id' => $where ]); | |
| 170 | + } | |
| 171 | + $query->andWhere([ | |
| 172 | + '!=', | |
| 173 | + 'product.product_id', | |
| 174 | + $product->product_id, | |
| 175 | + ]); | |
| 176 | + $query->groupBy('product.product_id'); | |
| 177 | + $query->limit($count); | |
| 178 | + $products = $query->asArray() | |
| 179 | + ->all(); | |
| 180 | + foreach($products as &$_product) { | |
| 181 | + $_product = Product::findOne($_product[ 'product_id' ]); | |
| 158 | 182 | } |
| 183 | + return $products; | |
| 159 | 184 | } |
| 160 | - | |
| 161 | - foreach($params as $key => $param){ | |
| 162 | - | |
| 163 | - switch ($key) { | |
| 164 | - case 'special': | |
| 165 | - foreach($param as $key => $value) { | |
| 166 | - $query->orFilterWhere([Product::tableName() .'.'. $key => $value]); | |
| 167 | - } | |
| 168 | - break; | |
| 169 | - case 'brands': | |
| 170 | - $query->andFilterWhere([Product::tableName() .'.brand_id' => $param]); | |
| 171 | - break; | |
| 172 | - case 'keywords': | |
| 173 | - break; | |
| 174 | - case 'prices': | |
| 175 | - if ($param['min'] > 0) { | |
| 176 | - $query->andWhere(['>=', ProductVariant::tableName() .'.price', $param['min']]); | |
| 177 | - } | |
| 178 | - if ($param['max'] > 0) { | |
| 179 | - $query->andWhere(['<=', ProductVariant::tableName() .'.price', $param['max']]); | |
| 180 | - } | |
| 181 | - break; | |
| 182 | - default: | |
| 183 | - $query->andWhere( | |
| 184 | - 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) . '\'))' | |
| 185 | - ); | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * @param ActiveQuery $query | |
| 188 | + * @param $params | |
| 189 | + * @param bool $setPriceLimits | |
| 190 | + */ | |
| 191 | + public static function _setQueryParams(&$query, $params, $setPriceLimits = true) | |
| 192 | + { | |
| 193 | + if(!empty( $params[ 'keywords' ] )) { | |
| 194 | + if(!is_array($params[ 'keywords' ])) { | |
| 195 | + $params[ 'keywords' ] = [ $params[ 'keywords' ] ]; | |
| 196 | + } | |
| 197 | + foreach($params[ 'keywords' ] as $keyword) { | |
| 198 | + $query->orFilterWhere([ | |
| 199 | + 'ilike', | |
| 200 | + Product::tableName() . '.name', | |
| 201 | + $keyword, | |
| 202 | + ]); | |
| 203 | + $query->orFilterWhere([ | |
| 204 | + 'ilike', | |
| 205 | + BrandName::tableName() . '.value', | |
| 206 | + $keyword, | |
| 207 | + ]); | |
| 208 | + $query->orFilterWhere([ | |
| 209 | + 'ilike', | |
| 210 | + CategoryName::tableName() . '.value', | |
| 211 | + $keyword, | |
| 212 | + ]); | |
| 213 | + $query->orFilterWhere([ | |
| 214 | + 'ilike', | |
| 215 | + ProductVariant::tableName() . '.sku', | |
| 216 | + $keyword, | |
| 217 | + ]); | |
| 218 | + } | |
| 219 | + } | |
| 220 | + | |
| 221 | + foreach($params as $key => $param) { | |
| 222 | + | |
| 223 | + switch($key) { | |
| 224 | + case 'special': | |
| 225 | + foreach($param as $key => $value) { | |
| 226 | + $query->orFilterWhere([ Product::tableName() . '.' . $key => $value ]); | |
| 227 | + } | |
| 228 | + break; | |
| 229 | + case 'brands': | |
| 230 | + $query->andFilterWhere([ Product::tableName() . '.brand_id' => $param ]); | |
| 231 | + break; | |
| 232 | + case 'keywords': | |
| 233 | + break; | |
| 234 | + case 'prices': | |
| 235 | + if($param[ 'min' ] > 0) { | |
| 236 | + $query->andWhere([ | |
| 237 | + '>=', | |
| 238 | + ProductVariant::tableName() . '.price', | |
| 239 | + $param[ 'min' ], | |
| 240 | + ]); | |
| 241 | + } | |
| 242 | + if($param[ 'max' ] > 0) { | |
| 243 | + $query->andWhere([ | |
| 244 | + '<=', | |
| 245 | + ProductVariant::tableName() . '.price', | |
| 246 | + $param[ 'max' ], | |
| 247 | + ]); | |
| 248 | + } | |
| 249 | + break; | |
| 250 | + default: | |
| 251 | + $query->andWhere( | |
| 252 | + Product::tableName() . '.product_id IN ( | |
| 253 | + SELECT DISTINCT products | |
| 254 | + FROM ( | |
| 255 | + SELECT product_id AS products | |
| 256 | + FROM product_option | |
| 257 | + INNER JOIN tax_option ON tax_option.tax_option_id = product_option.option_id | |
| 258 | + INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id | |
| 259 | + WHERE tax_group.alias LIKE \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\') OR product_id IN ( | |
| 260 | + (SELECT product_id AS products | |
| 261 | + FROM product_variant_option | |
| 262 | + INNER JOIN product_variant ON product_variant_option.product_variant_id = product_variant.product_variant_id | |
| 263 | + INNER JOIN tax_option ON tax_option.tax_option_id = product_variant_option.option_id | |
| 264 | + INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id | |
| 265 | + WHERE tax_group.alias LIKE \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\')) | |
| 266 | + ) | |
| 267 | + ) AS table_name | |
| 268 | + )' | |
| 269 | + ); | |
| 270 | + } | |
| 271 | + | |
| 186 | 272 | } |
| 187 | - | |
| 273 | + | |
| 188 | 274 | } |
| 189 | - | |
| 190 | - } | |
| 191 | - | |
| 192 | - public static function productCountQuery($category = null, $params, $excludeKeys = []) { | |
| 193 | - $p = []; | |
| 194 | - foreach ($params as $key => $param) { | |
| 195 | - if (in_array($key, $excludeKeys)) { | |
| 196 | - $p[$key] = $param; | |
| 275 | + | |
| 276 | + public static function productCountQuery($category = NULL, $params, $excludeKeys = [ ]) | |
| 277 | + { | |
| 278 | + $p = [ ]; | |
| 279 | + foreach($params as $key => $param) { | |
| 280 | + if(in_array($key, $excludeKeys)) { | |
| 281 | + $p[ $key ] = $param; | |
| 282 | + } | |
| 197 | 283 | } |
| 284 | + /** @var ActiveQuery $query */ | |
| 285 | + if(!empty( $category )) { | |
| 286 | + $query = $category->getProducts(); | |
| 287 | + } else { | |
| 288 | + $query = Product::find(); | |
| 289 | + } | |
| 290 | + ProductHelper::_setQueryParams($query, $params); | |
| 291 | + $query->select([ 'COUNT(product.product_id)' ]); | |
| 292 | + | |
| 293 | + return $query; | |
| 294 | + } | |
| 295 | + | |
| 296 | + public static function addLastCategory($category_id) { | |
| 297 | + \Yii::$app->session->set('last_category_id', $category_id); | |
| 198 | 298 | } |
| 199 | - /** @var ActiveQuery $query */ | |
| 200 | - if (!empty($category)) { | |
| 201 | - $query = $category->getProducts(); | |
| 202 | - } else { | |
| 203 | - $query = Product::find(); | |
| 299 | + | |
| 300 | + public static function getLastCategory() { | |
| 301 | + return \Yii::$app->session->get('last_category_id'); | |
| 204 | 302 | } |
| 205 | - ProductHelper::_setQueryParams($query, $params); | |
| 206 | - $query->select(['COUNT(product.product_id)']); | |
| 207 | - | |
| 208 | - return $query; | |
| 209 | - } | |
| 210 | -} | |
| 211 | 303 | \ No newline at end of file |
| 304 | + } | |
| 212 | 305 | \ No newline at end of file | ... | ... |
common/modules/product/models/Category.php
| ... | ... | @@ -10,6 +10,7 @@ use common\modules\rubrication\behaviors\ArtboxSynonymBehavior; |
| 10 | 10 | use common\modules\rubrication\models\TaxGroup; |
| 11 | 11 | use Yii; |
| 12 | 12 | use yii\base\ErrorException; |
| 13 | +use yii\db\Query; | |
| 13 | 14 | |
| 14 | 15 | /** |
| 15 | 16 | * This is the model class for table "category". |
| ... | ... | @@ -225,5 +226,57 @@ class Category extends \yii\db\ActiveRecord |
| 225 | 226 | CategoryName::deleteAll(['category_id' => $this->category_id]); |
| 226 | 227 | return true; |
| 227 | 228 | } |
| 229 | + | |
| 230 | + public function getActiveFilters() { | |
| 231 | + $query1 = (new Query()) | |
| 232 | + ->distinct() | |
| 233 | + ->select([ | |
| 234 | + 'option_id', | |
| 235 | + 'tax_option.*', | |
| 236 | + 'tax_group.*', | |
| 237 | + 'tax_option.alias as option_alias', | |
| 238 | + 'tax_group.alias as group_alias', | |
| 239 | + 'tax_value_string.value as value', | |
| 240 | + 'tax_option.sort AS tax_option_sort', | |
| 241 | + 'tax_group.sort AS tax_group_sort', | |
| 242 | + ]) | |
| 243 | + ->from('tax_option') | |
| 244 | + ->innerJoin('product_variant_option', 'tax_option.tax_option_id = product_variant_option.option_id') | |
| 245 | + ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') | |
| 246 | + ->innerJoin('product_variant', 'product_variant.product_variant_id = product_variant_option.product_variant_id') | |
| 247 | + ->innerJoin('product', 'product.product_id = product_variant.product_id') | |
| 248 | + ->innerJoin('product_category', 'product_category.product_id = product.product_id') | |
| 249 | + ->innerJoin('tax_value_string', 'tax_value_string.tax_option_id = tax_option.tax_option_id') | |
| 250 | + ->where(['product_category.category_id' => $this->category_id]) | |
| 251 | + ->andWhere(['!=', 'product_variant.stock', 0]); | |
| 252 | + | |
| 253 | + $query2 = (new Query()) | |
| 254 | + ->distinct() | |
| 255 | + ->select([ | |
| 256 | + 'option_id', | |
| 257 | + 'tax_option.*', | |
| 258 | + 'tax_group.*', | |
| 259 | + 'tax_option.alias as option_alias', | |
| 260 | + 'tax_group.alias as group_alias', | |
| 261 | + 'tax_value_string.value as value', | |
| 262 | + 'tax_option.sort AS tax_option_sort', | |
| 263 | + 'tax_group.sort AS tax_group_sort', | |
| 264 | + ]) | |
| 265 | + ->from('tax_option') | |
| 266 | + ->innerJoin('product_option', 'tax_option.tax_option_id = product_option.option_id') | |
| 267 | + ->innerJoin('tax_group', 'tax_group.tax_group_id = tax_option.tax_group_id') | |
| 268 | + ->innerJoin('product', 'product.product_id = product_option.product_id') | |
| 269 | + ->innerJoin('product_category', 'product_category.product_id = product.product_id') | |
| 270 | + ->innerJoin('product_variant', 'product_variant.product_id = product.product_id') | |
| 271 | + ->innerJoin('tax_value_string', 'tax_value_string.tax_option_id = tax_option.tax_option_id') | |
| 272 | + ->where(['product_category.category_id' => $this->category_id]) | |
| 273 | + ->andWhere(['!=', 'product_variant.stock', 0]) | |
| 274 | + ->orderBy('tax_group_sort, tax_option_sort'); | |
| 275 | + $query3 = (new Query()) | |
| 276 | + ->select('*') | |
| 277 | + ->from(['subquery' => $query1->union($query2)]) | |
| 278 | + ->orderBy('tax_group_sort, tax_option_sort'); | |
| 279 | + return $query3; | |
| 280 | + } | |
| 228 | 281 | |
| 229 | 282 | } | ... | ... |
common/modules/product/models/Product.php
| ... | ... | @@ -11,6 +11,7 @@ use common\modules\rubrication\models\TaxGroup; |
| 11 | 11 | use common\modules\rubrication\models\TaxOption; |
| 12 | 12 | use Yii; |
| 13 | 13 | use common\modules\relation\relationBehavior; |
| 14 | +use yii\db\ActiveQuery; | |
| 14 | 15 | use yii\db\ActiveRecord; |
| 15 | 16 | use yii\helpers\ArrayHelper; |
| 16 | 17 | |
| ... | ... | @@ -240,7 +241,10 @@ class Product extends \yii\db\ActiveRecord |
| 240 | 241 | public function getVariantsWithFilters(){ |
| 241 | 242 | return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->with('filters'); |
| 242 | 243 | } |
| 243 | - | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * @return ActiveQuery | |
| 247 | + */ | |
| 244 | 248 | public function getCategory() { |
| 245 | 249 | return $this->hasOne(Category::className(), ['category_id' => 'category_id'])->viaTable('product_category', ['product_id' => 'product_id']); |
| 246 | 250 | } |
| ... | ... | @@ -262,6 +266,20 @@ class Product extends \yii\db\ActiveRecord |
| 262 | 266 | } |
| 263 | 267 | return $groups; |
| 264 | 268 | } |
| 269 | + | |
| 270 | + public function getActiveProperties($category_id) { | |
| 271 | + $groups = $options = []; | |
| 272 | + foreach ($this->options as $option) { | |
| 273 | + $options[$option->tax_group_id][] = $option; | |
| 274 | + } | |
| 275 | + foreach (TaxGroup::find()->joinWith('categories')->where(['tax_group.tax_group_id' => array_keys($options), 'tax_group.display' => TRUE, 'category.category_id' => $category_id])->all() as $group) { | |
| 276 | + if (!empty($options[$group->tax_group_id])) { | |
| 277 | + $group->_options = $options[$group->tax_group_id]; | |
| 278 | + $groups[] = $group; | |
| 279 | + } | |
| 280 | + } | |
| 281 | + return $groups; | |
| 282 | + } | |
| 265 | 283 | |
| 266 | 284 | public function getStocks() { |
| 267 | 285 | return $this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_id' => 'product_id']); |
| ... | ... | @@ -418,6 +436,6 @@ class Product extends \yii\db\ActiveRecord |
| 418 | 436 | public function getTaxGroupsByLevel($level) |
| 419 | 437 | { |
| 420 | 438 | $categories = ArrayHelper::getColumn($this->categories, 'category_id'); |
| 421 | - return TaxGroup::find()->distinct()->innerJoin('relation', 'entity1_id = tax_group_id')->where(['relation.entity2_id' => $categories])->where(['level' => $level]); | |
| 439 | + return TaxGroup::find()->distinct()->innerJoin('relation', 'entity1_id = tax_group_id')->andWhere(['relation.entity2_id' => $categories])->andWhere(['level' => $level]); | |
| 422 | 440 | } |
| 423 | 441 | } | ... | ... |
common/modules/rubrication/models/TaxGroup.php
| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | namespace common\modules\rubrication\models; |
| 4 | 4 | |
| 5 | +use common\modules\product\models\Category; | |
| 5 | 6 | use common\modules\relation\relationBehavior; |
| 6 | 7 | use Yii; |
| 7 | 8 | |
| ... | ... | @@ -17,6 +18,8 @@ use Yii; |
| 17 | 18 | * @property string $settings |
| 18 | 19 | * @property boolean $is_filter |
| 19 | 20 | * @property integer $level |
| 21 | + * @property integer $sort | |
| 22 | + * @property boolean $display | |
| 20 | 23 | * @property TaxGroupToGroup[] $taxGroupToGroups |
| 21 | 24 | * @property TaxGroupToGroup[] $taxGroupToGroups0 |
| 22 | 25 | * @property TaxOption[] $taxOptions |
| ... | ... | @@ -62,8 +65,8 @@ class TaxGroup extends \yii\db\ActiveRecord |
| 62 | 65 | return [ |
| 63 | 66 | [['name', 'module'], 'required'], |
| 64 | 67 | [['description', 'settings'], 'string'], |
| 65 | - [['hierarchical', 'is_filter'], 'boolean'], | |
| 66 | - [['level'], 'integer'], | |
| 68 | + [['hierarchical', 'is_filter', 'display'], 'boolean'], | |
| 69 | + [['level', 'sort'], 'integer'], | |
| 67 | 70 | [['alias', 'module'], 'string', 'max' => 50], |
| 68 | 71 | [['name'], 'string', 'max' => 255], |
| 69 | 72 | [['group_to_category'], 'safe'] |
| ... | ... | @@ -84,6 +87,8 @@ class TaxGroup extends \yii\db\ActiveRecord |
| 84 | 87 | 'hierarchical' => 'Hierarchical', |
| 85 | 88 | // 'settings' => 'Settings', |
| 86 | 89 | 'is_filter' => 'Use in filter', |
| 90 | + 'sort' => 'Sort', | |
| 91 | + 'display' => 'Display', | |
| 87 | 92 | ]; |
| 88 | 93 | } |
| 89 | 94 | |
| ... | ... | @@ -95,6 +100,12 @@ class TaxGroup extends \yii\db\ActiveRecord |
| 95 | 100 | return $this->hasMany(TaxGroupToGroup::className(), ['tax_group1_id' => 'tax_group_id'])->inverseOf('taxGroup1'); |
| 96 | 101 | } |
| 97 | 102 | |
| 103 | + public function getCategories() | |
| 104 | + { | |
| 105 | + return $this->hasMany(Category::className(), ['category_id' => 'entity2_id']) | |
| 106 | + ->viaTable('relation', ['entity1_id' => 'tax_group_id']); | |
| 107 | + } | |
| 108 | + | |
| 98 | 109 | /** |
| 99 | 110 | * @return \yii\db\ActiveQuery |
| 100 | 111 | */ | ... | ... |
common/modules/rubrication/views/tax-group/_form.php
| ... | ... | @@ -35,6 +35,10 @@ use common\components\artboxtree\ArtboxTreeHelper; |
| 35 | 35 | <?php /*= $form->field($model, 'hierarchical')->checkbox()*/ ?> |
| 36 | 36 | |
| 37 | 37 | <?= $form->field($model, 'is_filter')->checkbox() ?> |
| 38 | + | |
| 39 | + <?= $form->field($model, 'display')->checkbox() ?> | |
| 40 | + | |
| 41 | + <?= $form->field($model, 'sort')->textInput() ?> | |
| 38 | 42 | |
| 39 | 43 | <div class="form-group"> |
| 40 | 44 | <?= Html::submitButton($model->isNewRecord ? Yii::t('rubrication', 'Create') : Yii::t('rubrication', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | ... | ... |
console/migrations/m160810_074009_add_tax_option_sort.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | +use yii\db\Migration; | |
| 4 | + | |
| 5 | +class m160810_074009_add_tax_option_sort extends Migration | |
| 6 | +{ | |
| 7 | + public function up() | |
| 8 | + { | |
| 9 | + $this->addColumn('tax_group', 'sort', $this->integer()->defaultValue(0)); | |
| 10 | + } | |
| 11 | + | |
| 12 | + public function down() | |
| 13 | + { | |
| 14 | + $this->dropColumn('tax_group', 'sort'); | |
| 15 | + } | |
| 16 | +} | ... | ... |
console/migrations/m160810_081231_add_tax_option_display.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | +use yii\db\Migration; | |
| 4 | + | |
| 5 | +class m160810_081231_add_tax_option_display extends Migration | |
| 6 | +{ | |
| 7 | + public function up() | |
| 8 | + { | |
| 9 | + $this->addColumn('tax_group', 'display', $this->boolean()->defaultValue(TRUE)); | |
| 10 | + } | |
| 11 | + | |
| 12 | + public function down() | |
| 13 | + { | |
| 14 | + $this->dropColumn('tax_group', 'display'); | |
| 15 | + } | |
| 16 | +} | ... | ... |
frontend/controllers/CatalogController.php
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -namespace frontend\controllers; | |
| 4 | - | |
| 5 | -use common\modules\product\Filter; | |
| 6 | -use common\modules\product\helpers\ProductHelper; | |
| 7 | -use common\modules\rubrication\models\TaxOptionSearch; | |
| 8 | -use frontend\models\ProductFrontendSearch; | |
| 9 | -use Yii; | |
| 10 | -use common\modules\product\models\Brand; | |
| 11 | -use common\modules\product\models\BrandSearch; | |
| 12 | -use common\modules\product\models\Category; | |
| 13 | -use common\modules\product\models\CategorySearch; | |
| 14 | -use common\modules\product\models\Product; | |
| 15 | -use common\modules\product\models\ProductCategory; | |
| 16 | -use common\modules\product\models\ProductOption; | |
| 17 | -use common\modules\product\models\ProductSearch; | |
| 18 | -use common\modules\product\models\ProductVariant; | |
| 19 | -use common\modules\rubrication\models\TaxGroup; | |
| 20 | -use common\modules\rubrication\models\TaxOption; | |
| 21 | -use common\modules\rubrication\models\TaxValueString; | |
| 22 | -use yii\data\ActiveDataProvider; | |
| 23 | -use yii\data\Pagination; | |
| 24 | -use yii\data\Sort; | |
| 25 | -use yii\db\ActiveQuery; | |
| 26 | -use yii\helpers\ArrayHelper; | |
| 27 | -use yii\web\HttpException; | |
| 28 | - | |
| 29 | -class CatalogController extends \yii\web\Controller | |
| 30 | -{ | |
| 31 | - public function actionSearch() { | |
| 32 | - // @todo | |
| 33 | - } | |
| 34 | - | |
| 35 | - public function actionCategory() | |
| 2 | + | |
| 3 | + namespace frontend\controllers; | |
| 4 | + | |
| 5 | + use common\modules\product\Filter; | |
| 6 | + use common\modules\product\helpers\ProductHelper; | |
| 7 | + use common\modules\rubrication\models\TaxOptionSearch; | |
| 8 | + use frontend\models\ProductFrontendSearch; | |
| 9 | + use Yii; | |
| 10 | + use common\modules\product\models\Brand; | |
| 11 | + use common\modules\product\models\BrandSearch; | |
| 12 | + use common\modules\product\models\Category; | |
| 13 | + use common\modules\product\models\CategorySearch; | |
| 14 | + use common\modules\product\models\Product; | |
| 15 | + use common\modules\product\models\ProductCategory; | |
| 16 | + use common\modules\product\models\ProductOption; | |
| 17 | + use common\modules\product\models\ProductSearch; | |
| 18 | + use common\modules\product\models\ProductVariant; | |
| 19 | + use common\modules\rubrication\models\TaxGroup; | |
| 20 | + use common\modules\rubrication\models\TaxOption; | |
| 21 | + use common\modules\rubrication\models\TaxValueString; | |
| 22 | + use yii\data\ActiveDataProvider; | |
| 23 | + use yii\data\Pagination; | |
| 24 | + use yii\data\Sort; | |
| 25 | + use yii\db\ActiveQuery; | |
| 26 | + use yii\helpers\ArrayHelper; | |
| 27 | + use yii\web\HttpException; | |
| 28 | + | |
| 29 | + class CatalogController extends \yii\web\Controller | |
| 36 | 30 | { |
| 37 | - /** @var Category $category */ | |
| 38 | - $category = Yii::$app->request->get('category'); | |
| 39 | - $filter = Yii::$app->request->get('filters', []); | |
| 40 | - $filter_check = $filter; | |
| 41 | - | |
| 42 | - if (empty($category->category_id) && empty($word)) { | |
| 43 | - return $this->render( | |
| 44 | - 'catalog' | |
| 45 | - ); | |
| 31 | + | |
| 32 | + public function actionSearch() | |
| 33 | + { | |
| 34 | + // @todo | |
| 46 | 35 | } |
| 47 | - | |
| 48 | - | |
| 49 | - $params = []; | |
| 50 | - | |
| 51 | - $optionsList = ArrayHelper::getColumn(TaxGroup::find()->where(['is_filter' => 'TRUE'])->all(),'alias'); | |
| 52 | - | |
| 53 | - if ( !empty($filter['brands']) ) { | |
| 54 | - unset($filter_check['brands']); | |
| 55 | - $brands = Brand::find()->select('brand_id')->where(['in', 'alias', $filter['brands']])->all(); | |
| 56 | - $params['brands'] = []; | |
| 57 | - foreach ($brands as $brand) { | |
| 58 | - $params['brands'][] = $brand->brand_id; | |
| 36 | + | |
| 37 | + public function actionCategory() | |
| 38 | + { | |
| 39 | + /** @var Category $category */ | |
| 40 | + $category = Yii::$app->request->get('category'); | |
| 41 | + $filter = Yii::$app->request->get('filters', [ ]); | |
| 42 | + $filter_check = $filter; | |
| 43 | + | |
| 44 | + if(empty( $category->category_id ) && empty( $word )) { | |
| 45 | + return $this->render('catalog'); | |
| 59 | 46 | } |
| 60 | - } | |
| 61 | - | |
| 62 | - if ( !empty($filter['special']) ) { | |
| 63 | - unset($filter_check['special']); | |
| 64 | - if (!is_array($filter['special'])) { | |
| 65 | - $filter['special'] = [$filter['special']]; | |
| 47 | + | |
| 48 | + ProductHelper::addLastCategory($category->category_id); | |
| 49 | + | |
| 50 | + $params = [ ]; | |
| 51 | + | |
| 52 | + $optionsList = ArrayHelper::getColumn(TaxGroup::find() | |
| 53 | + ->where([ 'is_filter' => 'TRUE' ]) | |
| 54 | + ->all(), 'alias'); | |
| 55 | + | |
| 56 | + if(!empty( $filter[ 'brands' ] )) { | |
| 57 | + unset( $filter_check[ 'brands' ] ); | |
| 58 | + $brands = Brand::find() | |
| 59 | + ->select('brand_id') | |
| 60 | + ->where([ | |
| 61 | + 'in', | |
| 62 | + 'alias', | |
| 63 | + $filter[ 'brands' ], | |
| 64 | + ]) | |
| 65 | + ->all(); | |
| 66 | + $params[ 'brands' ] = [ ]; | |
| 67 | + foreach($brands as $brand) { | |
| 68 | + $params[ 'brands' ][] = $brand->brand_id; | |
| 69 | + } | |
| 66 | 70 | } |
| 67 | - if (in_array('new', $filter['special'])) { | |
| 68 | - $params['special']['is_new'] = true; | |
| 71 | + | |
| 72 | + if(!empty( $filter[ 'special' ] )) { | |
| 73 | + unset( $filter_check[ 'special' ] ); | |
| 74 | + if(!is_array($filter[ 'special' ])) { | |
| 75 | + $filter[ 'special' ] = [ $filter[ 'special' ] ]; | |
| 76 | + } | |
| 77 | + if(in_array('new', $filter[ 'special' ])) { | |
| 78 | + $params[ 'special' ][ 'is_new' ] = true; | |
| 79 | + } | |
| 80 | + if(in_array('top', $filter[ 'special' ])) { | |
| 81 | + $params[ 'special' ][ 'is_top' ] = true; | |
| 82 | + } | |
| 83 | + if(in_array('promo', $filter[ 'special' ])) { | |
| 84 | + $params[ 'special' ][ 'akciya' ] = true; | |
| 85 | + } | |
| 69 | 86 | } |
| 70 | - if (in_array('top', $filter['special'])) { | |
| 71 | - $params['special']['is_top'] = true; | |
| 87 | + | |
| 88 | + if(!empty( $filter[ 'prices' ] )) { | |
| 89 | + unset( $filter_check[ 'prices' ] ); | |
| 90 | + $params[ 'prices' ] = $filter[ 'prices' ]; | |
| 72 | 91 | } |
| 73 | - if (in_array('promo', $filter['special'])) { | |
| 74 | - $params['special']['akciya'] = true; | |
| 92 | + | |
| 93 | + foreach($optionsList as $optionList) { | |
| 94 | + | |
| 95 | + if(isset( $filter[ $optionList ] )) { | |
| 96 | + unset( $filter_check[ $optionList ] ); | |
| 97 | + $params[ $optionList ] = $filter[ $optionList ]; | |
| 98 | + } | |
| 99 | + | |
| 75 | 100 | } |
| 76 | - } | |
| 77 | - | |
| 78 | - | |
| 79 | - if ( !empty($filter['prices']) ) { | |
| 80 | - unset($filter_check['prices']); | |
| 81 | - $params['prices'] = $filter['prices']; | |
| 82 | - } | |
| 83 | - | |
| 84 | - | |
| 85 | - | |
| 86 | - foreach($optionsList as $optionList){ | |
| 87 | - | |
| 88 | - if(isset($filter[$optionList])){ | |
| 89 | - unset($filter_check[$optionList]); | |
| 90 | - $params[$optionList] = $filter[$optionList]; | |
| 101 | + | |
| 102 | + if(!empty( $filter_check )) { | |
| 103 | + $filter = array_diff_key($filter, $filter_check); | |
| 104 | + Yii::$app->response->redirect([ | |
| 105 | + 'catalog/category', | |
| 106 | + 'category' => $category, | |
| 107 | + 'filters' => $filter, | |
| 108 | + ], 301); | |
| 91 | 109 | } |
| 92 | - | |
| 110 | + | |
| 111 | + $productModel = new ProductFrontendSearch(); | |
| 112 | + //$productQuery = $productModel->getSearchQuery($category, $params); | |
| 113 | + $productProvider = $productModel->search($category, $params); | |
| 114 | + | |
| 115 | + $brandModel = new BrandSearch(); | |
| 116 | + $brands = $brandModel->getBrands($category, $params) | |
| 117 | + ->all(); | |
| 118 | + // Old filters | |
| 119 | + // $optionsQuery = $productModel->optionsForCategory($category, $params); | |
| 120 | + // $groups = []; | |
| 121 | + // | |
| 122 | + // | |
| 123 | + // foreach ($optionsQuery->all() as $option) { | |
| 124 | + // if (!isset($groups[$option->tax_group_id])) { | |
| 125 | + // $groups[$option->tax_group_id] = $option->taxGroup; | |
| 126 | + // $groups[$option->tax_group_id]->_options = []; | |
| 127 | + // } | |
| 128 | + // $groups[$option->tax_group_id]->_options[] = $option; | |
| 129 | + // } | |
| 130 | + // foreach($groups as $i => $group) { | |
| 131 | + // if (empty($group->_options)) | |
| 132 | + // unset($groups[$i]); | |
| 133 | + // } | |
| 134 | + // New filters | |
| 135 | + $groups = $category->getActiveFilters()->all(); | |
| 136 | + $groups = ArrayHelper::index($groups, null, 'name'); | |
| 137 | + | |
| 138 | + $priceLimits = $productModel->priceLimits($category, $params); | |
| 139 | + | |
| 140 | + /* | |
| 141 | + * Greedy search for comments and rating | |
| 142 | + */ | |
| 143 | + $query = $productProvider->query; | |
| 144 | + $query->with([ | |
| 145 | + 'comments', | |
| 146 | + 'averageRating', | |
| 147 | + ]); | |
| 148 | + /* | |
| 149 | + * End of greedy search for rating and comments | |
| 150 | + */ | |
| 151 | + | |
| 152 | + return $this->render('products', [ | |
| 153 | + 'category' => $category, | |
| 154 | + 'brandModel' => $brandModel, | |
| 155 | + 'brands' => $brands, | |
| 156 | + 'filter' => $filter, | |
| 157 | + 'params' => $params, | |
| 158 | + 'productModel' => $productModel, | |
| 159 | + 'productProvider' => $productProvider, | |
| 160 | + 'groups' => $groups, | |
| 161 | + 'priceLimits' => $priceLimits, | |
| 162 | + ]); | |
| 163 | + | |
| 93 | 164 | } |
| 94 | - | |
| 95 | - if(!empty($filter_check)){ | |
| 96 | - $filter = array_diff_key($filter,$filter_check); | |
| 97 | - Yii::$app->response->redirect(['catalog/category', 'category' => $category, 'filters' =>$filter],301); | |
| 98 | - } | |
| 99 | - | |
| 100 | - $productModel = new ProductFrontendSearch(); | |
| 101 | - //$productQuery = $productModel->getSearchQuery($category, $params); | |
| 102 | - $productProvider = $productModel->search($category, $params); | |
| 103 | - | |
| 104 | - $brandModel = new BrandSearch(); | |
| 105 | - $brands = $brandModel->getBrands($category, $params)->all(); | |
| 106 | - | |
| 107 | - $optionsQuery = $productModel->optionsForCategory($category, $params); | |
| 108 | - $groups = []; | |
| 109 | - | |
| 110 | - | |
| 111 | - foreach ($optionsQuery->all() as $option) { | |
| 112 | - if (!isset($groups[$option->tax_group_id])) { | |
| 113 | - $groups[$option->tax_group_id] = $option->taxGroup; | |
| 114 | - $groups[$option->tax_group_id]->_options = []; | |
| 165 | + | |
| 166 | + public function actionProduct() | |
| 167 | + { | |
| 168 | + /** @var Product $product */ | |
| 169 | + $product = Yii::$app->request->get('product'); | |
| 170 | + | |
| 171 | + if(!$product->enabledVariant) { | |
| 172 | + throw new HttpException(404, 'Товар не найден'); | |
| 173 | + } | |
| 174 | + | |
| 175 | + ProductHelper::addLastProsucts($product->product_id); | |
| 176 | + | |
| 177 | + $category = null; | |
| 178 | + $last_category_id = ProductHelper::getLastCategory(); | |
| 179 | + if(!empty($last_category_id)) { | |
| 180 | + $category = $product->getCategory()->andWhere(['category_id' => $last_category_id])->one(); | |
| 115 | 181 | } |
| 116 | - $groups[$option->tax_group_id]->_options[] = $option; | |
| 182 | + if(empty($category)) { | |
| 183 | + $category = $product->category; | |
| 184 | + } | |
| 185 | + | |
| 186 | + return $this->render('product', [ | |
| 187 | + 'product' => $product, | |
| 188 | + 'category' => $category, | |
| 189 | + ]); | |
| 117 | 190 | } |
| 118 | - foreach($groups as $i => $group) { | |
| 119 | - if (empty($group->_options)) | |
| 120 | - unset($groups[$i]); | |
| 191 | + | |
| 192 | + public function actionBrands() | |
| 193 | + { | |
| 194 | + $dataProvider = new ActiveDataProvider([ | |
| 195 | + 'query' => Brand::find() | |
| 196 | + ->joinWith('brandName') | |
| 197 | + ->orderBy('brand_name.value'), | |
| 198 | + 'pagination' => [ | |
| 199 | + 'pageSize' => -1, | |
| 200 | + ], | |
| 201 | + ]); | |
| 202 | + | |
| 203 | + return $this->render('brands', [ | |
| 204 | + 'dataProvider' => $dataProvider, | |
| 205 | + ]); | |
| 121 | 206 | } |
| 122 | - | |
| 123 | - $priceLimits = $productModel->priceLimits($category, $params); | |
| 124 | - | |
| 125 | - /* | |
| 126 | - * Greedy search for comments and rating | |
| 127 | - */ | |
| 128 | - $query = $productProvider->query; | |
| 129 | - $query->with(['comments', 'averageRating']); | |
| 130 | - /* | |
| 131 | - * End of greedy search for rating and comments | |
| 132 | - */ | |
| 133 | - | |
| 134 | - return $this->render( | |
| 135 | - 'products', | |
| 136 | - [ | |
| 137 | - 'category' => $category, | |
| 138 | - 'brandModel' => $brandModel, | |
| 139 | - 'brands' => $brands, | |
| 140 | - 'filter' => $filter, | |
| 141 | - 'params' => $params, | |
| 142 | - 'productModel' => $productModel, | |
| 143 | - 'productProvider' => $productProvider, | |
| 144 | - 'groups' => $groups, | |
| 145 | - 'priceLimits' => $priceLimits, | |
| 146 | - ] | |
| 147 | - ); | |
| 148 | - | |
| 149 | - } | |
| 150 | - | |
| 151 | - public function actionProduct() | |
| 152 | - { | |
| 153 | - /** @var Product $product */ | |
| 154 | - $product = Yii::$app->request->get('product'); | |
| 155 | - | |
| 156 | - if(!$product->enabledVariant) { | |
| 157 | - throw new HttpException(404, 'Товар не найден'); | |
| 207 | + | |
| 208 | + public function actionBrand($brand) | |
| 209 | + { | |
| 210 | + $brand = BrandSearch::findByAlias($brand); | |
| 211 | + | |
| 212 | + $params = [ | |
| 213 | + 'brands' => $brand->brand_id, | |
| 214 | + ]; | |
| 215 | + | |
| 216 | + $productModel = new ProductFrontendSearch(); | |
| 217 | + $productProvider = $productModel->search(NULL, $params); | |
| 218 | + | |
| 219 | + $priceLimits = $productModel->priceLimits(NULL, $params); | |
| 220 | + | |
| 221 | + return $this->render('brand', [ | |
| 222 | + 'productModel' => $productModel, | |
| 223 | + 'productProvider' => $productProvider, | |
| 224 | + 'brand' => $brand, | |
| 225 | + 'priceLimits' => $priceLimits, | |
| 226 | + ]); | |
| 158 | 227 | } |
| 159 | - | |
| 160 | - ProductHelper::addLastProsucts($product->product_id); | |
| 161 | - | |
| 162 | - return $this->render('product', [ | |
| 163 | - 'product' => $product, | |
| 164 | - 'category' => $product->category, | |
| 165 | - ]); | |
| 166 | - } | |
| 167 | - | |
| 168 | - public function actionBrands() | |
| 169 | - { | |
| 170 | - $dataProvider = new ActiveDataProvider([ | |
| 171 | - 'query' => Brand::find()->joinWith('brandName')->orderBy('brand_name.value'), | |
| 172 | - 'pagination' => [ | |
| 173 | - 'pageSize' => -1, | |
| 174 | - ] | |
| 175 | - ]); | |
| 176 | - | |
| 177 | - return $this->render('brands', [ | |
| 178 | - 'dataProvider' => $dataProvider, | |
| 179 | - ]); | |
| 180 | - } | |
| 181 | - | |
| 182 | - public function actionBrand($brand) | |
| 183 | - { | |
| 184 | - $brand = BrandSearch::findByAlias($brand); | |
| 185 | - | |
| 186 | - $params = [ | |
| 187 | - 'brands' => $brand->brand_id, | |
| 188 | - ]; | |
| 189 | - | |
| 190 | - $productModel = new ProductFrontendSearch(); | |
| 191 | - $productProvider = $productModel->search(null, $params); | |
| 192 | - | |
| 193 | - $priceLimits = $productModel->priceLimits(null, $params); | |
| 194 | - | |
| 195 | - return $this->render('brand', [ | |
| 196 | - 'productModel' => $productModel, | |
| 197 | - 'productProvider' => $productProvider, | |
| 198 | - 'brand' => $brand, | |
| 199 | - 'priceLimits' => $priceLimits, | |
| 200 | - ]); | |
| 228 | + | |
| 201 | 229 | } |
| 202 | - | |
| 203 | -} | ... | ... |
frontend/models/ProductFrontendSearch.php
| ... | ... | @@ -48,8 +48,7 @@ class ProductFrontendSearch extends Product { |
| 48 | 48 | * @return ActiveDataProvider |
| 49 | 49 | */ |
| 50 | 50 | public function search($category = null, $params = []) { |
| 51 | - | |
| 52 | - | |
| 51 | + | |
| 53 | 52 | $dataProvider = new ActiveDataProvider([ |
| 54 | 53 | 'query' => $this->getSearchQuery($category, $params), |
| 55 | 54 | 'pagination' => [ | ... | ... |
frontend/views/catalog/product.php
| 1 | 1 | <?php |
| 2 | + /** | |
| 3 | + * @var Category $category | |
| 4 | + */ | |
| 2 | 5 | // use common\modules\comment\models\Comment; |
| 3 | 6 | // use common\modules\comment\models\Rating; |
| 4 | 7 | // use common\modules\comment\widgets\CommentWidget; |
| 5 | 8 | use common\modules\comment\widgets\CommentWidget; |
| 6 | - use kartik\rating\StarRating; | |
| 7 | - use yii\helpers\Html; | |
| 8 | - use yii\widgets\ActiveForm; | |
| 9 | - use yii\widgets\Breadcrumbs; | |
| 9 | + use common\modules\product\models\Category; | |
| 10 | 10 | use yii\web\View; |
| 11 | 11 | use yii\helpers\Url; |
| 12 | 12 | use frontend\widgets\Seo; |
| 13 | 13 | |
| 14 | - $this->params[ 'seo' ][ 'key' ] = $product->category->categoryName->value; | |
| 14 | + $this->params[ 'seo' ][ 'key' ] = $category->categoryName->value; | |
| 15 | 15 | $this->params[ 'seo' ][ 'fields' ][ 'name' ] = $product->fullname; |
| 16 | 16 | $this->params[ 'seo' ][ 'h1' ] = !empty( Seo::widget([ 'row' => 'h1' ]) ) ? Seo::widget([ 'row' => 'h1' ]) : $product->fullname; |
| 17 | 17 | |
| ... | ... | @@ -30,10 +30,10 @@ |
| 30 | 30 | 'url' => [ 'catalog/category' ], |
| 31 | 31 | ]; |
| 32 | 32 | $this->params[ 'breadcrumbs' ][] = [ |
| 33 | - 'label' => $product->category->categoryName->value, | |
| 33 | + 'label' => $category->categoryName->value, | |
| 34 | 34 | 'url' => [ |
| 35 | 35 | 'catalog/category', |
| 36 | - 'category' => $product->category, | |
| 36 | + 'category' => $category, | |
| 37 | 37 | ], |
| 38 | 38 | ]; |
| 39 | 39 | $this->params[ 'breadcrumbs' ][] = $product->fullname . ' #' . $product->enabledVariants[ 0 ]->sku; |
| ... | ... | @@ -126,8 +126,8 @@ |
| 126 | 126 | data-imageoriginal="<?= $variant->imageUrl ?>" |
| 127 | 127 | title="<?= $product->fullname ?>"> |
| 128 | 128 | <?= \common\components\artboximage\ArtboxImageHelper::getImage($variant->imageUrl, 'product_variant', [ |
| 129 | - 'alt' => $product->category->categoryName->value . ' ' . $product->fullname, | |
| 130 | - 'title' => $product->category->categoryName->value . ' ' . $product->fullname, | |
| 129 | + 'alt' => $category->categoryName->value . ' ' . $product->fullname, | |
| 130 | + 'title' => $category->categoryName->value . ' ' . $product->fullname, | |
| 131 | 131 | ]) ?> |
| 132 | 132 | </a> |
| 133 | 133 | </li> |
| ... | ... | @@ -228,7 +228,7 @@ |
| 228 | 228 | <li><a href="#">Характеристики</a> |
| 229 | 229 | <div class="info"> |
| 230 | 230 | <p>Бренд: <?= $product->brand->name ?></p> |
| 231 | - <?php foreach($product->properties as $group): ?> | |
| 231 | + <?php foreach($product->getActiveProperties($category->category_id) as $group): ?> | |
| 232 | 232 | <p><?= $group->name ?> <?php foreach($group->_options as $option) : ?> <?= $option->ValueRenderHTML ?><?php endforeach ?></p> |
| 233 | 233 | <?php endforeach; ?> |
| 234 | 234 | </div> |
| ... | ... | @@ -262,8 +262,8 @@ |
| 262 | 262 | <center> |
| 263 | 263 | <a href="#" rel="shadowbox[gal]" id="picoriginal"><?= \common\components\artboximage\ArtboxImageHelper::getImage($product->enabledVariants[ 0 ]->imageUrl, 'product_view', [ |
| 264 | 264 | 'id' => 'pic', |
| 265 | - 'alt' => $product->category->categoryName->value . ' ' . $product->fullname, | |
| 266 | - 'title' => $product->category->categoryName->value . ' ' . $product->fullname, | |
| 265 | + 'alt' => $category->categoryName->value . ' ' . $product->fullname, | |
| 266 | + 'title' => $category->categoryName->value . ' ' . $product->fullname, | |
| 267 | 267 | ]) ?></a> |
| 268 | 268 | </center> |
| 269 | 269 | </div> |
| ... | ... | @@ -271,8 +271,8 @@ |
| 271 | 271 | <?php foreach($product->images as $image): ?> |
| 272 | 272 | <li><a href="<?= $image->imageUrl ?>" rel="shadowbox[gal]"> |
| 273 | 273 | <?= \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'product_trumb2', [ |
| 274 | - 'alt' => $product->category->categoryName->value . ' ' . $product->fullname, | |
| 275 | - 'title' => $product->category->categoryName->value . ' ' . $product->fullname, | |
| 274 | + 'alt' => $category->categoryName->value . ' ' . $product->fullname, | |
| 275 | + 'title' => $category->categoryName->value . ' ' . $product->fullname, | |
| 276 | 276 | ]) ?> |
| 277 | 277 | </a></li> |
| 278 | 278 | <?php endforeach; ?> | ... | ... |
frontend/views/catalog/products.php
| ... | ... | @@ -2,9 +2,9 @@ |
| 2 | 2 | |
| 3 | 3 | /** @var $this \yii\web\View */ |
| 4 | 4 | /** @var $productProvider \yii\data\ActiveDataProvider */ |
| 5 | - | |
| 6 | - | |
| 7 | -use yii\helpers\Url; | |
| 5 | + | |
| 6 | + use yii\helpers\ArrayHelper; | |
| 7 | + use yii\helpers\Url; | |
| 8 | 8 | use yii\widgets\Breadcrumbs; |
| 9 | 9 | use yii\web\View; |
| 10 | 10 | use common\modules\product\helpers\ProductHelper; |
| ... | ... | @@ -62,9 +62,7 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[ |
| 62 | 62 | }); |
| 63 | 63 | <?php endif?> |
| 64 | 64 | </script> |
| 65 | - | |
| 66 | - | |
| 67 | - | |
| 65 | + | |
| 68 | 66 | <div class="loyout"> |
| 69 | 67 | <div class="leftbar"> |
| 70 | 68 | <?php /* |
| ... | ... | @@ -125,23 +123,44 @@ $this->registerJsFile(Yii::getAlias('@web/js/ion.rangeSlider.js'),[ |
| 125 | 123 | <?php endif?> |
| 126 | 124 | |
| 127 | 125 | <?php if (!empty($groups)) :?> |
| 126 | + <?php | |
| 127 | + foreach($groups as $group_name => $group) { | |
| 128 | + ?> | |
| 129 | + <div class="filters"> | |
| 130 | + <div class="begin"><?= $group_name?></div> | |
| 131 | + <ul> | |
| 132 | + <?php foreach($group as $option) : | |
| 133 | + $checked = (isset($filter[$option['group_alias']]) && in_array($option['option_alias'], $filter[$option['group_alias']])); | |
| 134 | + $option_url = Url::to(['catalog/category', 'category' => $category, 'filters' => FilterHelper::getFilterForOption($filter, $option['group_alias'], $option['option_alias'], $checked)]); | |
| 135 | + ?> | |
| 136 | + <li> | |
| 137 | + <input type="checkbox" onchange="document.location='<?= $option_url?>'" class="features-option" <?php /* name="option[<?= $group->alias?>][]"value="<?= $option->alias?>"*/?><?= $checked ? ' checked' : ''?> /> | |
| 138 | + | |
| 139 | + <a href="<?= $option_url?>"><?= $option['value']?></a> | |
| 140 | + </li> | |
| 141 | + <?php endforeach?> | |
| 142 | + </ul> | |
| 143 | + </div> | |
| 144 | + <?php | |
| 145 | + } | |
| 146 | + /*?> | |
| 128 | 147 | <?php foreach($groups as $group) :?> |
| 129 | 148 | <div class="filters"> |
| 130 | - <div class="begin"><?= $group->name?></div> | |
| 149 | + <div class="begin"><?= $group['name']?></div> | |
| 131 | 150 | <ul> |
| 132 | 151 | <?php foreach($group->_options as $option) : |
| 133 | 152 | $checked = (isset($filter[$group->alias]) && in_array($option->alias, $filter[$group->alias])); |
| 134 | 153 | $option_url = Url::to(['catalog/category', 'category' => $category, 'filters' => FilterHelper::getFilterForOption($filter, $option->taxGroup->alias, $option->alias, $checked)]); |
| 135 | 154 | ?> |
| 136 | 155 | <li> |
| 137 | - <input type="checkbox" onchange="document.location='<?= $option_url?>'" class="features-option" <?php /* name="option[<?= $group->alias?>][]"value="<?= $option->alias?>"*/?><?= $checked ? ' checked' : ''?> /> | |
| 156 | + <input type="checkbox" onchange="document.location='<?= $option_url?>'" class="features-option" <?php /* name="option[<?= $group->alias?>][]"value="<?= $option->alias?>"*//*?><?= $checked ? ' checked' : ''?> /> | |
| 138 | 157 | |
| 139 | 158 | <a href="<?= $option_url?>"><?= $option->ValueRenderHTML?></a> |
| 140 | 159 | </li> |
| 141 | 160 | <?php endforeach?> |
| 142 | 161 | </ul> |
| 143 | 162 | </div> |
| 144 | - <?php endforeach?> | |
| 163 | + <?php endforeach */?> | |
| 145 | 164 | <?php endif?> |
| 146 | 165 | |
| 147 | 166 | <?php if ($priceLimits['min'] < $priceLimits['max']) :?> | ... | ... |
frontend/views/search/index.php
| ... | ... | @@ -21,17 +21,6 @@ $this->params['breadcrumbs'][] = ['label' => 'Поиск', 'url' => ['catalog/ca |
| 21 | 21 | $this->params['seo']['meta']= 'noindex,follow'; |
| 22 | 22 | ?> |
| 23 | 23 | |
| 24 | - | |
| 25 | -<nav class="bread-crumbs"> | |
| 26 | - <?= \yii\widgets\Breadcrumbs::widget ([ | |
| 27 | - 'links' => $this->params['breadcrumbs'], | |
| 28 | - ]) | |
| 29 | - ?> | |
| 30 | - | |
| 31 | - | |
| 32 | - <div class="both"></div> | |
| 33 | -</nav> | |
| 34 | - | |
| 35 | 24 | <div class="loyout"> |
| 36 | 25 | <?php if(!empty($categories)) :?> |
| 37 | 26 | <div class="leftbar"> | ... | ... |