diff --git a/console/GenerateController.php b/console/GenerateController.php index ed0b4c9..c36f8e8 100644 --- a/console/GenerateController.php +++ b/console/GenerateController.php @@ -1,6 +1,6 @@ stderr('Task already executed'); + + public function actionProducts($lang) { + + Language::setCurrent($lang); + + if (file_exists(Yii::getAlias('@uploadDir/goProducts_'.$lang.'.lock'))) { + $this->errors[] = 'Task already executed'; return Controller::EXIT_CODE_ERROR; } - return fopen ($filename, 'r'); - } - - public function actionProducts() { -// if (file_exists(Yii::getAlias('@uploadDir/goProducts.lock'))) { -// $this->errors[] = 'Task already executed'; -// return Controller::EXIT_CODE_ERROR; -// } -// $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+'); -// fclose($ff); + $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+'); + fclose($ff); $model = new Import(); $model->goProducts(0, null); -// unlink(Yii::getAlias('@uploadDir/goProducts.lock')); + unlink(Yii::getAlias('@uploadDir/goProducts_'.$lang.'.lock')); return Controller::EXIT_CODE_NORMAL; } public function actionPrices() { + if (file_exists(Yii::getAlias('@uploadDir/goPrices.lock'))) { $this->stderr('Task already executed'); return Controller::EXIT_CODE_ERROR; @@ -47,18 +44,10 @@ class ImportController extends Controller { $ff = fopen(Yii::getAlias('@uploadDir/goPrices.lock'), 'w+'); fclose($ff); $model = new Import(); - $data = $model->goPrices(0, null); + $model->goPrices(0, null); unlink(Yii::getAlias('@uploadDir/goPrices.lock')); return Controller::EXIT_CODE_NORMAL; } - private function saveNotFoundRecord (array $line, $filename) - { - $str = implode (';', $line)."\n"; - $str = iconv ("UTF-8//TRANSLIT//IGNORE", "windows-1251", $str); - $fg = fopen (Yii::getAlias('@uploadDir') .'/'. $filename, 'a+'); - fputs ($fg, $str); - fclose ($fg); - } } \ No newline at end of file diff --git a/console/SiteMapController.php b/console/SiteMapController.php index 2e86e9a..026ac3e 100755 --- a/console/SiteMapController.php +++ b/console/SiteMapController.php @@ -1,6 +1,6 @@ lang->title; + $name = $variant->lang->title; $mods[] = $variant->sku . $this->generateID( $variant->remote_id ) . '=' . $this->convertFilterToString( $variant->filters - ) . '=' . $color . '=' . ( ( !empty( $variant->image ) ) ? $variant->image->image : '' ) . '=' . $variant->stock; + ) . '=' . $name . '=' . ( ( !empty( $variant->image ) ) ? $variant->image->image : '' ) . '=' . $variant->stock; } $fotos = []; @@ -95,7 +95,12 @@ $categories = []; foreach ($product->categories as $value) { - $categories[] = $value->lang->title . $this->generateID($value->remote_id); + if($value->parent_id){ + $categories[] = '['.$value->parent->lang->title. $this->generateID($value->parent->remote_id).'>' .$value->lang->title . $this->generateID($value->remote_id).']'; + } else { + $categories[] = '['.$value->lang->title . $this->generateID($value->remote_id).']'; + } + } $categories = implode(',', $categories); diff --git a/models/Import.php b/models/Import.php index a0581b1..d7e636f 100755 --- a/models/Import.php +++ b/models/Import.php @@ -166,12 +166,13 @@ // ===== Set stock ==== if ($city_name) { if (( $stock = Stock::find() - ->filterWhere([ 'title' => trim($city_name) ]) + ->joinWith('lang') + ->filterWhere([ 'stock_lang.title' => trim($city_name) ]) ->one() ) === null ) { // Create stock $stock = new Stock(); - $stock->title = trim($city_name); + $stock->lang->title = trim($city_name); $stock->save(false); } @@ -290,37 +291,109 @@ $category_id = []; foreach ($catalog_names as $catalog_name) { - // ==== Set category ==== - $parsed_name = $this->parseName($catalog_name); - if (!empty( $parsed_name[ 'remote_id' ] ) && ( $category = Category::find() - ->joinWith('lang') - ->andFilterWhere( - [ 'remote_id' => $parsed_name[ 'remote_id' ] ] - ) - ->one() ) !== null - ) { - if (!empty( $category->lang )) { - $category->lang->title = $parsed_name[ 'name' ]; - $category->lang->save(false); - } else { - throw new \Exception( - 'Category with ID ' . $category->id . ' and lang ' . Language::getCurrent( - )->id . ' doesn\'t exist' - ); + + + if(preg_match_all('/\[(.*)>(.*)\]/', $catalog_name, $out,PREG_SET_ORDER)){ + + $count = count($out[0]); + $parent_id = 0; + + for($i=1; $i<$count; $i++){ + + if(isset($out[0][$i])){ + + // ==== Set category ==== + $parsed_name = $this->parseName($out[0][$i]); + if (!empty( $parsed_name[ 'remote_id' ] ) && ( $category = Category::find() + ->joinWith('lang') + ->andFilterWhere( + [ 'remote_id' => $parsed_name[ 'remote_id' ] ] + ) + ->one() ) !== null + ) { + if (!empty( $category->lang )) { + if($i != 1){ + $category->parent_id = $parent_id; + }else{ + $parent_id = $category->id; + } + $category->lang->title = $parsed_name[ 'name' ]; + $category->lang->save(false); + } else { + throw new \Exception( + 'Category with ID ' . $category->id . ' and lang ' . Language::getCurrent( + )->id . ' doesn\'t exist' + ); + } + + } else { + // Create category + $category = new Category(); + $category->generateLangs(); + $category_langs = $category->modelLangs; + foreach ($category_langs as $category_lang) { + $category_lang->title = $parsed_name[ 'name' ]; + } + if($i != 1){ + $category->parent_id = $parent_id; + } + $category->remote_id = $parsed_name[ 'remote_id' ]; + $category->save(false); + if($i == 1){ + $parent_id = $category->id; + } + } + $category_id[] = $category->id; + } + } - - } else { - // Create category - $category = new Category(); - $category->generateLangs(); - $category_langs = $category->modelLangs; - foreach ($category_langs as $category_lang) { - $category_lang->title = $parsed_name[ 'name' ]; + + } else if(preg_match_all('/\[(.*)\]/', $catalog_name, $out,PREG_SET_ORDER)){ + + if(isset($out[0][1])){ + + // ==== Set category ==== + $parsed_name = $this->parseName($out[0][1]); + if (!empty( $parsed_name[ 'remote_id' ] ) && ( $category = Category::find() + ->joinWith('lang') + ->andFilterWhere( + [ 'remote_id' => $parsed_name[ 'remote_id' ] ] + ) + ->one() ) !== null + ) { + if (!empty( $category->lang )) { + $category->lang->title = $parsed_name[ 'name' ]; + $category->lang->save(false); + } else { + throw new \Exception( + 'Category with ID ' . $category->id . ' and lang ' . Language::getCurrent( + )->id . ' doesn\'t exist' + ); + } + + } else { + // Create category + $category = new Category(); + $category->generateLangs(); + $category_langs = $category->modelLangs; + foreach ($category_langs as $category_lang) { + $category_lang->title = $parsed_name[ 'name' ]; + } + $category->remote_id = $parsed_name[ 'remote_id' ]; + $category->save(false); + } + $category_id[] = $category->id; + } - $category->remote_id = $parsed_name[ 'remote_id' ]; - $category->save(false); + + + } else { + + throw new \Exception( + 'Wrong category format!' + ); } - $category_id[] = $category->id; + } return $category_id; } @@ -616,15 +689,7 @@ if (trim($data[ 12 ])) { $fotos = explode(',', trim($data[ 12 ])); } - - // $lang = \Yii::$app->session->get('export_lang', Language::getDefaultLanguage()->id); - // /** - // * @var Language $language - // */ - // $language = Language::find() - // ->where([ 'id' => $lang ]) - // ->one(); - // Language::setCurrent($language->url); + $categories = $this->saveCatalog($catalog_names); $brand_id = $this->saveBrand($brand_name); @@ -777,8 +842,7 @@ $taxGroup->lang->save(false); } else { throw new \Exception( - 'Tax group with ID ' . $taxGroup->id . ' and lang ' . Language::getCurrent( - )->id . ' doesn\'t exist' + 'Tax group with ID ' . $taxGroup->id . ' and lang ' . Language::getCurrent()->id . ' doesn\'t exist' ); } } else { -- libgit2 0.21.4