Commit 7c3b817e0aa763401ef545a6490134d781da8172
1 parent
b40c1cce
full commit
Showing
5 changed files
with
129 additions
and
71 deletions
Show diff stats
console/GenerateController.php
console/ImportController.php
| 1 | <?php | 1 | <?php |
| 2 | 2 | ||
| 3 | -namespace console\controllers; | 3 | +namespace artweb\artbox\ecommerce\console; |
| 4 | 4 | ||
| 5 | use artweb\artbox\ecommerce\models\Import; | 5 | use artweb\artbox\ecommerce\models\Import; |
| 6 | +use artweb\artbox\language\models\Language; | ||
| 6 | use Yii; | 7 | use Yii; |
| 7 | use yii\console\Controller; | 8 | use yii\console\Controller; |
| 8 | 9 | ||
| @@ -17,29 +18,25 @@ class ImportController extends Controller { | @@ -17,29 +18,25 @@ class ImportController extends Controller { | ||
| 17 | public $errors = []; | 18 | public $errors = []; |
| 18 | 19 | ||
| 19 | 20 | ||
| 20 | - private function getProductsFile($file_type = 'uploadFileProducts') { | ||
| 21 | - $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type); | ||
| 22 | - if (!is_file($filename)) { | ||
| 23 | - $this->stderr('Task already executed'); | 21 | + |
| 22 | + public function actionProducts($lang) { | ||
| 23 | + | ||
| 24 | + Language::setCurrent($lang); | ||
| 25 | + | ||
| 26 | + if (file_exists(Yii::getAlias('@uploadDir/goProducts_'.$lang.'.lock'))) { | ||
| 27 | + $this->errors[] = 'Task already executed'; | ||
| 24 | return Controller::EXIT_CODE_ERROR; | 28 | return Controller::EXIT_CODE_ERROR; |
| 25 | } | 29 | } |
| 26 | - return fopen ($filename, 'r'); | ||
| 27 | - } | ||
| 28 | - | ||
| 29 | - public function actionProducts() { | ||
| 30 | -// if (file_exists(Yii::getAlias('@uploadDir/goProducts.lock'))) { | ||
| 31 | -// $this->errors[] = 'Task already executed'; | ||
| 32 | -// return Controller::EXIT_CODE_ERROR; | ||
| 33 | -// } | ||
| 34 | -// $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+'); | ||
| 35 | -// fclose($ff); | 30 | + $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+'); |
| 31 | + fclose($ff); | ||
| 36 | $model = new Import(); | 32 | $model = new Import(); |
| 37 | $model->goProducts(0, null); | 33 | $model->goProducts(0, null); |
| 38 | -// unlink(Yii::getAlias('@uploadDir/goProducts.lock')); | 34 | + unlink(Yii::getAlias('@uploadDir/goProducts_'.$lang.'.lock')); |
| 39 | return Controller::EXIT_CODE_NORMAL; | 35 | return Controller::EXIT_CODE_NORMAL; |
| 40 | } | 36 | } |
| 41 | 37 | ||
| 42 | public function actionPrices() { | 38 | public function actionPrices() { |
| 39 | + | ||
| 43 | if (file_exists(Yii::getAlias('@uploadDir/goPrices.lock'))) { | 40 | if (file_exists(Yii::getAlias('@uploadDir/goPrices.lock'))) { |
| 44 | $this->stderr('Task already executed'); | 41 | $this->stderr('Task already executed'); |
| 45 | return Controller::EXIT_CODE_ERROR; | 42 | return Controller::EXIT_CODE_ERROR; |
| @@ -47,18 +44,10 @@ class ImportController extends Controller { | @@ -47,18 +44,10 @@ class ImportController extends Controller { | ||
| 47 | $ff = fopen(Yii::getAlias('@uploadDir/goPrices.lock'), 'w+'); | 44 | $ff = fopen(Yii::getAlias('@uploadDir/goPrices.lock'), 'w+'); |
| 48 | fclose($ff); | 45 | fclose($ff); |
| 49 | $model = new Import(); | 46 | $model = new Import(); |
| 50 | - $data = $model->goPrices(0, null); | 47 | + $model->goPrices(0, null); |
| 51 | unlink(Yii::getAlias('@uploadDir/goPrices.lock')); | 48 | unlink(Yii::getAlias('@uploadDir/goPrices.lock')); |
| 52 | return Controller::EXIT_CODE_NORMAL; | 49 | return Controller::EXIT_CODE_NORMAL; |
| 53 | } | 50 | } |
| 54 | 51 | ||
| 55 | - private function saveNotFoundRecord (array $line, $filename) | ||
| 56 | - { | ||
| 57 | - $str = implode (';', $line)."\n"; | ||
| 58 | - $str = iconv ("UTF-8//TRANSLIT//IGNORE", "windows-1251", $str); | ||
| 59 | 52 | ||
| 60 | - $fg = fopen (Yii::getAlias('@uploadDir') .'/'. $filename, 'a+'); | ||
| 61 | - fputs ($fg, $str); | ||
| 62 | - fclose ($fg); | ||
| 63 | - } | ||
| 64 | } | 53 | } |
| 65 | \ No newline at end of file | 54 | \ No newline at end of file |
console/SiteMapController.php
models/Export.php
| @@ -80,12 +80,12 @@ | @@ -80,12 +80,12 @@ | ||
| 80 | /** | 80 | /** |
| 81 | * @var ProductVariant $variant | 81 | * @var ProductVariant $variant |
| 82 | */ | 82 | */ |
| 83 | - $color = $variant->lang->title; | 83 | + $name = $variant->lang->title; |
| 84 | $mods[] = $variant->sku . $this->generateID( | 84 | $mods[] = $variant->sku . $this->generateID( |
| 85 | $variant->remote_id | 85 | $variant->remote_id |
| 86 | ) . '=' . $this->convertFilterToString( | 86 | ) . '=' . $this->convertFilterToString( |
| 87 | $variant->filters | 87 | $variant->filters |
| 88 | - ) . '=' . $color . '=' . ( ( !empty( $variant->image ) ) ? $variant->image->image : '' ) . '=' . $variant->stock; | 88 | + ) . '=' . $name . '=' . ( ( !empty( $variant->image ) ) ? $variant->image->image : '' ) . '=' . $variant->stock; |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | $fotos = []; | 91 | $fotos = []; |
| @@ -95,7 +95,12 @@ | @@ -95,7 +95,12 @@ | ||
| 95 | 95 | ||
| 96 | $categories = []; | 96 | $categories = []; |
| 97 | foreach ($product->categories as $value) { | 97 | foreach ($product->categories as $value) { |
| 98 | - $categories[] = $value->lang->title . $this->generateID($value->remote_id); | 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).']'; | ||
| 100 | + } else { | ||
| 101 | + $categories[] = '['.$value->lang->title . $this->generateID($value->remote_id).']'; | ||
| 102 | + } | ||
| 103 | + | ||
| 99 | } | 104 | } |
| 100 | 105 | ||
| 101 | $categories = implode(',', $categories); | 106 | $categories = implode(',', $categories); |
models/Import.php
| @@ -166,12 +166,13 @@ | @@ -166,12 +166,13 @@ | ||
| 166 | // ===== Set stock ==== | 166 | // ===== Set stock ==== |
| 167 | if ($city_name) { | 167 | if ($city_name) { |
| 168 | if (( $stock = Stock::find() | 168 | if (( $stock = Stock::find() |
| 169 | - ->filterWhere([ 'title' => trim($city_name) ]) | 169 | + ->joinWith('lang') |
| 170 | + ->filterWhere([ 'stock_lang.title' => trim($city_name) ]) | ||
| 170 | ->one() ) === null | 171 | ->one() ) === null |
| 171 | ) { | 172 | ) { |
| 172 | // Create stock | 173 | // Create stock |
| 173 | $stock = new Stock(); | 174 | $stock = new Stock(); |
| 174 | - $stock->title = trim($city_name); | 175 | + $stock->lang->title = trim($city_name); |
| 175 | $stock->save(false); | 176 | $stock->save(false); |
| 176 | } | 177 | } |
| 177 | 178 | ||
| @@ -290,37 +291,109 @@ | @@ -290,37 +291,109 @@ | ||
| 290 | $category_id = []; | 291 | $category_id = []; |
| 291 | 292 | ||
| 292 | foreach ($catalog_names as $catalog_name) { | 293 | foreach ($catalog_names as $catalog_name) { |
| 293 | - // ==== Set category ==== | ||
| 294 | - $parsed_name = $this->parseName($catalog_name); | ||
| 295 | - if (!empty( $parsed_name[ 'remote_id' ] ) && ( $category = Category::find() | ||
| 296 | - ->joinWith('lang') | ||
| 297 | - ->andFilterWhere( | ||
| 298 | - [ 'remote_id' => $parsed_name[ 'remote_id' ] ] | ||
| 299 | - ) | ||
| 300 | - ->one() ) !== null | ||
| 301 | - ) { | ||
| 302 | - if (!empty( $category->lang )) { | ||
| 303 | - $category->lang->title = $parsed_name[ 'name' ]; | ||
| 304 | - $category->lang->save(false); | ||
| 305 | - } else { | ||
| 306 | - throw new \Exception( | ||
| 307 | - 'Category with ID ' . $category->id . ' and lang ' . Language::getCurrent( | ||
| 308 | - )->id . ' doesn\'t exist' | ||
| 309 | - ); | 294 | + |
| 295 | + | ||
| 296 | + if(preg_match_all('/\[(.*)>(.*)\]/', $catalog_name, $out,PREG_SET_ORDER)){ | ||
| 297 | + | ||
| 298 | + $count = count($out[0]); | ||
| 299 | + $parent_id = 0; | ||
| 300 | + | ||
| 301 | + for($i=1; $i<$count; $i++){ | ||
| 302 | + | ||
| 303 | + if(isset($out[0][$i])){ | ||
| 304 | + | ||
| 305 | + // ==== Set category ==== | ||
| 306 | + $parsed_name = $this->parseName($out[0][$i]); | ||
| 307 | + if (!empty( $parsed_name[ 'remote_id' ] ) && ( $category = Category::find() | ||
| 308 | + ->joinWith('lang') | ||
| 309 | + ->andFilterWhere( | ||
| 310 | + [ 'remote_id' => $parsed_name[ 'remote_id' ] ] | ||
| 311 | + ) | ||
| 312 | + ->one() ) !== null | ||
| 313 | + ) { | ||
| 314 | + if (!empty( $category->lang )) { | ||
| 315 | + if($i != 1){ | ||
| 316 | + $category->parent_id = $parent_id; | ||
| 317 | + }else{ | ||
| 318 | + $parent_id = $category->id; | ||
| 319 | + } | ||
| 320 | + $category->lang->title = $parsed_name[ 'name' ]; | ||
| 321 | + $category->lang->save(false); | ||
| 322 | + } else { | ||
| 323 | + throw new \Exception( | ||
| 324 | + 'Category with ID ' . $category->id . ' and lang ' . Language::getCurrent( | ||
| 325 | + )->id . ' doesn\'t exist' | ||
| 326 | + ); | ||
| 327 | + } | ||
| 328 | + | ||
| 329 | + } else { | ||
| 330 | + // Create category | ||
| 331 | + $category = new Category(); | ||
| 332 | + $category->generateLangs(); | ||
| 333 | + $category_langs = $category->modelLangs; | ||
| 334 | + foreach ($category_langs as $category_lang) { | ||
| 335 | + $category_lang->title = $parsed_name[ 'name' ]; | ||
| 336 | + } | ||
| 337 | + if($i != 1){ | ||
| 338 | + $category->parent_id = $parent_id; | ||
| 339 | + } | ||
| 340 | + $category->remote_id = $parsed_name[ 'remote_id' ]; | ||
| 341 | + $category->save(false); | ||
| 342 | + if($i == 1){ | ||
| 343 | + $parent_id = $category->id; | ||
| 344 | + } | ||
| 345 | + } | ||
| 346 | + $category_id[] = $category->id; | ||
| 347 | + } | ||
| 348 | + | ||
| 310 | } | 349 | } |
| 311 | - | ||
| 312 | - } else { | ||
| 313 | - // Create category | ||
| 314 | - $category = new Category(); | ||
| 315 | - $category->generateLangs(); | ||
| 316 | - $category_langs = $category->modelLangs; | ||
| 317 | - foreach ($category_langs as $category_lang) { | ||
| 318 | - $category_lang->title = $parsed_name[ 'name' ]; | 350 | + |
| 351 | + } else if(preg_match_all('/\[(.*)\]/', $catalog_name, $out,PREG_SET_ORDER)){ | ||
| 352 | + | ||
| 353 | + if(isset($out[0][1])){ | ||
| 354 | + | ||
| 355 | + // ==== Set category ==== | ||
| 356 | + $parsed_name = $this->parseName($out[0][1]); | ||
| 357 | + if (!empty( $parsed_name[ 'remote_id' ] ) && ( $category = Category::find() | ||
| 358 | + ->joinWith('lang') | ||
| 359 | + ->andFilterWhere( | ||
| 360 | + [ 'remote_id' => $parsed_name[ 'remote_id' ] ] | ||
| 361 | + ) | ||
| 362 | + ->one() ) !== null | ||
| 363 | + ) { | ||
| 364 | + if (!empty( $category->lang )) { | ||
| 365 | + $category->lang->title = $parsed_name[ 'name' ]; | ||
| 366 | + $category->lang->save(false); | ||
| 367 | + } else { | ||
| 368 | + throw new \Exception( | ||
| 369 | + 'Category with ID ' . $category->id . ' and lang ' . Language::getCurrent( | ||
| 370 | + )->id . ' doesn\'t exist' | ||
| 371 | + ); | ||
| 372 | + } | ||
| 373 | + | ||
| 374 | + } else { | ||
| 375 | + // Create category | ||
| 376 | + $category = new Category(); | ||
| 377 | + $category->generateLangs(); | ||
| 378 | + $category_langs = $category->modelLangs; | ||
| 379 | + foreach ($category_langs as $category_lang) { | ||
| 380 | + $category_lang->title = $parsed_name[ 'name' ]; | ||
| 381 | + } | ||
| 382 | + $category->remote_id = $parsed_name[ 'remote_id' ]; | ||
| 383 | + $category->save(false); | ||
| 384 | + } | ||
| 385 | + $category_id[] = $category->id; | ||
| 386 | + | ||
| 319 | } | 387 | } |
| 320 | - $category->remote_id = $parsed_name[ 'remote_id' ]; | ||
| 321 | - $category->save(false); | 388 | + |
| 389 | + | ||
| 390 | + } else { | ||
| 391 | + | ||
| 392 | + throw new \Exception( | ||
| 393 | + 'Wrong category format!' | ||
| 394 | + ); | ||
| 322 | } | 395 | } |
| 323 | - $category_id[] = $category->id; | 396 | + |
| 324 | } | 397 | } |
| 325 | return $category_id; | 398 | return $category_id; |
| 326 | } | 399 | } |
| @@ -616,15 +689,7 @@ | @@ -616,15 +689,7 @@ | ||
| 616 | if (trim($data[ 12 ])) { | 689 | if (trim($data[ 12 ])) { |
| 617 | $fotos = explode(',', trim($data[ 12 ])); | 690 | $fotos = explode(',', trim($data[ 12 ])); |
| 618 | } | 691 | } |
| 619 | - | ||
| 620 | - // $lang = \Yii::$app->session->get('export_lang', Language::getDefaultLanguage()->id); | ||
| 621 | - // /** | ||
| 622 | - // * @var Language $language | ||
| 623 | - // */ | ||
| 624 | - // $language = Language::find() | ||
| 625 | - // ->where([ 'id' => $lang ]) | ||
| 626 | - // ->one(); | ||
| 627 | - // Language::setCurrent($language->url); | 692 | + |
| 628 | $categories = $this->saveCatalog($catalog_names); | 693 | $categories = $this->saveCatalog($catalog_names); |
| 629 | 694 | ||
| 630 | $brand_id = $this->saveBrand($brand_name); | 695 | $brand_id = $this->saveBrand($brand_name); |
| @@ -777,8 +842,7 @@ | @@ -777,8 +842,7 @@ | ||
| 777 | $taxGroup->lang->save(false); | 842 | $taxGroup->lang->save(false); |
| 778 | } else { | 843 | } else { |
| 779 | throw new \Exception( | 844 | throw new \Exception( |
| 780 | - 'Tax group with ID ' . $taxGroup->id . ' and lang ' . Language::getCurrent( | ||
| 781 | - )->id . ' doesn\'t exist' | 845 | + 'Tax group with ID ' . $taxGroup->id . ' and lang ' . Language::getCurrent()->id . ' doesn\'t exist' |
| 782 | ); | 846 | ); |
| 783 | } | 847 | } |
| 784 | } else { | 848 | } else { |