Blame view

common/modules/product/controllers/ManageController.php 12.7 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
      use common\modules\product\models\Export;

      use common\modules\product\models\Import;

      use common\modules\product\models\ProductImage;

36d1807a   Yarik   Big commit.
11
12
13
14
15
16
17
18
      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
              ];

          }

          

36d1807a   Yarik   Big commit.
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
          /**

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

          {

00731fef   Yarik   Before vitex test
64
65
66
67
68
69
70
              $model = $this->findModel($id);

              $categories = $model->getCategories()

                                  ->with('lang')

                                  ->all();

              $variants = $model->getVariants()

                                ->with('lang')

                                ->all();

4e55ce81   Yarik   Another one admin...
71
              $properties = $model->getProperties();

36d1807a   Yarik   Big commit.
72
              return $this->render('view', [

00731fef   Yarik   Before vitex test
73
74
75
                  'model'      => $this->findModel($id),

                  'categories' => $categories,

                  'variants'   => $variants,

4e55ce81   Yarik   Another one admin...
76
                  'properties' => $properties,

36d1807a   Yarik   Big commit.
77
78
79
80
81
82
83
84
85
86
87
              ]);

          }

          

          /**

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

c3c1539f   Yarik   Tax Option fix
88
              $model->detachBehavior('techSpec');

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

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

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

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

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

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

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

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

cc658b4c   Yarik   Big commit
97
98
99
                          $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
100
101
                          if($product_spec->upload()) {

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

d55d2fe0   Yarik   Multilanguage
102
103
104
105
                                  return $this->redirect([

                                      'view',

                                      'id' => $model->product_id,

                                  ]);

d55d2fe0   Yarik   Multilanguage
106
                              }

cc658b4c   Yarik   Big commit
107
                          }

d55d2fe0   Yarik   Multilanguage
108
109
                      }

                  }

d8c1a2e0   Yarik   Big commit artbox
110
              }

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

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

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

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

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

d8c1a2e0   Yarik   Big commit artbox
116
117
              ]);

          }

36d1807a   Yarik   Big commit.
118
119
120
121
122
123
124
125
126
127
128
129
          

          /**

           * 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
130
              $model->generateLangs();

cc658b4c   Yarik   Big commit
131
132
133
134
135
136
137
              if(!empty( $model->productSpec )) {

                  $product_spec = $model->productSpec;

              } else {

                  $product_spec = new ProductSpec([

                      'product_id' => $model->product_id,

                  ]);

              }

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

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

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

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

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

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

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

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

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

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

d55d2fe0   Yarik   Multilanguage
148
149
150
151
152
                                  return $this->redirect([

                                      'view',

                                      'id' => $model->product_id,

                                  ]);

                              }

cc658b4c   Yarik   Big commit
153
                          }

cc658b4c   Yarik   Big commit
154
                      }

d8c1a2e0   Yarik   Big commit artbox
155
                  }

d8c1a2e0   Yarik   Big commit artbox
156
              }

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

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

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

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

d55d2fe0   Yarik   Multilanguage
161
162
                  'groups'             => $groups,

                  'product_spec'       => $product_spec,

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

d8c1a2e0   Yarik   Big commit artbox
164
165
              ]);

          }

36d1807a   Yarik   Big commit.
166
167
168
169
170
171
172
173
174
175
176
177
178
          

          /**

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

36d1807a   Yarik   Big commit.
179
              return $this->redirect([ 'index' ]);

d8c1a2e0   Yarik   Big commit artbox
180
          }

36d1807a   Yarik   Big commit.
181
182
183
184
185
186
187
          

          public function actionDelimg($id)

          {

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

              

              if($image) {

                  $image->delete();

d8c1a2e0   Yarik   Big commit artbox
188
              }

36d1807a   Yarik   Big commit.
189
190
191
              

              print '1';

              exit;

d8c1a2e0   Yarik   Big commit artbox
192
          }

d55d2fe0   Yarik   Multilanguage
193
          

cc658b4c   Yarik   Big commit
194
195
196
          public function actionDelcert($id)

          {

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

d55d2fe0   Yarik   Multilanguage
197
              

cc658b4c   Yarik   Big commit
198
199
200
              if($certificate) {

                  $certificate->delete();

              }

d55d2fe0   Yarik   Multilanguage
201
              

cc658b4c   Yarik   Big commit
202
203
204
              print '1';

              exit;

          }

36d1807a   Yarik   Big commit.
205
206
207
208
209
210
211
212
213
214
          

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

36d1807a   Yarik   Big commit.
216
217
218
219
220
221
222
223
224
225
          

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

36d1807a   Yarik   Big commit.
227
228
229
230
231
232
233
234
235
236
          

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

36d1807a   Yarik   Big commit.
238
239
240
241
242
          

          public function actionImport()

          {

              $model = new Import();

              

e8ccb1b4   Yarik   Import beta
243
244
245
246
247
248
249
250
251
252
253
              $languages = Language::find()

                                   ->select([

                                       'name',

                                       'language_id',

                                   ])

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

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

                                   ->asArray()

                                   ->indexBy('language_id')

                                   ->column();

              

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

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

36d1807a   Yarik   Big commit.
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
                  $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,

                      ]);

36d1807a   Yarik   Big commit.
272
273
274
275
276
277
                  } else {

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

                  }

              }

              

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

e8ccb1b4   Yarik   Import beta
278
279
                  'model'     => $model,

                  'languages' => $languages,

36d1807a   Yarik   Big commit.
280
281
282
283
284
285
286
287
288
289
290
              ]);

          }

          

          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...
291
                  return $model->goProducts($from, 1);

36d1807a   Yarik   Big commit.
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
              }

          }

          

          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...
307
308
309
310
311
312
          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
313
                  return $model->process($filename, $from);

96410438   Yarik   Project admin com...
314
315
316
              }

          }

          

36d1807a   Yarik   Big commit.
317
318
319
          public function actionExport()

          {

              $model = new Export();

96410438   Yarik   Project admin com...
320
321
322
323
              

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

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

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

e8ccb1b4   Yarik   Import beta
324
325
                      'model'  => $model,

                      'method' => 'export',

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

36d1807a   Yarik   Big commit.
327
              }

96410438   Yarik   Project admin com...
328
329
330
331
              

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

                  'model' => $model,

              ]);

36d1807a   Yarik   Big commit.
332
333
334
335
336
337
338
339
340
341
342
343
344
          }

          

          /**

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

          {

00731fef   Yarik   Before vitex test
345
346
347
348
349
              if(( $model = Product::find()

                                   ->where([ 'product_id' => $id ])

                                   ->with('lang')

                                   ->one() ) !== NULL

              ) {

36d1807a   Yarik   Big commit.
350
351
352
353
                  return $model;

              } else {

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

              }

d8c1a2e0   Yarik   Big commit artbox
354
355
          }

      }