Blame view

common/modules/product/controllers/ManageController.php 15.5 KB
d8c1a2e0   Yarik   Big commit artbox
1
  <?php

36d1807a   Yarik   Big commit.
2
3
4
      

      namespace common\modules\product\controllers;

      

cc658b4c   Yarik   Big commit
5
6
      use common\models\ProductCertificate;

      use common\models\ProductSpec;

e8ccb1b4   Yarik   Import beta
7
      use common\modules\language\models\Language;

36d1807a   Yarik   Big commit.
8
9
10
11
12
13
14
15
16
17
18
19
      use common\modules\product\models\Export;

      use common\modules\product\models\Import;

      use common\modules\product\models\ProductImage;

      use common\modules\product\models\ProductVariant;

      use Yii;

      use common\modules\product\models\Product;

      use common\modules\product\models\ProductSearch;

      use yii\web\Controller;

      use yii\web\NotFoundHttpException;

      use yii\filters\VerbFilter;

      use yii\web\UploadedFile;

      

d8c1a2e0   Yarik   Big commit artbox
20
      /**

36d1807a   Yarik   Big commit.
21
       * ManageController implements the CRUD actions for Product model.

d8c1a2e0   Yarik   Big commit artbox
22
       */

36d1807a   Yarik   Big commit.
23
      class ManageController extends Controller

