Blame view

common/modules/product/controllers/ManageController.php 15.4 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;

36d1807a   Yarik   Big commit.
7
8
9
10
11
12
13
14
15
16
17
18
      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
19
      /**

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

d8c1a2e0   Yarik   Big commit artbox
21
       */

36d1807a   Yarik   Big commit.
22
      class ManageController extends Controller

d8c1a2e0   Yarik   Big commit artbox
23
      {

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

          /**

           * @inheritdoc

           */

          public function behaviors()

          {

              return [

                  'verbs' => [

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

                      'actions' => [

                          'delete' => [ 'POST' ],

                      ],

d8c1a2e0   Yarik   Big commit artbox
36
                  ],

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

          }

          

          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
54
          }

d8c1a2e0   Yarik   Big commit artbox
55
          

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

           * 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();

d55d2fe0   Yarik   Multilanguage
93
              $model_langs = $model->generateLangs();

36d1807a   Yarik   Big commit.
94
              

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

d55d2fe0   Yarik   Multilanguage
96
              $product_spec_langs = $product_spec->generateLangs();

cc658b4c   Yarik   Big commit
97
              

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

d55d2fe0   Yarik   Multilanguage
99
                  $model->loadLangs(\Yii::$app->request, $model_langs);

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

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

36d1807a   Yarik   Big commit.
102
103
104
105
106
107
108
109
110
111
                  

                  if($model->save()) {

                      

                      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
112
                      }

d55d2fe0   Yarik   Multilanguage
113
                      

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

                          foreach($certificates as $certificate) {

                              $certificateModel = new ProductCertificate([

                                  'product_id' => $model->product_id,

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

cc658b4c   Yarik   Big commit
119
120
121
122
                              ]);

                              $certificateModel->save(false);

                          }

                      }

d55d2fe0   Yarik   Multilanguage
123
                      

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

d55d2fe0   Yarik   Multilanguage
125
                          $product_spec->loadLangs(\Yii::$app->request, $product_spec_langs);

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

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

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

d55d2fe0   Yarik   Multilanguage
129
                          if($product_spec->upload() && $model->linkLangs($model_langs) && $model->saveLangs($model_langs)) {

cc658b4c   Yarik   Big commit
130
                              $product_spec->save(false);

d55d2fe0   Yarik   Multilanguage
131
132
133
134
135
136
137
138
139
140
141
                              if($product_spec->linkLangs($product_spec_langs) && $product_spec->saveLangs($product_spec_langs)) {

                                  return $this->redirect([

                                      'view',

                                      'id' => $model->product_id,

                                  ]);

                              } else {

                                  return $this->redirect([

                                      'update',

                                      'id' => $model->product_id,

                                  ]);

                              }

cc658b4c   Yarik   Big commit
142
                          }

d55d2fe0   Yarik   Multilanguage
143
                      } elseif($model->linkLangs($model_langs) && $model->saveLangs($model_langs)) {

cc658b4c   Yarik   Big commit
144
145
146
147
                          return $this->redirect([

                              'view',

                              'id' => $model->product_id,

                          ]);

d55d2fe0   Yarik   Multilanguage
148
149
150
151
152
153
154
                      } else {

                          return $this->redirect([

                              'update',

                              'id' => $model->product_id,

                          ]);

                      }

                  }

d8c1a2e0   Yarik   Big commit artbox
155
              }

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

d55d2fe0   Yarik   Multilanguage
157
158
159
160
                  'model'              => $model,

                  'model_langs'        => $model_langs,

                  'product_spec'       => $product_spec,

                  'product_spec_langs' => $product_spec_langs,

d8c1a2e0   Yarik   Big commit artbox
161
162
              ]);

          }

