Commit 73992f22f6116a426fa401acf75e02707ee8cbb4
1 parent
7a87683e
Ntr
Showing
3 changed files
with
25 additions
and
29 deletions
Show diff stats
backend/controllers/EventController.php
@@ -132,8 +132,7 @@ class EventController extends Controller | @@ -132,8 +132,7 @@ class EventController extends Controller | ||
132 | if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) { | 132 | if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) { |
133 | if(!empty($file)){ | 133 | if(!empty($file)){ |
134 | $file->saveAs(Yii::getAlias('@uploadDir/' . $file->name)); | 134 | $file->saveAs(Yii::getAlias('@uploadDir/' . $file->name)); |
135 | - $import = new Import(); | ||
136 | - $import->goEvent(Yii::getAlias('@uploadDir/' . $file->name)); | 135 | + $model->goEvent(Yii::getAlias('@uploadDir/' . $file->name)); |
137 | } | 136 | } |
138 | 137 | ||
139 | } | 138 | } |
common/models/Event.php
@@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
3 | namespace common\models; | 3 | namespace common\models; |
4 | use common\behaviors\SaveImgBehavior; | 4 | use common\behaviors\SaveImgBehavior; |
5 | use common\modules\product\models\Product; | 5 | use common\modules\product\models\Product; |
6 | +use common\modules\product\models\ProductVariant; | ||
6 | use Yii; | 7 | use Yii; |
7 | use yii\behaviors\TimestampBehavior; | 8 | use yii\behaviors\TimestampBehavior; |
8 | use yii\helpers\ArrayHelper; | 9 | use yii\helpers\ArrayHelper; |
@@ -138,7 +139,30 @@ class Event extends \yii\db\ActiveRecord | @@ -138,7 +139,30 @@ class Event extends \yii\db\ActiveRecord | ||
138 | return false; | 139 | return false; |
139 | } | 140 | } |
140 | 141 | ||
142 | + public function goEvent($file) { | ||
141 | 143 | ||
144 | + set_time_limit(0); | ||
145 | + | ||
146 | + | ||
147 | + $handle = fopen($file, 'r'); | ||
148 | + | ||
149 | + EventsToProducts::deleteAll(['event_id' =>iconv('cp1251', 'utf-8', $this->event_id)]); | ||
150 | + while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { | ||
151 | + if(isset($data[0]) && isset($data[1])){ | ||
152 | + $product = ProductVariant::find()->where(['sku' => iconv('cp1251', 'utf-8', $data[1])])->joinWith('product')->one(); | ||
153 | + $model = new EventsToProducts; | ||
154 | + $model->event_id = iconv('cp1251', 'utf-8', $data[0]); | ||
155 | + $model->product_id = $product->product->product_id; | ||
156 | + $model->save(); | ||
157 | + | ||
158 | + | ||
159 | + } | ||
160 | + | ||
161 | + } | ||
162 | + fclose($handle); | ||
163 | + unlink($file); | ||
164 | + | ||
165 | + } | ||
142 | 166 | ||
143 | public function getProducts(){ | 167 | public function getProducts(){ |
144 | return $this->hasMany(Product::className(),['product_id' => 'product_id'] )->viaTable('events_to_products', ['event_id' => 'event_id']); | 168 | return $this->hasMany(Product::className(),['product_id' => 'product_id'] )->viaTable('events_to_products', ['event_id' => 'event_id']); |
common/modules/product/models/Import.php
@@ -511,33 +511,6 @@ class Import extends Model { | @@ -511,33 +511,6 @@ class Import extends Model { | ||
511 | } | 511 | } |
512 | 512 | ||
513 | 513 | ||
514 | - public function goEvent($file) { | ||
515 | - | ||
516 | - set_time_limit(0); | ||
517 | - | ||
518 | - | ||
519 | - $handle = fopen($file, 'r'); | ||
520 | - | ||
521 | - | ||
522 | - while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { | ||
523 | - if(isset($data[0]) && isset($data[1])){ | ||
524 | - $product = ProductVariant::find()->where(['sku' => $data[1]])->joinWith('product')->one(); | ||
525 | - if($product instanceof ProductVariant){ | ||
526 | - $model= EventsToProducts::find()->where(['event_id' =>$data[0], 'product_id' => $product->product->product_id ])->one(); | ||
527 | - if(!$model instanceof EventsToProducts){ | ||
528 | - $model = new EventsToProducts; | ||
529 | - $model->event_id = $data[0]; | ||
530 | - $model->product_id = $product->product->product_id; | ||
531 | - $model->save(); | ||
532 | - } | ||
533 | - } | ||
534 | - } | ||
535 | - | ||
536 | - } | ||
537 | - fclose($handle); | ||
538 | - unlink($file); | ||
539 | - | ||
540 | - } | ||
541 | 514 | ||
542 | private function getProductsFile($file_type) { | 515 | private function getProductsFile($file_type) { |
543 | $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type); | 516 | $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type); |