Commit 31fecea46c5c13ea403dca3757604311f1d19bbe

Authored by Alexey Boroda
2 parents badb4dbc 9fd505a3

Merge remote-tracking branch 'origin/master'

console/GenerateController.php
1 1 <?php
2 2  
3   - namespace console\controllers;
  3 + namespace artweb\artbox\ecommerce\console;
4 4  
5 5 use artweb\artbox\ecommerce\models\Brand;
6 6 use artweb\artbox\ecommerce\models\Category;
... ...
console/ImportController.php
1 1 <?php
2 2  
3   -namespace console\controllers;
  3 +namespace artweb\artbox\ecommerce\console;
4 4  
5 5 use artweb\artbox\ecommerce\models\Import;
  6 +use artweb\artbox\language\models\Language;
6 7 use Yii;
7 8 use yii\console\Controller;
8 9  
... ... @@ -17,29 +18,25 @@ class ImportController extends Controller {
17 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 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 32 $model = new Import();
37 33 $model->goProducts(0, null);
38   -// unlink(Yii::getAlias('@uploadDir/goProducts.lock'));
  34 + unlink(Yii::getAlias('@uploadDir/goProducts_'.$lang.'.lock'));
39 35 return Controller::EXIT_CODE_NORMAL;
40 36 }
41 37  
42 38 public function actionPrices() {
  39 +
43 40 if (file_exists(Yii::getAlias('@uploadDir/goPrices.lock'))) {
44 41 $this->stderr('Task already executed');
45 42 return Controller::EXIT_CODE_ERROR;
... ... @@ -47,18 +44,10 @@ class ImportController extends Controller {
47 44 $ff = fopen(Yii::getAlias('@uploadDir/goPrices.lock'), 'w+');
48 45 fclose($ff);
49 46 $model = new Import();
50   - $data = $model->goPrices(0, null);
  47 + $model->goPrices(0, null);
51 48 unlink(Yii::getAlias('@uploadDir/goPrices.lock'));
52 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 54 \ No newline at end of file
... ...
console/SiteMapController.php
1 1 <?php
2 2  
3   - namespace console\controllers;
  3 + namespace artweb\artbox\ecommerce\console;
4 4  
5 5 use artweb\artbox\seo\models\Seo;
6 6 use artweb\artbox\ecommerce\models\Category;
... ...
controllers/ManageController.php
... ... @@ -308,7 +308,7 @@
308 308  
309 309 if (Yii::$app->request->isAjax) {
310 310 Yii::$app->response->format = Response::FORMAT_JSON;
311   - return $model->goProducts($from, 1);
  311 + return $model->goProducts($from, 10);
312 312 } else {
313 313 throw new \HttpRequestException('Must be AJAX');
314 314 }
... ...
models/Export.php
... ... @@ -80,12 +80,12 @@
80 80 /**
81 81 * @var ProductVariant $variant
82 82 */
83   - $color = $variant->lang->title;
  83 + $name = $variant->lang->title;
84 84 $mods[] = $variant->sku . $this->generateID(
85 85 $variant->remote_id
86 86 ) . '=' . $this->convertFilterToString(
87 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 91 $fotos = [];
... ... @@ -95,7 +95,12 @@
95 95  
96 96 $categories = [];
97 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 106 $categories = implode(',', $categories);
... ...
models/Import.php
... ... @@ -5,6 +5,8 @@
5 5 use artweb\artbox\language\models\Language;
6 6 use Yii;
7 7 use yii\base\Model;
  8 + use yii\db\pgsql\QueryBuilder;
  9 + use yii\db\Query;
8 10 use yii\helpers\ArrayHelper;
9 11  
10 12 /**
... ... @@ -166,12 +168,13 @@
166 168 // ===== Set stock ====
167 169 if ($city_name) {
168 170 if (( $stock = Stock::find()
169   - ->filterWhere([ 'title' => trim($city_name) ])
  171 + ->joinWith('lang')
  172 + ->filterWhere([ 'stock_lang.title' => trim($city_name) ])
170 173 ->one() ) === null
171 174 ) {
172 175 // Create stock
173 176 $stock = new Stock();
174   - $stock->title = trim($city_name);
  177 + $stock->lang->title = trim($city_name);
175 178 $stock->save(false);
176 179 }
177 180  
... ... @@ -290,37 +293,109 @@
290 293 $category_id = [];
291 294  
292 295 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   - );
  296 +
  297 +
  298 + if(preg_match_all('/\[(.*)>(.*)\]/', $catalog_name, $out,PREG_SET_ORDER)){
  299 +
  300 + $count = count($out[0]);
  301 + $parent_id = 0;
  302 +
  303 + for($i=1; $i<$count; $i++){
  304 +
  305 + if(isset($out[0][$i])){
  306 +
  307 + // ==== Set category ====
  308 + $parsed_name = $this->parseName($out[0][$i]);
  309 + if (!empty( $parsed_name[ 'remote_id' ] ) && ( $category = Category::find()
  310 + ->joinWith('lang')
  311 + ->andFilterWhere(
  312 + [ 'remote_id' => $parsed_name[ 'remote_id' ] ]
  313 + )
  314 + ->one() ) !== null
  315 + ) {
  316 + if (!empty( $category->lang )) {
  317 + if($i != 1){
  318 + $category->parent_id = $parent_id;
  319 + }else{
  320 + $parent_id = $category->id;
  321 + }
  322 + $category->lang->title = $parsed_name[ 'name' ];
  323 + $category->lang->save(false);
  324 + } else {
  325 + throw new \Exception(
  326 + 'Category with ID ' . $category->id . ' and lang ' . Language::getCurrent(
  327 + )->id . ' doesn\'t exist'
  328 + );
  329 + }
  330 +
  331 + } else {
  332 + // Create category
  333 + $category = new Category();
  334 + $category->generateLangs();
  335 + $category_langs = $category->modelLangs;
  336 + foreach ($category_langs as $category_lang) {
  337 + $category_lang->title = $parsed_name[ 'name' ];
  338 + }
  339 + if($i != 1){
  340 + $category->parent_id = $parent_id;
  341 + }
  342 + $category->remote_id = $parsed_name[ 'remote_id' ];
  343 + $category->save(false);
  344 + if($i == 1){
  345 + $parent_id = $category->id;
  346 + }
  347 + }
  348 + $category_id[] = $category->id;
  349 + }
  350 +
310 351 }
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' ];
  352 +
  353 + } else if(preg_match_all('/\[(.*)\]/', $catalog_name, $out,PREG_SET_ORDER)){
  354 +
  355 + if(isset($out[0][1])){
  356 +
  357 + // ==== Set category ====
  358 + $parsed_name = $this->parseName($out[0][1]);
  359 + if (!empty( $parsed_name[ 'remote_id' ] ) && ( $category = Category::find()
  360 + ->joinWith('lang')
  361 + ->andFilterWhere(
  362 + [ 'remote_id' => $parsed_name[ 'remote_id' ] ]
  363 + )
  364 + ->one() ) !== null
  365 + ) {
  366 + if (!empty( $category->lang )) {
  367 + $category->lang->title = $parsed_name[ 'name' ];
  368 + $category->lang->save(false);
  369 + } else {
  370 + throw new \Exception(
  371 + 'Category with ID ' . $category->id . ' and lang ' . Language::getCurrent(
  372 + )->id . ' doesn\'t exist'
  373 + );
  374 + }
  375 +
  376 + } else {
  377 + // Create category
  378 + $category = new Category();
  379 + $category->generateLangs();
  380 + $category_langs = $category->modelLangs;
  381 + foreach ($category_langs as $category_lang) {
  382 + $category_lang->title = $parsed_name[ 'name' ];
  383 + }
  384 + $category->remote_id = $parsed_name[ 'remote_id' ];
  385 + $category->save(false);
  386 + }
  387 + $category_id[] = $category->id;
  388 +
319 389 }
320   - $category->remote_id = $parsed_name[ 'remote_id' ];
321   - $category->save(false);
  390 +
  391 +
  392 + } else {
  393 +
  394 + throw new \Exception(
  395 + 'Wrong category format!'
  396 + );
322 397 }
323   - $category_id[] = $category->id;
  398 +
324 399 }
325 400 return $category_id;
326 401 }
... ... @@ -543,7 +618,27 @@
543 618 ) );
544 619  
545 620 $result_items = [];
546   -
  621 +
  622 + $connection = Yii::$app->getDb();
  623 +
  624 +
  625 + $connection->createCommand()->addPrimaryKey('product_variant_option_pkey','product_variant_option',['product_variant_id', 'option_id'])->execute();
  626 + $connection->createCommand()->addPrimaryKey('product_option_pkey','product_option',['product_id', 'option_id'])->execute();
  627 + $connection->createCommand()->addForeignKey('product_variant_option_product_variant_product_variant_id_fk','product_variant_option','product_variant_id','product_variant','id')->execute();
  628 + $connection->createCommand()->addForeignKey('product_variant_option_tax_option_tax_option_id_fk','product_variant_option','option_id','tax_option','id')->execute();
  629 + $connection->createCommand()->addForeignKey('product_option_product_product_id_fk','product_option','product_id','product','id','CASCADE','CASCADE' )->execute();
  630 + $connection->createCommand()->addForeignKey('product_option_tax_option_tax_option_id_fk','product_option','option_id', 'tax_option', 'id','CASCADE','CASCADE' )->execute();
  631 +
  632 +
  633 +// $connection->createCommand()->dropForeignKey('product_option_tax_option_tax_option_id_fk','product_option')->execute();
  634 +// $connection->createCommand()->dropForeignKey('product_option_product_product_id_fk','product_option')->execute();
  635 +// $connection->createCommand()->dropForeignKey('product_variant_option_tax_option_tax_option_id_fk','product_variant_option')->execute();
  636 +// $connection->createCommand()->dropForeignKey('product_variant_option_product_variant_product_variant_id_fk','product_variant_option')->execute();
  637 +// $connection->createCommand()->dropPrimaryKey('product_option_pkey','product_option')->execute();
  638 +// $connection->createCommand()->dropForeignKey('product_variant_option_pkey','product_variant_option')->execute();
  639 +
  640 +
  641 + sleep(1);
547 642 while (( empty( $limit ) || $j++ < $limit ) && ( $data = fgetcsv($handle, 10000, ";") ) !== false) {
548 643 try {
549 644  
... ... @@ -616,15 +711,7 @@
616 711 if (trim($data[ 12 ])) {
617 712 $fotos = explode(',', trim($data[ 12 ]));
618 713 }
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);
  714 +
628 715 $categories = $this->saveCatalog($catalog_names);
629 716  
630 717 $brand_id = $this->saveBrand($brand_name);
... ... @@ -703,6 +790,14 @@
703 790 }
704 791  
705 792 }
  793 + $connection->createCommand()->addPrimaryKey('product_variant_option_pkey','product_variant_option',['product_variant_id', 'option_id'])->execute();
  794 + $connection->createCommand()->addPrimaryKey('product_option_pkey','product_option',['product_id', 'option_id'])->execute();
  795 + $connection->createCommand()->addForeignKey('product_variant_option_product_variant_product_variant_id_fk','product_variant_option','product_variant_id','product_variant','id')->execute();
  796 + $connection->createCommand()->addForeignKey('product_variant_option_tax_option_tax_option_id_fk','product_variant_option','option_id','tax_option','id')->execute();
  797 + $connection->createCommand()->addForeignKey('product_option_product_product_id_fk','product_option','product_id','product','id','CASCADE','CASCADE' )->execute();
  798 + $connection->createCommand()->addForeignKey('product_option_tax_option_tax_option_id_fk','product_option','option_id', 'tax_option', 'id','CASCADE','CASCADE' )->execute();
  799 +
  800 +
706 801  
707 802 $result = [
708 803 'end' => feof($handle),
... ... @@ -777,8 +872,7 @@
777 872 $taxGroup->lang->save(false);
778 873 } else {
779 874 throw new \Exception(
780   - 'Tax group with ID ' . $taxGroup->id . ' and lang ' . Language::getCurrent(
781   - )->id . ' doesn\'t exist'
  875 + 'Tax group with ID ' . $taxGroup->id . ' and lang ' . Language::getCurrent()->id . ' doesn\'t exist'
782 876 );
783 877 }
784 878 } else {
... ...