From 73992f22f6116a426fa401acf75e02707ee8cbb4 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 15 Dec 2016 23:32:35 +0200 Subject: [PATCH] Ntr --- backend/controllers/EventController.php | 3 +-- common/models/Event.php | 24 ++++++++++++++++++++++++ common/modules/product/models/Import.php | 27 --------------------------- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/backend/controllers/EventController.php b/backend/controllers/EventController.php index 3e09590..e7bc382 100755 --- a/backend/controllers/EventController.php +++ b/backend/controllers/EventController.php @@ -132,8 +132,7 @@ class EventController extends Controller if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) { if(!empty($file)){ $file->saveAs(Yii::getAlias('@uploadDir/' . $file->name)); - $import = new Import(); - $import->goEvent(Yii::getAlias('@uploadDir/' . $file->name)); + $model->goEvent(Yii::getAlias('@uploadDir/' . $file->name)); } } diff --git a/common/models/Event.php b/common/models/Event.php index 9b052e5..dab6210 100755 --- a/common/models/Event.php +++ b/common/models/Event.php @@ -3,6 +3,7 @@ namespace common\models; use common\behaviors\SaveImgBehavior; use common\modules\product\models\Product; +use common\modules\product\models\ProductVariant; use Yii; use yii\behaviors\TimestampBehavior; use yii\helpers\ArrayHelper; @@ -138,7 +139,30 @@ class Event extends \yii\db\ActiveRecord return false; } + public function goEvent($file) { + set_time_limit(0); + + + $handle = fopen($file, 'r'); + + EventsToProducts::deleteAll(['event_id' =>iconv('cp1251', 'utf-8', $this->event_id)]); + while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { + if(isset($data[0]) && isset($data[1])){ + $product = ProductVariant::find()->where(['sku' => iconv('cp1251', 'utf-8', $data[1])])->joinWith('product')->one(); + $model = new EventsToProducts; + $model->event_id = iconv('cp1251', 'utf-8', $data[0]); + $model->product_id = $product->product->product_id; + $model->save(); + + + } + + } + fclose($handle); + unlink($file); + + } public function getProducts(){ return $this->hasMany(Product::className(),['product_id' => 'product_id'] )->viaTable('events_to_products', ['event_id' => 'event_id']); diff --git a/common/modules/product/models/Import.php b/common/modules/product/models/Import.php index eeed02c..b95eaec 100755 --- a/common/modules/product/models/Import.php +++ b/common/modules/product/models/Import.php @@ -511,33 +511,6 @@ class Import extends Model { } - public function goEvent($file) { - - set_time_limit(0); - - - $handle = fopen($file, 'r'); - - - while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { - if(isset($data[0]) && isset($data[1])){ - $product = ProductVariant::find()->where(['sku' => $data[1]])->joinWith('product')->one(); - if($product instanceof ProductVariant){ - $model= EventsToProducts::find()->where(['event_id' =>$data[0], 'product_id' => $product->product->product_id ])->one(); - if(!$model instanceof EventsToProducts){ - $model = new EventsToProducts; - $model->event_id = $data[0]; - $model->product_id = $product->product->product_id; - $model->save(); - } - } - } - - } - fclose($handle); - unlink($file); - - } private function getProductsFile($file_type) { $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type); -- libgit2 0.21.4