Commit 1c02ab59f51e0de4f8212aaf6b35313c02cabb21

Authored by Administrator
1 parent 50775039

29.06.16

common/modules/product/models/Import.php
... ... @@ -7,6 +7,7 @@ use common\modules\product\models\Category;
7 7 use common\modules\product\models\CategoryName;
8 8 use common\modules\product\models\ProductImage;
9 9 use common\modules\product\models\ProductVariantType;
  10 +use common\modules\rubrication\models\TaxGroup;
10 11 use common\modules\rubrication\models\TaxOption;
11 12 use common\modules\rubrication\models\TaxValueString;
12 13 use Yii;
... ... @@ -190,6 +191,7 @@ class Import extends Model {
190 191 }
191 192  
192 193 public function goProducts($from = 0, $limit = null) {
  194 +
193 195 set_time_limit(0);
194 196 $new_products = $linked_products = 0;
195 197  
... ... @@ -200,6 +202,7 @@ class Import extends Model {
200 202  
201 203 $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'));
202 204  
  205 +
203 206 if ($from) {
204 207 fseek($handle, $from);
205 208 }
... ... @@ -212,6 +215,8 @@ class Import extends Model {
212 215  
213 216 while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE && (empty($limit) || $j++ < $limit))
214 217 {
  218 +
  219 +
215 220 foreach ($data as &$value)
216 221 {
217 222 if (!$is_utf) {
... ... @@ -229,9 +234,10 @@ class Import extends Model {
229 234 }
230 235 }
231 236  
  237 +
232 238 // 1 Группа (категория)
233   - $catalog_name = $data[0];
234   - if (empty ($catalog_name))
  239 + $catalog_names = explode(',',$data[0]);
  240 + if (empty ($catalog_names))
235 241 {
236 242 $result_items[] = "Не указана категория (строка $j)";
237 243 continue;
... ... @@ -259,54 +265,43 @@ class Import extends Model {
259 265 // 5 Описание Рус
260 266 $product_body_ru = $data[4];
261 267  
262   - // 6 Фильтр (через запятую)
263   - $filters = explode (',', $data[5]);
264   -
265   - // 7 Доп фильтр через запятую
266   - $filters_extra = explode (',', $data[6]);
267   -
268   - // 8 Пол череззапятую (мужской, женский, детский, унисекс)
269   - $gender = explode (',', $data[7]);
270   -
271   - // 9 Год
272   - $years = explode (',', $data[8]);
  268 + // 6 Фильтр (['pol'='мужской']*['god' = '2013']*['volume'='25 л']*['size'='49 x 30 x 20см']*['composition'='600D полиэстер'])
  269 + $filters = explode ('*', $data[5]);
273 270  
274 271 // 11 Цена акция
275   - $product_cost_old = floatval($data[10]);
  272 + $product_cost_old = floatval($data[7]);
276 273  
277 274 // 10 Цена
278 275 if ($product_cost_old) {
279   - $product_cost_old = floatval($data[9]);
280   - $product_cost = floatval($data[10]);
  276 + $product_cost_old = floatval($data[6]);
  277 + $product_cost = floatval($data[7]);
281 278 }
282 279  
283 280 // 12 Акция
284   - $product_akciya = (bool)$data[11];
  281 + $product_akciya = (bool)$data[8];
285 282  
286 283 // 13 Сопуд. Тов.
287   - $similar = explode (',', $data[12]);
  284 + $similar = explode (',', $data[9]);
288 285  
289 286 // 14 Новинки
290   - $product_new = (bool)$data[13];
  287 + $product_new = (bool)$data[10];
291 288  
292 289 // 15 Топ продаж
293   - $product_top = (bool)$data[14];
  290 + $product_top = (bool)$data[11];
294 291  
295   - // 16 Сетка Характеристик
296   - $feature = explode ('=', $data[15]);
297 292  
298 293 // 17 ВИДЕО КОД
299   - $product_video = $data[16];
  294 + $product_video = $data[12];
300 295  
301 296 // 18 Галлерея фото
302   - if (trim($data[17])) {
303   - $fotos = explode (',', trim($data[17]));
  297 + if (trim($data[13])) {
  298 + $fotos = explode (',', trim($data[13]));
304 299 }
305 300  
306 301 // 19 Штрих код товара.
307 302 // расшифровал - это модификации товара!
308 303  
309   - $product_image = explode ('=', $data[18]);
  304 + $product_image = explode ('=', $data[14]);
310 305 $product_image = @$product_image[3];
311 306  
312 307 if ( ($_product = Product::find()->filterWhere(['ilike', 'name', trim($product_name)])->one()) === null ) {
... ... @@ -315,14 +310,20 @@ class Import extends Model {
315 310  
316 311 $is_new_product = empty($_product->product_id);
317 312  
318   - // ==== Set category ====
319   - if ( ($category = CategoryName::find()->filterWhere(['ilike', 'value', trim($catalog_name)])->one()) === null ) {
320   - // Create category
321   - $category = new Category();
322   - $category->name = trim($catalog_name);
323   - $category->save();
  313 + foreach($catalog_names as $catalog_name){
  314 + // ==== Set category ====
  315 + if ( ($category = CategoryName::find()->filterWhere(['ilike', 'value', trim($catalog_name)])->one()) === null ) {
  316 + // Create category
  317 + $category = new Category();
  318 + $category->name = trim($catalog_name);
  319 + $category->save();
  320 + }
  321 +
  322 + $category_id[] = $category->category_id;
324 323 }
325   - $_product->categories = [$category->category_id];
  324 +
  325 +
  326 + $_product->categories = $category_id;
326 327  
327 328 // ===== Set brand ====
328 329 if ( $brand_name ) {
... ... @@ -349,6 +350,8 @@ class Import extends Model {
349 350 continue;
350 351 }
351 352  
  353 +
  354 +
352 355 if (!empty($fotos)) {
353 356 foreach($fotos as $foto) {
354 357 $source_image = Yii::getAlias('@uploadDir') . '/product_images/'. urlencode($foto);
... ... @@ -365,18 +368,20 @@ class Import extends Model {
365 368 }
366 369 }
367 370  
  371 +
368 372 // нужно для проставления характеристик относящихся к модификациям
369 373 $MOD_ARRAY = [];
370 374  
371   - for ($i = 18; $i < count ($data); $i ++)
  375 + for ($i = 14; $i < count ($data); $i ++)
372 376 {
373 377 if (! empty ($data[$i]))
374 378 {
375 379 $mod_arr = explode ('=', $data[$i]);
376 380 $mod_art = $mod_arr[0];
377   - $mod_size = $mod_arr[1];
  381 + $variant_filters = explode ('*', $mod_arr[1]);
378 382 $mod_color = $mod_arr[2];
379 383 $mod_image = $mod_arr[3];
  384 + $mod_stock = isset($mod_arr[4]) ?$mod_arr[4]:1;
380 385 $mod_cost = floatval($product_cost);
381 386 $mod_old_cost = floatval($product_cost_old);
382 387  
... ... @@ -390,18 +395,26 @@ class Import extends Model {
390 395 $_productVariant->sku = $mod_art;
391 396 $_productVariant->price = $mod_cost;
392 397 $_productVariant->price_old = $mod_old_cost;
393   - $_productVariant->stock = 1;
  398 + $_productVariant->stock = $mod_stock;
394 399  
395 400 $product_variant_type_name = '';
396 401 if (! empty ($mod_color)) {
397 402 $product_variant_type_name = 'Цвет';
398 403 $_productVariant->name = $mod_color;
399 404 }
400   - elseif (! empty ($mod_size)) {
401   - $product_variant_type_name = 'Размер';
402   - $_productVariant->name = $mod_size;
  405 +
  406 + if (! empty ($variant_filters)) {
  407 +
  408 + $variants_options = $this->saveFilters($variant_filters,1,$category_id);
  409 +
  410 + }
  411 +
  412 +
  413 + if (isset($variants_options) && !empty($variants_options)) {
  414 + $_productVariant->options = $variants_options;
403 415 }
404 416  
  417 +
405 418 // ===== Set variant type ====
406 419 if ( $product_variant_type_name ) {
407 420 if ( ($product_variant_type = ProductVariantType::find()->filterWhere(['ilike', 'name', $product_variant_type_name])->one()) !== null ) {
... ... @@ -413,6 +426,7 @@ class Import extends Model {
413 426 $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id;
414 427 }
415 428 }
  429 +
416 430 $_productVariant->save(false);
417 431  
418 432 $MOD_ARRAY[] = $_productVariant->product_variant_id;
... ... @@ -433,84 +447,17 @@ class Import extends Model {
433 447 }
434 448 }
435 449  
436   - $options = [];
  450 +
437 451  
438 452 if (! empty ($filters)) {
439   - // Set Naznachenie (tax_group_id = 20)
440   - foreach($filters as $filter) {
441   - $filter = trim($filter);
442   - if (!$filter) {
443   - continue;
444   - }
445   - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => ProductHelper::PRODUCT_TAX_GROUP_ID_TARGET])->one()) === null ) {
446   - // Create option
447   - $option = new TaxOption();
448   - $option->tax_group_id = 20;
449   - $option->save();
450   -
451   - $value = new TaxValueString();
452   - $value->tax_option_id = $option->tax_option_id;
453   - $value->value = $filter;
454   - $value->save();
455   -
456   - $option->default_value = $value->tax_value_id;
457   - $option->save();
458   - }
459   - $options[] = $value->tax_option_id;
460   - }
461   - }
462 453  
463   - if (! empty ($years)) {
464   - // Set God (tax_group_id = 21)
465   - foreach($years as $filter) {
466   - $filter = trim($filter);
467   - if (!$filter) {
468   - continue;
469   - }
470   - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => ProductHelper::PRODUCT_TAX_GROUP_ID_YEAR])->one()) === null ) {
471   - // Create option
472   - $option = new TaxOption();
473   - $option->tax_group_id = 21;
474   - $option->save();
475   -
476   - $value = new TaxValueString();
477   - $value->tax_option_id = $option->tax_option_id;
478   - $value->value = $filter;
479   - $value->save();
480   -
481   - $option->default_value = $value->tax_value_id;
482   - $option->save();
483   - }
484   - $options[] = $value->tax_option_id;
485   - }
486   - }
  454 + $options = $this->saveFilters($filters,0,$category_id);
487 455  
488   - if (! empty ($gender)) {
489   - // Set Pol (tax_group_id = 22)
490   - foreach($gender as $filter) {
491   - $filter = trim($filter);
492   - if (!$filter) {
493   - continue;
494   - }
495   - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => ProductHelper::PRODUCT_TAX_GROUP_ID_SEX])->one()) === null ) {
496   - // Create option
497   - $option = new TaxOption();
498   - $option->tax_group_id = 22;
499   - $option->save();
500   -
501   - $value = new TaxValueString();
502   - $value->tax_option_id = $option->tax_option_id;
503   - $value->value = $filter;
504   - $value->save();
505   -
506   - $option->default_value = $value->tax_value_id;
507   - $option->save();
508   - }
509   - $options[] = $value->tax_option_id;
510   - }
511 456 }
512 457  
513   - if (!empty($options)) {
  458 +
  459 + if (isset($options) && !empty($options)) {
  460 + print_r($options);
514 461 $_product->options = $options;
515 462 }
516 463  
... ... @@ -529,9 +476,9 @@ class Import extends Model {
529 476  
530 477 fclose ($handle);
531 478  
532   - if ($result['end']) {
533   - unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'));
534   - }
  479 +// if ($result['end']) {
  480 +// unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'));
  481 +// }
535 482  
536 483 return $result;
537 484 }
... ... @@ -554,4 +501,65 @@ class Import extends Model {
554 501 fputs ($fg, $str);
555 502 fclose ($fg);
556 503 }
  504 +
  505 +
  506 + /**
  507 + * @param $filters array of filters like [['pol'='мужской'],['god' = '2013'],['volume'='25 л']*['size'='49 x 30 x 20см'],['composition'='600D полиэстер']]
  508 + * @param $level 0 for products and 1 for product variant
  509 + * @param $catalog_names array catalogs id
  510 + * @return array
  511 + */
  512 + private function saveFilters($filters, $level,$catalog_names){
  513 + $options = [];
  514 + foreach($filters as $filter) {
  515 +
  516 + preg_match_all('/\[(.*):(.*)\]/',$filter,$filter);
  517 +
  518 + if (empty($filter[1][0])) {
  519 + continue;
  520 + }
  521 + $filter_name = trim($filter[1][0]);
  522 +
  523 + $taxGroup = TaxGroup::find()->where(['alias'=>$filter_name])->one();
  524 + if(!$taxGroup instanceof TaxGroup){
  525 + $taxGroup = new TaxGroup();
  526 + $taxGroup->alias = $filter_name;
  527 + $taxGroup->level = $level;
  528 + $taxGroup->name = $filter_name;
  529 + $taxGroup->module = 'string';
  530 + $taxGroup->hierarchical = FALSE;
  531 + $taxGroup->group_to_category = $catalog_names;
  532 + $taxGroup->is_filter = FALSE;
  533 + $taxGroup->save();
  534 + }
  535 +
  536 + $filters_options = explode(',',$filter[2][0]);
  537 +
  538 + foreach($filters_options as $filter_options){
  539 + $value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter_options])->andFilterWhere(['tax_option.tax_group_id' => $taxGroup->tax_group_id])->one();
  540 +
  541 + if (!$value instanceof TaxValueString) {
  542 + // Create option
  543 + $option = new TaxOption();
  544 + $option->tax_group_id = $taxGroup->tax_group_id;
  545 + $option->alias = $filter_options;
  546 + $option->save();
  547 +
  548 + $value = new TaxValueString();
  549 + $value->tax_option_id = $option->tax_option_id;
  550 + $value->value = $filter_options;
  551 + $value->save();
  552 +
  553 + $option->default_value = $value->tax_value_id;
  554 + $option->save();
  555 + }
  556 + $options[] = $value->tax_option_id;
  557 +
  558 + }
  559 +
  560 +
  561 + }
  562 +
  563 + return $options;
  564 + }
557 565 }
558 566 \ No newline at end of file
... ...
common/modules/relation/relationBehavior.php
... ... @@ -32,6 +32,7 @@ class relationBehavior extends Behavior {
32 32 * Inicialize behavior (read and prepare params)
33 33 */
34 34 public function init() {
  35 +
35 36 foreach ($this->relations as $relation_key => &$relation) {
36 37 if (is_string($relation)) {
37 38 // Get data from module's data
... ... @@ -45,6 +46,7 @@ class relationBehavior extends Behavior {
45 46 }
46 47 }
47 48 $this->fields[$relation['field']] = $relation_key;
  49 +
48 50 }
49 51 }
50 52  
... ... @@ -61,6 +63,7 @@ class relationBehavior extends Behavior {
61 63 }
62 64  
63 65 public function relationsAfterSave($insert) {
  66 +
64 67 if (is_array($modelPrimaryKey = $this->owner->getPrimaryKey())) {
65 68 throw new ErrorException('This behavior does not support composite primary keys');
66 69 }
... ... @@ -93,6 +96,7 @@ class relationBehavior extends Behavior {
93 96 ->execute();
94 97  
95 98 if (!empty($values)) {
  99 +
96 100 foreach($values as $value) {
97 101 $insertData = [
98 102 $relation['inner']['linked_key'] => $this->owner->{$relation['inner']['key']},
... ...
console/controllers/ImportController.php
... ... @@ -20,6 +20,9 @@ use yii\console\Controller;
20 20 use yii\helpers\Console;
21 21  
22 22 class ImportController extends Controller {
  23 + public $errors = [];
  24 +
  25 +
23 26 private function getProductsFile($file_type = 'uploadFileProducts') {
24 27 $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type);
25 28 if (!is_file($filename)) {
... ... @@ -30,16 +33,16 @@ class ImportController extends Controller {
30 33 }
31 34  
32 35 public function actionProducts() {
33   - if (file_exists(Yii::getAlias('@uploadDir/goProducts.lock'))) {
34   - $this->errors[] = 'Task already executed';
35   - return Controller::EXIT_CODE_ERROR;
36   - }
37   - $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+');
38   - fclose($ff);
  36 +// if (file_exists(Yii::getAlias('@uploadDir/goProducts.lock'))) {
  37 +// $this->errors[] = 'Task already executed';
  38 +// return Controller::EXIT_CODE_ERROR;
  39 +// }
  40 +// $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+');
  41 +// fclose($ff);
39 42 $model = new Import();
40   - $data = $model->goProducts(0, null);
41   - unlink(Yii::getAlias('@uploadDir/goProducts.lock'));
42   - return Controller::EXIT_CODE_NORMAL;
  43 + $model->goProducts(0, null);
  44 +// unlink(Yii::getAlias('@uploadDir/goProducts.lock'));
  45 +// return Controller::EXIT_CODE_NORMAL;
43 46 }
44 47  
45 48 public function actionPrices() {
... ...
frontend/widgets/Seo.php
... ... @@ -94,17 +94,13 @@ class Seo extends Widget
94 94  
95 95  
96 96  
97   - if (isset($filter['brands']) && count($filter['brands']) == 1 ) {
  97 + if (isset($filter['brands']) && count($filter['brands']) == 1) {
98 98 $model = Brand::find()->where(['alias' => $filter['brands'][0]])->one();
  99 + return $this->selectSeoData(self::H1) ? $this->selectSeoData(self::H1) : $model->name;
99 100  
100   - if($this->selectSeoData(self::H1) == $this->category_name) {
101   - return $this->selectSeoData(self::H1) . ' ' . $model->name;
102   - }else {
103   - return $this->selectSeoData(self::H1);
104   - }
105 101  
106 102  
107   - } else if (isset($filter['options']["naznacenie"]) && count($filter['options']["naznacenie"]) == 1) {
  103 + } if (isset($filter['options']["naznacenie"]) && count($filter['options']["naznacenie"]) == 1) {
108 104  
109 105 $model = TaxOption::find()->where(['alias' => $filter['options']["naznacenie"]])->one();
110 106 return $this->selectSeoData(self::H1).' '.$model->value->value;
... ... @@ -117,25 +113,11 @@ class Seo extends Widget
117 113 case self::TITLE:
118 114  
119 115 $filter = \Yii::$app->request->get('filter', []);
120   - // var_dump($filter );die();
121   -// if(!empty($filter)){
122   -// $filter_row = '';
123   -// foreach($filter as $sub_filter_name => $sub_filter_array){
124   -// if($sub_filter_name=='options'){
125   -// foreach($sub_filter_array as $f_name=>$f_values){
126   -// $filter_row .= $f_name.':'.implode(',',$f_values).'|';
127   -// }
128   -// }
129   -//
130   -// }
131   -// $this->fields['name'] = $filter_row;
132   -// }
  116 +
133 117  
134 118 $title = $this->selectSeoData(self::TITLE);
135 119  
136   -// if(!empty($filter_row)){
137   -// return $filter_row;
138   -// }
  120 +
139 121 if(!empty($filter) && $title == $this->fields['meta-title']) {
140 122  
141 123 $array = [
... ...