Blame view

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

36d1807a   Yarik   Big commit.
2
3
4
      

      namespace common\modules\product\controllers;

      

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

36d1807a   Yarik   Big commit.
6
7
8
      use common\modules\product\models\Export;

      use common\modules\product\models\Import;

      use common\modules\product\models\ProductImage;

36d1807a   Yarik   Big commit.
9
10
11
      use Yii;

      use common\modules\product\models\Product;

      use common\modules\product\models\ProductSearch;

5c2eb7c8   Yarik   Big commit almost...
12
      use yii\db\ActiveQuery;

36d1807a   Yarik   Big commit.
13
14
15
      use yii\web\Controller;

      use yii\web\NotFoundHttpException;

      use yii\filters\VerbFilter;

5c2eb7c8   Yarik   Big commit almost...
16
      use yii\web\Response;

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

           * Lists all Product models.

4428da8c   Yarik   Almost all databa...
42
           *

36d1807a   Yarik   Big commit.
43
44
45
46
47
48
49
           * @return mixed

           */

          public function actionIndex()

          {

              $searchModel = new ProductSearch();

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

              

4428da8c   Yarik   Almost all databa...
50
51
52
53
54
55
56
              return $this->render(

                  'index',

                  [

                      'searchModel'  => $searchModel,

                      'dataProvider' => $dataProvider,

                  ]

              );

36d1807a   Yarik   Big commit.
57
58
59
60
61
62
63
64
65
66
67
          }

          

          /**

           * Displays a single Product model.

           *

           * @param integer $id

           *

           * @return mixed

           */

          public function actionView($id)

          {

00731fef   Yarik   Before vitex test
68
69
70
71
72
73
74
              $model = $this->findModel($id);

              $categories = $model->getCategories()

                                  ->with('lang')

                                  ->all();

              $variants = $model->getVariants()

                                ->with('lang')

                                ->all();

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

4428da8c   Yarik   Almost all databa...
76
77
78
79
80
81
82
83
84
              return $this->render(

                  'view',

                  [

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

                      'categories' => $categories,

                      'variants'   => $variants,

                      'properties' => $properties,

                  ]

              );

36d1807a   Yarik   Big commit.
85
86
87
88
89
          }

          

          /**

           * Creates a new Product model.

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

4428da8c   Yarik   Almost all databa...
90
           *

36d1807a   Yarik   Big commit.
91
92
93
94
95
           * @return mixed

           */

          public function actionCreate()

          {

              $model = new Product();

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

4428da8c   Yarik   Almost all databa...
97
              if ($model->load(Yii::$app->request->post())) {

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

4428da8c   Yarik   Almost all databa...
99
100
101
102
103
104
105
                  if ($model->save() && $model->transactionStatus) {

                      return $this->redirect(

                          [

                              'view',

                              'id' => $model->id,

                          ]

                      );

d55d2fe0   Yarik   Multilanguage
106
                  }

d8c1a2e0   Yarik   Big commit artbox
107
              }

4428da8c   Yarik   Almost all databa...
108
109
110
111
112
113
114
              return $this->render(

                  'create',

                  [

                      'model'      => $model,

                      'modelLangs' => $model->modelLangs,

                  ]

              );

d8c1a2e0   Yarik   Big commit artbox
115
          }

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

          /**

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

4428da8c   Yarik   Almost all databa...
129
              if ($model->load(Yii::$app->request->post())) {

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

4428da8c   Yarik   Almost all databa...
131
132
133
134
135
136
137
                  if ($model->save() && $model->transactionStatus) {

                      return $this->redirect(

                          [

                              'view',

                              'id' => $model->id,

                          ]

                      );

d8c1a2e0   Yarik   Big commit artbox
138
                  }

d8c1a2e0   Yarik   Big commit artbox
139
              }

5c2eb7c8   Yarik   Big commit almost...
140
141
142
              /**

               * @var ActiveQuery $groups

               */

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

4428da8c   Yarik   Almost all databa...
144
145
146
147
148
149
150
151
              return $this->render(

                  'update',

                  [

                      'model'      => $model,

                      'modelLangs' => $model->modelLangs,

                      'groups'     => $groups,

                  ]

              );

d8c1a2e0   Yarik   Big commit artbox
152
          }

36d1807a   Yarik   Big commit.
153
154
155
156
157
158
159
160
161
162
163
164
165
          

          /**

           * 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.
166
              return $this->redirect([ 'index' ]);

d8c1a2e0   Yarik   Big commit artbox
167
          }

36d1807a   Yarik   Big commit.
168
          

c70f24ea   Yarik   For Leha commit.
169
170
171
172
173
174
          /**

           * Deletes an existing ProductImage model.

           *

           * @param int $id

           */

          public function actionDeleteImage($id)

