Commit 91fc49014d0e5423ff7574485a89f173096bb8d7
1 parent
c2a935a6
Import fix
Showing
1 changed file
with
601 additions
and
547 deletions
Show diff stats
common/modules/product/models/Import.php
1 | <?php | 1 | <?php |
2 | - | ||
3 | -namespace common\modules\product\models; | ||
4 | - | ||
5 | -use common\models\EventsToProducts; | ||
6 | -use common\modules\product\helpers\ProductHelper; | ||
7 | -use common\modules\product\models\Category; | ||
8 | -use common\modules\product\models\CategoryName; | ||
9 | -use common\modules\product\models\ProductImage; | ||
10 | -use common\modules\product\models\ProductVariantType; | ||
11 | -use common\modules\rubrication\models\TaxGroup; | ||
12 | -use common\modules\rubrication\models\TaxOption; | ||
13 | -use common\modules\rubrication\models\TaxValueString; | ||
14 | -use Yii; | ||
15 | -use common\modules\product\models\Brand; | ||
16 | -use common\modules\product\models\BrandName; | ||
17 | -use common\modules\product\models\Product; | ||
18 | -use common\modules\product\models\ProductVariant; | ||
19 | -use common\modules\product\models\RemoteProducts; | ||
20 | -use yii\base\Model; | ||
21 | -use yii\helpers\ArrayHelper; | ||
22 | - | ||
23 | -class Import extends Model { | ||
24 | - public $file; | ||
25 | - public $type; | ||
26 | - | ||
27 | - public $errors = []; | ||
28 | - public $output = []; | ||
29 | - | ||
30 | - /** | ||
31 | - * @inheritdoc | ||
32 | - */ | ||
33 | - public function rules() | ||
34 | - { | ||
35 | - return [ | ||
36 | - [['type'], 'required'], | ||
37 | - [['type'], 'string'], | ||
38 | -// [['file'], 'safe'], | ||
39 | - [['file'], 'file', 'extensions' => 'csv'], | ||
40 | - ]; | ||
41 | - } | ||
42 | - | ||
43 | - /** | ||
44 | - * @inheritdoc | ||
45 | - */ | ||
46 | - public function attributeLabels() | 2 | + |
3 | + namespace common\modules\product\models; | ||
4 | + | ||
5 | + use common\models\EventsToProducts; | ||
6 | + use common\modules\product\helpers\ProductHelper; | ||
7 | + use common\modules\product\models\Category; | ||
8 | + use common\modules\product\models\CategoryName; | ||
9 | + use common\modules\product\models\ProductImage; | ||
10 | + use common\modules\product\models\ProductVariantType; | ||
11 | + use common\modules\rubrication\models\TaxGroup; | ||
12 | + use common\modules\rubrication\models\TaxOption; | ||
13 | + use common\modules\rubrication\models\TaxValueString; | ||
14 | + use Yii; | ||
15 | + use common\modules\product\models\Brand; | ||
16 | + use common\modules\product\models\BrandName; | ||
17 | + use common\modules\product\models\Product; | ||
18 | + use common\modules\product\models\ProductVariant; | ||
19 | + use common\modules\product\models\RemoteProducts; | ||
20 | + use yii\base\Model; | ||
21 | + use yii\helpers\ArrayHelper; | ||
22 | + | ||
23 | + class Import extends Model | ||
47 | { | 24 | { |
48 | - return [ | ||
49 | - 'file' => Yii::t('product', 'File'), | ||
50 | - ]; | ||
51 | - } | ||
52 | - | ||
53 | - public function getType() { | ||
54 | - if (!$this->type) { | ||
55 | - $this->type = 'products'; | ||
56 | - } | ||
57 | - return $this->type; | ||
58 | - } | ||
59 | - | ||
60 | - public function goPrices($from = 0, $limit = null) { | ||
61 | - set_time_limit(0); | ||
62 | - $new_products = $linked_products = 0; | ||
63 | - | ||
64 | - | ||
65 | - if ( !($handle = $this->getProductsFile('uploadFilePrices')) ) { | ||
66 | - $this->errors[] = 'File not found'; | ||
67 | - return FALSE; | ||
68 | - } | ||
69 | - | ||
70 | - if (file_exists(Yii::getAlias('@uploadDir/goPrices.lock'))) { | ||
71 | - return 'Task already executed'; | 25 | + public $file; |
26 | + public $type; | ||
27 | + | ||
28 | + public $errors = []; | ||
29 | + public $output = []; | ||
30 | + | ||
31 | + /** | ||
32 | + * @inheritdoc | ||
33 | + */ | ||
34 | + public function rules() | ||
35 | + { | ||
36 | + return [ | ||
37 | + [ | ||
38 | + [ 'type' ], | ||
39 | + 'required', | ||
40 | + ], | ||
41 | + [ | ||
42 | + [ 'type' ], | ||
43 | + 'string', | ||
44 | + ], | ||
45 | + // [['file'], 'safe'], | ||
46 | + [ | ||
47 | + [ 'file' ], | ||
48 | + 'file', | ||
49 | + 'extensions' => 'csv', | ||
50 | + ], | ||
51 | + ]; | ||
72 | } | 52 | } |
73 | - $ff = fopen(Yii::getAlias('@uploadDir/goPrices.lock'), 'w+'); | ||
74 | - fclose($ff); | ||
75 | - | ||
76 | - $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices')); | ||
77 | - if ($from) { | ||
78 | - fseek($handle, $from); | ||
79 | - | 53 | + |
54 | + /** | ||
55 | + * @inheritdoc | ||
56 | + */ | ||
57 | + public function attributeLabels() | ||
58 | + { | ||
59 | + return [ | ||
60 | + 'file' => Yii::t('product', 'File'), | ||
61 | + ]; | ||
80 | } | 62 | } |
81 | - | ||
82 | - $j = 0; | ||
83 | - | ||
84 | - $is_utf = (preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices'), null, null, null, 1000000))); | ||
85 | - | ||
86 | - | ||
87 | - if($from == 0){ | ||
88 | - ProductStock::updateAll(['quantity' => 0 ]); | ||
89 | - ProductVariant::updateAll(['status' => 1 ]); | 63 | + |
64 | + public function getType() | ||
65 | + { | ||
66 | + if (!$this->type) { | ||
67 | + $this->type = 'products'; | ||
68 | + } | ||
69 | + return $this->type; | ||
90 | } | 70 | } |
91 | - | ||
92 | - | ||
93 | - | ||
94 | - while (empty($limit) || $j++ < $limit) | 71 | + |
72 | + public function goPrices($from = 0, $limit = null) | ||
95 | { | 73 | { |
96 | - | ||
97 | - | ||
98 | - if(!(($data = fgetcsv ($handle, 10000, ";")) !== FALSE)){ | ||
99 | - break; | 74 | + set_time_limit(0); |
75 | + $new_products = $linked_products = 0; | ||
76 | + | ||
77 | + if (!( $handle = $this->getProductsFile('uploadFilePrices') )) { | ||
78 | + $this->errors[] = 'File not found'; | ||
79 | + return false; | ||
100 | } | 80 | } |
101 | - | ||
102 | - foreach ($data as &$value) | ||
103 | - { | ||
104 | - if (!$is_utf) { | ||
105 | - $value = iconv ('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value); | ||
106 | - } | ||
107 | - $value = trim ($value); | 81 | + |
82 | + if (file_exists(Yii::getAlias('@uploadDir/goPrices.lock'))) { | ||
83 | + return 'Task already executed'; | ||
108 | } | 84 | } |
109 | - | ||
110 | - // данные строк | ||
111 | - $modification_code = @$data[0]; | ||
112 | - $price = floatval(@$data[1]); | ||
113 | - $price_promo = floatval(@$data[2]); | ||
114 | - $count = intval(@$data[3]); | ||
115 | - $city_name = @$data[4]; | ||
116 | - $product_title = @$data[5]; | ||
117 | - | ||
118 | - if (empty ($modification_code)) { | ||
119 | - continue; | 85 | + $ff = fopen(Yii::getAlias('@uploadDir/goPrices.lock'), 'w+'); |
86 | + fclose($ff); | ||
87 | + | ||
88 | + $filesize = filesize(Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFilePrices')); | ||
89 | + if ($from) { | ||
90 | + fseek($handle, $from); | ||
91 | + | ||
120 | } | 92 | } |
121 | - // товары в пути | ||
122 | -// if (empty ($city_name)) | ||
123 | -// { | ||
124 | -// $this->saveNotFoundRecord ( | ||
125 | -// [$modification_code, $product_title], | ||
126 | -// Yii::getAlias('@uploadFilePricesAway') | ||
127 | -// ); | ||
128 | -// | ||
129 | -// $this->output[] = 'Товар '. $product_title . ' в пути'; | ||
130 | -// | ||
131 | -// continue; | ||
132 | -// } | ||
133 | - | ||
134 | - if ( ($productVariant = ProductVariant::find()->filterWhere(['sku' => $modification_code])->one()) === null ) { | ||
135 | - // 'Нет даной модификации в базе'; | ||
136 | -// $this->saveNotFoundRecord ( | ||
137 | -// [$modification_code, $product_title], | ||
138 | -// Yii::getAlias('@uploadFilePricesNoVariant') | ||
139 | -// ); | ||
140 | - | ||
141 | - $this->output[] = 'Для товара '. $product_title . ' не найдено соотвествие'; | ||
142 | - | ||
143 | - continue; | 93 | + |
94 | + $j = 0; | ||
95 | + | ||
96 | + $is_utf = ( preg_match( | ||
97 | + '//u', | ||
98 | + file_get_contents( | ||
99 | + Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFilePrices'), | ||
100 | + null, | ||
101 | + null, | ||
102 | + null, | ||
103 | + 1000000 | ||
104 | + ) | ||
105 | + ) ); | ||
106 | + | ||
107 | + if ($from == 0) { | ||
108 | + ProductStock::updateAll([ 'quantity' => 0 ]); | ||
109 | + ProductVariant::updateAll([ 'status' => 1 ]); | ||
144 | } | 110 | } |
145 | - | ||
146 | - | ||
147 | - | ||
148 | - | ||
149 | - // ===== Set stock ==== | ||
150 | - if ( $city_name ) { | ||
151 | - if ( ($stock = Stock::find()->filterWhere(['name' => trim($city_name)])->one()) === null ) { | ||
152 | - | 111 | + |
112 | + while (empty( $limit ) || $j++ < $limit) { | ||
113 | + | ||
114 | + if (!( ( $data = fgetcsv($handle, 10000, ";") ) !== false )) { | ||
115 | + break; | ||
116 | + } | ||
117 | + | ||
118 | + foreach ($data as &$value) { | ||
119 | + if (!$is_utf) { | ||
120 | + $value = iconv('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value); | ||
121 | + } | ||
122 | + $value = trim($value); | ||
123 | + } | ||
124 | + | ||
125 | + // данные строк | ||
126 | + $modification_code = @$data[ 0 ]; | ||
127 | + $price = floatval(@$data[ 1 ]); | ||
128 | + $price_promo = floatval(@$data[ 2 ]); | ||
129 | + $count = intval(@$data[ 3 ]); | ||
130 | + $city_name = @$data[ 4 ]; | ||
131 | + $product_title = @$data[ 5 ]; | ||
132 | + | ||
133 | + if (empty ( $modification_code )) { | ||
134 | + continue; | ||
135 | + } | ||
136 | + // товары в пути | ||
137 | + // if (empty ($city_name)) | ||
138 | + // { | ||
139 | + // $this->saveNotFoundRecord ( | ||
140 | + // [$modification_code, $product_title], | ||
141 | + // Yii::getAlias('@uploadFilePricesAway') | ||
142 | + // ); | ||
143 | + // | ||
144 | + // $this->output[] = 'Товар '. $product_title . ' в пути'; | ||
145 | + // | ||
146 | + // continue; | ||
147 | + // } | ||
148 | + | ||
149 | + if (( $productVariant = ProductVariant::find() | ||
150 | + ->filterWhere([ 'sku' => $modification_code ]) | ||
151 | + ->one() ) === null | ||
152 | + ) { | ||
153 | + // 'Нет даной модификации в базе'; | ||
154 | + // $this->saveNotFoundRecord ( | ||
155 | + // [$modification_code, $product_title], | ||
156 | + // Yii::getAlias('@uploadFilePricesNoVariant') | ||
157 | + // ); | ||
158 | + | ||
159 | + $this->output[] = 'Для товара ' . $product_title . ' не найдено соотвествие'; | ||
160 | + | ||
161 | + continue; | ||
162 | + } | ||
163 | + | ||
164 | + // ===== Set stock ==== | ||
165 | + if (!$city_name) { | ||
166 | + if (!$count) { | ||
167 | + continue; | ||
168 | + } | ||
169 | + $city_name = 'Склад'; | ||
170 | + } | ||
171 | + if (( $stock = Stock::find() | ||
172 | + ->filterWhere([ 'name' => trim($city_name) ]) | ||
173 | + ->one() ) === null | ||
174 | + ) { | ||
175 | + | ||
153 | // Create stock | 176 | // Create stock |
154 | $stock = new Stock(); | 177 | $stock = new Stock(); |
155 | $stock->name = trim($city_name); | 178 | $stock->name = trim($city_name); |
156 | $stock->save(); | 179 | $stock->save(); |
157 | } | 180 | } |
158 | - | ||
159 | - $productStock = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id, 'stock_id' => $stock->stock_id])->one(); | ||
160 | - if(!$productStock instanceof ProductStock) { | 181 | + |
182 | + $productStock = ProductStock::find() | ||
183 | + ->where( | ||
184 | + [ | ||
185 | + 'product_variant_id' => $productVariant->product_variant_id, | ||
186 | + 'stock_id' => $stock->stock_id, | ||
187 | + ] | ||
188 | + ) | ||
189 | + ->one(); | ||
190 | + if (!$productStock instanceof ProductStock) { | ||
161 | $productStock = new ProductStock; | 191 | $productStock = new ProductStock; |
162 | $productStock->product_variant_id = $productVariant->product_variant_id; | 192 | $productStock->product_variant_id = $productVariant->product_variant_id; |
163 | $productStock->stock_id = $stock->stock_id; | 193 | $productStock->stock_id = $stock->stock_id; |
164 | $productStock->product_id = $productVariant->product_id; | 194 | $productStock->product_id = $productVariant->product_id; |
165 | } | 195 | } |
166 | $productStock->quantity = $count; | 196 | $productStock->quantity = $count; |
167 | - | 197 | + |
168 | $productStock->save(); | 198 | $productStock->save(); |
169 | - $productStocks = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id])->andWhere(['<>', 'stock_id', $stock->stock_id])->all(); | ||
170 | - | 199 | + $productStocks = ProductStock::find() |
200 | + ->where([ 'product_variant_id' => $productVariant->product_variant_id ]) | ||
201 | + ->andWhere( | ||
202 | + [ | ||
203 | + '<>', | ||
204 | + 'stock_id', | ||
205 | + $stock->stock_id, | ||
206 | + ] | ||
207 | + ) | ||
208 | + ->all(); | ||
209 | + | ||
171 | $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count; | 210 | $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count; |
172 | - } else { | ||
173 | - if ($count == 0) { | ||
174 | - continue; | 211 | + |
212 | + if ($price_promo) { | ||
213 | + $productVariant->price_old = $price; | ||
214 | + $productVariant->price = $price_promo; | ||
175 | } else { | 215 | } else { |
176 | - $productStocks = ProductStock::find()->where(['product_variant_id' => $productVariant->product_variant_id])->all(); | ||
177 | - | ||
178 | - $quantity = array_sum(ArrayHelper::getColumn($productStocks, 'quantity')) + $count; | 216 | + $productVariant->price = $price; |
217 | + $productVariant->price_old = $price_promo; | ||
179 | } | 218 | } |
219 | + | ||
220 | + $productVariant->stock = $quantity; | ||
221 | + $productVariant->status = 0; | ||
222 | + $productVariant->save(); | ||
223 | + | ||
224 | + $this->output[] = '<font style="color:blue">Товар ' . $product_title . ' успешно сохранен</font>'; | ||
180 | } | 225 | } |
181 | - | ||
182 | - if ($price_promo) { | ||
183 | - $productVariant->price_old = $price; | ||
184 | - $productVariant->price = $price_promo; | ||
185 | - } else { | ||
186 | - $productVariant->price = $price; | ||
187 | - $productVariant->price_old = $price_promo; | 226 | + |
227 | + $result = [ | ||
228 | + 'end' => feof($handle), | ||
229 | + 'from' => ftell($handle), | ||
230 | + 'totalsize' => $filesize, | ||
231 | + 'items' => $this->output, | ||
232 | + | ||
233 | + ]; | ||
234 | + | ||
235 | + fclose($handle); | ||
236 | + | ||
237 | + if ($result[ 'end' ]) { | ||
238 | + unlink(Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFilePrices')); | ||
188 | } | 239 | } |
189 | - | ||
190 | - $productVariant->stock = $quantity; | ||
191 | - $productVariant->status = 0; | ||
192 | - $productVariant->save(); | ||
193 | - | ||
194 | - $this->output[] = '<font style="color:blue">Товар '. $product_title .' успешно сохранен</font>'; | ||
195 | - } | ||
196 | - | ||
197 | - $result = [ | ||
198 | - 'end' => feof($handle), | ||
199 | - 'from' => ftell($handle), | ||
200 | - 'totalsize' => $filesize, | ||
201 | - 'items' => $this->output, | ||
202 | - | ||
203 | - ]; | ||
204 | - | ||
205 | - fclose ($handle); | ||
206 | - | ||
207 | - if ($result['end']) { | ||
208 | - unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFilePrices')); | ||
209 | - } | ||
210 | - unlink(Yii::getAlias('@uploadDir/goPrices.lock')); | ||
211 | - return $result; | ||
212 | - } | ||
213 | - | ||
214 | - public function goProducts($from = 0, $limit = null) { | ||
215 | - | ||
216 | - set_time_limit(0); | ||
217 | - $new_products = $linked_products = 0; | ||
218 | - | ||
219 | - | ||
220 | - if ( !($handle = $this->getProductsFile('uploadFileProducts')) ) { | ||
221 | - $this->errors[] = 'File not found'; | ||
222 | - return FALSE; | ||
223 | - } | ||
224 | - if (file_exists(Yii::getAlias('@uploadDir/goProducts.lock'))) { | ||
225 | - return 'Task already executed'; | 240 | + unlink(Yii::getAlias('@uploadDir/goPrices.lock')); |
241 | + return $result; | ||
226 | } | 242 | } |
227 | - $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+'); | ||
228 | - fclose($ff); | ||
229 | - | ||
230 | - $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts')); | ||
231 | - | ||
232 | - | ||
233 | - if ($from) { | ||
234 | - fseek($handle, $from); | ||
235 | - } | ||
236 | - | ||
237 | - $j = 0; | ||
238 | - | ||
239 | - $is_utf = (preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'), null, null, null, 1000000))); | ||
240 | - | ||
241 | - $result_items = []; | ||
242 | - | ||
243 | - | ||
244 | - | ||
245 | - while ( empty($limit) || $j++ < $limit) | 243 | + |
244 | + public function goProducts($from = 0, $limit = null) | ||
246 | { | 245 | { |
247 | - if(!(($data = fgetcsv ($handle, 10000, ";")) !== FALSE)){ | ||
248 | - break; | ||
249 | - } | ||
250 | - | ||
251 | - foreach ($data as &$value) | ||
252 | - { | ||
253 | - if (!$is_utf) { | ||
254 | - $value = iconv ('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value); | ||
255 | - } | ||
256 | - $value = trim ($value); | ||
257 | - } | ||
258 | - | ||
259 | - // будет всегда 19 элементов | ||
260 | - for ($i = 0; $i <= 18; $i++) | ||
261 | - { | ||
262 | - if (! isset ($data[$i])) | ||
263 | - { | ||
264 | - $data[$i] = null; | ||
265 | - } | 246 | + |
247 | + set_time_limit(0); | ||
248 | + $new_products = $linked_products = 0; | ||
249 | + | ||
250 | + if (!( $handle = $this->getProductsFile('uploadFileProducts') )) { | ||
251 | + $this->errors[] = 'File not found'; | ||
252 | + return false; | ||
266 | } | 253 | } |
267 | - | ||
268 | - | ||
269 | - // 1 Группа (категория) | ||
270 | - $catalog_names = explode(',',$data[0]); | ||
271 | - | ||
272 | - if (empty ($catalog_names)) | ||
273 | - { | ||
274 | - $result_items[] = "Не указана категория (строка $j) "; | ||
275 | - continue; | 254 | + if (file_exists(Yii::getAlias('@uploadDir/goProducts.lock'))) { |
255 | + return 'Task already executed'; | ||
276 | } | 256 | } |
277 | - | ||
278 | - // 2 Бренд | ||
279 | - $brand_name = $data[1]; | ||
280 | - if (empty ($brand_name)) | ||
281 | - { | ||
282 | - $result_items[] = "Не указан бренд (строка $j)"; | ||
283 | - continue; | 257 | + $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+'); |
258 | + fclose($ff); | ||
259 | + | ||
260 | + $filesize = filesize(Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFileProducts')); | ||
261 | + | ||
262 | + if ($from) { | ||
263 | + fseek($handle, $from); | ||
284 | } | 264 | } |
285 | - | ||
286 | - // 3 Название товара | ||
287 | - $product_name = $data[2]; | ||
288 | - if (empty ($product_name)) | ||
289 | - { | ||
290 | - $result_items[] = "Не указано наименование товара (строка $j)"; | ||
291 | - continue; | ||
292 | - } | ||
293 | - | ||
294 | - // 4 Описание Укр | ||
295 | - $product_body_uk = $data[3]; | ||
296 | - | ||
297 | - // 5 Описание Рус | ||
298 | - $product_body_ru = $data[4]; | ||
299 | - | ||
300 | - // 6 Фильтр [god:2013-2014]*[pol:мужской]*[naznacenie-germo-beg:Для вещей]*[material-germo-bag:нет]*[value-germo-bag:нет]*[weight-germo-bag:нет]*[ipx-germo-bag:нет]*[in-pacage-bag:нет]*[size-germo-bag:нет]*[rekomend-germo-bag:нет] | ||
301 | - $filters = explode ('*', $data[5]); | ||
302 | - | ||
303 | - // 11 Цена акция | ||
304 | - $product_cost_old = floatval($data[7]); | ||
305 | - $product_cost = ''; | ||
306 | - // 10 Цена | ||
307 | - if ($product_cost_old) { | ||
308 | - $product_cost_old = floatval($data[6]); | ||
309 | - $product_cost = floatval($data[7]); | ||
310 | - } | ||
311 | - | ||
312 | - // 12 Акция | ||
313 | - $product_akciya = (bool)$data[8]; | ||
314 | - | ||
315 | - // 13 Сопуд. Тов. | ||
316 | - $similar = explode (',', $data[9]); | ||
317 | - | ||
318 | - // 14 Новинки | ||
319 | - $product_new = (bool)$data[10]; | ||
320 | - | ||
321 | - // 15 Топ продаж | ||
322 | - $product_top = (bool)$data[11]; | ||
323 | - | ||
324 | - | ||
325 | - // 17 ВИДЕО КОД | ||
326 | - $product_video = $data[12]; | ||
327 | - | ||
328 | - // 18 Галлерея фото | ||
329 | - if (trim($data[13])) { | ||
330 | - $fotos = explode (',', trim($data[13])); | ||
331 | - } | ||
332 | - | ||
333 | - // 19 Штрих код товара. | ||
334 | - // расшифровал - это модификации товара! | ||
335 | - | ||
336 | - $product_image = explode ('=', $data[14]); | ||
337 | - $product_image = @$product_image[3]; | ||
338 | - | ||
339 | - if ( ($_product = Product::find()->filterWhere(['name' => trim($product_name)])->one()) === null ) { | ||
340 | - $_product = new Product(); | ||
341 | - } | ||
342 | - | ||
343 | - $is_new_product = empty($_product->product_id); | ||
344 | - $category_id = []; | ||
345 | - foreach($catalog_names as $catalog_name){ | ||
346 | - | ||
347 | - // ==== Set category ==== | ||
348 | - if ( ($category = Category::find()->filterWhere([ 'name' => trim($catalog_name)])->one()) === null ) { | ||
349 | - // Create category | ||
350 | - $category = new Category(); | ||
351 | - $category->name = trim($catalog_name); | ||
352 | - $category->save(); | ||
353 | - } | ||
354 | - | ||
355 | - $category_id[] = $category->category_id; | ||
356 | - } | ||
357 | - | ||
358 | - | ||
359 | - $_product->categories = $category_id; | ||
360 | - | ||
361 | - // ===== Set brand ==== | ||
362 | - if ( $brand_name ) { | ||
363 | - if ( ($brand = Brand::find()->filterWhere(['name' => trim($brand_name)])->one()) !== null ) { | ||
364 | - $_product->brand_id = $brand->brand_id; | ||
365 | - } else { | ||
366 | - // Create brand | ||
367 | - $brand = new Brand(); | ||
368 | - $brand->name = trim($brand_name); | ||
369 | - $brand->save(); | ||
370 | - $_product->brand_id = $brand->brand_id; | 265 | + |
266 | + $j = 0; | ||
267 | + | ||
268 | + $is_utf = ( preg_match( | ||
269 | + '//u', | ||
270 | + file_get_contents( | ||
271 | + Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFileProducts'), | ||
272 | + null, | ||
273 | + null, | ||
274 | + null, | ||
275 | + 1000000 | ||
276 | + ) | ||
277 | + ) ); | ||
278 | + | ||
279 | + $result_items = []; | ||
280 | + | ||
281 | + while (empty( $limit ) || $j++ < $limit) { | ||
282 | + if (!( ( $data = fgetcsv($handle, 10000, ";") ) !== false )) { | ||
283 | + break; | ||
371 | } | 284 | } |
372 | - } | ||
373 | - | ||
374 | - $_product->name = $product_name; | ||
375 | - $_product->video = $product_video; | ||
376 | - $_product->description = $product_body_ru; | ||
377 | - $_product->is_top = $product_top; | ||
378 | - $_product->akciya = $product_akciya; | ||
379 | - $_product->is_new = $product_new; | ||
380 | - | ||
381 | - if (!$_product->save()) { | ||
382 | - $result_items[] = 'Product #'. $_product->name .' not saved' . " (строка $j)"; | ||
383 | - continue; | ||
384 | - } | ||
385 | - | ||
386 | - | ||
387 | - | ||
388 | - if (!empty($fotos)) { | ||
389 | - foreach($fotos as $foto) { | ||
390 | - $source_image = Yii::getAlias('@uploadDir') . '/product_images/'. urlencode($foto); | ||
391 | - | ||
392 | - if (file_exists($source_image)) { | ||
393 | - if (($productImage = ProductImage::find()->andFilterWhere(['image'=> $foto])->andFilterWhere(['product_id' => $_product->product_id])->one()) === null) { | ||
394 | - copy($source_image, Yii::getAlias('@productsDir') . "/" . $foto); | ||
395 | - $productImage = new ProductImage(); | ||
396 | - $productImage->product_id = $_product->product_id; | ||
397 | - $productImage->image = $foto; | ||
398 | - $productImage->save(); | ||
399 | - } | 285 | + |
286 | + foreach ($data as &$value) { | ||
287 | + if (!$is_utf) { | ||
288 | + $value = iconv('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value); | ||
400 | } | 289 | } |
290 | + $value = trim($value); | ||
401 | } | 291 | } |
402 | - } | ||
403 | - | ||
404 | - | ||
405 | - // нужно для проставления характеристик относящихся к модификациям | ||
406 | - $MOD_ARRAY = []; | ||
407 | - | ||
408 | - for ($i = 14; $i < count ($data); $i ++) | ||
409 | - { | ||
410 | - if (! empty ($data[$i])) | ||
411 | - { | ||
412 | - $mod_arr = explode ('=', $data[$i]); | ||
413 | - $mod_art = $mod_arr[0]; | ||
414 | - $variant_filters = explode ('*', $mod_arr[1]); | ||
415 | - $mod_color = $mod_arr[2]; | ||
416 | - $mod_image = $mod_arr[3]; | ||
417 | - $mod_stock = isset($mod_arr[4]) ?$mod_arr[4]:1; | ||
418 | - $mod_cost = isset($product_cost) ? floatval($product_cost) : 0; | ||
419 | - $mod_old_cost = floatval($product_cost_old); | ||
420 | - | ||
421 | - // Check product variant | ||
422 | - if ( ($_productVariant = ProductVariant::find()->andFilterWhere([ 'sku'=> $mod_art])->andFilterWhere(['product_id' => $_product->product_id])->one()) === null ) { | ||
423 | - $_productVariant = new ProductVariant(); | ||
424 | - $_productVariant->product_id = $_product->product_id; | ||
425 | - } | ||
426 | - $_productVariant->product_unit_id = 1; | ||
427 | - | ||
428 | - $_productVariant->sku = $mod_art; | ||
429 | - $_productVariant->price = $mod_cost; | ||
430 | - $_productVariant->price_old = $mod_old_cost; | ||
431 | - $_productVariant->stock = $mod_stock; | ||
432 | - | ||
433 | - $product_variant_type_name = ''; | ||
434 | - if (! empty ($mod_color)) { | ||
435 | - $product_variant_type_name = 'Цвет'; | ||
436 | - $_productVariant->name = $mod_color; | 292 | + |
293 | + // будет всегда 19 элементов | ||
294 | + for ($i = 0; $i <= 18; $i++) { | ||
295 | + if (!isset ( $data[ $i ] )) { | ||
296 | + $data[ $i ] = null; | ||
437 | } | 297 | } |
438 | - | ||
439 | - if (! empty ($variant_filters)) { | ||
440 | - | ||
441 | - $variants_options = $this->saveFilters($variant_filters,1,$category_id); | ||
442 | - | 298 | + } |
299 | + | ||
300 | + // 1 Группа (категория) | ||
301 | + $catalog_names = explode(',', $data[ 0 ]); | ||
302 | + | ||
303 | + if (empty ( $catalog_names )) { | ||
304 | + $result_items[] = "Не указана категория (строка $j) "; | ||
305 | + continue; | ||
306 | + } | ||
307 | + | ||
308 | + // 2 Бренд | ||
309 | + $brand_name = $data[ 1 ]; | ||
310 | + if (empty ( $brand_name )) { | ||
311 | + $result_items[] = "Не указан бренд (строка $j)"; | ||
312 | + continue; | ||
313 | + } | ||
314 | + | ||
315 | + // 3 Название товара | ||
316 | + $product_name = $data[ 2 ]; | ||
317 | + if (empty ( $product_name )) { | ||
318 | + $result_items[] = "Не указано наименование товара (строка $j)"; | ||
319 | + continue; | ||
320 | + } | ||
321 | + | ||
322 | + // 4 Описание Укр | ||
323 | + $product_body_uk = $data[ 3 ]; | ||
324 | + | ||
325 | + // 5 Описание Рус | ||
326 | + $product_body_ru = $data[ 4 ]; | ||
327 | + | ||
328 | + // 6 Фильтр [god:2013-2014]*[pol:мужской]*[naznacenie-germo-beg:Для вещей]*[material-germo-bag:нет]*[value-germo-bag:нет]*[weight-germo-bag:нет]*[ipx-germo-bag:нет]*[in-pacage-bag:нет]*[size-germo-bag:нет]*[rekomend-germo-bag:нет] | ||
329 | + $filters = explode('*', $data[ 5 ]); | ||
330 | + | ||
331 | + // 11 Цена акция | ||
332 | + $product_cost_old = floatval($data[ 7 ]); | ||
333 | + $product_cost = ''; | ||
334 | + // 10 Цена | ||
335 | + if ($product_cost_old) { | ||
336 | + $product_cost_old = floatval($data[ 6 ]); | ||
337 | + $product_cost = floatval($data[ 7 ]); | ||
338 | + } | ||
339 | + | ||
340 | + // 12 Акция | ||
341 | + $product_akciya = (bool) $data[ 8 ]; | ||
342 | + | ||
343 | + // 13 Сопуд. Тов. | ||
344 | + $similar = explode(',', $data[ 9 ]); | ||
345 | + | ||
346 | + // 14 Новинки | ||
347 | + $product_new = (bool) $data[ 10 ]; | ||
348 | + | ||
349 | + // 15 Топ продаж | ||
350 | + $product_top = (bool) $data[ 11 ]; | ||
351 | + | ||
352 | + // 17 ВИДЕО КОД | ||
353 | + $product_video = $data[ 12 ]; | ||
354 | + | ||
355 | + // 18 Галлерея фото | ||
356 | + if (trim($data[ 13 ])) { | ||
357 | + $fotos = explode(',', trim($data[ 13 ])); | ||
358 | + } | ||
359 | + | ||
360 | + // 19 Штрих код товара. | ||
361 | + // расшифровал - это модификации товара! | ||
362 | + | ||
363 | + $product_image = explode('=', $data[ 14 ]); | ||
364 | + $product_image = @$product_image[ 3 ]; | ||
365 | + | ||
366 | + if (( $_product = Product::find() | ||
367 | + ->filterWhere([ 'name' => trim($product_name) ]) | ||
368 | + ->one() ) === null | ||
369 | + ) { | ||
370 | + $_product = new Product(); | ||
371 | + } | ||
372 | + | ||
373 | + $is_new_product = empty( $_product->product_id ); | ||
374 | + $category_id = []; | ||
375 | + foreach ($catalog_names as $catalog_name) { | ||
376 | + | ||
377 | + // ==== Set category ==== | ||
378 | + if (( $category = Category::find() | ||
379 | + ->filterWhere([ 'name' => trim($catalog_name) ]) | ||
380 | + ->one() ) === null | ||
381 | + ) { | ||
382 | + // Create category | ||
383 | + $category = new Category(); | ||
384 | + $category->name = trim($catalog_name); | ||
385 | + $category->save(); | ||
443 | } | 386 | } |
444 | - | ||
445 | - | ||
446 | - if (isset($variants_options) && !empty($variants_options)) { | ||
447 | - $_productVariant->options = $variants_options; | 387 | + |
388 | + $category_id[] = $category->category_id; | ||
389 | + } | ||
390 | + | ||
391 | + $_product->categories = $category_id; | ||
392 | + | ||
393 | + // ===== Set brand ==== | ||
394 | + if ($brand_name) { | ||
395 | + if (( $brand = Brand::find() | ||
396 | + ->filterWhere([ 'name' => trim($brand_name) ]) | ||
397 | + ->one() ) !== null | ||
398 | + ) { | ||
399 | + $_product->brand_id = $brand->brand_id; | ||
400 | + } else { | ||
401 | + // Create brand | ||
402 | + $brand = new Brand(); | ||
403 | + $brand->name = trim($brand_name); | ||
404 | + $brand->save(); | ||
405 | + $_product->brand_id = $brand->brand_id; | ||
448 | } | 406 | } |
449 | - | ||
450 | - | ||
451 | - // ===== Set variant type ==== | ||
452 | - if ( $product_variant_type_name ) { | ||
453 | - if ( ($product_variant_type = ProductVariantType::find()->filterWhere([ 'name'=> $product_variant_type_name])->one()) !== null ) { | ||
454 | - $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id; | ||
455 | - } else { | ||
456 | - $product_variant_type = new ProductVariantType(); | ||
457 | - $product_variant_type->name = $product_variant_type_name; | ||
458 | - $product_variant_type->save(); | ||
459 | - $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id; | 407 | + } |
408 | + | ||
409 | + $_product->name = $product_name; | ||
410 | + $_product->video = $product_video; | ||
411 | + $_product->description = $product_body_ru; | ||
412 | + $_product->is_top = $product_top; | ||
413 | + $_product->akciya = $product_akciya; | ||
414 | + $_product->is_new = $product_new; | ||
415 | + | ||
416 | + if (!$_product->save()) { | ||
417 | + $result_items[] = 'Product #' . $_product->name . ' not saved' . " (строка $j)"; | ||
418 | + continue; | ||
419 | + } | ||
420 | + | ||
421 | + if (!empty( $fotos )) { | ||
422 | + foreach ($fotos as $foto) { | ||
423 | + $source_image = Yii::getAlias('@uploadDir') . '/product_images/' . urlencode($foto); | ||
424 | + | ||
425 | + if (file_exists($source_image)) { | ||
426 | + if (( $productImage = ProductImage::find() | ||
427 | + ->andFilterWhere([ 'image' => $foto ]) | ||
428 | + ->andFilterWhere( | ||
429 | + [ 'product_id' => $_product->product_id ] | ||
430 | + ) | ||
431 | + ->one() ) === null | ||
432 | + ) { | ||
433 | + copy($source_image, Yii::getAlias('@productsDir') . "/" . $foto); | ||
434 | + $productImage = new ProductImage(); | ||
435 | + $productImage->product_id = $_product->product_id; | ||
436 | + $productImage->image = $foto; | ||
437 | + $productImage->save(); | ||
438 | + } | ||
460 | } | 439 | } |
461 | } | 440 | } |
462 | - | ||
463 | - $_productVariant->save(); | ||
464 | - | ||
465 | - $MOD_ARRAY[] = $_productVariant->product_variant_id; | ||
466 | - | ||
467 | - if ($mod_image) { | ||
468 | - $source_image = Yii::getAlias('@productsDir') . '/'. urlencode($mod_image); | ||
469 | - if (file_exists($source_image)) { | ||
470 | - if (($variantImage = ProductImage::find()->andFilterWhere([ 'image' => $mod_image])->andFilterWhere(['product_variant_id' => $_productVariant->product_variant_id])->one()) === null) { | ||
471 | -// copy($source_image, Yii::getAlias('@productsDir') . "/" . $mod_image); | ||
472 | - $variantImage = new ProductImage(); | ||
473 | - $variantImage->product_id = $_product->product_id; | ||
474 | - $variantImage->product_variant_id = $_productVariant->product_variant_id; | ||
475 | - $variantImage->image = $mod_image; | ||
476 | - $variantImage->save(); | 441 | + } |
442 | + | ||
443 | + // нужно для проставления характеристик относящихся к модификациям | ||
444 | + $MOD_ARRAY = []; | ||
445 | + | ||
446 | + for ($i = 14; $i < count($data); $i++) { | ||
447 | + if (!empty ( $data[ $i ] )) { | ||
448 | + $mod_arr = explode('=', $data[ $i ]); | ||
449 | + $mod_art = $mod_arr[ 0 ]; | ||
450 | + $variant_filters = explode('*', $mod_arr[ 1 ]); | ||
451 | + $mod_color = $mod_arr[ 2 ]; | ||
452 | + $mod_image = $mod_arr[ 3 ]; | ||
453 | + $mod_stock = isset( $mod_arr[ 4 ] ) ? $mod_arr[ 4 ] : 1; | ||
454 | + $mod_cost = isset( $product_cost ) ? floatval($product_cost) : 0; | ||
455 | + $mod_old_cost = floatval($product_cost_old); | ||
456 | + | ||
457 | + // Check product variant | ||
458 | + if (( $_productVariant = ProductVariant::find() | ||
459 | + ->andFilterWhere([ 'sku' => $mod_art ]) | ||
460 | + ->andFilterWhere( | ||
461 | + [ 'product_id' => $_product->product_id ] | ||
462 | + ) | ||
463 | + ->one() ) === null | ||
464 | + ) { | ||
465 | + $_productVariant = new ProductVariant(); | ||
466 | + $_productVariant->product_id = $_product->product_id; | ||
467 | + } | ||
468 | + $_productVariant->product_unit_id = 1; | ||
469 | + | ||
470 | + $_productVariant->sku = $mod_art; | ||
471 | + $_productVariant->price = $mod_cost; | ||
472 | + $_productVariant->price_old = $mod_old_cost; | ||
473 | + $_productVariant->stock = $mod_stock; | ||
474 | + | ||
475 | + $product_variant_type_name = ''; | ||
476 | + if (!empty ( $mod_color )) { | ||
477 | + $product_variant_type_name = 'Цвет'; | ||
478 | + $_productVariant->name = $mod_color; | ||
479 | + } | ||
480 | + | ||
481 | + if (!empty ( $variant_filters )) { | ||
482 | + | ||
483 | + $variants_options = $this->saveFilters($variant_filters, 1, $category_id); | ||
484 | + | ||
485 | + } | ||
486 | + | ||
487 | + if (isset( $variants_options ) && !empty( $variants_options )) { | ||
488 | + $_productVariant->options = $variants_options; | ||
489 | + } | ||
490 | + | ||
491 | + // ===== Set variant type ==== | ||
492 | + if ($product_variant_type_name) { | ||
493 | + if (( $product_variant_type = ProductVariantType::find() | ||
494 | + ->filterWhere( | ||
495 | + [ 'name' => $product_variant_type_name ] | ||
496 | + ) | ||
497 | + ->one() ) !== null | ||
498 | + ) { | ||
499 | + $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id; | ||
500 | + } else { | ||
501 | + $product_variant_type = new ProductVariantType(); | ||
502 | + $product_variant_type->name = $product_variant_type_name; | ||
503 | + $product_variant_type->save(); | ||
504 | + $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id; | ||
505 | + } | ||
506 | + } | ||
507 | + | ||
508 | + $_productVariant->save(); | ||
509 | + | ||
510 | + $MOD_ARRAY[] = $_productVariant->product_variant_id; | ||
511 | + | ||
512 | + if ($mod_image) { | ||
513 | + $source_image = Yii::getAlias('@productsDir') . '/' . urlencode($mod_image); | ||
514 | + if (file_exists($source_image)) { | ||
515 | + if (( $variantImage = ProductImage::find() | ||
516 | + ->andFilterWhere([ 'image' => $mod_image ]) | ||
517 | + ->andFilterWhere( | ||
518 | + [ 'product_variant_id' => $_productVariant->product_variant_id ] | ||
519 | + ) | ||
520 | + ->one() ) === null | ||
521 | + ) { | ||
522 | + // copy($source_image, Yii::getAlias('@productsDir') . "/" . $mod_image); | ||
523 | + $variantImage = new ProductImage(); | ||
524 | + $variantImage->product_id = $_product->product_id; | ||
525 | + $variantImage->product_variant_id = $_productVariant->product_variant_id; | ||
526 | + $variantImage->image = $mod_image; | ||
527 | + $variantImage->save(); | ||
528 | + } | ||
477 | } | 529 | } |
478 | } | 530 | } |
479 | } | 531 | } |
480 | } | 532 | } |
533 | + | ||
534 | + if (!empty ( $filters )) { | ||
535 | + | ||
536 | + $options = $this->saveFilters($filters, 0, $category_id); | ||
537 | + | ||
538 | + } | ||
539 | + | ||
540 | + if (isset( $options ) && !empty( $options )) { | ||
541 | + $_product->options = $options; | ||
542 | + } | ||
543 | + | ||
544 | + $_product->save(); | ||
545 | + | ||
546 | + $result_items[] = "Product {$_product->name} #{$_product->product_id} saved (" . ( $is_new_product ? 'new product' : 'exists product' ) . ")" . " (строка $j)"; | ||
481 | } | 547 | } |
482 | - | ||
483 | - | ||
484 | - | ||
485 | - if (! empty ($filters)) { | ||
486 | - | ||
487 | - $options = $this->saveFilters($filters,0,$category_id); | ||
488 | - | ||
489 | - } | ||
490 | - | ||
491 | - | ||
492 | - if (isset($options) && !empty($options)) { | ||
493 | - $_product->options = $options; | 548 | + |
549 | + $result = [ | ||
550 | + 'end' => feof($handle), | ||
551 | + 'from' => ftell($handle), | ||
552 | + 'totalsize' => $filesize, | ||
553 | + 'items' => $result_items, | ||
554 | + | ||
555 | + ]; | ||
556 | + | ||
557 | + fclose($handle); | ||
558 | + | ||
559 | + if ($result[ 'end' ]) { | ||
560 | + unlink(Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFileProducts')); | ||
494 | } | 561 | } |
495 | - | ||
496 | - $_product->save(); | ||
497 | - | ||
498 | - $result_items[] = "Product {$_product->name} #{$_product->product_id} saved (". ($is_new_product ? 'new product' : 'exists product') .")" . " (строка $j)"; | 562 | + unlink(Yii::getAlias('@uploadDir/goProducts.lock')); |
563 | + return $result; | ||
499 | } | 564 | } |
500 | - | ||
501 | - $result = [ | ||
502 | - 'end' => feof($handle), | ||
503 | - 'from' => ftell($handle), | ||
504 | - 'totalsize' => $filesize, | ||
505 | - 'items' => $result_items, | ||
506 | - | ||
507 | - ]; | ||
508 | - | ||
509 | - fclose ($handle); | ||
510 | - | ||
511 | - if ($result['end']) { | ||
512 | - unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts')); | 565 | + |
566 | + private function getProductsFile($file_type) | ||
567 | + { | ||
568 | + $filename = Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@' . $file_type); | ||
569 | + if (!is_file($filename)) { | ||
570 | + $this->errors[] = "File $filename not found"; | ||
571 | + return false; | ||
572 | + } | ||
573 | + return fopen($filename, 'r'); | ||
513 | } | 574 | } |
514 | - unlink(Yii::getAlias('@uploadDir/goProducts.lock')); | ||
515 | - return $result; | ||
516 | - } | ||
517 | - | ||
518 | - | ||
519 | - | ||
520 | - private function getProductsFile($file_type) { | ||
521 | - $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type); | ||
522 | - if (!is_file($filename)) { | ||
523 | - $this->errors[] = "File $filename not found"; | ||
524 | - return FALSE; | 575 | + |
576 | + private function saveNotFoundRecord(array $line, $filename) | ||
577 | + { | ||
578 | + $str = implode(';', $line) . "\n"; | ||
579 | + $str = iconv("UTF-8//TRANSLIT//IGNORE", "windows-1251", $str); | ||
580 | + | ||
581 | + $fg = fopen(Yii::getAlias('@uploadDir') . '/' . $filename, 'a+'); | ||
582 | + fputs($fg, $str); | ||
583 | + fclose($fg); | ||
525 | } | 584 | } |
526 | - return fopen ($filename, 'r'); | ||
527 | - } | ||
528 | - | ||
529 | - private function saveNotFoundRecord (array $line, $filename) | ||
530 | - { | ||
531 | - $str = implode (';', $line)."\n"; | ||
532 | - $str = iconv ("UTF-8//TRANSLIT//IGNORE", "windows-1251", $str); | ||
533 | - | ||
534 | - $fg = fopen (Yii::getAlias('@uploadDir') .'/'. $filename, 'a+'); | ||
535 | - fputs ($fg, $str); | ||
536 | - fclose ($fg); | ||
537 | - } | ||
538 | - | ||
539 | - | ||
540 | - /** | ||
541 | - * @param $filters array of filters like [['pol'='мужской'],['god' = '2013'],['volume'='25 л']*['size'='49 x 30 x 20см'],['composition'='600D полиэстер']] | ||
542 | - * @param $level 0 for products and 1 for product variant | ||
543 | - * @param $catalog_names array catalogs id | ||
544 | - * @return array | ||
545 | - */ | ||
546 | - private function saveFilters($filters, $level,$catalog_names){ | ||
547 | - $options = []; | ||
548 | - foreach($filters as $filter) { | ||
549 | - | ||
550 | - preg_match_all('/\[(.*):(.*)\]/',$filter,$filter); | ||
551 | - | ||
552 | - if (empty($filter[1][0])) { | ||
553 | - continue; | ||
554 | - } | ||
555 | - $filter_name = trim($filter[1][0]); | ||
556 | - | ||
557 | - $taxGroup = TaxGroup::find()->where(['alias'=>$filter_name])->one(); | ||
558 | - if(!$taxGroup instanceof TaxGroup){ | ||
559 | - $taxGroup = new TaxGroup(); | ||
560 | - $taxGroup->alias = $filter_name; | ||
561 | - $taxGroup->level = $level; | ||
562 | - $taxGroup->name = $filter_name; | ||
563 | - $taxGroup->module = 'string'; | ||
564 | - $taxGroup->hierarchical = FALSE; | ||
565 | - $taxGroup->categories = $catalog_names; | ||
566 | - $taxGroup->is_filter = FALSE; | ||
567 | - $taxGroup->save(); | ||
568 | - } | ||
569 | - | ||
570 | - $filters_options = explode(',',$filter[2][0]); | ||
571 | - | ||
572 | - foreach($filters_options as $filter_options){ | ||
573 | - $value = TaxValueString::find()->innerJoinWith('taxOption')->andWhere(['value'=>$filter_options])->andWhere(['tax_option.tax_group_id' => $taxGroup->tax_group_id])->one(); | ||
574 | - | ||
575 | - if (!$value instanceof TaxValueString) { | ||
576 | - // Create option | ||
577 | - $option = new TaxOption(); | ||
578 | - $option->tax_group_id = $taxGroup->tax_group_id; | ||
579 | - $option->name = $filter_options; | ||
580 | - $option->save(); | ||
581 | - | ||
582 | - $value = new TaxValueString(); | ||
583 | - $value->tax_option_id = $option->tax_option_id; | ||
584 | - $value->value = $filter_options; | ||
585 | - $value->save(); | ||
586 | - | ||
587 | - $option->default_value = $value->tax_value_id; | ||
588 | - $option->save(); | 585 | + |
586 | + /** | ||
587 | + * @param $filters array of filters like [['pol'='мужской'],['god' = '2013'],['volume'='25 | ||
588 | + * л']*['size'='49 x 30 x 20см'],['composition'='600D полиэстер']] | ||
589 | + * @param $level 0 for products and 1 for product variant | ||
590 | + * @param $catalog_names array catalogs id | ||
591 | + * | ||
592 | + * @return array | ||
593 | + */ | ||
594 | + private function saveFilters($filters, $level, $catalog_names) | ||
595 | + { | ||
596 | + $options = []; | ||
597 | + foreach ($filters as $filter) { | ||
598 | + | ||
599 | + preg_match_all('/\[(.*):(.*)\]/', $filter, $filter); | ||
600 | + | ||
601 | + if (empty( $filter[ 1 ][ 0 ] )) { | ||
602 | + continue; | ||
603 | + } | ||
604 | + $filter_name = trim($filter[ 1 ][ 0 ]); | ||
605 | + | ||
606 | + $taxGroup = TaxGroup::find() | ||
607 | + ->where([ 'alias' => $filter_name ]) | ||
608 | + ->one(); | ||
609 | + if (!$taxGroup instanceof TaxGroup) { | ||
610 | + $taxGroup = new TaxGroup(); | ||
611 | + $taxGroup->alias = $filter_name; | ||
612 | + $taxGroup->level = $level; | ||
613 | + $taxGroup->name = $filter_name; | ||
614 | + $taxGroup->module = 'string'; | ||
615 | + $taxGroup->hierarchical = false; | ||
616 | + $taxGroup->categories = $catalog_names; | ||
617 | + $taxGroup->is_filter = false; | ||
618 | + $taxGroup->save(); | ||
619 | + } | ||
620 | + | ||
621 | + $filters_options = explode(',', $filter[ 2 ][ 0 ]); | ||
622 | + | ||
623 | + foreach ($filters_options as $filter_options) { | ||
624 | + $value = TaxValueString::find() | ||
625 | + ->innerJoinWith('taxOption') | ||
626 | + ->andWhere([ 'value' => $filter_options ]) | ||
627 | + ->andWhere([ 'tax_option.tax_group_id' => $taxGroup->tax_group_id ]) | ||
628 | + ->one(); | ||
629 | + | ||
630 | + if (!$value instanceof TaxValueString) { | ||
631 | + // Create option | ||
632 | + $option = new TaxOption(); | ||
633 | + $option->tax_group_id = $taxGroup->tax_group_id; | ||
634 | + $option->name = $filter_options; | ||
635 | + $option->save(); | ||
636 | + | ||
637 | + $value = new TaxValueString(); | ||
638 | + $value->tax_option_id = $option->tax_option_id; | ||
639 | + $value->value = $filter_options; | ||
640 | + $value->save(); | ||
641 | + | ||
642 | + $option->default_value = $value->tax_value_id; | ||
643 | + $option->save(); | ||
644 | + } | ||
645 | + $options[] = $value->tax_option_id; | ||
646 | + | ||
589 | } | 647 | } |
590 | - $options[] = $value->tax_option_id; | ||
591 | - | 648 | + |
592 | } | 649 | } |
593 | - | ||
594 | - | 650 | + |
651 | + return $options; | ||
595 | } | 652 | } |
596 | - | ||
597 | - return $options; | ||
598 | - } | ||
599 | -} | ||
600 | \ No newline at end of file | 653 | \ No newline at end of file |
654 | + } | ||
601 | \ No newline at end of file | 655 | \ No newline at end of file |