Blame view

app/modules/admin/models/Import.php 15.6 KB
bf807468   Alex Savenko   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  <?php

  

  namespace app\modules\admin\models;

  

  use Yii;

  use yii\base\Model;

  use yii\web\UploadedFile;

  use app\components\resize;

  use app\components\Translite;

  use app\modules\admin\models\Catalog;

  use app\modules\admin\models\Products;

  use app\modules\admin\models\Mod;

  use app\modules\admin\models\Fotos;

  use app\modules\admin\models\Type;

  use app\modules\admin\models\ProductsType;

  use app\modules\admin\models\Brends;

  use app\modules\admin\models\ProductsBrends;

  use app\modules\admin\models\Fasovka;

  use app\modules\admin\models\ProductsFasovka;

  /**

   * ContactForm is the model behind the contact form.

   */

  class Import extends Model

  {

      public $file;

  

  

      /**

       * @return array the validation rules.

       */

      public function rules()

      {

          return [

                 [['file'], 'file', 'extensions'=>'csv', 'skipOnEmpty'=>false],

  

          ];

      }

  

      /**

       * @return array customized attribute labels

       */

      public function attributeLabels()

      {

          return [

              'file' => 'Файл .csv',

          ];

      }

  

  

      public function go()

      { 

   

              $db = Yii::$app->db;

              $this->file = UploadedFile::getInstance($this, 'file');

            

          if (($handle = fopen($this->file->tempName, "r")) !== FALSE) {

              while (($data = fgetcsv($handle, 100000, ";")) !== FALSE) {

                 // print_r($data);

                  $catalogParent = trim($data[0]);

                  if(!$catalogParent)continue;

                  $catalogParent_image = trim($data[1]);

                  $catalog = trim($data[2]);

                  $catalog_image = trim($data[3]);

                  $p = explode('=',trim($data[4]));

                  $art = trim($p[0]);

                  $product = trim($p[1]);

                  $product_image = trim($data[5]);

                  $product_char = trim($data[6]);

                  $product_body = trim($data[7]);

                  $mods = (!empty($data[8])) ? explode('|',trim($data[8])) : [];

                  $fotos = (!empty($data[9])) ? explode('|',trim($data[9])) : [];

  

                  $types = (!empty($data[10])) ? explode('|',trim($data[10])) : [];

  

                  $brends = (!empty($data[11])) ? explode('|',trim($data[11])) : [];

                  $fasovkas = (!empty($data[12])) ? explode('|',trim($data[12])) : [];

                  $product_new = (!empty($data[13])) ? trim($data[13]) : 0;

                  $product_top = (!empty($data[14])) ? trim($data[14]) : 0;

                  $product_akciya = (!empty($data[15])) ? trim($data[15]) : 0;

                  $product_out_of_stock = (!empty($data[16])) ? trim($data[16]) : 0;

  

                  //*********catalogParent*********//

                  $modelCatalog = Catalog::find()->where('name=:name and parent_id=0',[':name'=>$catalogParent])->one();

                              

                          if(empty($modelCatalog->id)){    

                              $db->createCommand()->insert('catalog', [

                                 'name' => $catalogParent,

                                 'image' => $catalogParent_image,

                                 'translit' => Translite::rusencode($catalogParent),

                             ])->execute();

                             $catalogParent_id = Yii::$app->db->lastInsertID;

                          }else{

                              

                              $db ->createCommand()

                              ->update('catalog', [ 

                                  'name' => $catalogParent,

                                  'image' => $catalogParent_image,

                                  'translit' => Translite::rusencode($catalogParent),

                                  ], 'id = '.$modelCatalog->id)

                              ->execute();

                              $catalogParent_id = $modelCatalog->id;

                             

                          }

                          if(is_file('upload/catalog/'.$catalogParent_image)){

  			$resizeObj = new resize('upload/catalog/'.$catalogParent_image);

  			$resizeObj -> resizeImage(195, 186, 'crop');

                          $resizeObj -> saveImage('upload/catalog/ico/'.$catalogParent_image, 100);

                          }

                          //*********END catalogParent*********//

                          

                  //*********catalog*********//

                  $modelCatalog = Catalog::find()->where('name=:name and parent_id='.$catalogParent_id,[':name'=>$catalog])->one();

                              

                          if(empty($modelCatalog->id)){    

                              $db->createCommand()->insert('catalog', [

                                 'parent_id'=>$catalogParent_id,

                                  'name' => $catalog,

                                 'image' => $catalog_image,

                                 'translit' => Translite::rusencode($catalog),

                             ])->execute();

                             $catalog_id = Yii::$app->db->lastInsertID;

                          }else{

                              

                              $db ->createCommand()

                              ->update('catalog', [ 

                                  'parent_id'=>$catalogParent_id,

                                  'name' => $catalog,

                                  'image' => $catalog_image,

                                  'translit' => Translite::rusencode($catalog),

                                  ], 'id = '.$modelCatalog->id)

                              ->execute();

                              $catalog_id = $modelCatalog->id;

                             

                          }

  			if(is_file('upload/catalog/'.$catalog_image)){

                          $resizeObj = new resize('upload/catalog/'.$catalog_image);

  			$resizeObj -> resizeImage(195, 186, 'crop');

                          $resizeObj -> saveImage('upload/catalog/ico/'.$catalog_image, 100);

                          }

                          //*********END catalog*********// 

                          

                  //*********product*********//

                  $modelProducts = Products::find()->where('art=:art',[':art'=>$art])->one();

                              

                          if(empty($modelProducts->id)){    

                              $db->createCommand()->insert('products', [

                                 'catalog_id'=>$catalog_id,

                                  'name' => $product,

                                  'art' => $art,

                                 'image' => $product_image,

                                 'translit' => Translite::rusencode($product),

                                  'char'=>$product_char,

                                  'body'=>$product_body,

                                  'new'=>$product_new,

                                  'top'=>$product_top,

                                  'akciya'=>$product_akciya,

                                  'out_of_stock'=>$product_out_of_stock,

                             ])->execute();

                             $product_id = Yii::$app->db->lastInsertID;

                          }else{

                              

                              $db ->createCommand()

                              ->update('products', [ 

                                 'catalog_id'=>$catalog_id,

                                  'name' => $product,

                                  'art' => $art,

                                 'image' => $product_image,

                                 'translit' => Translite::rusencode($product),

                                  'char'=>$product_char,

                                  'body'=>$product_body,

                                  'new'=>$product_new,

                                  'top'=>$product_top,

                                  'akciya'=>$product_akciya,

                                  'out_of_stock'=>$product_out_of_stock,

                              ], 'id = '.$modelProducts->id)

                              ->execute();

                              $product_id = $modelProducts->id;

                             

                          }

  			if(is_file('upload/products/'.$product_image)){

                          $resizeObj = new resize('upload/products/'.$product_image);

  			$resizeObj -> resizeImage(180, 240, 'landscape');

                          $resizeObj -> saveImage('upload/products/ico/'.$product_image, 100);

  			$resizeObj -> resizeImage(400, 400, 'landscape');

                          $resizeObj -> saveImage('upload/products/big/'.$product_image, 100);                        

                          

                          }

                          //*********END product*********//                         

                          

                          //*********mods*********// 

                          foreach($mods as $mod){

                              $mod_arr = explode('=',$mod);

                              if(count($mod_arr)<2)continue;

                              $mod_art = trim($mod_arr[0]);

                              $mod_name = trim($mod_arr[1]);

                              $mod_cost = trim($mod_arr[2]);

                              $fields = [

                                  'product_id'=>$product_id,

                                  'art'=>$mod_art,

                                  'name'=>$mod_name,

                                  'cost'=>$mod_cost,

                              ];

                              $modelMod = Mod::find()->where('art=:art',[':art'=>$mod_art])->one();

                              if(empty($modelMod->id)){    

                                  $db->createCommand()->insert('mod', $fields)->execute();

                              }else{

                                  $db->createCommand()->update('mod', $fields, 'id = '.$modelMod->id)->execute();                                

                              }  

                          }

                          //*********END mods*********//

                          // 

                          //*********fotos*********// 

                          foreach($fotos as $foto){

                              $fields = [

                                  'product_id'=>$product_id,

                                  'image'=>$foto,

                              ];                            

                              $modelFotos = Fotos::find()->where('image=:image',[':image'=>$foto])->one(); 

                              if(empty($modelFotos->id)){    

                                  $db->createCommand()->insert('products_fotos', $fields)->execute();

                              }else{

                                  $db->createCommand()->update('products_fotos', $fields, 'id = '.$modelFotos->id)->execute();                                

                              }

                              if(is_file('upload/fotos/'.$foto)){

                              $resizeObj = new resize('upload/fotos/'.$foto);

                              $resizeObj -> resizeImage(100, 100, 'crop');

                              $resizeObj -> saveImage('upload/fotos/ico/'.$foto, 100);

                              $resizeObj -> resizeImage(400, 400, 'crop');

                              $resizeObj -> saveImage('upload/fotos/big/'.$foto, 100);

                              }                            

                          }                        

                          //*********END fotos*********//

                          

                          //*********types*********//

                          ProductsType::deleteAll(['product_id' => $product_id]);

                          foreach($types as $type){

                              $fields = [

                                  'catalog_id'=>$catalog_id,

                                  'name'=>$type,

                              ];

                              $modelType = Type::find()->where('name=:name and catalog_id=:catalog_id',[':name'=>$type,':catalog_id'=>$catalog_id])->one();

                              if(empty($modelType->id)){    

                                  $db->createCommand()->insert('catalog_type', $fields)->execute();

                                  $type_id = Yii::$app->db->lastInsertID;

                              }else{

                                  $db->createCommand()->update('catalog_type', $fields, 'id = '.$modelType->id)->execute();                                

                                  $type_id = $modelType->id;

                                  

                              }

                              $db->createCommand()->insert('productsType', ['type_id'=>$type_id,'product_id'=>$product_id])->execute();

                              

                              

                          }

                          //*********END types*********//

                          //*********brends*********//

                          ProductsBrends::deleteAll(['product_id' => $product_id]);

                          foreach($brends as $brend){

                              $fields = [

                                  'catalog_id'=>$catalog_id,

                                  'name'=>$brend,

                              ];

                              $modelBrends = Brends::find()->where('name=:name and catalog_id=:catalog_id',[':name'=>$brend,':catalog_id'=>$catalog_id])->one();

                              if(empty($modelBrends->id)){    

                                  $db->createCommand()->insert('catalog_brends', $fields)->execute();

                                  $brend_id = Yii::$app->db->lastInsertID;

                              }else{

                                  $db->createCommand()->update('catalog_brends', $fields, 'id = '.$modelBrends->id)->execute();                                

                                  $brend_id = $modelBrends->id;

                                  

                              }

                              $db->createCommand()->insert('productsBrends', ['brend_id'=>$brend_id,'product_id'=>$product_id])->execute();

                              

                              

                          }

                          //*********END brends*********//

                          //*********fasovka*********//

                          ProductsFasovka::deleteAll(['product_id' => $product_id]);

                          foreach($fasovkas as $fasovka){

                              $fields = [

                                  'catalog_id'=>$catalog_id,

                                  'name'=>$fasovka,

                              ];

                              $modelFasovka = Fasovka::find()->where('name=:name and catalog_id=:catalog_id',[':name'=>$fasovka,':catalog_id'=>$catalog_id])->one();

                              if(empty($modelFasovka->id)){    

                                  $db->createCommand()->insert('catalog_fasovka', $fields)->execute();

                                  $fasovka_id = Yii::$app->db->lastInsertID;

                              }else{

                                  $db->createCommand()->update('catalog_fasovka', $fields, 'id = '.$modelFasovka->id)->execute();                                

                                  $fasovka_id = $modelFasovka->id;

                                  

                              }

                              $db->createCommand()->insert('productsFasovka', ['fasovka_id'=>$fasovka_id,'product_id'=>$product_id])->execute();

                              

                              

                          }

                          //*********END fasovka*********//                          

                          

                 // print"<pre>";print_r($data);	

  

              }

              fclose($handle);

          }            

              

              

              

              

              

              

  

              return true;

  

      }

  }