36d1807a   Yarik   Big commit.
175
176
177
          {

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

              

4428da8c   Yarik   Almost all databa...
178
              if ($image) {

36d1807a   Yarik   Big commit.
179
                  $image->delete();

d8c1a2e0   Yarik   Big commit artbox
180
              }

36d1807a   Yarik   Big commit.
181
182
183
              

              print '1';

              exit;

d8c1a2e0   Yarik   Big commit artbox
184
          }

d55d2fe0   Yarik   Multilanguage
185
          

c70f24ea   Yarik   For Leha commit.
186
187
188
189
190
191
192
          /**

           * Toggle product top status

           *

           * @param int $id Product ID

           *

           * @return \yii\web\Response

           */

8af13427   Yarik   For leha commit.
193
          public function actionIsTop($id)

36d1807a   Yarik   Big commit.
194
195
196
197
198
199
200
201
          {

              $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
202
          }

36d1807a   Yarik   Big commit.
203
          

c70f24ea   Yarik   For Leha commit.
204
205
206
207
208
209
210
          /**

           * Toggle product new status

           *

           * @param int $id Product ID

           *

           * @return \yii\web\Response

           */

8af13427   Yarik   For leha commit.
211
          public function actionIsNew($id)

36d1807a   Yarik   Big commit.
212
213
214
215
216
217
218
219
          {

              $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
220
          }

36d1807a   Yarik   Big commit.
221
          

c70f24ea   Yarik   For Leha commit.
222
223
224
225
226
227
228
          /**

           * Toggle product discount status

           *

           * @param int $id Product ID

           *

           * @return \yii\web\Response

           */

4428da8c   Yarik   Almost all databa...
229
          public function actionIsDiscount($id)

36d1807a   Yarik   Big commit.
230
231
232
          {

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

              

4428da8c   Yarik   Almost all databa...
233
              $model->is_discount = intval(empty( $model->is_discount ));

36d1807a   Yarik   Big commit.
234
              

4428da8c   Yarik   Almost all databa...
235
              $model->save(false, [ 'is_discount' ]);

36d1807a   Yarik   Big commit.
236
237
              

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

d8c1a2e0   Yarik   Big commit artbox
238
          }

36d1807a   Yarik   Big commit.
239
          

c70f24ea   Yarik   For Leha commit.
240
241
242
243
244
          /**

           * Perform product import

           *

           * @return string

           */

36d1807a   Yarik   Big commit.
245
246
247
248
          public function actionImport()

          {

              $model = new Import();

              

e8ccb1b4   Yarik   Import beta
249
              $languages = Language::find()

4428da8c   Yarik   Almost all databa...
250
251
252
253
254
255
                                   ->select(

                                       [

                                           'name',

                                           'id',

                                       ]

                                   )

e8ccb1b4   Yarik   Import beta
256
257
258
                                   ->where([ 'status' => 1 ])

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

                                   ->asArray()

8af13427   Yarik   For leha commit.
259
                                   ->indexBy('id')

e8ccb1b4   Yarik   Import beta
260
261
                                   ->column();

              

4428da8c   Yarik   Almost all databa...
262
              if ($model->load(Yii::$app->request->post())) {

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

36d1807a   Yarik   Big commit.
264
265
266
                  $file = UploadedFile::getInstances($model, 'file');

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

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

4428da8c   Yarik   Almost all databa...
267
                  if (empty( $file )) {

36d1807a   Yarik   Big commit.
268
                      $model->errors[] = 'File not upload';

4428da8c   Yarik   Almost all databa...
269
                  } elseif ($method == 'goPrices' && $file[ 0 ]->name != 'file_1.csv') {

36d1807a   Yarik   Big commit.
270
                      $model->errors[] = 'File need "file_1.csv"';

4428da8c   Yarik   Almost all databa...
271
                  } elseif ($method == 'goProducts' && $file[ 0 ]->name == 'file_1.csv') {

36d1807a   Yarik   Big commit.
272
                      $model->errors[] = 'File can not "file_1.csv"';

4428da8c   Yarik   Almost all databa...
273
                  } elseif ($model->validate() && $file[ 0 ]->saveAs($target)) {

36d1807a   Yarik   Big commit.
274
                      // PROCESS PAGE

4428da8c   Yarik   Almost all databa...
275
276
277
278
279
280
281
282
                      return $this->render(

                          'import-process',

                          [

                              'model'  => $model,

                              'method' => $model->type,

                              'target' => $target,

                          ]

                      );

36d1807a   Yarik   Big commit.
283
284
285
286
287
                  } else {

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

                  }

              }

              

4428da8c   Yarik   Almost all databa...
288
289
290
291
292
293
294
              return $this->render(

                  'import',

                  [

                      'model'     => $model,

                      'languages' => $languages,

                  ]

              );

36d1807a   Yarik   Big commit.
295
296
          }

          