d8c1a2e0   Yarik   Big commit artbox
24
      {

36d1807a   Yarik   Big commit.
25
26
27
28
29
30
31
32
33
34
35
36
          

          /**

           * @inheritdoc

           */

          public function behaviors()

          {

              return [

                  'verbs' => [

                      'class'   => VerbFilter::className(),

                      'actions' => [

                          'delete' => [ 'POST' ],

                      ],

d8c1a2e0   Yarik   Big commit artbox
37
                  ],

36d1807a   Yarik   Big commit.
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
              ];

          }

          

          public function actionDev()

          {

              foreach(ProductVariant::find()

                                    ->where([

                                        '>',

                                        'price_old',

                                        0,

                                    ])

                                    ->all() as $item) {

                  if($item->price >= $item->price_old || $item->price == 0) {

                      print $item->price . ' | ' . $item->price_old . "<br>\n";

                  }

              }

              print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~';

d8c1a2e0   Yarik   Big commit artbox
55
          }

d8c1a2e0   Yarik   Big commit artbox
56
          

36d1807a   Yarik   Big commit.
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
          /**

           * Lists all Product models.

           * @return mixed

           */

          public function actionIndex()

          {

              $searchModel = new ProductSearch();

              $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

              

              return $this->render('index', [

                  'searchModel'  => $searchModel,

                  'dataProvider' => $dataProvider,

              ]);

          }

          

          /**

           * Displays a single Product model.

           *

           * @param integer $id

           *

           * @return mixed

           */

          public function actionView($id)

          {

              return $this->render('view', [

                  'model' => $this->findModel($id),

              ]);

          }

          

          /**

           * Creates a new Product model.

           * If creation is successful, the browser will be redirected to the 'view' page.

           * @return mixed

           */

          public function actionCreate()

          {

              $model = new Product();

9f8be5c9   Yarik   Import browser be...
94
              $model->generateLangs();

36d1807a   Yarik   Big commit.
95
              

cc658b4c   Yarik   Big commit
96
              $product_spec = new ProductSpec();

9f8be5c9   Yarik   Import browser be...
97
              $product_spec->generateLangs();

cc658b4c   Yarik   Big commit
98
              

36d1807a   Yarik   Big commit.
99
              if($model->load(Yii::$app->request->post())) {

9f8be5c9   Yarik   Import browser be...
100
                  $model->loadLangs(\Yii::$app->request);

36d1807a   Yarik   Big commit.
101
                  $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload');

cc658b4c   Yarik   Big commit
102
                  $model->certificateUpload = UploadedFile::getInstances($model, 'certificateUpload');

36d1807a   Yarik   Big commit.
103
                  

72a992f5   Yarik   Import browser v1.0
104
                  if($model->save() && $model->transactionStatus) {

36d1807a   Yarik   Big commit.
105
106
107
108
109
110
111
112
                      

                      if(!empty( $model->imagesUpload ) && ( ( $images = $model->imagesUpload() ) !== false )) {

                          foreach($images as $image) {

                              $imageModel = new ProductImage();

                              $imageModel->product_id = $model->product_id;

                              $imageModel->image = $image;

                              $imageModel->save();

                          }

d8c1a2e0   Yarik   Big commit artbox
113
                      }

d55d2fe0   Yarik   Multilanguage
114
                      

cc658b4c   Yarik   Big commit
115
116
117
118
                      if(!empty( $model->certificateUpload ) && ( ( $certificates = $model->certificateUpload() ) !== false )) {

                          foreach($certificates as $certificate) {

                              $certificateModel = new ProductCertificate([

                                  'product_id' => $model->product_id,

d55d2fe0   Yarik   Multilanguage
119
                                  'link'       => $certificate,

cc658b4c   Yarik   Big commit
120
121
122
123
                              ]);

                              $certificateModel->save(false);

                          }

                      }

d55d2fe0   Yarik   Multilanguage
124
                      

cc658b4c   Yarik   Big commit
125
                      if($product_spec->load(Yii::$app->request->post())) {

9f8be5c9   Yarik   Import browser be...
126
                          $product_spec->loadLangs(\Yii::$app->request);

cc658b4c   Yarik   Big commit
127
128
129
                          $product_spec->product_id = $model->product_id;

                          $product_spec->techSpecFile = UploadedFile::getInstance($product_spec, 'techSpecFile');

                          $product_spec->techCharFile = UploadedFile::getInstance($product_spec, 'techCharFile');

72a992f5   Yarik   Import browser v1.0
130
131
                          if($product_spec->upload()) {

                              if($product_spec->save(false) && $product_spec->transactionStatus) {

d55d2fe0   Yarik   Multilanguage
132
133
134
135
                                  return $this->redirect([

                                      'view',

                                      'id' => $model->product_id,

                                  ]);

d55d2fe0   Yarik   Multilanguage
136
                              }

cc658b4c   Yarik   Big commit
137
                          }

d55d2fe0   Yarik   Multilanguage
138
139
                      }

                  }

d8c1a2e0   Yarik   Big commit artbox
140
              }

d8c1a2e0   Yarik   Big commit artbox
141
              return $this->render('create', [

d55d2fe0   Yarik   Multilanguage
142
                  'model'              => $model,

9f8be5c9   Yarik   Import browser be...
143
                  'model_langs'        => $model->model_langs,

d55d2fe0   Yarik   Multilanguage
144
                  'product_spec'       => $product_spec,

9f8be5c9   Yarik   Import browser be...
145
                  'product_spec_langs' => $product_spec->model_langs,

d8c1a2e0   Yarik   Big commit artbox
146
147
              ]);

          }

36d1807a   Yarik   Big commit.
148
149
150
151
152
153
154
155
156
157
158
159
          

          /**

           * Updates an existing Product model.

           * If update is successful, the browser will be redirected to the 'view' page.

           *

           * @param integer $id

           *

           * @return mixed

           */

          public function actionUpdate($id)

          {

              $model = $this->findModel($id);

72a992f5   Yarik   Import browser v1.0
160
              $model->generateLangs();

cc658b4c   Yarik   Big commit
161
162
163
164
165
166
167
              if(!empty( $model->productSpec )) {

                  $product_spec = $model->productSpec;

              } else {

                  $product_spec = new ProductSpec([

                      'product_id' => $model->product_id,

                  ]);

              }

72a992f5   Yarik   Import browser v1.0
168
              $product_spec->generateLangs();

36d1807a   Yarik   Big commit.
169
              if($model->load(Yii::$app->request->post())) {

72a992f5   Yarik   Import browser v1.0
170
                  $model->loadLangs(\Yii::$app->request);

36d1807a   Yarik   Big commit.
171
                  $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload');

cc658b4c   Yarik   Big commit
172
                  $model->certificateUpload = UploadedFile::getInstances($model, 'certificateUpload');

72a992f5   Yarik   Import browser v1.0
173
                  if($model->save() && $model->transactionStatus) {

36d1807a   Yarik   Big commit.
174
175
176
177
178
179
180
                      if(!empty( $model->imagesUpload ) && ( ( $images = $model->imagesUpload() ) !== false )) {

                          foreach($images as $image) {

                              $imageModel = new ProductImage();

                              $imageModel->product_id = $model->product_id;

                              $imageModel->image = $image;

                              $imageModel->save();

                          }

d8c1a2e0   Yarik   Big commit artbox
181
                      }

d55d2fe0   Yarik   Multilanguage
182
                      if(!empty( $model->certificateUpload ) && ( ( $certificates = $model->certificateUpload() ) !== false )) {

cc658b4c   Yarik   Big commit
183
184
185
                          foreach($certificates as $certificate) {

                              $certificateModel = new ProductCertificate([

                                  'product_id' => $model->product_id,

d55d2fe0   Yarik   Multilanguage
186
                                  'link'       => $certificate,

cc658b4c   Yarik   Big commit
187
188
189
190
                              ]);

                              $certificateModel->save(false);

                          }

                      }

cc658b4c   Yarik   Big commit
191
                      if($product_spec->load(Yii::$app->request->post())) {

72a992f5   Yarik   Import browser v1.0
192
                          $product_spec->loadLangs(\Yii::$app->request);

cc658b4c   Yarik   Big commit
193
194
                          $product_spec->techSpecFile = UploadedFile::getInstance($product_spec, 'techSpecFile');

                          $product_spec->techCharFile = UploadedFile::getInstance($product_spec, 'techCharFile');

72a992f5   Yarik   Import browser v1.0
195
196
                          if($product_spec->upload()) {

                              if($product_spec->save(false) && $product_spec->transactionStatus) {

d55d2fe0   Yarik   Multilanguage
197
198
199
200
201
                                  return $this->redirect([

                                      'view',

                                      'id' => $model->product_id,

                                  ]);

                              }

cc658b4c   Yarik   Big commit
202
                          }

cc658b4c   Yarik   Big commit
203
                      }

d8c1a2e0   Yarik   Big commit artbox
204
                  }

d8c1a2e0   Yarik   Big commit artbox
205
              }

d8c1a2e0   Yarik   Big commit artbox
206
              $groups = $model->getTaxGroupsByLevel(0);

d8c1a2e0   Yarik   Big commit artbox
207
              return $this->render('update', [

d55d2fe0   Yarik   Multilanguage
208
                  'model'              => $model,

72a992f5   Yarik   Import browser v1.0
209
                  'model_langs'        => $model->model_langs,

d55d2fe0   Yarik   Multilanguage
210
211
                  'groups'             => $groups,

                  'product_spec'       => $product_spec,

72a992f5   Yarik   Import browser v1.0
212
                  'product_spec_langs' => $product_spec->model_langs,

d8c1a2e0   Yarik   Big commit artbox
213
214
              ]);

          }

36d1807a   Yarik   Big commit.
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
          

          /**

           * Deletes an existing Product model.

           * If deletion is successful, the browser will be redirected to the 'index' page.

           *

           * @param integer $id

           *

           * @return mixed

           */

          public function actionDelete($id)

          {

              $this->findModel($id)

                   ->delete();

              

              return $this->redirect([ 'index' ]);

d8c1a2e0   Yarik   Big commit artbox
230
          }

36d1807a   Yarik   Big commit.
231
232
233
234
235
236
237
          

          public function actionDelimg($id)

          {

              $image = ProductImage::findOne($id);

              

              if($image) {

                  $image->delete();

d8c1a2e0   Yarik   Big commit artbox
238
              }

36d1807a   Yarik   Big commit.
239
240
241
              

              print '1';

              exit;

d8c1a2e0   Yarik   Big commit artbox
242
          }

d55d2fe0   Yarik   Multilanguage
243
          

cc658b4c   Yarik   Big commit
244
245
246
          public function actionDelcert($id)

          {

              $certificate = ProductCertificate::findOne($id);

d55d2fe0   Yarik   Multilanguage
247
              

cc658b4c   Yarik   Big commit
248
249
250
              if($certificate) {

                  $certificate->delete();

              }

d55d2fe0   Yarik   Multilanguage
251
              

cc658b4c   Yarik   Big commit
252
253
254
              print '1';

              exit;

          }

36d1807a   Yarik   Big commit.
255
256
257
258
259
260
261
262
263
264
          

          public function actionIs_top($id)

          {

              $model = $this->findModel($id);

              

              $model->is_top = intval(empty( $model->is_top ));

              

              $model->save(false, [ 'is_top' ]);

              

              return $this->redirect([ 'index' ]);

d8c1a2e0   Yarik   Big commit artbox
265
          }

36d1807a   Yarik   Big commit.
266
267
268
269
270
271
272
273
274
275
          

          public function actionIs_new($id)

          {

              $model = $this->findModel($id);

              

              $model->is_new = intval(empty( $model->is_new ));

              

              $model->save(false, [ 'is_new' ]);

              

              return $this->redirect([ 'index' ]);

d8c1a2e0   Yarik   Big commit artbox
276
          }

36d1807a   Yarik   Big commit.
277
278
279
280
281
282
283
284
285
286
          

          public function actionAkciya($id)

          {

              $model = $this->findModel($id);

              

              $model->akciya = intval(empty( $model->akciya ));

              

              $model->save(false, [ 'akciya' ]);

              

              return $this->redirect([ 'index' ]);

d8c1a2e0   Yarik   Big commit artbox
287
          }

36d1807a   Yarik   Big commit.
288
289
290
291
292
          

          public function actionImport()

          {

              $model = new Import();

              

e8ccb1b4   Yarik   Import beta
293
294
295
296
297
298
299
300
301
302
303
              $languages = Language::find()

                                   ->select([

                                       'name',

                                       'language_id',

                                   ])

                                   ->where([ 'status' => 1 ])

                                   ->orderBy([ 'default' => SORT_DESC ])

                                   ->asArray()

                                   ->indexBy('language_id')

                                   ->column();

              

36d1807a   Yarik   Big commit.
304
              if($model->load(Yii::$app->request->post())) {

e8ccb1b4   Yarik   Import beta
305
                  \Yii::$app->session->set('export_lang', $model->lang);

36d1807a   Yarik   Big commit.
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
                  $file = UploadedFile::getInstances($model, 'file');

                  $method = 'go' . ucfirst($model->type);

                  $target = Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFile' . ucfirst($model->type));

                  if(empty( $file )) {

                      $model->errors[] = 'File not upload';

                  } elseif($method == 'goPrices' && $file[ 0 ]->name != 'file_1.csv') {

                      $model->errors[] = 'File need "file_1.csv"';

                  } elseif($method == 'goProducts' && $file[ 0 ]->name == 'file_1.csv') {

                      $model->errors[] = 'File can not "file_1.csv"';

                  } elseif($model->validate() && $file[ 0 ]->saveAs($target)) {

                      // PROCESS PAGE

                      return $this->render('import-process', [

                          'model'  => $model,

                          'method' => $model->type,

                          'target' => $target,

                      ]);

                      //                $model->$method();

                  } else {

                      $model->errors[] = 'File can not be upload or other error';

                  }

              }

              

              return $this->render('import', [

e8ccb1b4   Yarik   Import beta
329
330
                  'model'     => $model,

                  'languages' => $languages,

36d1807a   Yarik   Big commit.
331
332
333
334
335
336
337
338
339
340
341
              ]);

          }

          

          public function actionProducts()

          {

              $from = Yii::$app->request->get('from', 0);

              

              $model = new Import();

              

              if(Yii::$app->request->isAjax) {

                  Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

7d10d812   Yarik   Import browser be...
342
                  return $model->goProducts($from, 1);

36d1807a   Yarik   Big commit.
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
              }

          }

          

          public function actionPrices()

          {

              $from = Yii::$app->request->get('from', 0);

              

              $model = new Import();

              

              if(Yii::$app->request->isAjax) {

                  Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

                  return $model->goPrices($from, 10);

              }

          }

          

96410438   Yarik   Project admin com...
358
359
360
361
362
363
          public function actionExportProcess($from, $filename)

          {

              

              $model = new Export();

              if(Yii::$app->request->isAjax) {

                  Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

e8ccb1b4   Yarik   Import beta
364
                  return $model->process($filename, $from);

96410438   Yarik   Project admin com...
365
366
367
              }

          }

          

36d1807a   Yarik   Big commit.
368
369
370
          public function actionExport()

          {

              $model = new Export();

96410438   Yarik   Project admin com...
371
372
373
374
              

              if($model->load(Yii::$app->request->post())) {

                  \Yii::$app->session->set('export_lang', $model->lang);

                  return $this->render('export-process', [

e8ccb1b4   Yarik   Import beta
375
376
                      'model'  => $model,

                      'method' => 'export',

96410438   Yarik   Project admin com...
377
                  ]);

36d1807a   Yarik   Big commit.
378
              }

96410438   Yarik   Project admin com...
379
380
381
382
383
384
385
386
387
388
              

              return $this->render('export', [

                  'model' => $model,

              ]);

              //            $model = new Export();

              //            if(( $file = $model->process(Yii::getAlias('@uploadDir')) )) {

              //                return Yii::$app->response->sendFile($file)

              //                                          ->send();

              //            }

              //            throw new NotFoundHttpException('Error');

36d1807a   Yarik   Big commit.
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
          }

          

          /**

           * Finds the Product model based on its primary key value.

           * If the model is not found, a 404 HTTP exception will be thrown.

           *

           * @param integer $id

           *

           * @return Product the loaded model

           * @throws NotFoundHttpException if the model cannot be found

           */

          protected function findModel($id)

          {

              if(( $model = Product::findOne($id) ) !== NULL) {

                  return $model;

              } else {

                  throw new NotFoundHttpException('The requested page does not exist.');

              }

d8c1a2e0   Yarik   Big commit artbox
407
408
          }

      }