36d1807a   Yarik   Big commit.
163
164
165
166
167
168
169
170
171
172
173
174
          

          /**

           * 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);

d55d2fe0   Yarik   Multilanguage
175
              $model_langs = $model->generateLangs();

cc658b4c   Yarik   Big commit
176
177
178
179
180
181
182
              if(!empty( $model->productSpec )) {

                  $product_spec = $model->productSpec;

              } else {

                  $product_spec = new ProductSpec([

                      'product_id' => $model->product_id,

                  ]);

              }

d55d2fe0   Yarik   Multilanguage
183
              $product_spec_langs = $product_spec->generateLangs();

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

d55d2fe0   Yarik   Multilanguage
185
                  $model->loadLangs(\Yii::$app->request, $model_langs);

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

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

36d1807a   Yarik   Big commit.
188
189
190
191
192
193
194
195
                  if($model->save()) {

                      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
196
                      }

36d1807a   Yarik   Big commit.
197
                      

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

cc658b4c   Yarik   Big commit
199
200
201
                          foreach($certificates as $certificate) {

                              $certificateModel = new ProductCertificate([

                                  'product_id' => $model->product_id,

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

cc658b4c   Yarik   Big commit
203
204
205
206
207
208
                              ]);

                              $certificateModel->save(false);

                          }

                      }

                      

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

d55d2fe0   Yarik   Multilanguage
209
                          $product_spec->loadLangs(\Yii::$app->request, $product_spec_langs);

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

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

d55d2fe0   Yarik   Multilanguage
212
                          if($product_spec->upload() && $model->linkLangs($model_langs) && $model->saveLangs($model_langs)) {

cc658b4c   Yarik   Big commit
213
                              $product_spec->save(false);

d55d2fe0   Yarik   Multilanguage
214
215
216
217
218
219
                              if($product_spec->linkLangs($product_spec_langs) && $product_spec->saveLangs($product_spec_langs)) {

                                  return $this->redirect([

                                      'view',

                                      'id' => $model->product_id,

                                  ]);

                              }

cc658b4c   Yarik   Big commit
220
                          }

d55d2fe0   Yarik   Multilanguage
221
                      } elseif($model->linkLangs($model_langs) && $model->saveLangs($model_langs)) {

cc658b4c   Yarik   Big commit
222
223
224
225
226
                          return $this->redirect([

                              'view',

                              'id' => $model->product_id,

                          ]);

                      }

d8c1a2e0   Yarik   Big commit artbox
227
                  }

d8c1a2e0   Yarik   Big commit artbox
228
              }

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

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

d55d2fe0   Yarik   Multilanguage
231
232
233
234
235
                  'model'              => $model,

                  'model_langs'        => $model_langs,

                  'groups'             => $groups,

                  'product_spec'       => $product_spec,

                  'product_spec_langs' => $product_spec_langs,

d8c1a2e0   Yarik   Big commit artbox
236
237
              ]);

          }

36d1807a   Yarik   Big commit.
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
          

          /**

           * 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
253
          }

36d1807a   Yarik   Big commit.
254
255
256
257
258
259
260
          

          public function actionDelimg($id)

          {

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

              

              if($image) {

                  $image->delete();

d8c1a2e0   Yarik   Big commit artbox
261
              }

36d1807a   Yarik   Big commit.
262
263
264
              

              print '1';

              exit;

d8c1a2e0   Yarik   Big commit artbox
265
          }

d55d2fe0   Yarik   Multilanguage
266
          

cc658b4c   Yarik   Big commit
267
268
269
          public function actionDelcert($id)

          {

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

d55d2fe0   Yarik   Multilanguage
270
              

cc658b4c   Yarik   Big commit
271
272
273
              if($certificate) {

                  $certificate->delete();

              }

d55d2fe0   Yarik   Multilanguage
274
              

cc658b4c   Yarik   Big commit
275
276
277
              print '1';

              exit;

          }

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

          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
288
          }

36d1807a   Yarik   Big commit.
289
290
291
292
293
294
295
296
297
298
          

          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
299
          }

36d1807a   Yarik   Big commit.
300
301
302
303
304
305
306
307
308
309
          

          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
310
          }

36d1807a   Yarik   Big commit.
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
          

          public function actionImport()

          {

              $model = new Import();

              

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

                  $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', [

                  'model' => $model,

              ]);

          }

          

          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;

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

              }

          }

          

          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);

              }

          }

          

          public function actionExport()

          {

              $model = new Export();

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

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

                                            ->send();

              }

              throw new NotFoundHttpException('Error');

          }

          

          /**

           * 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
394
395
          }

      }