Commit 0426401574fa5bae56845a39fbca7506202e9586
Merge remote-tracking branch 'origin/master'
Showing
7 changed files
with
384 additions
and
310 deletions
Show diff stats
CatalogUrlManager.php
@@ -49,6 +49,8 @@ | @@ -49,6 +49,8 @@ | ||
49 | throw new HttpException(404, 'Page not found'); | 49 | throw new HttpException(404, 'Page not found'); |
50 | } | 50 | } |
51 | $params[ 'category' ] = $category; | 51 | $params[ 'category' ] = $category; |
52 | + } else { | ||
53 | + throw new HttpException(404, 'Page not found'); | ||
52 | } | 54 | } |
53 | if (!empty( $paths[ 2 ] )) { | 55 | if (!empty( $paths[ 2 ] )) { |
54 | // Filter | 56 | // Filter |
helpers/FilterHelper.php
1 | <?php | 1 | <?php |
2 | - | ||
3 | - namespace artweb\artbox\ecommerce\helpers; | ||
4 | - | ||
5 | - use artweb\artbox\ecommerce\models\BrandLang; | ||
6 | - use artweb\artbox\ecommerce\models\CategoryLang; | ||
7 | - use artweb\artbox\ecommerce\models\Product; | ||
8 | - use artweb\artbox\ecommerce\models\ProductLang; | ||
9 | - use artweb\artbox\ecommerce\models\ProductVariant; | ||
10 | - use artweb\artbox\ecommerce\models\ProductVariantLang; | ||
11 | - use artweb\artbox\ecommerce\models\TaxGroup; | ||
12 | - use yii\base\Object; | ||
13 | - use yii\db\ActiveQuery; | ||
14 | - use yii\db\Query; | ||
15 | - use yii\helpers\ArrayHelper; | ||
16 | - | ||
17 | - class FilterHelper extends Object | 2 | + |
3 | +namespace artweb\artbox\ecommerce\helpers; | ||
4 | + | ||
5 | +use artweb\artbox\ecommerce\models\BrandLang; | ||
6 | +use artweb\artbox\ecommerce\models\CategoryLang; | ||
7 | +use artweb\artbox\ecommerce\models\Product; | ||
8 | +use artweb\artbox\ecommerce\models\ProductLang; | ||
9 | +use artweb\artbox\ecommerce\models\ProductVariant; | ||
10 | +use artweb\artbox\ecommerce\models\ProductVariantLang; | ||
11 | +use artweb\artbox\ecommerce\models\TaxGroup; | ||
12 | +use yii\base\Object; | ||
13 | +use yii\db\ActiveQuery; | ||
14 | +use yii\db\Query; | ||
15 | +use yii\helpers\ArrayHelper; | ||
16 | + | ||
17 | +class FilterHelper extends Object | ||
18 | +{ | ||
19 | + | ||
20 | + public static $optionsList = []; | ||
21 | + | ||
22 | + /** | ||
23 | + * Get TaxGroups | ||
24 | + * | ||
25 | + * @return array | ||
26 | + */ | ||
27 | + public static function optionsTemplate() | ||
18 | { | 28 | { |
19 | - | ||
20 | - public static $optionsList = []; | ||
21 | - | ||
22 | - /** | ||
23 | - * Get TaxGroups | ||
24 | - * | ||
25 | - * @return array | ||
26 | - */ | ||
27 | - public static function optionsTemplate() | ||
28 | - { | ||
29 | - if (empty( static::$optionsList )) { | ||
30 | - return static::$optionsList = ArrayHelper::getColumn( | ||
31 | - TaxGroup::find() | ||
32 | - ->joinWith('lang') | ||
33 | - ->where([ 'is_filter' => 'TRUE' ]) | ||
34 | - ->all(), | ||
35 | - 'lang.alias' | ||
36 | - ); | ||
37 | - } else { | ||
38 | - return static::$optionsList; | ||
39 | - } | ||
40 | - | 29 | + if (empty( static::$optionsList )) { |
30 | + return static::$optionsList = ArrayHelper::getColumn( | ||
31 | + TaxGroup::find() | ||
32 | + ->joinWith('lang') | ||
33 | + ->where([ 'is_filter' => 'TRUE' ]) | ||
34 | + ->all(), | ||
35 | + 'lang.alias' | ||
36 | + ); | ||
37 | + } else { | ||
38 | + return static::$optionsList; | ||
41 | } | 39 | } |
42 | - | ||
43 | - /** | ||
44 | - * Return custom filter-option link | ||
45 | - * | ||
46 | - * @param array $filter | ||
47 | - * @param string $key | ||
48 | - * @param mixed $value | ||
49 | - * @param bool $remove | ||
50 | - * | ||
51 | - * @return array | ||
52 | - */ | ||
53 | - public static function getFilterForOption(array $filter, string $key, $value, bool $remove = false) | ||
54 | - { | ||
55 | - | ||
56 | - $optionsTemplate = self::optionsTemplate(); | ||
57 | - array_unshift($optionsTemplate, "special", "brands"); | ||
58 | - | ||
59 | - $result = $filter; | ||
60 | - | ||
61 | - if (is_array($value)) { | ||
62 | - foreach ($value as $value_key => $value_items) { | ||
63 | - if (!is_array($value_items)) { | ||
64 | - $value_items = [ $value_items ]; | ||
65 | - } | ||
66 | - foreach ($value_items as $value_item) { | ||
67 | - if ($remove && isset( $result[ $key ] ) && ( $i = array_search( | 40 | + |
41 | + } | ||
42 | + | ||
43 | + /** | ||
44 | + * Return custom filter-option link | ||
45 | + * | ||
46 | + * @param array $filter | ||
47 | + * @param string $key | ||
48 | + * @param mixed $value | ||
49 | + * @param bool $remove | ||
50 | + * | ||
51 | + * @return array | ||
52 | + */ | ||
53 | + public static function getFilterForOption(array $filter, string $key, $value, bool $remove = false) | ||
54 | + { | ||
55 | + | ||
56 | + $optionsTemplate = self::optionsTemplate(); | ||
57 | + array_unshift($optionsTemplate, "special", "brands"); | ||
58 | + | ||
59 | + $result = $filter; | ||
60 | + | ||
61 | + if (is_array($value)) { | ||
62 | + foreach ($value as $value_key => $value_items) { | ||
63 | + if (!is_array($value_items)) { | ||
64 | + $value_items = [ $value_items ]; | ||
65 | + } | ||
66 | + foreach ($value_items as $value_item) { | ||
67 | + if ($remove && isset( $result[ $key ] ) && ( $i = array_search( | ||
68 | + $value_item, | ||
69 | + $result[ $key ][ $value_key ] | ||
70 | + ) ) !== false | ||
71 | + ) { | ||
72 | + unset( $result[ $key ][ $value_key ][ $i ] ); | ||
73 | + if (empty( $result[ $key ][ $value_key ] )) { | ||
74 | + unset( $result[ $key ][ $value_key ] ); | ||
75 | + } | ||
76 | + } else { | ||
77 | + if (!isset( $result[ $key ][ $value_key ] ) || array_search( | ||
68 | $value_item, | 78 | $value_item, |
69 | $result[ $key ][ $value_key ] | 79 | $result[ $key ][ $value_key ] |
70 | - ) ) !== false | 80 | + ) === false |
71 | ) { | 81 | ) { |
72 | - unset( $result[ $key ][ $value_key ][ $i ] ); | ||
73 | - if (empty( $result[ $key ][ $value_key ] )) { | ||
74 | - unset( $result[ $key ][ $value_key ] ); | ||
75 | - } | ||
76 | - } else { | ||
77 | - if (!isset( $result[ $key ][ $value_key ] ) || array_search( | ||
78 | - $value_item, | ||
79 | - $result[ $key ][ $value_key ] | ||
80 | - ) === false | ||
81 | - ) { | ||
82 | - $result[ $key ][ $value_key ][] = $value_item; | ||
83 | - } | 82 | + $result[ $key ][ $value_key ][] = $value_item; |
84 | } | 83 | } |
85 | } | 84 | } |
86 | } | 85 | } |
87 | - } else { | ||
88 | - if ($remove && isset( $result[ $key ] ) && ( $i = array_search($value, $result[ $key ]) ) !== false) { | ||
89 | - unset( $result[ $key ][ $i ] ); | ||
90 | - if (empty( $result[ $key ] )) { | ||
91 | - unset( $result[ $key ] ); | ||
92 | - } | ||
93 | - } else { | ||
94 | - if (!isset( $result[ $key ] ) || array_search($value, $result[ $key ]) === false) { | ||
95 | - $result[ $key ][] = $value; | ||
96 | - } | ||
97 | - } | ||
98 | } | 86 | } |
99 | - | ||
100 | - $filterView = []; | ||
101 | - | ||
102 | - foreach ($optionsTemplate as $optionKey) { | ||
103 | - if (isset( $result[ $optionKey ] )) { | ||
104 | - $filterView[ $optionKey ] = $result[ $optionKey ]; | 87 | + } else { |
88 | + if ($remove && isset( $result[ $key ] ) && ( $i = array_search($value, $result[ $key ]) ) !== false) { | ||
89 | + unset( $result[ $key ][ $i ] ); | ||
90 | + if (empty( $result[ $key ] )) { | ||
91 | + unset( $result[ $key ] ); | ||
105 | } | 92 | } |
106 | - | ||
107 | - } | ||
108 | - | ||
109 | - return $filterView; | ||
110 | - } | ||
111 | - | ||
112 | - /** | ||
113 | - * Fill query with filter conditions | ||
114 | - * | ||
115 | - * @param ActiveQuery $query | ||
116 | - * @param array $params | ||
117 | - */ | ||
118 | - public static function setQueryParams(ActiveQuery $query, array $params) | ||
119 | - { | ||
120 | - $last_query = null; | ||
121 | - foreach ($params as $key => $param) { | ||
122 | - switch ($key) { | ||
123 | - case 'special': | ||
124 | - self::filterSpecial($param, $query); | ||
125 | - break; | ||
126 | - case 'brands': | ||
127 | - self::filterBrands($param, $query); | ||
128 | - break; | ||
129 | - case 'keywords': | ||
130 | - self::filterKeywords($param, $query); | ||
131 | - break; | ||
132 | - case 'prices': | ||
133 | - self::filterPrices($param, $query); | ||
134 | - break; | ||
135 | - default: | ||
136 | - $last_query = self::filterOptions($param, $last_query); | ||
137 | - break; | 93 | + } else { |
94 | + if (!isset( $result[ $key ] ) || array_search($value, $result[ $key ]) === false) { | ||
95 | + $result[ $key ][] = $value; | ||
138 | } | 96 | } |
139 | } | 97 | } |
140 | - // If tax option filters were provided filter query with them | ||
141 | - if (!empty( $last_query )) { | ||
142 | - $query->andWhere([ 'product.id' => $last_query ]); | ||
143 | - } | ||
144 | } | 98 | } |
145 | - | ||
146 | - /** | ||
147 | - * Tax Option filter | ||
148 | - * | ||
149 | - * @param string[] $params | ||
150 | - * @param \yii\db\Query|null $last_query | ||
151 | - * | ||
152 | - * @return Query | ||
153 | - */ | ||
154 | - private static function filterOptions(array $params, Query $last_query = null): Query | ||
155 | - { | ||
156 | - $variant_query = ( new Query() )->distinct() | ||
157 | - ->select('product_variant.product_id as products') | ||
158 | - ->from('product_variant_option') | ||
159 | - ->innerJoin( | ||
160 | - 'product_variant', | ||
161 | - 'product_variant_option.product_variant_id = product_variant.id' | ||
162 | - ) | ||
163 | - ->innerJoin( | ||
164 | - 'tax_option', | ||
165 | - 'tax_option.id = product_variant_option.option_id' | ||
166 | - ) | ||
167 | - ->innerJoin( | ||
168 | - 'tax_option_lang', | ||
169 | - 'tax_option_lang.tax_option_id = tax_option.id' | ||
170 | - ) | ||
171 | - ->where([ 'tax_option_lang.alias' => $params ]); | ||
172 | - $product_query = ( new Query() )->distinct() | ||
173 | - ->select('product_option.product_id as products') | ||
174 | - ->from('product_option') | ||
175 | - ->innerJoin('tax_option', 'product_option.option_id = tax_option.id') | ||
176 | - ->innerJoin( | ||
177 | - 'tax_option_lang', | ||
178 | - 'tax_option_lang.tax_option_id = tax_option.id' | ||
179 | - ) | ||
180 | - ->where( | ||
181 | - [ 'tax_option_lang.alias' => $params ] | ||
182 | - ) | ||
183 | - ->union($variant_query); | ||
184 | - $query = ( new Query() )->select('products') | ||
185 | - ->from([ 'result_table' => $product_query ]); | ||
186 | - if (!empty( $last_query )) { | ||
187 | - $query->andWhere([ 'product.id' => $last_query ]); | 99 | + |
100 | + $filterView = []; | ||
101 | + | ||
102 | + foreach ($optionsTemplate as $optionKey) { | ||
103 | + if (isset( $result[ $optionKey ] )) { | ||
104 | + $filterView[ $optionKey ] = $result[ $optionKey ]; | ||
188 | } | 105 | } |
189 | - return $query; | 106 | + |
190 | } | 107 | } |
191 | - | ||
192 | - /** | ||
193 | - * Fill $query with special filters (used in Product) | ||
194 | - * | ||
195 | - * @param array $params | ||
196 | - * @param \yii\db\ActiveQuery $query | ||
197 | - */ | ||
198 | - private static function filterSpecial(array $params, ActiveQuery $query) | ||
199 | - { | ||
200 | - $conditions = []; | ||
201 | - /** | ||
202 | - * @var string $key | ||
203 | - */ | ||
204 | - foreach ($params as $key => $param) { | ||
205 | - $conditions[] = [ | ||
206 | - '=', | ||
207 | - Product::tableName() . '.' . $key, | ||
208 | - $param, | ||
209 | - ]; | 108 | + |
109 | + return $filterView; | ||
110 | + } | ||
111 | + | ||
112 | + | ||
113 | + | ||
114 | + | ||
115 | + | ||
116 | + public static function setNewQueryParams(ActiveQuery $query, array $params) | ||
117 | + { | ||
118 | + $last_query = null; | ||
119 | + foreach ($params as $key => $param) { | ||
120 | + switch ($key) { | ||
121 | + case 'special': | ||
122 | + self::filterSpecial($param, $query); | ||
123 | + break; | ||
124 | + case 'brands': | ||
125 | + self::filterBrands($param, $query); | ||
126 | + break; | ||
127 | + case 'keywords': | ||
128 | + self::filterKeywords($param, $query); | ||
129 | + break; | ||
130 | + case 'prices': | ||
131 | + self::filterPrices($param, $query); | ||
132 | + break; | ||
133 | + default: | ||
134 | + self::filterNewOptions($key, $param, $query); | ||
135 | + break; | ||
210 | } | 136 | } |
211 | - /* If 2 or more special conditions get all that satisfy at least one of them. */ | ||
212 | - if (count($conditions) > 1) { | ||
213 | - array_unshift($conditions, 'or'); | ||
214 | - } else { | ||
215 | - $conditions = $conditions[ 0 ]; | 137 | + } |
138 | + } | ||
139 | + | ||
140 | + | ||
141 | + /** | ||
142 | + * @param $key | ||
143 | + * @param array $param | ||
144 | + * @param ActiveQuery $query | ||
145 | + */ | ||
146 | + private static function filterNewOptions($key, array $param, &$query) | ||
147 | + { | ||
148 | + $query->andWhere( | ||
149 | + 'product.id IN ( | ||
150 | + SELECT DISTINCT products | ||
151 | + FROM ( | ||
152 | + SELECT id AS products FROM product WHERE id IN( | ||
153 | + SELECT product_id FROM product_option | ||
154 | + INNER JOIN tax_option ON tax_option.id = product_option.option_id | ||
155 | + INNER JOIN tax_group ON tax_group.id = tax_option.tax_group_id | ||
156 | + INNER JOIN tax_group_lang ON tax_group.id = tax_group_lang.tax_group_id | ||
157 | + INNER JOIN tax_option_lang ON tax_option.id = tax_option_lang.tax_option_id | ||
158 | + WHERE tax_group_lang.alias = \''. $key .'\' AND tax_option_lang.alias IN (\'' . implode('\',\'', $param) . '\')) | ||
159 | + OR id IN ( | ||
160 | + (SELECT product_id AS products | ||
161 | + FROM product_variant_option | ||
162 | + INNER JOIN product_variant ON product_variant_option.product_variant_id = product_variant.id | ||
163 | + INNER JOIN tax_option ON tax_option.id = product_variant_option.option_id | ||
164 | + INNER JOIN tax_group ON tax_group.id = tax_option.tax_group_id | ||
165 | + INNER JOIN tax_group_lang ON tax_group.id = tax_group_lang.tax_group_id | ||
166 | + INNER JOIN tax_option_lang ON tax_option.id = tax_option_lang.tax_option_id | ||
167 | + WHERE tax_group_lang.alias = \''. $key .'\' AND tax_option_lang.alias IN (\'' . implode('\',\'', $param) . '\')) | ||
168 | + ) | ||
169 | + ) AS table_name | ||
170 | + )' | ||
171 | + ); | ||
172 | + | ||
173 | + | ||
174 | + } | ||
175 | + | ||
176 | + | ||
177 | + /** | ||
178 | + * Fill query with filter conditions | ||
179 | + * | ||
180 | + * @param ActiveQuery $query | ||
181 | + * @param array $params | ||
182 | + */ | ||
183 | + public static function setQueryParams(ActiveQuery $query, array $params) | ||
184 | + { | ||
185 | + $last_query = null; | ||
186 | + foreach ($params as $key => $param) { | ||
187 | + switch ($key) { | ||
188 | + case 'special': | ||
189 | + self::filterSpecial($param, $query); | ||
190 | + break; | ||
191 | + case 'brands': | ||
192 | + self::filterBrands($param, $query); | ||
193 | + break; | ||
194 | + case 'keywords': | ||
195 | + self::filterKeywords($param, $query); | ||
196 | + break; | ||
197 | + case 'prices': | ||
198 | + self::filterPrices($param, $query); | ||
199 | + break; | ||
200 | + default: | ||
201 | + $last_query = self::filterOptions($param, $last_query); | ||
202 | + break; | ||
216 | } | 203 | } |
217 | - $query->andFilterWhere($conditions); | ||
218 | } | 204 | } |
219 | - | ||
220 | - /** | ||
221 | - * Fill query with brands filter | ||
222 | - * | ||
223 | - * @param int[] $param | ||
224 | - * @param \yii\db\ActiveQuery $query | ||
225 | - */ | ||
226 | - private static function filterBrands(array $param, ActiveQuery $query) | ||
227 | - { | ||
228 | - $query->andFilterWhere([ Product::tableName() . '.brand_id' => $param ]); | 205 | + // If tax option filters were provided filter query with them |
206 | + if (!empty( $last_query )) { | ||
207 | + $query->andWhere([ 'product.id' => $last_query ]); | ||
229 | } | 208 | } |
230 | - | ||
231 | - /** | ||
232 | - * Fill query with keywords filter | ||
233 | - * | ||
234 | - * @param array $params | ||
235 | - * @param \yii\db\ActiveQuery $query | ||
236 | - */ | ||
237 | - private static function filterKeywords(array $params, ActiveQuery $query) | ||
238 | - { | ||
239 | - $conditions = []; | ||
240 | - if (!empty( $params )) { | ||
241 | - if (!is_array($params)) { | ||
242 | - $params = [ $params ]; | ||
243 | - } | ||
244 | - /** | ||
245 | - * @var string $param Inputed keyword | ||
246 | - */ | ||
247 | - foreach ($params as $param) { | ||
248 | - | ||
249 | - if(iconv_strlen($param) >= 3){ | ||
250 | - $conditions[] = [ | ||
251 | - 'or', | ||
252 | - [ | ||
253 | - 'ilike', | ||
254 | - ProductLang::tableName() . '.title', | ||
255 | - $param, | ||
256 | - ], | ||
257 | - [ | ||
258 | - 'ilike', | ||
259 | - BrandLang::tableName() . '.title', | ||
260 | - $param, | ||
261 | - ], | ||
262 | - [ | ||
263 | - 'ilike', | ||
264 | - CategoryLang::tableName() . '.title', | ||
265 | - $param, | ||
266 | - ], | ||
267 | - [ | ||
268 | - 'ilike', | ||
269 | - ProductVariantLang::tableName() . '.title', | ||
270 | - $param, | ||
271 | - ], | ||
272 | - [ | ||
273 | - 'ilike', | ||
274 | - ProductVariant::tableName() . '.sku', | ||
275 | - $param, | ||
276 | - ] | ||
277 | - | ||
278 | - ]; | ||
279 | - } | 209 | + } |
280 | 210 | ||
281 | 211 | ||
282 | - } | ||
283 | - } | ||
284 | - if (count($conditions) > 1) { | ||
285 | - array_unshift($conditions, 'or'); | ||
286 | - } else { | ||
287 | - $conditions = $conditions[ 0 ]; | ||
288 | - } | ||
289 | - $query->andFilterWhere($conditions); | 212 | + |
213 | + /** | ||
214 | + * Tax Option filter | ||
215 | + * | ||
216 | + * @param string[] $params | ||
217 | + * @param \yii\db\Query|null $last_query | ||
218 | + * @param bool $in_stock | ||
219 | + * | ||
220 | + * @return \yii\db\Query | ||
221 | + */ | ||
222 | + private static function filterOptions(array $params, Query $last_query = null, bool $in_stock = true): Query | ||
223 | + { | ||
224 | + | ||
225 | + $variant_query = ( new Query() )->distinct() | ||
226 | + ->select('product_variant.product_id as products') | ||
227 | + ->from('product_variant_option') | ||
228 | + ->innerJoin( | ||
229 | + 'product_variant', | ||
230 | + 'product_variant_option.product_variant_id = product_variant.id' | ||
231 | + ) | ||
232 | + ->innerJoin( | ||
233 | + 'tax_option', | ||
234 | + 'tax_option.id = product_variant_option.option_id' | ||
235 | + ) | ||
236 | + ->innerJoin( | ||
237 | + 'tax_option_lang', | ||
238 | + 'tax_option_lang.tax_option_id = tax_option.id' | ||
239 | + ) | ||
240 | + ->where([ 'tax_option_lang.alias' => $params ]); | ||
241 | + if($in_stock) { | ||
242 | + $variant_query->andWhere(['!=', 'product_variant.stock', 0]); | ||
243 | + } | ||
244 | + $product_query = ( new Query() )->distinct() | ||
245 | + ->select('product_option.product_id as products') | ||
246 | + ->from('product_option') | ||
247 | + ->innerJoin('tax_option', 'product_option.option_id = tax_option.id') | ||
248 | + ->innerJoin( | ||
249 | + 'tax_option_lang', | ||
250 | + 'tax_option_lang.tax_option_id = tax_option.id' | ||
251 | + ) | ||
252 | + ->where( | ||
253 | + [ 'tax_option_lang.alias' => $params ] | ||
254 | + ) | ||
255 | + ->union($variant_query); | ||
256 | + $query = ( new Query() )->select('products') | ||
257 | + ->from([ 'result_table' => $product_query ]); | ||
258 | + if (!empty( $last_query )) { | ||
259 | + $query->andWhere([ 'product.id' => $last_query ]); | ||
290 | } | 260 | } |
291 | - | 261 | + return $query; |
262 | + } | ||
263 | + | ||
264 | + /** | ||
265 | + * Fill $query with special filters (used in Product) | ||
266 | + * | ||
267 | + * @param array $params | ||
268 | + * @param \yii\db\ActiveQuery $query | ||
269 | + */ | ||
270 | + private static function filterSpecial(array $params, ActiveQuery $query) | ||
271 | + { | ||
272 | + $conditions = []; | ||
292 | /** | 273 | /** |
293 | - * Fill query with price limits filter | ||
294 | - * | ||
295 | - * @param array $params | ||
296 | - * @param \yii\db\ActiveQuery $query | 274 | + * @var string $key |
297 | */ | 275 | */ |
298 | - private static function filterPrices(array $params, ActiveQuery $query) | ||
299 | - { | ||
300 | - $conditions = []; | ||
301 | - if (!empty( $params[ 'min' ] ) && $params[ 'min' ] > 0) { | ||
302 | - $conditions[] = [ | ||
303 | - '>=', | ||
304 | - ProductVariant::tableName() . '.price', | ||
305 | - $params[ 'min' ], | ||
306 | - ]; | ||
307 | - } | ||
308 | - if (!empty( $params[ 'max' ] ) && $params[ 'max' ] > 0) { | ||
309 | - $conditions[] = [ | ||
310 | - '<=', | ||
311 | - ProductVariant::tableName() . '.price', | ||
312 | - $params[ 'max' ], | ||
313 | - ]; | 276 | + foreach ($params as $key => $param) { |
277 | + $conditions[] = [ | ||
278 | + '=', | ||
279 | + Product::tableName() . '.' . $key, | ||
280 | + $param, | ||
281 | + ]; | ||
282 | + } | ||
283 | + /* If 2 or more special conditions get all that satisfy at least one of them. */ | ||
284 | + if (count($conditions) > 1) { | ||
285 | + array_unshift($conditions, 'or'); | ||
286 | + } else { | ||
287 | + $conditions = $conditions[ 0 ]; | ||
288 | + } | ||
289 | + $query->andFilterWhere($conditions); | ||
290 | + } | ||
291 | + | ||
292 | + /** | ||
293 | + * Fill query with brands filter | ||
294 | + * | ||
295 | + * @param int[] $param | ||
296 | + * @param \yii\db\ActiveQuery $query | ||
297 | + */ | ||
298 | + private static function filterBrands(array $param, ActiveQuery $query) | ||
299 | + { | ||
300 | + $query->andFilterWhere([ Product::tableName() . '.brand_id' => $param ]); | ||
301 | + } | ||
302 | + | ||
303 | + /** | ||
304 | + * Fill query with keywords filter | ||
305 | + * | ||
306 | + * @param array $params | ||
307 | + * @param \yii\db\ActiveQuery $query | ||
308 | + */ | ||
309 | + private static function filterKeywords(array $params, ActiveQuery $query) | ||
310 | + { | ||
311 | + $conditions = []; | ||
312 | + if (!empty( $params )) { | ||
313 | + if (!is_array($params)) { | ||
314 | + $params = [ $params ]; | ||
314 | } | 315 | } |
315 | - if (count($conditions) > 1) { | ||
316 | - array_unshift($conditions, 'and'); | ||
317 | - } else { | ||
318 | - $conditions = $conditions[ 0 ]; | 316 | + /** |
317 | + * @var string $param Inputed keyword | ||
318 | + */ | ||
319 | + foreach ($params as $param) { | ||
320 | + | ||
321 | + if(iconv_strlen($param) >= 3){ | ||
322 | + $conditions[] = [ | ||
323 | + 'or', | ||
324 | + [ | ||
325 | + 'ilike', | ||
326 | + ProductLang::tableName() . '.title', | ||
327 | + $param, | ||
328 | + ], | ||
329 | + [ | ||
330 | + 'ilike', | ||
331 | + BrandLang::tableName() . '.title', | ||
332 | + $param, | ||
333 | + ], | ||
334 | + [ | ||
335 | + 'ilike', | ||
336 | + CategoryLang::tableName() . '.title', | ||
337 | + $param, | ||
338 | + ], | ||
339 | + [ | ||
340 | + 'ilike', | ||
341 | + ProductVariantLang::tableName() . '.title', | ||
342 | + $param, | ||
343 | + ], | ||
344 | + [ | ||
345 | + 'ilike', | ||
346 | + ProductVariant::tableName() . '.sku', | ||
347 | + $param, | ||
348 | + ] | ||
349 | + | ||
350 | + ]; | ||
351 | + } | ||
352 | + | ||
353 | + | ||
319 | } | 354 | } |
320 | - $query->andFilterWhere($conditions); | ||
321 | } | 355 | } |
322 | - | 356 | + if (count($conditions) > 1) { |
357 | + array_unshift($conditions, 'or'); | ||
358 | + } else { | ||
359 | + $conditions = $conditions[ 0 ]; | ||
360 | + } | ||
361 | + $query->andFilterWhere($conditions); | ||
362 | + } | ||
363 | + | ||
364 | + /** | ||
365 | + * Fill query with price limits filter | ||
366 | + * | ||
367 | + * @param array $params | ||
368 | + * @param \yii\db\ActiveQuery $query | ||
369 | + */ | ||
370 | + private static function filterPrices(array $params, ActiveQuery $query) | ||
371 | + { | ||
372 | + $conditions = []; | ||
373 | + if (!empty( $params[ 'min' ] ) && $params[ 'min' ] > 0) { | ||
374 | + $conditions[] = [ | ||
375 | + '>=', | ||
376 | + ProductVariant::tableName() . '.price', | ||
377 | + $params[ 'min' ], | ||
378 | + ]; | ||
379 | + } | ||
380 | + if (!empty( $params[ 'max' ] ) && $params[ 'max' ] > 0) { | ||
381 | + $conditions[] = [ | ||
382 | + '<=', | ||
383 | + ProductVariant::tableName() . '.price', | ||
384 | + $params[ 'max' ], | ||
385 | + ]; | ||
386 | + } | ||
387 | + if (count($conditions) > 1) { | ||
388 | + array_unshift($conditions, 'and'); | ||
389 | + } else { | ||
390 | + $conditions = $conditions[ 0 ]; | ||
391 | + } | ||
392 | + $query->andFilterWhere($conditions); | ||
323 | } | 393 | } |
394 | + | ||
395 | +} | ||
324 | 396 | ||
325 | \ No newline at end of file | 397 | \ No newline at end of file |
models/Category.php
@@ -151,7 +151,7 @@ | @@ -151,7 +151,7 @@ | ||
151 | */ | 151 | */ |
152 | public function getProductUnit() | 152 | public function getProductUnit() |
153 | { | 153 | { |
154 | - return $this->hasOne(ProductUnit::className(), [ 'id' => 'product_unit_id' ])->inverseOf('product'); | 154 | + return $this->hasOne(ProductUnit::className(), [ 'id' => 'product_unit_id' ]); |
155 | } | 155 | } |
156 | 156 | ||
157 | /** | 157 | /** |
@@ -160,7 +160,7 @@ | @@ -160,7 +160,7 @@ | ||
160 | public function getProducts() | 160 | public function getProducts() |
161 | { | 161 | { |
162 | return $this->hasMany(Product::className(), [ 'id' => 'product_id' ]) | 162 | return $this->hasMany(Product::className(), [ 'id' => 'product_id' ]) |
163 | - ->viaTable('product_category', [ 'category_id' => 'id' ])->inverseOf('categories'); | 163 | + ->viaTable('product_category', [ 'category_id' => 'id' ]); |
164 | } | 164 | } |
165 | 165 | ||
166 | /** | 166 | /** |
models/Export.php
@@ -95,12 +95,14 @@ | @@ -95,12 +95,14 @@ | ||
95 | 95 | ||
96 | $categories = []; | 96 | $categories = []; |
97 | foreach ($product->categories as $value) { | 97 | foreach ($product->categories as $value) { |
98 | - if($value->parent_id){ | ||
99 | - $categories[] = '['.$value->parent->lang->title. $this->generateID($value->parent->remote_id).'>' .$value->lang->title . $this->generateID($value->remote_id).']'; | 98 | + if ($value->parent_id) { |
99 | + $categories[] = '[' . $value->parent->lang->title . $this->generateID( | ||
100 | + $value->parent->remote_id | ||
101 | + ) . '>' . $value->lang->title . $this->generateID($value->remote_id) . ']'; | ||
100 | } else { | 102 | } else { |
101 | - $categories[] = '['.$value->lang->title . $this->generateID($value->remote_id).']'; | 103 | + $categories[] = '[' . $value->lang->title . $this->generateID($value->remote_id) . ']'; |
102 | } | 104 | } |
103 | - | 105 | + |
104 | } | 106 | } |
105 | 107 | ||
106 | $categories = implode(',', $categories); | 108 | $categories = implode(',', $categories); |
models/Product.php
@@ -429,7 +429,7 @@ | @@ -429,7 +429,7 @@ | ||
429 | public function getCategories() | 429 | public function getCategories() |
430 | { | 430 | { |
431 | return $this->hasMany(Category::className(), [ 'id' => 'category_id' ]) | 431 | return $this->hasMany(Category::className(), [ 'id' => 'category_id' ]) |
432 | - ->viaTable('product_category', [ 'product_id' => 'id' ])->inverseOf('products'); | 432 | + ->viaTable('product_category', [ 'product_id' => 'id' ]); |
433 | } | 433 | } |
434 | 434 | ||
435 | /** | 435 | /** |
models/ProductFrontendSearch.php
@@ -116,15 +116,15 @@ | @@ -116,15 +116,15 @@ | ||
116 | 116 | ||
117 | return $dataProvider; | 117 | return $dataProvider; |
118 | } | 118 | } |
119 | - | 119 | + |
120 | public function getSearchQuery($category = null, $params = [], $in_stock = true) | 120 | public function getSearchQuery($category = null, $params = [], $in_stock = true) |
121 | { | 121 | { |
122 | - | 122 | + |
123 | if (!empty( $category )) { | 123 | if (!empty( $category )) { |
124 | /** @var ActiveQuery $query */ | 124 | /** @var ActiveQuery $query */ |
125 | /**@var Category $category * */ | 125 | /**@var Category $category * */ |
126 | $query = $category->getProducts(); | 126 | $query = $category->getProducts(); |
127 | - | 127 | + |
128 | } else { | 128 | } else { |
129 | $query = Product::find() | 129 | $query = Product::find() |
130 | ->joinWith( | 130 | ->joinWith( |
@@ -175,9 +175,8 @@ | @@ -175,9 +175,8 @@ | ||
175 | 'product_variant.price', | 175 | 'product_variant.price', |
176 | ] | 176 | ] |
177 | ); | 177 | ); |
178 | - | 178 | + |
179 | FilterHelper::setQueryParams($query, $params); | 179 | FilterHelper::setQueryParams($query, $params); |
180 | - | ||
181 | return $query; | 180 | return $query; |
182 | } | 181 | } |
183 | 182 | ||
@@ -202,7 +201,7 @@ | @@ -202,7 +201,7 @@ | ||
202 | 201 | ||
203 | // Price filter fix | 202 | // Price filter fix |
204 | unset( $params[ 'prices' ] ); | 203 | unset( $params[ 'prices' ] ); |
205 | - | 204 | + |
206 | FilterHelper::setQueryParams($query, $params); | 205 | FilterHelper::setQueryParams($query, $params); |
207 | $query->andWhere( | 206 | $query->andWhere( |
208 | [ | 207 | [ |
models/TaxGroup.php
@@ -138,7 +138,7 @@ | @@ -138,7 +138,7 @@ | ||
138 | public function getCategories() | 138 | public function getCategories() |
139 | { | 139 | { |
140 | return $this->hasMany(Category::className(), [ 'id' => 'category_id' ]) | 140 | return $this->hasMany(Category::className(), [ 'id' => 'category_id' ]) |
141 | - ->viaTable('tax_group_to_category', [ 'tax_group_id' => 'id' ])->inverseOf('taxGroups'); | 141 | + ->viaTable('tax_group_to_category', [ 'tax_group_id' => 'id' ]); |
142 | } | 142 | } |
143 | 143 | ||
144 | /** | 144 | /** |
@@ -172,8 +172,7 @@ | @@ -172,8 +172,7 @@ | ||
172 | */ | 172 | */ |
173 | public function getTaxOptions() | 173 | public function getTaxOptions() |
174 | { | 174 | { |
175 | - return $this->hasMany(TaxOption::className(), [ 'tax_group_id' => 'id' ]) | ||
176 | - ->inverseOf('taxGroup'); | 175 | + return $this->hasMany(TaxOption::className(), [ 'tax_group_id' => 'id' ]); |
177 | } | 176 | } |
178 | 177 | ||
179 | /** | 178 | /** |