From 424ec7e0b4d2ebeacedd1107084b40f691cc36cd Mon Sep 17 00:00:00 2001 From: yarik Date: Wed, 12 Oct 2016 17:16:29 +0300 Subject: [PATCH] Import browser beta v0.1 --- common/modules/language/behaviors/LanguageBehavior.php | 10 +--------- common/modules/product/models/Import.php | 84 +++++++++++++++++++++++++++++++++++++++--------------------------------------------- common/modules/product/models/Product.php | 103 +++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------- common/modules/rubrication/models/TaxGroup.php | 1 + common/modules/rubrication/models/TaxOption.php | 8 ++++---- 5 files changed, 92 insertions(+), 114 deletions(-) diff --git a/common/modules/language/behaviors/LanguageBehavior.php b/common/modules/language/behaviors/LanguageBehavior.php index 556a74f..ec955c0 100755 --- a/common/modules/language/behaviors/LanguageBehavior.php +++ b/common/modules/language/behaviors/LanguageBehavior.php @@ -237,7 +237,7 @@ $success = true; $model_langs = $this->model_langs; foreach($model_langs as $model_lang) { - if($model_lang->save(true) === false) { + if($model_lang->save() === false) { $success = false; } } @@ -251,11 +251,7 @@ */ $owner = $this->owner; $db = $owner::getDb(); - if($owner instanceof Product) { - var_dump('HERE'); die(); - } $this->_transaction = $db->beginTransaction(); - } public function afterSave($event) @@ -264,10 +260,6 @@ * @var ActiveRecord $owner */ $owner = $this->owner; - $this->_transaction->commit(); - if($owner instanceof Product) { - var_dump($owner->product_id); die(); - } if(!empty( $this->model_langs )) { if($this->linkLangs() && $this->saveLangs()) { $this->_transaction->commit(); diff --git a/common/modules/product/models/Import.php b/common/modules/product/models/Import.php index 3770d7c..021577a 100755 --- a/common/modules/product/models/Import.php +++ b/common/modules/product/models/Import.php @@ -439,7 +439,6 @@ $result_items = []; while(( empty( $limit ) || $j++ < $limit ) && ( $data = fgetcsv($handle, 10000, ";") ) !== false) { - try { foreach($data as &$value) { if(!$is_utf) { @@ -519,14 +518,13 @@ // ->where([ 'language_id' => $lang ]) // ->one(); // Language::setCurrent($language->url); - $categories = $this->saveCatalog($catalog_names); - + $brand_id = $this->saveBrand($brand_name); $options = []; if(!empty ( $filters )) { - + $options = $this->saveFilters($filters, 0, $categories); } $parsed_name = $this->parseName($product_name); @@ -568,18 +566,14 @@ if(!empty( $options )) { $_product->options = $options; } - + if(!empty( $_product->lang )) { $product_name_inserted = $_product->lang->name; } else { $product_name_inserted = $_product->model_langs[ Language::$current->language_id ]->name; } - - $_product->validate(); - die(var_dump($_product->getErrors())); - var_dump($_product->save()); die(); - if(!$_product->transactionStatus) { + if(($_product->save() === false) || !$_product->transactionStatus) { $result_items[] = 'Product #' . $product_name_inserted . ' not saved' . " (line $j)"; continue; } @@ -639,15 +633,15 @@ { $options = []; foreach($filters as $filter) { - + preg_match_all('/\[(.*):(.*)\]/', $filter, $filter); - + if(empty( $filter[ 1 ][ 0 ] )) { continue; } $filter_name = trim($filter[ 1 ][ 0 ]); $parsed_group_name = $this->parseName($filter_name); - + /** * @var TaxGroup $taxGroup */ @@ -674,38 +668,38 @@ $taxGroup->is_filter = false; $taxGroup->save(); } - $filters_options = explode(',', $filter[ 2 ][ 0 ]); - foreach($filters_options as $filter_options) { - $parsed_option_name = $this->parseName($filter_options); - /** - * @var TaxOption $option - */ - - if(!empty( $parsed_option_name[ 'remote_id' ] ) && ( $option = TaxOption::find() - ->joinWith('lang') - ->andFilterWhere([ 'remote_id' => $parsed_option_name[ 'remote_id' ] ]) - ->andFilterWhere([ 'tax_group_id' => $taxGroup->tax_group_id ]) - ->one() ) !== NULL - ) { - if(!empty( $option->lang )) { - $option->lang->value = $parsed_option_name[ 'name' ]; - $option->lang->save(); - } else { - throw new \Exception('Tax option with ID ' . $option->tax_option_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist'); - } - } else { - // Create option - $option = new TaxOption(); - $option->generateLangs(); - $option_langs = $option->model_langs; - foreach($option_langs as $option_lang) { - $option_lang->value = $parsed_option_name[ 'name' ]; - } - $option->tax_group_id = $taxGroup->tax_group_id; - $option->save(); - } - $options[] = $option->tax_option_id; - } +// $filters_options = explode(',', $filter[ 2 ][ 0 ]); +// foreach($filters_options as $filter_options) { +// $parsed_option_name = $this->parseName($filter_options); +// /** +// * @var TaxOption $option +// */ +// +// if(!empty( $parsed_option_name[ 'remote_id' ] ) && ( $option = TaxOption::find() +// ->joinWith('lang') +// ->andFilterWhere([ 'remote_id' => $parsed_option_name[ 'remote_id' ] ]) +// ->andFilterWhere([ 'tax_group_id' => $taxGroup->tax_group_id ]) +// ->one() ) !== NULL +// ) { +// if(!empty( $option->lang )) { +// $option->lang->value = $parsed_option_name[ 'name' ]; +// $option->lang->save(); +// } else { +// throw new \Exception('Tax option with ID ' . $option->tax_option_id . ' and lang ' . Language::getCurrent()->language_id . ' doesn\'t exist'); +// } +// } else { +// // Create option +// $option = new TaxOption(); +// $option->generateLangs(); +// $option_langs = $option->model_langs; +// foreach($option_langs as $option_lang) { +// $option_lang->value = $parsed_option_name[ 'name' ]; +// } +// $option->tax_group_id = $taxGroup->tax_group_id; +// $option->save(); +// } +// $options[] = $option->tax_option_id; +// } } return $options; } diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index b35e986..033188d 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -156,15 +156,6 @@ { return $this->hasOne(Brand::className(), [ 'brand_id' => 'brand_id' ]); } - - - - public function beforeValidate() - { - if(parent::beforeValidate()){ - die('here'); - } - } /** * @return \yii\db\ActiveQuery @@ -354,53 +345,53 @@ ->sum('quantity'); } - public function afterSave($insert, $changedAttributes) - { - parent::afterSave($insert, $changedAttributes); - - - if(!empty($this->categories)){ - $categories = Category::findAll($this->categories); - $this->unlinkAll('categories', true); - foreach($categories as $category){ - $this->link('categories', $category); - } - } - - if(!empty($this->options)){ - $options = TaxOption::findAll($this->options); - $this->unlinkAll('options',true); - foreach($options as $option){ - $this->link('options', $option); - } - } - - - if (!empty($this->_variants)) { - $todel = []; - foreach ($this->variants ?: [] as $_variant) { - $todel[$_variant->product_variant_id] = $_variant->product_variant_id; - } - foreach ($this->_variants as $_variant) { - if (!is_array($_variant)) { - return; - } - if (!empty($_variant['product_variant_id'])) { - unset($todel[$_variant['product_variant_id']]); - $model = ProductVariant::findOne($_variant['product_variant_id']); - } else { - $model = new ProductVariant(); - } - $_variant['product_id'] = $this->product_id; - $model->load(['ProductVariant' => $_variant]); - $model->product_id = $this->product_id; - $model->save(); - } - if (!empty($todel)) { - ProductVariant::deleteAll(['product_variant_id' => $todel]); - } - } - } +// public function afterSave($insert, $changedAttributes) +// { +// parent::afterSave($insert, $changedAttributes); +// +// +// if(!empty($this->categories)){ +// $categories = Category::findAll($this->categories); +// $this->unlinkAll('categories', true); +// foreach($categories as $category){ +// $this->link('categories', $category); +// } +// } +// +// if(!empty($this->options)){ +// $options = TaxOption::findAll($this->options); +// $this->unlinkAll('options',true); +// foreach($options as $option){ +// $this->link('options', $option); +// } +// } +// +// +// if (!empty($this->_variants)) { +// $todel = []; +// foreach ($this->variants ?: [] as $_variant) { +// $todel[$_variant->product_variant_id] = $_variant->product_variant_id; +// } +// foreach ($this->_variants as $_variant) { +// if (!is_array($_variant)) { +// return; +// } +// if (!empty($_variant['product_variant_id'])) { +// unset($todel[$_variant['product_variant_id']]); +// $model = ProductVariant::findOne($_variant['product_variant_id']); +// } else { +// $model = new ProductVariant(); +// } +// $_variant['product_id'] = $this->product_id; +// $model->load(['ProductVariant' => $_variant]); +// $model->product_id = $this->product_id; +// $model->save(); +// } +// if (!empty($todel)) { +// ProductVariant::deleteAll(['product_variant_id' => $todel]); +// } +// } +// } public function imagesUpload() { diff --git a/common/modules/rubrication/models/TaxGroup.php b/common/modules/rubrication/models/TaxGroup.php index 32ce977..e3b6227 100755 --- a/common/modules/rubrication/models/TaxGroup.php +++ b/common/modules/rubrication/models/TaxGroup.php @@ -121,6 +121,7 @@ public function afterSave($insert, $changedAttributes) { + parent::afterSave($insert, $changedAttributes); $this->unlinkAll('categories', true); $categories = []; if(!empty( $this->categories )) { diff --git a/common/modules/rubrication/models/TaxOption.php b/common/modules/rubrication/models/TaxOption.php index b3291e8..b3e7126 100755 --- a/common/modules/rubrication/models/TaxOption.php +++ b/common/modules/rubrication/models/TaxOption.php @@ -63,10 +63,10 @@ ], ], ], -// 'artboxtree' => [ -// 'class' => ArtboxTreeBehavior::className(), -// 'keyNameGroup' => 'tax_group_id', -// ], + 'artboxtree' => [ + 'class' => ArtboxTreeBehavior::className(), + 'keyNameGroup' => 'tax_group_id', + ], 'language' => [ 'class' => LanguageBehavior::className(), ], -- libgit2 0.21.4