c70f24ea   Yarik   For Leha commit.
297
298
299
300
301
302
          /**

           * Import products via AJAX

           *

           * @return array

           * @throws \HttpRequestException

           */

36d1807a   Yarik   Big commit.
303
304
305
306
307
308
          public function actionProducts()

          {

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

              

              $model = new Import();

              

4428da8c   Yarik   Almost all databa...
309
              if (Yii::$app->request->isAjax) {

5c2eb7c8   Yarik   Big commit almost...
310
                  Yii::$app->response->format = Response::FORMAT_JSON;

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

5c2eb7c8   Yarik   Big commit almost...
312
313
              } else {

                  throw new \HttpRequestException('Must be AJAX');

36d1807a   Yarik   Big commit.
314
315
316
              }

          }

          

c70f24ea   Yarik   For Leha commit.
317
318
319
320
321
322
          /**

           * Import prices via AJAX

           *

           * @return array

           * @throws \HttpRequestException

           */

36d1807a   Yarik   Big commit.
323
324
325
326
327
328
          public function actionPrices()

          {

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

              

              $model = new Import();

              

4428da8c   Yarik   Almost all databa...
329
              if (Yii::$app->request->isAjax) {

5c2eb7c8   Yarik   Big commit almost...
330
                  Yii::$app->response->format = Response::FORMAT_JSON;

36d1807a   Yarik   Big commit.
331
                  return $model->goPrices($from, 10);

5c2eb7c8   Yarik   Big commit almost...
332
333
              } else {

                  throw new \HttpRequestException('Must be AJAX');

36d1807a   Yarik   Big commit.
334
335
336
              }

          }

          

c70f24ea   Yarik   For Leha commit.
337
338
339
340
341
342
343
344
345
          /**

           * Export proccess via AJAX

           *

           * @param int    $from

           * @param string $filename

           *

           * @return array

           * @throws \HttpRequestException

           */

96410438   Yarik   Project admin com...
346
347
348
349
          public function actionExportProcess($from, $filename)

          {

              

              $model = new Export();

4428da8c   Yarik   Almost all databa...
350
              if (Yii::$app->request->isAjax) {

5c2eb7c8   Yarik   Big commit almost...
351
                  Yii::$app->response->format = Response::FORMAT_JSON;

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

5c2eb7c8   Yarik   Big commit almost...
353
354
              } else {

                  throw new \HttpRequestException('Must be AJAX');

96410438   Yarik   Project admin com...
355
356
              }

          }

c70f24ea   Yarik   For Leha commit.
357
358
359
360
361
362
      

          /**

           * Perform export

           *

           * @return string

           */

36d1807a   Yarik   Big commit.
363
364
365
          public function actionExport()

          {

              $model = new Export();

96410438   Yarik   Project admin com...
366
              

4428da8c   Yarik   Almost all databa...
367
              if ($model->load(Yii::$app->request->post())) {

96410438   Yarik   Project admin com...
368
                  \Yii::$app->session->set('export_lang', $model->lang);

4428da8c   Yarik   Almost all databa...
369
370
371
372
373
374
375
                  return $this->render(

                      'export-process',

                      [

                          'model'  => $model,

                          'method' => 'export',

                      ]

                  );

36d1807a   Yarik   Big commit.
376
              }

96410438   Yarik   Project admin com...
377
              

4428da8c   Yarik   Almost all databa...
378
379
380
381
382
383
              return $this->render(

                  'export',

                  [

                      'model' => $model,

                  ]

              );

36d1807a   Yarik   Big commit.
384
385
386
387
388
389
390
391
392
393
394
395
396
          }

          

          /**

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

          {

4428da8c   Yarik   Almost all databa...
397
398
399
400
              if (( $model = Product::find()

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

                                    ->with('lang')

                                    ->one() ) !== null

00731fef   Yarik   Before vitex test
401
              ) {

36d1807a   Yarik   Big commit.
402
403
404
405
                  return $model;

              } else {

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

              }

d8c1a2e0   Yarik   Big commit artbox
406
407
          }

      }