Commit 1fd392dfb8561682f338465522c2ae64422f0a31
1 parent
2f5c8d11
add variantSku
Showing
1 changed file
with
350 additions
and
356 deletions
Show diff stats
helpers/FilterHelper.php
| 1 | 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 | 78 | $value_item, |
| 69 | 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 ] ); | |
| 92 | + } | |
| 93 | + } else { | |
| 94 | + if (!isset( $result[ $key ] ) || array_search($value, $result[ $key ]) === false) { | |
| 95 | + $result[ $key ][] = $value; | |
| 105 | 96 | } |
| 106 | - | |
| 107 | 97 | } |
| 108 | - | |
| 109 | - return $filterView; | |
| 110 | 98 | } |
| 111 | 99 | |
| 100 | + $filterView = []; | |
| 112 | 101 | |
| 102 | + foreach ($optionsTemplate as $optionKey) { | |
| 103 | + if (isset( $result[ $optionKey ] )) { | |
| 104 | + $filterView[ $optionKey ] = $result[ $optionKey ]; | |
| 105 | + } | |
| 113 | 106 | |
| 114 | - /** | |
| 115 | - * select options for product variants with selected category | |
| 116 | - * | |
| 117 | - * @param integer $categoryId | |
| 118 | - * @param integer $langId | |
| 119 | - * @return mixed | |
| 120 | - */ | |
| 121 | - public static function getProductVariantOptions($categoryId,$langId){ | |
| 122 | - $result = \Yii::$app->db->cache(function () use($categoryId,$langId) { | |
| 123 | - return ( new Query() )->distinct()->select('tax_group_lang.alias') | |
| 124 | - ->from('product_variant_option') | |
| 125 | - ->innerJoin( | |
| 126 | - 'tax_option', | |
| 127 | - 'product_variant_option.option_id = tax_option.id' | |
| 128 | - ) | |
| 129 | - ->innerJoin( | |
| 130 | - 'tax_group', | |
| 131 | - 'tax_group.id = tax_option.tax_group_id' | |
| 132 | - ) | |
| 133 | - ->innerJoin( | |
| 134 | - 'tax_group_lang', | |
| 135 | - 'tax_group_lang.tax_group_id = tax_group.id' | |
| 136 | - ) | |
| 137 | - ->innerJoin( | |
| 138 | - 'tax_group_to_category', | |
| 139 | - 'tax_group_to_category.tax_group_id = tax_group.id' | |
| 140 | - ) | |
| 141 | - ->where([ | |
| 142 | - 'tax_group_lang.language_id' => $langId, | |
| 143 | - 'tax_group_to_category.category_id' => $categoryId, | |
| 144 | - 'tax_group.is_filter' => true | |
| 145 | - ])->all(); | |
| 146 | - },60*60*24); | |
| 147 | - return ArrayHelper::getColumn($result,'alias'); | |
| 148 | 107 | } |
| 149 | 108 | |
| 109 | + return $filterView; | |
| 110 | + } | |
| 150 | 111 | |
| 151 | - /** | |
| 152 | - * Fill query with filter conditions | |
| 153 | - * | |
| 154 | - * @param ActiveQuery $query | |
| 155 | - * @param array $params | |
| 156 | - * @param integer $categoryId | |
| 157 | - * @param integer $langId | |
| 158 | - */ | |
| 159 | - public static function setQueryParams(ActiveQuery $query, array $params, $categoryId, $langId) | |
| 160 | - { | |
| 161 | - $last_query = null; | |
| 162 | - $productVariantOptions = self::getProductVariantOptions($categoryId,$langId); | |
| 163 | - foreach ($params as $key => $param) { | |
| 164 | - switch ($key) { | |
| 165 | - case 'special': | |
| 166 | - unset($params[$key]); | |
| 167 | - self::filterSpecial($param, $query); | |
| 168 | - break; | |
| 169 | - case 'brands': | |
| 170 | - unset($params[$key]); | |
| 171 | - self::filterBrands($param, $query); | |
| 172 | - break; | |
| 173 | - case 'keywords': | |
| 174 | - unset($params[$key]); | |
| 175 | - self::filterKeywords($param, $query); | |
| 176 | - break; | |
| 177 | - case 'prices': | |
| 178 | - unset($params[$key]); | |
| 179 | - self::filterPrices($param, $query); | |
| 180 | - break; | |
| 181 | 112 | |
| 182 | - } | |
| 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; | |
| 183 | 136 | } |
| 184 | - self::filterOptions($params, $productVariantOptions, $query); | |
| 185 | 137 | } |
| 138 | + } | |
| 186 | 139 | |
| 187 | 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 | + ); | |
| 188 | 172 | |
| 189 | - /** | |
| 190 | - * Tax Option filter | |
| 191 | - * | |
| 192 | - * @param string[] $params | |
| 193 | - * @param string[] $productVariantOptions | |
| 194 | - * @param \yii\db\Query|null $query | |
| 195 | - * @param bool $in_stock | |
| 196 | - * | |
| 197 | - * @return \yii\db\Query | |
| 198 | - */ | |
| 199 | - private static function filterOptions(array $params,$productVariantOptions, Query &$query = null, bool $in_stock = true) | |
| 200 | - { | |
| 201 | - $variant_query = ( new Query() )->distinct() | |
| 202 | - ->select('product_variant.product_id as products') | |
| 203 | - ->from('product_variant'); | |
| 204 | - | |
| 205 | - foreach ($params as $key=>$param){ | |
| 206 | - if(in_array($key, $productVariantOptions)){ | |
| 207 | - unset($params[$key]); | |
| 208 | - $product_variant_id = ( new Query() )->distinct()->select('product_variant_option.product_variant_id as id') | |
| 209 | - ->from('product_variant_option') | |
| 210 | - ->innerJoin( | |
| 211 | - 'tax_option', | |
| 212 | - 'tax_option.id = product_variant_option.option_id' | |
| 213 | - ) | |
| 214 | - ->innerJoin( | |
| 215 | - 'tax_option_lang', | |
| 216 | - 'tax_option_lang.tax_option_id = tax_option.id' | |
| 217 | - ) | |
| 218 | - ->innerJoin( | |
| 219 | - 'tax_group', | |
| 220 | - 'tax_group.id = tax_option.tax_group_id' | |
| 221 | - ) | |
| 222 | - ->innerJoin( | |
| 223 | - 'tax_group_lang', | |
| 224 | - 'tax_group_lang.tax_group_id = tax_group.id' | |
| 225 | - ) | |
| 226 | - ->where([ 'tax_group_lang.alias' => $key ]) | |
| 227 | - ->andWhere([ 'tax_option_lang.alias' => $param ]); | |
| 228 | - $variant_query->andWhere(['product_variant.id'=>$product_variant_id]); | |
| 229 | - } | |
| 230 | 173 | |
| 231 | - } | |
| 174 | + } | |
| 232 | 175 | |
| 233 | 176 | |
| 234 | - if($in_stock) { | |
| 235 | - $variant_query->andWhere(['!=', 'product_variant.stock', 0]); | |
| 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; | |
| 236 | 203 | } |
| 237 | - $product_query = ( new Query() )->distinct() | |
| 238 | - ->select('product_option.product_id as products') | |
| 239 | - ->from('product_option') | |
| 240 | - ->innerJoin( | |
| 241 | - 'tax_option', | |
| 242 | - 'tax_option.id = product_option.option_id' | |
| 243 | - ) | |
| 244 | - ->innerJoin( | |
| 245 | - 'tax_option_lang', | |
| 246 | - 'tax_option_lang.tax_option_id = tax_option.id' | |
| 247 | - ) | |
| 248 | - ->innerJoin( | |
| 249 | - 'tax_group', | |
| 250 | - 'tax_group.id = tax_option.tax_group_id' | |
| 251 | - ) | |
| 252 | - ->innerJoin( | |
| 253 | - 'tax_group_lang', | |
| 254 | - 'tax_group_lang.tax_group_id = tax_group.id' | |
| 255 | - ); | |
| 256 | - foreach ($params as $key=>$param){ | |
| 257 | - $product_query | |
| 258 | - ->where([ 'tax_group_lang.alias' => $key ]) | |
| 259 | - ->andWhere([ 'tax_option_lang.alias' => $param ]); | |
| 204 | + } | |
| 205 | + // If tax option filters were provided filter query with them | |
| 206 | + if (!empty( $last_query )) { | |
| 207 | + $query->andWhere([ 'product.id' => $last_query ]); | |
| 208 | + } | |
| 209 | + } | |
| 260 | 210 | |
| 261 | - } | |
| 262 | 211 | |
| 263 | - $query->andWhere([ 'product.id' => $product_query ]); | |
| 264 | - $query->andWhere([ 'product.id' => $variant_query ]); | |
| 265 | 212 | |
| 266 | - } | |
| 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 | + { | |
| 267 | 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 ]); | |
| 260 | + } | |
| 261 | + return $query; | |
| 262 | + } | |
| 268 | 263 | |
| 269 | - | |
| 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 = []; | |
| 270 | 273 | /** |
| 271 | - * Fill $query with special filters (used in Product) | |
| 272 | - * | |
| 273 | - * @param array $params | |
| 274 | - * @param \yii\db\ActiveQuery $query | |
| 274 | + * @var string $key | |
| 275 | 275 | */ |
| 276 | - private static function filterSpecial(array $params, ActiveQuery $query) | |
| 277 | - { | |
| 278 | - $conditions = []; | |
| 279 | - /** | |
| 280 | - * @var string $key | |
| 281 | - */ | |
| 282 | - foreach ($params as $key => $param) { | |
| 283 | - $conditions[] = [ | |
| 284 | - '=', | |
| 285 | - Product::tableName() . '.' . $key, | |
| 286 | - $param, | |
| 287 | - ]; | |
| 288 | - } | |
| 289 | - /* If 2 or more special conditions get all that satisfy at least one of them. */ | |
| 290 | - if (count($conditions) > 1) { | |
| 291 | - array_unshift($conditions, 'or'); | |
| 292 | - } else { | |
| 293 | - $conditions = $conditions[ 0 ]; | |
| 294 | - } | |
| 295 | - $query->andFilterWhere($conditions); | |
| 276 | + foreach ($params as $key => $param) { | |
| 277 | + $conditions[] = [ | |
| 278 | + '=', | |
| 279 | + Product::tableName() . '.' . $key, | |
| 280 | + $param, | |
| 281 | + ]; | |
| 296 | 282 | } |
| 297 | - | |
| 298 | - /** | |
| 299 | - * Fill query with brands filter | |
| 300 | - * | |
| 301 | - * @param int[] $param | |
| 302 | - * @param \yii\db\ActiveQuery $query | |
| 303 | - */ | |
| 304 | - private static function filterBrands(array $param, ActiveQuery $query) | |
| 305 | - { | |
| 306 | - $query->andFilterWhere([ Product::tableName() . '.brand_id' => $param ]); | |
| 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 ]; | |
| 307 | 288 | } |
| 308 | - | |
| 309 | - /** | |
| 310 | - * Fill query with keywords filter | |
| 311 | - * | |
| 312 | - * @param array $params | |
| 313 | - * @param \yii\db\ActiveQuery $query | |
| 314 | - */ | |
| 315 | - private static function filterKeywords(array $params, ActiveQuery $query) | |
| 316 | - { | |
| 317 | - $conditions = []; | |
| 318 | - if (!empty( $params )) { | |
| 319 | - if (!is_array($params)) { | |
| 320 | - $params = [ $params ]; | |
| 321 | - } | |
| 322 | - /** | |
| 323 | - * @var string $param Inputed keyword | |
| 324 | - */ | |
| 325 | - foreach ($params as $param) { | |
| 326 | - | |
| 327 | - if(iconv_strlen($param) >= 3){ | |
| 328 | - $conditions[] = [ | |
| 329 | - 'or', | |
| 330 | - [ | |
| 331 | - 'ilike', | |
| 332 | - ProductLang::tableName() . '.title', | |
| 333 | - $param, | |
| 334 | - ], | |
| 335 | - [ | |
| 336 | - 'ilike', | |
| 337 | - BrandLang::tableName() . '.title', | |
| 338 | - $param, | |
| 339 | - ], | |
| 340 | - [ | |
| 341 | - 'ilike', | |
| 342 | - CategoryLang::tableName() . '.title', | |
| 343 | - $param, | |
| 344 | - ], | |
| 345 | - [ | |
| 346 | - 'ilike', | |
| 347 | - ProductVariantLang::tableName() . '.title', | |
| 348 | - $param, | |
| 349 | - ], | |
| 350 | - [ | |
| 351 | - 'ilike', | |
| 352 | - ProductVariant::tableName() . '.sku', | |
| 353 | - $param, | |
| 354 | - ] | |
| 355 | - | |
| 356 | - ]; | |
| 357 | - } | |
| 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 ]; | |
| 315 | + } | |
| 316 | + /** | |
| 317 | + * @var string $param Inputed keyword | |
| 318 | + */ | |
| 319 | + foreach ($params as $param) { | |
| 358 | 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 | + ] | |
| 359 | 349 | |
| 350 | + ]; | |
| 360 | 351 | } |
| 352 | + | |
| 353 | + | |
| 361 | 354 | } |
| 362 | - if (count($conditions) > 1) { | |
| 363 | - array_unshift($conditions, 'or'); | |
| 364 | - } else { | |
| 365 | - $conditions = $conditions[ 0 ]; | |
| 366 | - } | |
| 367 | - $query->andFilterWhere($conditions); | |
| 368 | 355 | } |
| 369 | - | |
| 370 | - /** | |
| 371 | - * Fill query with price limits filter | |
| 372 | - * | |
| 373 | - * @param array $params | |
| 374 | - * @param \yii\db\ActiveQuery $query | |
| 375 | - */ | |
| 376 | - private static function filterPrices(array $params, ActiveQuery $query) | |
| 377 | - { | |
| 378 | - $conditions = []; | |
| 379 | - if (!empty( $params[ 'min' ] ) && $params[ 'min' ] > 0) { | |
| 380 | - $conditions[] = [ | |
| 381 | - '>=', | |
| 382 | - ProductVariant::tableName() . '.price', | |
| 383 | - $params[ 'min' ], | |
| 384 | - ]; | |
| 385 | - } | |
| 386 | - if (!empty( $params[ 'max' ] ) && $params[ 'max' ] > 0) { | |
| 387 | - $conditions[] = [ | |
| 388 | - '<=', | |
| 389 | - ProductVariant::tableName() . '.price', | |
| 390 | - $params[ 'max' ], | |
| 391 | - ]; | |
| 392 | - } | |
| 393 | - if (count($conditions) > 1) { | |
| 394 | - array_unshift($conditions, 'and'); | |
| 395 | - } else { | |
| 396 | - $conditions = $conditions[ 0 ]; | |
| 397 | - } | |
| 398 | - $query->andFilterWhere($conditions); | |
| 356 | + if (count($conditions) > 1) { | |
| 357 | + array_unshift($conditions, 'or'); | |
| 358 | + } else { | |
| 359 | + $conditions = $conditions[ 0 ]; | |
| 399 | 360 | } |
| 400 | - | |
| 361 | + $query->andFilterWhere($conditions); | |
| 401 | 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); | |
| 393 | + } | |
| 394 | + | |
| 395 | +} | |
| 402 | 396 | |
| 403 | 397 | \ No newline at end of file | ... | ... |