From aac46b7995f0245e3ac4e2fbb323339e72899cc7 Mon Sep 17 00:00:00 2001 From: yarik Date: Sat, 11 Mar 2017 16:09:31 +0200 Subject: [PATCH] Import test --- common/modules/product/models/Export.php | 10 ++++++++-- common/modules/product/models/Import.php | 52 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/common/modules/product/models/Export.php b/common/modules/product/models/Export.php index 42b69a7..c07558e 100644 --- a/common/modules/product/models/Export.php +++ b/common/modules/product/models/Export.php @@ -3,6 +3,7 @@ namespace common\modules\product\models; use common\modules\rubrication\models\TaxGroup; + use common\modules\rubrication\models\TaxOption; use yii\base\Model; class Export extends Model @@ -101,14 +102,19 @@ return $result; } - + + /** + * @param TaxOption[] $filters + * + * @return string + */ public function convertFilterToString($filters) { if(!empty($filters)){ $fittersArray = []; foreach($filters as $filter) { if($filter->taxGroup instanceof TaxGroup){ - $fittersArray[ $filter->taxGroup->alias ][] = $filter->value; + $fittersArray[ $filter->taxGroup->alias ][] = $filter->value . '(#' . $filter->alias . '#)'; } } diff --git a/common/modules/product/models/Import.php b/common/modules/product/models/Import.php index 6395ecc..c8ea675 100755 --- a/common/modules/product/models/Import.php +++ b/common/modules/product/models/Import.php @@ -267,7 +267,7 @@ $is_utf = ( preg_match( '//u', file_get_contents( - Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFileProducts'), + Yii::getAlias('@uploadDir') . '/' . Yiiа::getAlias('@uploadFileProducts'), null, null, null, @@ -317,9 +317,9 @@ $result_items[] = "Не указано наименование товара (строка $j)"; continue; } - if(preg_match('/^.+\(#(\d+)#\)$/', trim($product_name), $remote_id)) { - if(!empty($remote_id[1])) { - $remote_id = $remote_id[1]; + if (preg_match('/^.+\(#(\d+)#\)$/', trim($product_name), $remote_id)) { + if (!empty( $remote_id[ 1 ] )) { + $remote_id = $remote_id[ 1 ]; $product_name = substr($product_name, 0, strpos($product_name, '(#' . $remote_id . '#)')); } } @@ -368,13 +368,16 @@ $product_image = explode('=', $data[ 14 ]); $product_image = @$product_image[ 3 ]; - if(!empty($remote_id)) { - if(($_product = Product::find()->filterWhere(['product_id' => $remote_id])->one()) === null) { + if (!empty( $remote_id )) { + if (( $_product = Product::find() + ->filterWhere([ 'product_id' => $remote_id ]) + ->one() ) === null + ) { $_product = new Product(); } } elseif (( $_product = Product::find() - ->filterWhere([ 'name' => trim($product_name) ]) - ->one() ) === null + ->filterWhere([ 'name' => trim($product_name) ]) + ->one() ) === null ) { $_product = new Product(); } @@ -630,22 +633,31 @@ $filters_options = explode(',', $filter[ 2 ][ 0 ]); foreach ($filters_options as $filter_options) { - $value = TaxValueString::find() - ->innerJoinWith('taxOption') - ->andWhere([ 'value' => $filter_options ]) - ->andWhere([ 'tax_option.tax_group_id' => $taxGroup->tax_group_id ]) - ->one(); + $parsed_filter = $this->parseFilter($filter_options); + $value = null; + if ($parsed_filter !== false) { + $value = TaxValueString::find() + ->innerJoinWith('taxOption') + ->andWhere([ 'tax_option.alias' => $parsed_filter[ 2 ] ]) + ->andWhere([ 'tax_option.tax_group_id' => $taxGroup->tax_group_id ]) + ->one(); + } if (!$value instanceof TaxValueString) { // Create option $option = new TaxOption(); $option->tax_group_id = $taxGroup->tax_group_id; - $option->name = $filter_options; + if($parsed_filter !== false) { + $option->name = $parsed_filter[1]; + $option->alias = $parsed_filter[2]; + } else { + $option->name = $filter_options; + } $option->save(); $value = new TaxValueString(); $value->tax_option_id = $option->tax_option_id; - $value->value = $filter_options; + $value->value = $option->name; $value->save(); $option->default_value = $value->tax_value_id; @@ -659,4 +671,14 @@ return $options; } + + private function parseFilter($filter) + { + $regex = '/^(.+)\(#(.+)#\)$/'; + if (preg_match($regex, $filter, $result)) { + return $result; + } else { + return false; + } + } } \ No newline at end of file -- libgit2 0.21.4