Commit 73992f22f6116a426fa401acf75e02707ee8cbb4

Authored by Administrator
1 parent 7a87683e

Ntr

backend/controllers/EventController.php
... ... @@ -132,8 +132,7 @@ class EventController extends Controller
132 132 if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) {
133 133 if(!empty($file)){
134 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 3 namespace common\models;
4 4 use common\behaviors\SaveImgBehavior;
5 5 use common\modules\product\models\Product;
  6 +use common\modules\product\models\ProductVariant;
6 7 use Yii;
7 8 use yii\behaviors\TimestampBehavior;
8 9 use yii\helpers\ArrayHelper;
... ... @@ -138,7 +139,30 @@ class Event extends \yii\db\ActiveRecord
138 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 167 public function getProducts(){
144 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 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 515 private function getProductsFile($file_type) {
543 516 $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type);
... ...